svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / swing / pickercontroller / impl / EnvelopePickerControllerImpl.java @ 44002
History | View | Annotate | Download (12.9 KB)
1 |
package org.gvsig.fmap.mapcontrol.swing.pickercontroller.impl; |
---|---|
2 |
|
3 |
import org.gvsig.tools.swing.api.documentfilters.DoubleDocumentFilter; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.Image; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.net.URL; |
9 |
import javax.swing.ImageIcon; |
10 |
import javax.swing.JButton; |
11 |
import javax.swing.JTextField; |
12 |
import javax.swing.JToggleButton; |
13 |
import javax.swing.SwingConstants; |
14 |
import org.gvsig.fmap.dal.DataTypes; |
15 |
import org.gvsig.fmap.geom.Geometry; |
16 |
import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
17 |
import org.gvsig.fmap.geom.GeometryLocator; |
18 |
import org.gvsig.fmap.geom.GeometryManager; |
19 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
20 |
import org.gvsig.fmap.geom.exception.GeometryRuntimeException; |
21 |
import org.gvsig.fmap.geom.primitive.Envelope; |
22 |
import org.gvsig.fmap.geom.primitive.Point; |
23 |
import org.gvsig.fmap.mapcontrol.MapControl; |
24 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior; |
25 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
26 |
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent; |
27 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener; |
28 |
import org.gvsig.tools.IllegalValueException; |
29 |
import org.gvsig.tools.ToolsLocator; |
30 |
import org.gvsig.tools.dataTypes.CoercionException; |
31 |
import org.gvsig.tools.dataTypes.DataType; |
32 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
33 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
34 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
35 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @author jjdelcerro
|
40 |
*/
|
41 |
public class EnvelopePickerControllerImpl extends AbstractPickerController<Envelope>{ |
42 |
|
43 |
public static void selfRegister() { |
44 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
45 |
IconTheme theme = iconThemeManager.getCurrent(); |
46 |
|
47 |
URL url = ScalePickerControllerImpl.class.getClassLoader().getResource("images/picker-envelope-cursor-capture.png"); |
48 |
if( url != null ) { |
49 |
ImageIcon icon = new ImageIcon(url); |
50 |
theme.registerDefault("mapcontrol", "picker", "picker-envelope-cursor-capture", icon, url); |
51 |
} |
52 |
url = ScalePickerControllerImpl.class.getClassLoader().getResource("images/picker-envelope-capture.png");
|
53 |
if( url != null ) { |
54 |
ImageIcon icon = new ImageIcon(url); |
55 |
theme.registerDefault("mapcontrol", "picker", "picker-envelope-capture", icon, url); |
56 |
} |
57 |
url = ScalePickerControllerImpl.class.getClassLoader().getResource("images/picker-envelope-from-mapcontrol.png");
|
58 |
if( url != null ) { |
59 |
ImageIcon icon = new ImageIcon(url); |
60 |
theme.registerDefault("mapcontrol", "picker", "picker-envelope-from-mapcontrol", icon, url); |
61 |
} |
62 |
} |
63 |
|
64 |
|
65 |
private class CaptureEnvelopeListener implements RectangleListener { |
66 |
|
67 |
@Override
|
68 |
public void rectangle(EnvelopeEvent event) throws BehaviorException { |
69 |
set(event.getWorldCoordRect()); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public Image getImageCursor() { |
74 |
return captureCursor.getImage();
|
75 |
} |
76 |
|
77 |
@Override
|
78 |
public boolean cancelDrawing() { |
79 |
return false; |
80 |
} |
81 |
|
82 |
} |
83 |
|
84 |
|
85 |
private final MapControl mapControl; |
86 |
private final JTextField txtUpperLeftX; |
87 |
private final JTextField txtUpperLeftY; |
88 |
private final JTextField txtLowerRightX; |
89 |
private final JTextField txtLowerRightY; |
90 |
private final JTextField txtEnvelope; |
91 |
private final JButton btnMapControlEnvelope; |
92 |
private final JToggleButton btnCapture; |
93 |
|
94 |
private ImageIcon captureCursor; |
95 |
private RectangleBehavior captureTool;
|
96 |
private String previosTool; |
97 |
private Envelope envelope;
|
98 |
|
99 |
public EnvelopePickerControllerImpl(
|
100 |
MapControl mapControl, |
101 |
JTextField txtUpperLeftX,
|
102 |
JTextField txtUpperLeftY,
|
103 |
JTextField txtLowerRightX,
|
104 |
JTextField txtLowerRightY,
|
105 |
JButton btnMapControlEnvelope,
|
106 |
final JToggleButton btnCapture |
107 |
) { |
108 |
this.mapControl = mapControl;
|
109 |
this.txtEnvelope = null; |
110 |
this.txtUpperLeftX = txtUpperLeftX;
|
111 |
this.txtUpperLeftY = txtUpperLeftY;
|
112 |
this.txtLowerRightX = txtLowerRightX;
|
113 |
this.txtLowerRightY = txtLowerRightY;
|
114 |
this.btnMapControlEnvelope = btnMapControlEnvelope;
|
115 |
this.btnCapture = btnCapture;
|
116 |
|
117 |
this.initComponents();
|
118 |
} |
119 |
|
120 |
public EnvelopePickerControllerImpl(
|
121 |
MapControl mapControl, |
122 |
JTextField txtEnvelope,
|
123 |
JButton btnMapControlEnvelope,
|
124 |
final JToggleButton btnCapture |
125 |
) { |
126 |
this.mapControl = mapControl;
|
127 |
this.txtEnvelope = txtEnvelope;
|
128 |
this.txtUpperLeftX = null; |
129 |
this.txtUpperLeftY = null; |
130 |
this.txtLowerRightX = null; |
131 |
this.txtLowerRightY = null; |
132 |
this.btnMapControlEnvelope = btnMapControlEnvelope;
|
133 |
this.btnCapture = btnCapture;
|
134 |
|
135 |
this.initComponents();
|
136 |
} |
137 |
|
138 |
private void initComponents() { |
139 |
this.captureCursor.setImage(this.getIcon("picker-envelope-cursor-capture").getImage()); |
140 |
this.btnCapture.setIcon(this.getIcon("picker-envelope-cursor-capture")); |
141 |
this.btnMapControlEnvelope.setIcon(this.getIcon("picker-envelope-cursor-capture")); |
142 |
|
143 |
this.txtUpperLeftX.setHorizontalAlignment(SwingConstants.RIGHT); |
144 |
this.txtUpperLeftY.setHorizontalAlignment(SwingConstants.RIGHT); |
145 |
this.txtLowerRightX.setHorizontalAlignment(SwingConstants.RIGHT); |
146 |
this.txtLowerRightY.setHorizontalAlignment(SwingConstants.RIGHT); |
147 |
|
148 |
if( this.btnMapControlEnvelope!=null ) { |
149 |
this.btnMapControlEnvelope.addActionListener(new ActionListener() { |
150 |
@Override
|
151 |
public void actionPerformed(ActionEvent e) { |
152 |
doSetEnvelopeToMapControlEnvelope(); |
153 |
} |
154 |
}); |
155 |
} |
156 |
this.btnCapture.addActionListener(new ActionListener() { |
157 |
@Override
|
158 |
public void actionPerformed(ActionEvent e) { |
159 |
doCaptureEnvelope(btnCapture.isSelected()); |
160 |
} |
161 |
}); |
162 |
|
163 |
this.captureTool = new RectangleBehavior(new CaptureEnvelopeListener()); |
164 |
this.mapControl.addBehavior("picker-envelope-capture", captureTool); |
165 |
this.previosTool = this.mapControl.getCurrentTool(); |
166 |
|
167 |
if( this.txtEnvelope==null ) { |
168 |
this.txtUpperLeftX.setText("#########,00000"); |
169 |
Dimension dim = this.txtUpperLeftX.getPreferredSize(); |
170 |
this.txtUpperLeftX.setText(""); |
171 |
|
172 |
this.txtUpperLeftX.setPreferredSize(dim);
|
173 |
this.txtUpperLeftY.setPreferredSize(dim);
|
174 |
this.txtLowerRightX.setPreferredSize(dim);
|
175 |
this.txtLowerRightY.setPreferredSize(dim);
|
176 |
|
177 |
DoubleDocumentFilter.install(this.txtUpperLeftX);
|
178 |
DoubleDocumentFilter.install(this.txtUpperLeftY);
|
179 |
DoubleDocumentFilter.install(this.txtLowerRightX);
|
180 |
DoubleDocumentFilter.install(this.txtLowerRightY);
|
181 |
} else {
|
182 |
this.txtEnvelope.setEditable(false); |
183 |
} |
184 |
} |
185 |
|
186 |
protected void doSetEnvelopeToMapControlEnvelope() { |
187 |
if( !this.isEditable() ) { |
188 |
return;
|
189 |
} |
190 |
try {
|
191 |
Envelope theEnvelope = (Envelope) this.mapControl.getViewPort().getEnvelope().clone();
|
192 |
this.set(theEnvelope);
|
193 |
} catch (Exception ex) { |
194 |
LOG.warn("Can't get envelope from mapcontrol.", ex);
|
195 |
} |
196 |
} |
197 |
|
198 |
protected void doCaptureEnvelope(boolean enabled) { |
199 |
if( !this.isEditable() ) { |
200 |
return;
|
201 |
} |
202 |
if( enabled ) {
|
203 |
this.previosTool = this.mapControl.getCurrentTool(); |
204 |
this.mapControl.setTool("picker-envelope-capture"); |
205 |
} else {
|
206 |
this.mapControl.setTool(this.previosTool); |
207 |
} |
208 |
} |
209 |
|
210 |
protected String getEnvelopeAsString(Envelope value) { |
211 |
String s = String.format( |
212 |
"%+f, %+f, %+f, %+f",
|
213 |
value.getMinimum(Geometry.DIMENSIONS.X), |
214 |
value.getMaximum(Geometry.DIMENSIONS.Y), |
215 |
value.getMaximum(Geometry.DIMENSIONS.X), |
216 |
value.getMinimum(Geometry.DIMENSIONS.Y) |
217 |
); |
218 |
return s;
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
public boolean isValid() { |
223 |
if( this.txtEnvelope==null ) { |
224 |
if( !DoubleDocumentFilter.isValid(this.txtUpperLeftX) ) { |
225 |
return false; |
226 |
} |
227 |
if( !DoubleDocumentFilter.isValid(this.txtUpperLeftY) ) { |
228 |
return false; |
229 |
} |
230 |
if( !DoubleDocumentFilter.isValid(this.txtLowerRightX) ) { |
231 |
return false; |
232 |
} |
233 |
if( !DoubleDocumentFilter.isValid(this.txtLowerRightY) ) { |
234 |
return false; |
235 |
} |
236 |
return true; |
237 |
} else {
|
238 |
return true; |
239 |
} |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public boolean isEmpty() { |
244 |
if( this.txtEnvelope==null ) { |
245 |
if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) ) { |
246 |
return true; |
247 |
} |
248 |
if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftY) ) { |
249 |
return true; |
250 |
} |
251 |
if( DoubleDocumentFilter.isEmpty(this.txtLowerRightX) ) { |
252 |
return true; |
253 |
} |
254 |
if( DoubleDocumentFilter.isEmpty(this.txtLowerRightY) ) { |
255 |
return true; |
256 |
} |
257 |
return false; |
258 |
} else {
|
259 |
return this.envelope==null; |
260 |
} |
261 |
} |
262 |
|
263 |
@Override
|
264 |
public Envelope get() {
|
265 |
if( !this.isValid() ) { |
266 |
throw new IllegalValueException(); |
267 |
} |
268 |
if( this.isEmpty() ) { |
269 |
return null; |
270 |
} |
271 |
|
272 |
if( this.txtEnvelope==null ) { |
273 |
try {
|
274 |
double upperLeftX = DoubleDocumentFilter.getValue(this.txtUpperLeftX); |
275 |
double upperLeftY = DoubleDocumentFilter.getValue(this.txtUpperLeftY); |
276 |
double lowerRightX = DoubleDocumentFilter.getValue(this.txtLowerRightX); |
277 |
double lowerRightY = DoubleDocumentFilter.getValue(this.txtLowerRightY); |
278 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
279 |
Point upperLeft = geomManager.createPoint(upperLeftX, upperLeftY, Geometry.SUBTYPES.GEOM2D);
|
280 |
Point lowerRight = geomManager.createPoint(lowerRightX, lowerRightY, Geometry.SUBTYPES.GEOM2D);
|
281 |
this.envelope.setUpperCorner(upperLeft);
|
282 |
this.envelope.setLowerCorner(lowerRight);
|
283 |
} catch (CreateGeometryException ex) {
|
284 |
throw new GeometryRuntimeException("Can't create envelope", ex); |
285 |
} |
286 |
} |
287 |
return this.envelope; |
288 |
} |
289 |
|
290 |
@Override
|
291 |
public void set(Envelope envelope) { |
292 |
try {
|
293 |
this.envelope = (Envelope) envelope.clone();
|
294 |
} catch (CloneNotSupportedException ex) { |
295 |
throw new IllegalArgumentException("Can't use the envelope.", ex); |
296 |
} |
297 |
if( this.txtEnvelope==null ) { |
298 |
this.txtUpperLeftX.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.X))); |
299 |
this.txtUpperLeftY.setText(String.format("%+f", envelope.getMaximum(DIMENSIONS.Y))); |
300 |
this.txtLowerRightX.setText(String.format("%+f", envelope.getMaximum(DIMENSIONS.X))); |
301 |
this.txtLowerRightY.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.Y))); |
302 |
} else {
|
303 |
this.txtEnvelope.setText(getEnvelopeAsString(this.envelope)); |
304 |
} |
305 |
} |
306 |
|
307 |
@Override
|
308 |
public void coerceAndSet(Object value) { |
309 |
DataType dataType = ToolsLocator.getDataTypesManager() |
310 |
.get(DataTypes.ENVELOPE); |
311 |
try {
|
312 |
this.set((Envelope) dataType.coerce(value));
|
313 |
} catch (CoercionException ex) {
|
314 |
LOG.warn("Can't set value.", ex);
|
315 |
} |
316 |
} |
317 |
|
318 |
@Override
|
319 |
public void setEnabled(boolean enabled) { |
320 |
if( this.txtEnvelope==null ) { |
321 |
this.txtUpperLeftX.setEnabled(enabled);
|
322 |
this.txtUpperLeftY.setEnabled(enabled);
|
323 |
this.txtLowerRightX.setEnabled(enabled);
|
324 |
this.txtLowerRightY.setEnabled(enabled);
|
325 |
} else {
|
326 |
this.txtEnvelope.setEnabled(enabled);
|
327 |
} |
328 |
this.btnCapture.setEnabled(enabled);
|
329 |
this.btnMapControlEnvelope.setEnabled(enabled);
|
330 |
} |
331 |
|
332 |
@Override
|
333 |
public boolean isEnabled() { |
334 |
if( this.txtEnvelope!=null ) { |
335 |
return this.txtEnvelope.isEnabled(); |
336 |
} |
337 |
return true; |
338 |
} |
339 |
|
340 |
@Override
|
341 |
public void setEditable(boolean editable) { |
342 |
super.setEditable(editable);
|
343 |
if( this.txtEnvelope==null ) { |
344 |
this.txtUpperLeftX.setEditable(editable);
|
345 |
this.txtUpperLeftY.setEditable(editable);
|
346 |
this.txtLowerRightX.setEditable(editable);
|
347 |
this.txtLowerRightY.setEditable(editable);
|
348 |
} else {
|
349 |
this.txtEnvelope.setEditable(editable);
|
350 |
} |
351 |
} |
352 |
|
353 |
|
354 |
} |