Statistics
| Revision:

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 @ 43953

History | View | Annotate | Download (13.1 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.mapcontrol.swing.pickercontroller.impl;
7

    
8
import org.gvsig.tools.swing.api.documentfilters.DoubleDocumentFilter;
9
import java.awt.Dimension;
10
import java.awt.Image;
11
import java.awt.event.ActionEvent;
12
import java.awt.event.ActionListener;
13
import java.net.URL;
14
import javax.swing.ImageIcon;
15
import javax.swing.JButton;
16
import javax.swing.JTextField;
17
import javax.swing.JToggleButton;
18
import javax.swing.SwingConstants;
19
import org.gvsig.fmap.dal.DataTypes;
20
import org.gvsig.fmap.geom.Geometry;
21
import org.gvsig.fmap.geom.Geometry.DIMENSIONS;
22
import org.gvsig.fmap.geom.GeometryLocator;
23
import org.gvsig.fmap.geom.GeometryManager;
24
import org.gvsig.fmap.geom.exception.CreateGeometryException;
25
import org.gvsig.fmap.geom.exception.GeometryRuntimeException;
26
import org.gvsig.fmap.geom.primitive.Envelope;
27
import org.gvsig.fmap.geom.primitive.Point;
28
import org.gvsig.fmap.mapcontrol.MapControl;
29
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior;
30
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
31
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent;
32
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener;
33
import org.gvsig.tools.IllegalValueException;
34
import org.gvsig.tools.ToolsLocator;
35
import org.gvsig.tools.dataTypes.CoercionException;
36
import org.gvsig.tools.dataTypes.DataType;
37
import org.gvsig.tools.swing.api.ToolsSwingLocator;
38
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController;
39
import org.gvsig.tools.swing.icontheme.IconTheme;
40
import org.gvsig.tools.swing.icontheme.IconThemeManager;
41

    
42
/**
43
 *
44
 * @author jjdelcerro
45
 */
46
public class EnvelopePickerControllerImpl extends AbstractPickerController<Envelope>{
47

    
48
        public static void selfRegister() {
49
            IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager();
50
            IconTheme theme = iconThemeManager.getCurrent();
51
            
52
            URL url = ScalePickerControllerImpl.class.getClass().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-cursor-capture", icon, url);
56
            }
57
            url = ScalePickerControllerImpl.class.getClass().getClassLoader().getResource("images/picker-envelope-capture.png");
58
            if( url != null ) {
59
                ImageIcon icon = new ImageIcon(url);
60
                theme.registerDefault("mapcontrol", "picker", "picker-envelope-capture", icon, url);
61
            }
62
            url = ScalePickerControllerImpl.class.getClass().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-from-mapcontrol", icon, url);
66
            }
67
    }
68
    
69

    
70
    private class CaptureEnvelopeListener implements RectangleListener  {
71

    
72
        @Override
73
        public void rectangle(EnvelopeEvent event) throws BehaviorException {
74
            set(event.getWorldCoordRect());
75
        }
76

    
77
        @Override
78
        public Image getImageCursor() {
79
            return captureCursor.getImage();
80
        }
81

    
82
        @Override
83
        public boolean cancelDrawing() {
84
            return false;
85
        }
86
        
87
    }
88
    
89

    
90
    private final MapControl mapControl;
91
    private final JTextField txtUpperLeftX;
92
    private final JTextField txtUpperLeftY;
93
    private final JTextField txtLowerRightX;
94
    private final JTextField txtLowerRightY;
95
    private final JTextField txtEnvelope;
96
    private final JButton btnMapControlEnvelope;
97
    private final JToggleButton btnCapture;
98

    
99
    private ImageIcon captureCursor;    
100
    private RectangleBehavior captureTool;
101
    private String previosTool;
102
    private Envelope envelope;
103
    
104
    public EnvelopePickerControllerImpl(
105
            MapControl mapControl,
106
            JTextField txtUpperLeftX, 
107
            JTextField txtUpperLeftY, 
108
            JTextField txtLowerRightX, 
109
            JTextField txtLowerRightY, 
110
            JButton btnMapControlEnvelope, 
111
            final JToggleButton btnCapture
112
        ) {
113
        this.mapControl = mapControl;
114
        this.txtEnvelope = null;
115
        this.txtUpperLeftX = txtUpperLeftX;
116
        this.txtUpperLeftY = txtUpperLeftY;
117
        this.txtLowerRightX = txtLowerRightX;
118
        this.txtLowerRightY = txtLowerRightY;
119
        this.btnMapControlEnvelope = btnMapControlEnvelope;
120
        this.btnCapture = btnCapture;
121

    
122
        this.initComponents();      
123
    }
124
    
