root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / NewMapControl.java @ 284
History | View | Annotate | Download (3.97 KB)
1 |
package com.iver.cit.gvsig.fmap; |
---|---|
2 |
|
3 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
4 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
5 |
import com.iver.cit.gvsig.fmap.tools.MapTools.MapTool; |
6 |
|
7 |
import java.awt.Dimension; |
8 |
import java.awt.Graphics; |
9 |
import java.awt.Graphics2D; |
10 |
import java.awt.event.ComponentEvent; |
11 |
import java.awt.event.ComponentListener; |
12 |
import java.awt.event.MouseListener; |
13 |
import java.awt.image.BufferedImage; |
14 |
|
15 |
import java.util.HashMap; |
16 |
|
17 |
import javax.swing.JComponent; |
18 |
|
19 |
|
20 |
/**
|
21 |
* DOCUMENT ME!
|
22 |
*
|
23 |
* @author Fernando Gonz?lez Cort?s
|
24 |
*/
|
25 |
public class NewMapControl extends JComponent implements ComponentListener { |
26 |
public static final int ACTUALIZADO = 0; |
27 |
public static final int DESACTUALIZADO = 1; |
28 |
private FMap mapContext = null; |
29 |
private HashMap namesMapTools = new HashMap(); |
30 |
private HashMap namesMapToolsNames = new HashMap(); |
31 |
private MapTool currentMapTool = null; |
32 |
private int status = ACTUALIZADO; |
33 |
private BufferedImage image = null; |
34 |
private String currentTool; |
35 |
|
36 |
|
37 |
/**
|
38 |
* Crea un nuevo NewMapControl.
|
39 |
*/
|
40 |
public NewMapControl() {
|
41 |
setDoubleBuffered(true);
|
42 |
|
43 |
ViewPort vp = new ViewPort();
|
44 |
mapContext = new FMap(vp);
|
45 |
|
46 |
this.addComponentListener(this); |
47 |
} |
48 |
|
49 |
/**
|
50 |
* DOCUMENT ME!
|
51 |
*
|
52 |
* @param model DOCUMENT ME!
|
53 |
*/
|
54 |
public void setMapContext(FMap model) { |
55 |
mapContext = model; |
56 |
} |
57 |
|
58 |
/**
|
59 |
* DOCUMENT ME!
|
60 |
*
|
61 |
* @return
|
62 |
*/
|
63 |
public FMap getMapContext() {
|
64 |
return mapContext;
|
65 |
} |
66 |
|
67 |
/**
|
68 |
* Registra una herramienta (tool).
|
69 |
*
|
70 |
* @param name DOCUMENT ME!
|
71 |
* @param tool
|
72 |
*/
|
73 |
public void addMapTool(String name, MapTool tool) { |
74 |
namesMapTools.put(name, tool); |
75 |
tool.setMapControl(this);
|
76 |
} |
77 |
|
78 |
public void addTool(String toolName, String mapToolName,ToolListener tl){ |
79 |
namesMapToolsNames.put(toolName, mapToolName); |
80 |
MapTool mt = (MapTool) namesMapTools.get(mapToolName); |
81 |
mt.setListener(tl); |
82 |
} |
83 |
|
84 |
public void setTool(String toolName){ |
85 |
if (currentMapTool != null){ |
86 |
this.removeMouseListener(currentMapTool);
|
87 |
this.removeMouseMotionListener(currentMapTool);
|
88 |
this.removeMouseWheelListener(currentMapTool);
|
89 |
} |
90 |
|
91 |
String mapToolName = (String) namesMapToolsNames.get(toolName); |
92 |
MapTool mapTool = (MapTool) namesMapTools.get(mapToolName); |
93 |
currentMapTool = mapTool; |
94 |
currentTool = toolName; |
95 |
|
96 |
this.addMouseListener(currentMapTool);
|
97 |
this.addMouseMotionListener(currentMapTool);
|
98 |
this.addMouseWheelListener(currentMapTool);
|
99 |
} |
100 |
|
101 |
public String getTool(){ |
102 |
return currentTool;
|
103 |
} |
104 |
|
105 |
/**
|
106 |
* Cancelar el dibujado.
|
107 |
*/
|
108 |
public void cancelDrawing() { |
109 |
//TODO Implementar el cancelado bien
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
114 |
*/
|
115 |
protected void paintComponent(Graphics g) { |
116 |
if (status == ACTUALIZADO) {
|
117 |
if (currentMapTool != null) { |
118 |
currentMapTool.paintComponent(g); |
119 |
} |
120 |
} else if (status == DESACTUALIZADO) { |
121 |
image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB); |
122 |
try {
|
123 |
//TODO lanzar un thread a parte que dibuje
|
124 |
mapContext.draw(image, (Graphics2D) image.getGraphics());
|
125 |
status = ACTUALIZADO; |
126 |
repaint(); |
127 |
} catch (DriverIOException e) {
|
128 |
e.printStackTrace(); |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
/**
|
134 |
* DOCUMENT ME!
|
135 |
*
|
136 |
* @return
|
137 |
*/
|
138 |
public BufferedImage getImage() { |
139 |
return image;
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* DOCUMENT ME!
|
144 |
*/
|
145 |
public void drawMap() { |
146 |
status = DESACTUALIZADO; |
147 |
repaint(); |
148 |
} |
149 |
|
150 |
/**
|
151 |
* @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
|
152 |
*/
|
153 |
public void componentHidden(ComponentEvent e) { |
154 |
} |
155 |
|
156 |
/**
|
157 |
* @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
|
158 |
*/
|
159 |
public void componentMoved(ComponentEvent e) { |
160 |
} |
161 |
|
162 |
/**
|
163 |
* @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
|
164 |
*/
|
165 |
public void componentResized(ComponentEvent e) { |
166 |
getMapContext().getViewPort().setImageSize(new Dimension(getWidth(), getHeight())); |
167 |
drawMap(); |
168 |
} |
169 |
|
170 |
/**
|
171 |
* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
|
172 |
*/
|
173 |
public void componentShown(ComponentEvent e) { |
174 |
} |
175 |
} |