1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.model; |
16 | |
17 | import java.awt.Graphics2D; |
18 | import java.awt.Rectangle; |
19 | import java.awt.image.BufferedImage; |
20 | import java.util.ArrayList; |
21 | import java.util.List; |
22 | |
23 | import net.sf.ehcache.Cache; |
24 | import net.sourceforge.hiveboard.Board; |
25 | import net.sourceforge.hiveboard.Event; |
26 | import net.sourceforge.hiveboard.EventType; |
27 | import net.sourceforge.hiveboard.drawing.CompoundHiliteAction; |
28 | import net.sourceforge.hiveboard.drawing.DrawingAction; |
29 | import net.sourceforge.hiveboard.drawing.HiliterPrefs; |
30 | import net.sourceforge.hiveboard.event.ConstraintEventFilter; |
31 | import net.sourceforge.hiveboard.event.Constraints; |
32 | import net.sourceforge.hiveboard.event.DeferredConsumer; |
33 | import net.sourceforge.hiveboard.event.EventsPriorities; |
34 | import net.sourceforge.hiveboard.util.ImageCache; |
35 | import net.sourceforge.hiveboard.util.ImageHolder; |
36 | import net.sourceforge.hiveboard.util.RGBImageHolder; |
37 | import net.sourceforge.hiveevents.Channel; |
38 | import net.sourceforge.hiveevents.Consumer; |
39 | import net.sourceforge.hiveevents.Filter; |
40 | import net.sourceforge.hiveutils.service.ObjectTools; |
41 | |
42 | public class BoardImageModel extends ImageCache |
43 | { |
44 | public BoardImageModel( Channel<Event> eventChannel, |
45 | ObjectTools tools, |
46 | HiliterPrefs hilitePrefs, |
47 | Channel<Object> prefsChange, |
48 | Cache cache, |
49 | Board board, |
50 | byte[] rasterContent, |
51 | byte[] hiliteActions) |
52 | { |
53 | super(cache, board.getId()); |
54 | _hilitePrefs = hilitePrefs; |
55 | _hiliter.setHiliteStroke(_hilitePrefs.getStroke()); |
56 | _hiliter.setHiliteColor(_hilitePrefs.getColor()); |
57 | _board = board; |
58 | _tools = tools; |
59 | _width = board.getWidth(); |
60 | _height = board.getHeight(); |
61 | ImageHolder holder = new RGBImageHolder(); |
62 | holder.setImage(_width, _height, rasterContent); |
63 | writeCache(holder); |
64 | if (hiliteActions != null) |
65 | { |
66 | applyAction(hiliteActions); |
67 | } |
68 | |
69 | int priority = EventsPriorities.IMAGE_MODEL_UPDATE; |
70 | String constraint; |
71 | constraint = Constraints.eventTypeWhere(board.getId(), EventType.EVT_SEND_DRAWING); |
72 | Filter<Event> filter = new ConstraintEventFilter(constraint); |
73 | _eventConsumer = new DeferredConsumer() |
74 | { |
75 | @Override protected void pushEvent(Event event) |
76 | { |
77 | applyAction(event.getDrawingAction()); |
78 | } |
79 | }; |
80 | eventChannel.registerPushConsumer(priority, filter, _eventConsumer); |
81 | |
82 | priority = EventsPriorities.DEFAULT_PRIORITY; |
83 | _prefsConsumer = new Consumer<Object>() |
84 | { |
85 | public void push(Object event) |
86 | { |
87 | updateHilitePrefs(); |
88 | } |
89 | }; |
90 | prefsChange.registerPushConsumer(priority, _prefsConsumer); |
91 | } |
92 | |
93 | public void dispose() |
94 | { |
95 | removeCache(); |
96 | } |
97 | |
98 | public Board getBoard() |
99 | { |
100 | return _board; |
101 | } |
102 | |
103 | public int getWidth() |
104 | { |
105 | return _width; |
106 | } |
107 | |
108 | public int getHeight() |
109 | { |
110 | return _height; |
111 | } |
112 | |
113 | public BufferedImage getImage() |
114 | { |
115 | ImageHolder holder = readCache(); |
116 | if (holder != null) |
117 | { |
118 | return holder.getImage(); |
119 | } |
120 | else |
121 | { |
122 | return null; |
123 | } |
124 | } |
125 | |
126 | public void paintHilites(Graphics2D graf) |
127 | { |
128 | if (!_hiliter.isEmpty()) |
129 | { |
130 | _hiliter.perform(graf); |
131 | } |
132 | } |
133 | |
134 | public void addBoardImageModelListener(BoardImageModelListener listener) |
135 | { |
136 | _listeners.add(listener); |
137 | } |
138 | |
139 | public void removeBoardImageModelListener(BoardImageModelListener listener) |
140 | { |
141 | _listeners.remove(listener); |
142 | } |
143 | |
144 | private void applyAction(byte[] action) |
145 | { |
146 | applyAction((DrawingAction) _tools.deserialize(action)); |
147 | } |
148 | |
149 | private void applyAction(DrawingAction action) |
150 | { |
151 | Rectangle area; |
152 | if (_hiliter.addAction(action)) |
153 | { |
154 | area = getHiliteRefreshArea(action); |
155 | } |
156 | else |
157 | { |
158 | if (!_hiliter.isEmpty()) |
159 | { |
160 | // Calculate refresh area to make sure all hilites zones will be cleared |
161 | area = getHiliteRefreshArea(_hiliter); |
162 | area = union(area, action.getActionArea()); |
163 | _hiliter.clear(); |
164 | } |
165 | else |
166 | { |
167 | area = action.getActionArea(); |
168 | } |
169 | ImageHolder holder = readCache(); |
170 | BufferedImage image = holder.getImage(); |
171 | action.perform(image); |
172 | holder.setImage(image); |
173 | writeCache(holder); |
174 | } |
175 | fireContentRefreshed(area); |
176 | } |
177 | |
178 | private Rectangle getHiliteRefreshArea(DrawingAction action) |
179 | { |
180 | Rectangle area = new Rectangle(action.getActionArea()); |
181 | // Grow area according to hilite stroke width |
182 | int width = (int) (_hilitePrefs.getStroke().getLineWidth() * 2.0 + 1.0); |
183 | area.grow(width, width); |
184 | return area; |
185 | } |
186 | |
187 | private void updateHilitePrefs() |
188 | { |
189 | _hiliter.setHiliteStroke(_hilitePrefs.getStroke()); |
190 | _hiliter.setHiliteColor(_hilitePrefs.getColor()); |
191 | if (!_hiliter.isEmpty()) |
192 | { |
193 | fireContentRefreshed(getHiliteRefreshArea(_hiliter)); |
194 | } |
195 | } |
196 | |
197 | static private Rectangle union(Rectangle r1, Rectangle r2) |
198 | { |
199 | if (r1 == null || r2 == null) |
200 | { |
201 | return null; |
202 | } |
203 | else |
204 | { |
205 | return r1.union(r2); |
206 | } |
207 | } |
208 | |
209 | private void fireContentRefreshed(Rectangle area) |
210 | { |
211 | for (BoardImageModelListener listener: _listeners) |
212 | { |
213 | listener.contentRefreshed(area); |
214 | } |
215 | } |
216 | |
217 | final private HiliterPrefs _hilitePrefs; |
218 | final private Board _board; |
219 | private int _width; |
220 | private int _height; |
221 | final private List<BoardImageModelListener> _listeners = |
222 | new ArrayList<BoardImageModelListener>(); |
223 | final private CompoundHiliteAction _hiliter = new CompoundHiliteAction(); |
224 | final private ObjectTools _tools; |
225 | final private DeferredConsumer _eventConsumer; |
226 | final private Consumer<Object> _prefsConsumer; |
227 | } |