125
    public EnvelopePickerControllerImpl(
126
            MapControl mapControl,
127
            JTextField txtEnvelope, 
128
            JButton btnMapControlEnvelope, 
129
            final JToggleButton btnCapture
130
        ) {
131
        this.mapControl = mapControl;
132
        this.txtEnvelope = txtEnvelope;
133
        this.txtUpperLeftX = null;
134
        this.txtUpperLeftY = null;
135
        this.txtLowerRightX = null;
136
        this.txtLowerRightY = null;
137
        this.btnMapControlEnvelope = btnMapControlEnvelope;
138
        this.btnCapture = btnCapture;
139
        
140
        this.initComponents();
141
    }
142

    
143
    private void initComponents() {
144
        this.captureCursor.setImage(this.getIcon("picker-envelope-cursor-capture").getImage());
145
        this.btnCapture.setIcon(this.getIcon("picker-envelope-cursor-capture"));
146
        this.btnMapControlEnvelope.setIcon(this.getIcon("picker-envelope-cursor-capture"));
147
        
148
        this.txtUpperLeftX.setHorizontalAlignment(SwingConstants.RIGHT);
149
        this.txtUpperLeftY.setHorizontalAlignment(SwingConstants.RIGHT);
150
        this.txtLowerRightX.setHorizontalAlignment(SwingConstants.RIGHT);
151
        this.txtLowerRightY.setHorizontalAlignment(SwingConstants.RIGHT);
152
            
153
        if( this.btnMapControlEnvelope!=null ) {
154
            this.btnMapControlEnvelope.addActionListener(new ActionListener() {
155
                @Override
156
                public void actionPerformed(ActionEvent e) {
157
                    doSetEnvelopeToMapControlEnvelope();
158
                }
159
            });
160
        }
161
        this.btnCapture.addActionListener(new ActionListener() {
162
            @Override
163
            public void actionPerformed(ActionEvent e) {
164
                doCaptureEnvelope(btnCapture.isSelected());
165
            }
166
        });
167

    
168
        this.captureTool = new RectangleBehavior(new CaptureEnvelopeListener());
169
        this.mapControl.addBehavior("picker-envelope-capture", captureTool);
170
        this.previosTool = this.mapControl.getCurrentTool();
171
        
172
        if( this.txtEnvelope==null ) {
173
            this.txtUpperLeftX.setText("#########,00000");
174
            Dimension dim = this.txtUpperLeftX.getPreferredSize();
175
            this.txtUpperLeftX.setText("");
176
            
177
            this.txtUpperLeftX.setPreferredSize(dim);
178
            this.txtUpperLeftY.setPreferredSize(dim);
179
            this.txtLowerRightX.setPreferredSize(dim);
180
            this.txtLowerRightY.setPreferredSize(dim);
181
            
182
            DoubleDocumentFilter.install(this.txtUpperLeftX);
183
            DoubleDocumentFilter.install(this.txtUpperLeftY);
184
            DoubleDocumentFilter.install(this.txtLowerRightX);
185
            DoubleDocumentFilter.install(this.txtLowerRightY);
186
        } else {
187
            this.txtEnvelope.setEditable(false);
188
        }
189
    }
190
            
191
    protected void doSetEnvelopeToMapControlEnvelope() {
192
        if( !this.isEditable() ) {
193
            return;
194
        }
195
        try {
196
            Envelope theEnvelope = (Envelope) this.mapControl.getViewPort().getEnvelope().clone();
197
            this.set(theEnvelope);
198
        } catch (Exception ex) {
199
            LOG.warn("Can't get envelope from mapcontrol.", ex);
200
        }
201
    }
202
    
203
    protected void doCaptureEnvelope(boolean enabled) {
204
        if( !this.isEditable() ) {
205
            return;
206
        }
207
        if( enabled ) {
208
            this.previosTool = this.mapControl.getCurrentTool();
209
            this.mapControl.setTool("picker-envelope-capture");
210
        } else {
211
            this.mapControl.setTool(this.previosTool);
212
        }
213
    }
214

    
215
    protected String getEnvelopeAsString(Envelope value) {
216
        String s = String.format(
217
            "%+f, %+f, %+f, %+f",
218
            value.getMinimum(Geometry.DIMENSIONS.X), 
219
            value.getMaximum(Geometry.DIMENSIONS.Y),
220
            value.getMaximum(Geometry.DIMENSIONS.X), 
221
            value.getMinimum(Geometry.DIMENSIONS.Y)
222
        );
223
        return s;
224
    }
225

    
226
    @Override
227
    public boolean isValid() {
228
        if( this.txtEnvelope==null ) {
229
            if( !DoubleDocumentFilter.isValid(this.txtUpperLeftX) ) {
230
                return false;
231
            }
232
            if( !DoubleDocumentFilter.isValid(this.txtUpperLeftY) ) {
233
                return false;
234
            }
235
            if( !DoubleDocumentFilter.isValid(this.txtLowerRightX) ) {
236
                return false;
237
            }
238
            if( !DoubleDocumentFilter.isValid(this.txtLowerRightY) ) {
239
                return false;
240
            }
241
            return true;
242
        } else {
243
            return true;
244
        }
245
    }
