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