root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / NewMapControl.java @ 354
History | View | Annotate | Download (7.73 KB)
1 | 230 | fernando | package com.iver.cit.gvsig.fmap; |
---|---|---|---|
2 | |||
3 | 349 | fjp | import java.awt.Color; |
4 | 230 | fernando | import java.awt.Dimension; |
5 | import java.awt.Graphics; |
||
6 | import java.awt.Graphics2D; |
||
7 | 336 | vcaballero | import java.awt.event.ActionEvent; |
8 | import java.awt.event.ActionListener; |
||
9 | 230 | fernando | import java.awt.event.ComponentEvent; |
10 | import java.awt.event.ComponentListener; |
||
11 | import java.awt.image.BufferedImage; |
||
12 | import java.util.HashMap; |
||
13 | |||
14 | import javax.swing.JComponent; |
||
15 | 338 | fernando | import javax.swing.Timer; |
16 | 230 | fernando | |
17 | 330 | vcaballero | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
18 | 338 | fernando | import com.iver.cit.gvsig.fmap.operations.Cancellable; |
19 | 345 | vcaballero | import com.iver.cit.gvsig.fmap.tools.Behavior.MapTool; |
20 | 330 | vcaballero | import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
21 | 230 | fernando | |
22 | 330 | vcaballero | |
23 | 230 | fernando | /**
|
24 | * DOCUMENT ME!
|
||
25 | *
|
||
26 | * @author Fernando Gonz?lez Cort?s
|
||
27 | */
|
||
28 | public class NewMapControl extends JComponent implements ComponentListener { |
||
29 | 338 | fernando | /** DOCUMENT ME! */
|
30 | 330 | vcaballero | public static final int ACTUALIZADO = 0; |
31 | 230 | fernando | |
32 | 338 | fernando | /** DOCUMENT ME! */
|
33 | 330 | vcaballero | public static final int DESACTUALIZADO = 1; |
34 | private FMap mapContext = null; |
||
35 | private HashMap namesMapTools = new HashMap(); |
||
36 | private HashMap namesMapToolsNames = new HashMap(); |
||
37 | private MapTool currentMapTool = null; |
||
38 | private int status = ACTUALIZADO; |
||
39 | private BufferedImage image = null; |
||
40 | private String currentTool; |
||
41 | private CancelDraw canceldraw;
|
||
42 | 338 | fernando | private boolean isCancelled = true; |
43 | private Timer timer; |
||
44 | private ViewPort vp;
|
||
45 | 349 | fjp | private Color backColor = Color.WHITE; |
46 | 338 | fernando | |
47 | 330 | vcaballero | /**
|
48 | * Crea un nuevo NewMapControl.
|
||
49 | */
|
||
50 | public NewMapControl() {
|
||
51 | setDoubleBuffered(true);
|
||
52 | 339 | fernando | |
53 | //Clase usada para cancelar el dibujado
|
||
54 | 338 | fernando | canceldraw = new CancelDraw();
|
55 | |||
56 | 339 | fernando | //Modelo de datos y ventana del mismo
|
57 | 338 | fernando | vp = new ViewPort();
|
58 | 330 | vcaballero | mapContext = new FMap(vp);
|
59 | 230 | fernando | |
60 | 339 | fernando | //eventos
|
61 | 330 | vcaballero | this.addComponentListener(this); |
62 | 338 | fernando | |
63 | 339 | fernando | //Timer para mostrar el redibujado mientras se dibuja
|
64 | 338 | fernando | timer = new Timer(300, new ActionListener() { |
65 | public void actionPerformed(ActionEvent e) { |
||
66 | NewMapControl.this.repaint(); |
||
67 | } |
||
68 | }); |
||
69 | 330 | vcaballero | } |
70 | 230 | fernando | |
71 | 330 | vcaballero | /**
|
72 | * DOCUMENT ME!
|
||
73 | *
|
||
74 | * @param model DOCUMENT ME!
|
||
75 | */
|
||
76 | public void setMapContext(FMap model) { |
||
77 | mapContext = model; |
||
78 | 338 | fernando | mapContext.setViewPort(vp); |
79 | 330 | vcaballero | } |
80 | 230 | fernando | |
81 | 330 | vcaballero | /**
|
82 | * DOCUMENT ME!
|
||
83 | *
|
||
84 | * @return
|
||
85 | */
|
||
86 | public FMap getMapContext() {
|
||
87 | return mapContext;
|
||
88 | } |
||
89 | 230 | fernando | |
90 | 330 | vcaballero | /**
|
91 | * Registra una herramienta (tool).
|
||
92 | *
|
||
93 | * @param name DOCUMENT ME!
|
||
94 | * @param tool
|
||
95 | */
|
||
96 | public void addMapTool(String name, MapTool tool) { |
||
97 | namesMapTools.put(name, tool); |
||
98 | tool.setMapControl(this);
|
||
99 | } |
||
100 | 230 | fernando | |
101 | 330 | vcaballero | /**
|
102 | * DOCUMENT ME!
|
||
103 | *
|
||
104 | * @param toolName DOCUMENT ME!
|
||
105 | * @param mapToolName DOCUMENT ME!
|
||
106 | * @param tl DOCUMENT ME!
|
||
107 | */
|
||
108 | public void addTool(String toolName, String mapToolName, ToolListener tl) { |
||
109 | namesMapToolsNames.put(toolName, mapToolName); |
||
110 | 230 | fernando | |
111 | 330 | vcaballero | MapTool mt = (MapTool) namesMapTools.get(mapToolName); |
112 | mt.setListener(tl); |
||
113 | } |
||
114 | 230 | fernando | |
115 | 330 | vcaballero | /**
|
116 | * DOCUMENT ME!
|
||
117 | *
|
||
118 | * @param toolName DOCUMENT ME!
|
||
119 | */
|
||
120 | public void setTool(String toolName) { |
||
121 | if (currentMapTool != null) { |
||
122 | this.removeMouseListener(currentMapTool);
|
||
123 | this.removeMouseMotionListener(currentMapTool);
|
||
124 | this.removeMouseWheelListener(currentMapTool);
|
||
125 | } |
||
126 | 230 | fernando | |
127 | 330 | vcaballero | String mapToolName = (String) namesMapToolsNames.get(toolName); |
128 | MapTool mapTool = (MapTool) namesMapTools.get(mapToolName); |
||
129 | currentMapTool = mapTool; |
||
130 | currentTool = toolName; |
||
131 | 230 | fernando | |
132 | 330 | vcaballero | this.addMouseListener(currentMapTool);
|
133 | this.addMouseMotionListener(currentMapTool);
|
||
134 | this.addMouseWheelListener(currentMapTool);
|
||
135 | } |
||
136 | 230 | fernando | |
137 | 330 | vcaballero | /**
|
138 | * DOCUMENT ME!
|
||
139 | *
|
||
140 | * @return DOCUMENT ME!
|
||
141 | */
|
||
142 | public String getTool() { |
||
143 | return currentTool;
|
||
144 | } |
||
145 | |||
146 | /**
|
||
147 | * Cancelar el dibujado.
|
||
148 | */
|
||
149 | public void cancelDrawing() { |
||
150 | //TODO Implementar el cancelado bien
|
||
151 | 338 | fernando | canceldraw.setCancel(true);
|
152 | } |
||
153 | 330 | vcaballero | |
154 | /**
|
||
155 | * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
||
156 | */
|
||
157 | protected void paintComponent(Graphics g) { |
||
158 | if (status == ACTUALIZADO) {
|
||
159 | if (currentMapTool != null) { |
||
160 | currentMapTool.paintComponent(g); |
||
161 | } |
||
162 | } else if (status == DESACTUALIZADO) { |
||
163 | image = new BufferedImage(this.getWidth(), this.getHeight(), |
||
164 | BufferedImage.TYPE_INT_ARGB);
|
||
165 | 349 | fjp | image.getGraphics().setColor(backColor); |
166 | image.getGraphics().fillRect(0,0,image.getWidth(), image.getHeight()); |
||
167 | 330 | vcaballero | |
168 | 349 | fjp | g.setColor(backColor); |
169 | g.fillRect(0,0,image.getWidth(), image.getHeight()); |
||
170 | |||
171 | 330 | vcaballero | cancelDrawing(); |
172 | 338 | fernando | |
173 | while (!isCancelled) {
|
||
174 | } |
||
175 | |||
176 | isCancelled = false;
|
||
177 | |||
178 | Drawer drawer = new Drawer(image, (Graphics2D) image.getGraphics(), |
||
179 | canceldraw); |
||
180 | drawer.start(); |
||
181 | timer.start(); |
||
182 | status = ACTUALIZADO; |
||
183 | 330 | vcaballero | } |
184 | } |
||
185 | |||
186 | /**
|
||
187 | * DOCUMENT ME!
|
||
188 | *
|
||
189 | * @return
|
||
190 | */
|
||
191 | public BufferedImage getImage() { |
||
192 | return image;
|
||
193 | } |
||
194 | |||
195 | /**
|
||
196 | * DOCUMENT ME!
|
||
197 | */
|
||
198 | public void drawMap() { |
||
199 | status = DESACTUALIZADO; |
||
200 | repaint(); |
||
201 | } |
||
202 | |||
203 | /**
|
||
204 | * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
||
205 | */
|
||
206 | public void componentHidden(ComponentEvent e) { |
||
207 | } |
||
208 | |||
209 | /**
|
||
210 | * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
||
211 | */
|
||
212 | public void componentMoved(ComponentEvent e) { |
||
213 | } |
||
214 | |||
215 | /**
|
||
216 | * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
||
217 | */
|
||
218 | public void componentResized(ComponentEvent e) { |
||
219 | 338 | fernando | getMapContext().getViewPort().setImageSize(new Dimension(getWidth(), |
220 | 330 | vcaballero | getHeight())); |
221 | drawMap(); |
||
222 | } |
||
223 | |||
224 | /**
|
||
225 | * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
||
226 | */
|
||
227 | public void componentShown(ComponentEvent e) { |
||
228 | } |
||
229 | |||
230 | /**
|
||
231 | * DOCUMENT ME!
|
||
232 | *
|
||
233 | * @author Vicente Caballero Navarro
|
||
234 | */
|
||
235 | public class Drawer extends Thread { |
||
236 | 338 | fernando | private Graphics g; |
237 | 330 | vcaballero | private BufferedImage image; |
238 | private CancelDraw cancel;
|
||
239 | 338 | fernando | private boolean threadCancel = false; |
240 | |||
241 | 330 | vcaballero | /**
|
242 | * Crea un nuevo Drawer.
|
||
243 | *
|
||
244 | * @param image DOCUMENT ME!
|
||
245 | * @param g DOCUMENT ME!
|
||
246 | 338 | fernando | * @param cancel DOCUMENT ME!
|
247 | 330 | vcaballero | */
|
248 | 338 | fernando | public Drawer(BufferedImage image, Graphics g, CancelDraw cancel) { |
249 | this.g = g;
|
||
250 | 330 | vcaballero | this.image = image;
|
251 | 338 | fernando | this.cancel = cancel;
|
252 | } |
||
253 | 330 | vcaballero | |
254 | /**
|
||
255 | * @see java.lang.Runnable#run()
|
||
256 | */
|
||
257 | public void run() { |
||
258 | try {
|
||
259 | synchronized (Drawer.class) {
|
||
260 | 338 | fernando | mapContext.draw(image, (Graphics2D) image.getGraphics(),
|
261 | cancel); |
||
262 | timer.stop(); |
||
263 | isCancelled = true;
|
||
264 | repaint(); |
||
265 | 330 | vcaballero | } |
266 | } catch (DriverIOException e) {
|
||
267 | } finally {
|
||
268 | } |
||
269 | 338 | fernando | } |
270 | } |
||
271 | 330 | vcaballero | |
272 | 338 | fernando | /**
|
273 | * DOCUMENT ME!
|
||
274 | *
|
||
275 | * @author Fernando Gonz?lez Cort?s
|
||
276 | */
|
||
277 | public class CancelDraw implements Cancellable { |
||
278 | private boolean isCanceled = false; |
||
279 | |||
280 | /**
|
||
281 | * Crea un nuevo CancelDraw.
|
||
282 | */
|
||
283 | public CancelDraw() {
|
||
284 | 330 | vcaballero | } |
285 | 336 | vcaballero | |
286 | 338 | fernando | /**
|
287 | * DOCUMENT ME!
|
||
288 | *
|
||
289 | * @param b DOCUMENT ME!
|
||
290 | */
|
||
291 | public void setCancel(boolean b) { |
||
292 | isCanceled = b; |
||
293 | 336 | vcaballero | } |
294 | 338 | fernando | |
295 | /**
|
||
296 | * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
|
||
297 | */
|
||
298 | public boolean isCanceled() { |
||
299 | return false; |
||
300 | } |
||
301 | 330 | vcaballero | } |
302 | 349 | fjp | public Color getBackColor() { |
303 | return backColor;
|
||
304 | } |
||
305 | public void setBackColor(Color backColor) { |
||
306 | this.backColor = backColor;
|
||
307 | } |
||
308 | 230 | fernando | } |