246

    
247
    @Override
248
    public boolean isEmpty() {
249
        if( this.txtEnvelope==null ) {
250
            if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) ) {
251
                return true;
252
            }
253
            if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftY) ) {
254
                return true;
255
            }
256
            if( DoubleDocumentFilter.isEmpty(this.txtLowerRightX) ) {
257
                return true;
258
            }
259
            if( DoubleDocumentFilter.isEmpty(this.txtLowerRightY) ) {
260
                return true;
261
            }
262
            return false;
263
        } else {
264
            return this.envelope==null;
265
        }
266
    }
267
    
268
    @Override
269
    public Envelope get() {
270
        if( !this.isValid() ) {
271
            throw new IllegalValueException();
272
        }
273
        if( this.isEmpty() ) {
274
            return null;
275
        }
276
        
277
        if( this.txtEnvelope==null ) {
278
            try {
279
                double upperLeftX = DoubleDocumentFilter.getValue(this.txtUpperLeftX);
280
                double upperLeftY = DoubleDocumentFilter.getValue(this.txtUpperLeftY);
281
                double lowerRightX = DoubleDocumentFilter.getValue(this.txtLowerRightX);
282
                double lowerRightY = DoubleDocumentFilter.getValue(this.txtLowerRightY);
283
                GeometryManager geomManager = GeometryLocator.getGeometryManager();
284
                Point upperLeft = geomManager.createPoint(upperLeftX, upperLeftY, Geometry.SUBTYPES.GEOM2D);
285
                Point lowerRight = geomManager.createPoint(lowerRightX, lowerRightY, Geometry.SUBTYPES.GEOM2D);
286
                this.envelope.setUpperCorner(upperLeft);
287
                this.envelope.setLowerCorner(lowerRight);
288
            } catch (CreateGeometryException ex) {
289
                throw new GeometryRuntimeException("Can't create envelope", ex);
290
            }
291
        }
292
        return this.envelope;
293
    }
294

    
295
    @Override
296
    public void set(Envelope envelope) {
297
        try {
298
            this.envelope = (Envelope) envelope.clone();
299
        } catch (CloneNotSupportedException ex) {
300
            throw new IllegalArgumentException("Can't use the envelope.", ex);
301
        }
302
        if( this.txtEnvelope==null ) {
303
            this.txtUpperLeftX.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.X)));
304
            this.txtUpperLeftY.setText(String.format("%+f", envelope.getMaximum(DIMENSIONS.Y)));
305
            this.txtLowerRightX.setText(String.format("%+f", envelope.getMaximum(DIMENSIONS.X)));
306
            this.txtLowerRightY.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.Y)));
307
        } else {
308
            this.txtEnvelope.setText(getEnvelopeAsString(this.envelope));
309
        }
310
    }
311

    
312
    @Override
313
    public void coerceAndSet(Object value) {
314
        DataType dataType = ToolsLocator.getDataTypesManager()
315
                .get(DataTypes.ENVELOPE);
316
        try {
317
            this.set((Envelope) dataType.coerce(value));
318
        } catch (CoercionException ex) {
319
            LOG.warn("Can't set value.", ex);
320
        }
321
    }
322

    
323
    @Override
324
    public void setEnabled(boolean enabled) {
325
        if( this.txtEnvelope==null ) {
326
            this.txtUpperLeftX.setEnabled(enabled);
327
            this.txtUpperLeftY.setEnabled(enabled);
328
            this.txtLowerRightX.setEnabled(enabled);
329
            this.txtLowerRightY.setEnabled(enabled);
330
        } else {
331
            this.txtEnvelope.setEnabled(enabled);
332
        }
333
        this.btnCapture.setEnabled(enabled);
334
        this.btnMapControlEnvelope.setEnabled(enabled);
335
    }
336

    
337
    @Override
338
    public boolean isEnabled() {
339
        if( this.txtEnvelope!=null ) {
340
            return this.txtEnvelope.isEnabled();
341
        }
342
        return true;
343
    }
344

    
345
    @Override
346
    public void setEditable(boolean editable) {
347
        super.setEditable(editable);
348
        if( this.txtEnvelope==null ) {
349
            this.txtUpperLeftX.setEditable(editable);
350
            this.txtUpperLeftY.setEditable(editable);
351
            this.txtLowerRightX.setEditable(editable);
352
            this.txtLowerRightY.setEditable(editable);
353
        } else {
354
            this.txtEnvelope.setEditable(editable);
355
        }
356
    }
357

    
358
    
359
}