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 @ 47426
History | View | Annotate | Download (16.8 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.net.URL; |
8 |
import javax.swing.ImageIcon; |
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JTextField; |
11 |
import javax.swing.JToggleButton; |
12 |
import javax.swing.SwingConstants; |
13 |
import javax.swing.text.JTextComponent; |
14 |
import org.apache.commons.lang.StringUtils; |
15 |
import org.gvsig.fmap.dal.DataTypes; |
16 |
import org.gvsig.fmap.geom.Geometry; |
17 |
import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
18 |
import org.gvsig.fmap.geom.GeometryLocator; |
19 |
import org.gvsig.fmap.geom.GeometryManager; |
20 |
import org.gvsig.fmap.geom.GeometryUtils; |
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.i18n.I18nManager; |
33 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
34 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
35 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
36 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
37 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
38 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
39 |
|
40 |
/**
|
41 |
*
|
42 |
* @author jjdelcerro
|
43 |
*/
|
44 |
@SuppressWarnings("UseSpecificCatch") |
45 |
public class EnvelopePickerControllerImpl extends AbstractPickerController<Envelope>{ |
46 |
|
47 |
public static void selfRegister() { |
48 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
49 |
IconTheme theme = iconThemeManager.getActive(); |
50 |
|
51 |
URL url = ScalePickerControllerImpl.class.getClassLoader().getResource("images/picker-envelope-cursor-capture.png"); |
52 |
if( url != null ) { |
53 |
ImageIcon icon = new ImageIcon(url); |
54 |
theme.registerDefault("mapcontrol", "picker", "picker-envelope", "picker-envelope-cursor-capture", icon, url); |
55 |
} |
56 |
url = ScalePickerControllerImpl.class.getClassLoader().getResource("images/picker-envelope-capture.png");
|
57 |
if( url != null ) { |
58 |
ImageIcon icon = new ImageIcon(url); |
59 |
theme.registerDefault("mapcontrol", "picker", "picker-envelope", "picker-envelope-capture", icon, url); |
60 |
} |
61 |
url = ScalePickerControllerImpl.class.getClassLoader().getResource("images/picker-envelope-from-mapcontrol.png");
|
62 |
if( url != null ) { |
63 |
ImageIcon icon = new ImageIcon(url); |
64 |
theme.registerDefault("mapcontrol", "picker", "picker-envelope", "picker-envelope-from-mapcontrol", icon, url); |
65 |
} |
66 |
ToolsSwingUtils.registerSubgroupIconScreenshot( |
67 |
EnvelopePickerControllerImpl.class, |
68 |
"picker",
|
69 |
"picker-envelope",
|
70 |
"/images/screenshots/picker-envelope1.png"
|
71 |
); |
72 |
} |
73 |
|
74 |
|
75 |
private class CaptureEnvelopeListener implements RectangleListener { |
76 |
|
77 |
@Override
|
78 |
public void rectangle(EnvelopeEvent event) throws BehaviorException { |
79 |
set(event.getWorldCoordRect()); |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public Image getImageCursor() { |
84 |
return captureCursor.getImage();
|
85 |
} |
86 |
|
87 |
@Override
|
88 |
public boolean cancelDrawing() { |
89 |
return false; |
90 |
} |
91 |
|
92 |
} |
93 |
|
94 |
|
95 |
private final MapControl mapControl; |
96 |
private final JTextField txtUpperLeftX; |
97 |
private final JTextField txtUpperLeftY; |
98 |
private final JTextField txtLowerRightX; |
99 |
private final JTextField txtLowerRightY; |
100 |
private final JTextComponent txtEnvelope; |
101 |
private final JButton btnMapControlEnvelope; |
102 |
private final JToggleButton btnCapture; |
103 |
|
104 |
private ImageIcon captureCursor; |
105 |
private RectangleBehavior captureTool;
|
106 |
private String previosTool; |
107 |
|
108 |
public EnvelopePickerControllerImpl(
|
109 |
MapControl mapControl, |
110 |
JTextField txtUpperLeftX,
|
111 |
JTextField txtUpperLeftY,
|
112 |
JTextField txtLowerRightX,
|
113 |
JTextField txtLowerRightY,
|
114 |
JButton btnMapControlEnvelope,
|
115 |
final JToggleButton btnCapture |
116 |
) { |
117 |
this.mapControl = mapControl;
|
118 |
this.txtEnvelope = null; |
119 |
this.txtUpperLeftX = txtUpperLeftX;
|
120 |
this.txtUpperLeftY = txtUpperLeftY;
|
121 |
this.txtLowerRightX = txtLowerRightX;
|
122 |
this.txtLowerRightY = txtLowerRightY;
|
123 |
this.btnMapControlEnvelope = btnMapControlEnvelope;
|
124 |
this.btnCapture = btnCapture;
|
125 |
|
126 |
this.initComponents();
|
127 |
} |
128 |
|
129 |
public EnvelopePickerControllerImpl(
|
130 |
MapControl mapControl, |
131 |
JTextComponent txtEnvelope,
|
132 |
JButton btnMapControlEnvelope,
|
133 |
final JToggleButton btnCapture |
134 |
) { |
135 |
this.mapControl = mapControl;
|
136 |
this.txtEnvelope = txtEnvelope;
|
137 |
this.txtUpperLeftX = new JTextField(); |
138 |
this.txtUpperLeftY = new JTextField(); |
139 |
this.txtLowerRightX = new JTextField(); |
140 |
this.txtLowerRightY = new JTextField(); |
141 |
this.btnMapControlEnvelope = btnMapControlEnvelope;
|
142 |
this.btnCapture = btnCapture;
|
143 |
|
144 |
this.initComponents();
|
145 |
} |
146 |
|
147 |
private void initComponents() { |
148 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
149 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
150 |
this.captureCursor = new ImageIcon(this.getIcon("picker-envelope-cursor-capture").getImage()); |
151 |
|
152 |
if( this.btnCapture!=null ) { |
153 |
this.btnCapture.setIcon(this.getIcon("picker-envelope-capture")); |
154 |
this.btnCapture.setToolTipText(i18n.getTranslation("_Capture_an_area_in_the_view")); |
155 |
if( this.btnCapture.getText().equals("...") ) { |
156 |
this.btnCapture.setText(""); |
157 |
} |
158 |
this.btnCapture.addActionListener((ActionEvent e) -> { |
159 |
doCaptureEnvelope(btnCapture.isSelected()); |
160 |
}); |
161 |
this.btnCapture.setEnabled(this.mapControl!=null); |
162 |
} |
163 |
if( this.btnMapControlEnvelope!=null ) { |
164 |
this.btnMapControlEnvelope.setIcon(this.getIcon("picker-envelope-from-mapcontrol")); |
165 |
this.btnMapControlEnvelope.setToolTipText(i18n.getTranslation("_Capture_the_view_area")); |
166 |
if( this.btnMapControlEnvelope.getText().equals("...") ) { |
167 |
this.btnMapControlEnvelope.setText(""); |
168 |
} |
169 |
this.btnMapControlEnvelope.addActionListener((ActionEvent e) -> { |
170 |
doSetEnvelopeFromMapControlEnvelope(); |
171 |
}); |
172 |
this.btnMapControlEnvelope.setEnabled(this.mapControl!=null); |
173 |
} |
174 |
if( this.mapControl!=null ) { |
175 |
this.captureTool = new RectangleBehavior(new CaptureEnvelopeListener()); |
176 |
this.mapControl.addBehavior("picker-envelope-capture", captureTool); |
177 |
this.previosTool = this.mapControl.getCurrentTool(); |
178 |
} |
179 |
|
180 |
if( this.txtEnvelope==null ) { |
181 |
toolsSwingManager.addClearButton(this.txtUpperLeftX);
|
182 |
toolsSwingManager.addClearButton(this.txtUpperLeftY);
|
183 |
toolsSwingManager.addClearButton(this.txtLowerRightX);
|
184 |
toolsSwingManager.addClearButton(this.txtLowerRightY);
|
185 |
|
186 |
toolsSwingManager.setDefaultPopupMenu(this.txtUpperLeftX);
|
187 |
toolsSwingManager.setDefaultPopupMenu(this.txtUpperLeftY);
|
188 |
toolsSwingManager.setDefaultPopupMenu(this.txtLowerRightX);
|
189 |
toolsSwingManager.setDefaultPopupMenu(this.txtLowerRightY);
|
190 |
|
191 |
this.txtUpperLeftX.setHorizontalAlignment(SwingConstants.RIGHT); |
192 |
this.txtUpperLeftY.setHorizontalAlignment(SwingConstants.RIGHT); |
193 |
this.txtLowerRightX.setHorizontalAlignment(SwingConstants.RIGHT); |
194 |
this.txtLowerRightY.setHorizontalAlignment(SwingConstants.RIGHT); |
195 |
|
196 |
this.txtUpperLeftX.setText("#########,00000"); |
197 |
Dimension dim = this.txtUpperLeftX.getPreferredSize(); |
198 |
this.txtUpperLeftX.setText(""); |
199 |
|
200 |
this.txtUpperLeftX.setPreferredSize(dim);
|
201 |
this.txtUpperLeftY.setPreferredSize(dim);
|
202 |
this.txtLowerRightX.setPreferredSize(dim);
|
203 |
this.txtLowerRightY.setPreferredSize(dim);
|
204 |
|
205 |
DoubleDocumentFilter.install(this.txtUpperLeftX);
|
206 |
DoubleDocumentFilter.install(this.txtUpperLeftY);
|
207 |
DoubleDocumentFilter.install(this.txtLowerRightX);
|
208 |
DoubleDocumentFilter.install(this.txtLowerRightY);
|
209 |
} else {
|
210 |
toolsSwingManager.addClearButton(this.txtEnvelope);
|
211 |
toolsSwingManager.setDefaultPopupMenu(this.txtEnvelope);
|
212 |
// this.txtEnvelope.setEditable(false);
|
213 |
} |
214 |
} |
215 |
|
216 |
protected void doSetEnvelopeFromMapControlEnvelope() { |
217 |
if( !this.isEditable() || mapControl==null ) { |
218 |
return;
|
219 |
} |
220 |
try {
|
221 |
Envelope env = this.mapControl.getViewPort().getEnvelope();
|
222 |
if( env == null ) { |
223 |
return;
|
224 |
} |
225 |
Envelope theEnvelope = (Envelope) env.clone(); |
226 |
this.set(theEnvelope);
|
227 |
} catch (Exception ex) { |
228 |
LOG.warn("Can't get envelope from mapcontrol.", ex);
|
229 |
} |
230 |
} |
231 |
|
232 |
protected void doCaptureEnvelope(boolean enabled) { |
233 |
if( !this.isEditable() || mapControl==null ) { |
234 |
return;
|
235 |
} |
236 |
Envelope env = this.mapControl.getViewPort().getEnvelope();
|
237 |
if( env == null ) { |
238 |
return;
|
239 |
} |
240 |
if( enabled ) {
|
241 |
this.previosTool = this.mapControl.getCurrentTool(); |
242 |
this.mapControl.setTool("picker-envelope-capture"); |
243 |
} else {
|
244 |
this.mapControl.setTool(this.previosTool); |
245 |
} |
246 |
} |
247 |
|
248 |
protected String getEnvelopeAsString(Envelope value) { |
249 |
String s = String.format( |
250 |
"%+f, %+f, %+f, %+f",
|
251 |
value.getMinimum(Geometry.DIMENSIONS.X), |
252 |
value.getMaximum(Geometry.DIMENSIONS.Y), |
253 |
value.getMaximum(Geometry.DIMENSIONS.X), |
254 |
value.getMinimum(Geometry.DIMENSIONS.Y) |
255 |
); |
256 |
return s;
|
257 |
} |
258 |
|
259 |
@Override
|
260 |
public boolean isValid() { |
261 |
if( this.txtEnvelope==null ) { |
262 |
if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) |
263 |
&& DoubleDocumentFilter.isEmpty(this.txtUpperLeftY)
|
264 |
&& DoubleDocumentFilter.isEmpty(this.txtLowerRightX)
|
265 |
&& DoubleDocumentFilter.isEmpty(this.txtLowerRightY) ) {
|
266 |
return true; |
267 |
} |
268 |
if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) |
269 |
|| DoubleDocumentFilter.isEmpty(this.txtUpperLeftY)
|
270 |
|| DoubleDocumentFilter.isEmpty(this.txtLowerRightX)
|
271 |
|| DoubleDocumentFilter.isEmpty(this.txtLowerRightY) ) {
|
272 |
return false; |
273 |
} |
274 |
if( !DoubleDocumentFilter.isValid(this.txtUpperLeftX) ) { |
275 |
return false; |
276 |
} |
277 |
if( !DoubleDocumentFilter.isValid(this.txtUpperLeftY) ) { |
278 |
return false; |
279 |
} |
280 |
if( !DoubleDocumentFilter.isValid(this.txtLowerRightX) ) { |
281 |
return false; |
282 |
} |
283 |
if( !DoubleDocumentFilter.isValid(this.txtLowerRightY) ) { |
284 |
return false; |
285 |
} |
286 |
return true; |
287 |
} else {
|
288 |
if( StringUtils.isBlank(this.txtEnvelope.getText()) ) { |
289 |
return true; |
290 |
} |
291 |
try {
|
292 |
GeometryUtils.createFrom(this.txtEnvelope.getText());
|
293 |
} catch(Exception ex) { |
294 |
return false; |
295 |
} |
296 |
return true; |
297 |
} |
298 |
} |
299 |
|
300 |
@Override
|
301 |
public boolean isEmpty() { |
302 |
if( this.txtEnvelope==null ) { |
303 |
if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) |
304 |
&& DoubleDocumentFilter.isEmpty(this.txtUpperLeftY)
|
305 |
&& DoubleDocumentFilter.isEmpty(this.txtLowerRightX)
|
306 |
&& DoubleDocumentFilter.isEmpty(this.txtLowerRightY) ) {
|
307 |
return true; |
308 |
} |
309 |
return false; |
310 |
} else {
|
311 |
return StringUtils.isBlank(this.txtEnvelope.getText()); |
312 |
} |
313 |
} |
314 |
|
315 |
@Override
|
316 |
public Envelope get() {
|
317 |
if( this.isEmpty() ) { |
318 |
return null; |
319 |
} |
320 |
if( !this.isValid() ) { |
321 |
throw new IllegalValueException(); |
322 |
} |
323 |
Envelope envelope = null;
|
324 |
if( this.txtEnvelope==null ) { |
325 |
try {
|
326 |
double upperLeftX = DoubleDocumentFilter.getValue(this.txtUpperLeftX); |
327 |
double upperLeftY = DoubleDocumentFilter.getValue(this.txtUpperLeftY); |
328 |
double lowerRightX = DoubleDocumentFilter.getValue(this.txtLowerRightX); |
329 |
double lowerRightY = DoubleDocumentFilter.getValue(this.txtLowerRightY); |
330 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
331 |
envelope = geomManager.createEnvelope(Geometry.SUBTYPES.GEOM2D); |
332 |
Point upperLeft = geomManager.createPoint(upperLeftX, upperLeftY, Geometry.SUBTYPES.GEOM2D);
|
333 |
Point lowerRight = geomManager.createPoint(lowerRightX, lowerRightY, Geometry.SUBTYPES.GEOM2D);
|
334 |
envelope.setUpperCorner(upperLeft); |
335 |
envelope.setLowerCorner(lowerRight); |
336 |
} catch (Exception ex) { |
337 |
return null; |
338 |
} |
339 |
} else {
|
340 |
try {
|
341 |
Geometry geom = GeometryUtils.createFrom(this.txtEnvelope.getText());
|
342 |
envelope = geom.getEnvelope(); |
343 |
} catch(Exception ex) { |
344 |
return null; |
345 |
} |
346 |
} |
347 |
if( this.mapControl!=null ) { |
348 |
envelope.setProjection(this.mapControl.getProjection());
|
349 |
} |
350 |
return envelope;
|
351 |
} |
352 |
|
353 |
@Override
|
354 |
public void set(Envelope envelope) { |
355 |
if( envelope == null ) { |
356 |
this.clear();
|
357 |
return;
|
358 |
} |
359 |
if( envelope.isCollapsed() ) {
|
360 |
this.clear();
|
361 |
return;
|
362 |
} |
363 |
if( this.txtEnvelope==null ) { |
364 |
this.txtUpperLeftX.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.X))); |
365 |
this.txtUpperLeftY.setText(String.format("%+f", envelope.getMaximum(DIMENSIONS.Y))); |
366 |
this.txtLowerRightX.setText(String.format("%+f", envelope.getMaximum(DIMENSIONS.X))); |
367 |
this.txtLowerRightY.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.Y))); |
368 |
} else {
|
369 |
this.txtEnvelope.setText(envelope.getBox2D().convertToWKTQuietly());
|
370 |
} |
371 |
fireChangeEvent(); |
372 |
} |
373 |
|
374 |
@Override
|
375 |
public void coerceAndSet(Object value) { |
376 |
if( value == null ) { |
377 |
this.clear();
|
378 |
return;
|
379 |
} |
380 |
DataType dataType = ToolsLocator.getDataTypesManager() |
381 |
.get(DataTypes.ENVELOPE); |
382 |
try {
|
383 |
this.set((Envelope) dataType.coerce(value));
|
384 |
} catch (CoercionException ex) {
|
385 |
LOG.warn("Can't set value.", ex);
|
386 |
} |
387 |
} |
388 |
|
389 |
@Override
|
390 |
public void setEnabled(boolean enabled) { |
391 |
if( this.txtEnvelope==null ) { |
392 |
this.txtUpperLeftX.setEnabled(enabled);
|
393 |
this.txtUpperLeftY.setEnabled(enabled);
|
394 |
this.txtLowerRightX.setEnabled(enabled);
|
395 |
this.txtLowerRightY.setEnabled(enabled);
|
396 |
} else {
|
397 |
this.txtEnvelope.setEnabled(enabled);
|
398 |
} |
399 |
if( this.mapControl == null ) { |
400 |
this.btnCapture.setEnabled(false); |
401 |
this.btnMapControlEnvelope.setEnabled(false); |
402 |
} else {
|
403 |
this.btnCapture.setEnabled(enabled);
|
404 |
this.btnMapControlEnvelope.setEnabled(enabled);
|
405 |
} |
406 |
} |
407 |
|
408 |
@Override
|
409 |
public boolean isEnabled() { |
410 |
if( this.txtEnvelope!=null ) { |
411 |
return this.txtEnvelope.isEnabled(); |
412 |
} |
413 |
return true; |
414 |
} |
415 |
|
416 |
@Override
|
417 |
public void setEditable(boolean editable) { |
418 |
super.setEditable(editable);
|
419 |
if( this.txtEnvelope==null ) { |
420 |
this.txtUpperLeftX.setEditable(editable);
|
421 |
this.txtUpperLeftY.setEditable(editable);
|
422 |
this.txtLowerRightX.setEditable(editable);
|
423 |
this.txtLowerRightY.setEditable(editable);
|
424 |
} else {
|
425 |
this.txtEnvelope.setEditable(editable);
|
426 |
} |
427 |
} |
428 |
|
429 |
public void clear() { |
430 |
if( this.txtEnvelope==null ) { |
431 |
this.txtUpperLeftX.setText(""); |
432 |
this.txtUpperLeftY.setText(""); |
433 |
this.txtLowerRightX.setText(""); |
434 |
this.txtLowerRightY.setText(""); |
435 |
} else {
|
436 |
this.txtEnvelope.setText(""); |
437 |
} |
438 |
fireChangeEvent(); |
439 |
} |
440 |
|
441 |
|
442 |
} |