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

History | View | Annotate | Download (12.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.pickercontroller.AbstractPickerController;
38

    
39
/**
40
 *
41
 * @author jjdelcerro
42
 */
43
public class EnvelopePickerControllerImpl extends AbstractPickerController<Envelope>{
44

    
45
    private class CaptureEnvelopeListener implements RectangleListener  {
46

    
47
        @Override
48
        public void rectangle(EnvelopeEvent event) throws BehaviorException {
49
            set(event.getWorldCoordRect());
50
        }
51

    
52
        @Override
53
        public Image getImageCursor() {
54
            return captureCursor.getImage();
55
        }
56

    
57
        @Override
58
        public boolean cancelDrawing() {
59
            return false;
60
        }
61
        
62
    }
63
    
64

    
65
    private final MapControl mapControl;
66
    private final JTextField txtUpperLeftX;
67
    private final JTextField txtUpperLeftY;
68
    private final JTextField txtLowerRightX;
69
    private final JTextField txtLowerRightY;
70
    private final JTextField txtEnvelope;
71
    private final JButton btnMapControlEnvelope;
72
    private final JToggleButton btnCapture;
73

    
74
    private ImageIcon captureCursor;    
75
    private RectangleBehavior captureTool;
76
    private String previosTool;
77
    private Envelope envelope;
78
    
79
    public EnvelopePickerControllerImpl(
80
            MapControl mapControl,
81
            JTextField txtUpperLeftX, 
82
            JTextField txtUpperLeftY, 
83
            JTextField txtLowerRightX, 
84
            JTextField txtLowerRightY, 
85
            JButton btnMapControlEnvelope, 
86
            final JToggleButton btnCapture
87
        ) {
88
        this.mapControl = mapControl;
89
        this.txtEnvelope = null;
90
        this.txtUpperLeftX = txtUpperLeftX;
91
        this.txtUpperLeftY = txtUpperLeftY;
92
        this.txtLowerRightX = txtLowerRightX;
93
        this.txtLowerRightY = txtLowerRightY;
94
        this.btnMapControlEnvelope = btnMapControlEnvelope;
95
        this.btnCapture = btnCapture;
96

    
97
        this.initComponents();      
98
    }
99
    
100
    public EnvelopePickerControllerImpl(
101
            MapControl mapControl,
102
            JTextField txtEnvelope, 
103
            JButton btnMapControlEnvelope, 
104
            final JToggleButton btnCapture
105
        ) {
106
        this.mapControl = mapControl;
107
        this.txtEnvelope = txtEnvelope;
108
        this.txtUpperLeftX = null;
109
        this.txtUpperLeftY = null;
110
        this.txtLowerRightX = null;
111
        this.txtLowerRightY = null;
112
        this.btnMapControlEnvelope = btnMapControlEnvelope;
113
        this.btnCapture = btnCapture;
114
        
115
        this.initComponents();
116
    }
117

    
118
    private void initComponents() {
119
        URL url = this.getClass().getClassLoader().getResource("images/picker-envelope-cursor-capture.png");
120
        if( url != null ) {
121
            this.captureCursor = new ImageIcon(url);
122
        }
123
        url = this.getClass().getClassLoader().getResource("images/picker-envelope-capture.png");
124
        if( url != null ) {
125
            this.btnCapture.setIcon(new ImageIcon(url));
126
        }
127
        url = this.getClass().getClassLoader().getResource("images/picker-envelope-from-mapcontrol.png");
128
        if( url != null ) {
129
            this.btnMapControlEnvelope.setIcon(new ImageIcon(url));
130
        }
131
        
132
        this.txtUpperLeftX.setHorizontalAlignment(SwingConstants.RIGHT);
133
        this.txtUpperLeftY.setHorizontalAlignment(SwingConstants.RIGHT);
134
        this.txtLowerRightX.setHorizontalAlignment(SwingConstants.RIGHT);
135
        this.txtLowerRightY.setHorizontalAlignment(SwingConstants.RIGHT);
136
            
137
        if( this.btnMapControlEnvelope!=null ) {
138
            this.btnMapControlEnvelope.addActionListener(new ActionListener() {
139
                @Override
140
                public void actionPerformed(ActionEvent e) {
141
                    doSetEnvelopeToMapControlEnvelope();
142
                }
143
            });
144
        }
145
        this.btnCapture.addActionListener(new ActionListener() {
146
            @Override
147
            public void actionPerformed(ActionEvent e) {
148
                doCaptureEnvelope(btnCapture.isSelected());
149
            }
150
        });
151

    
152
        this.captureTool = new RectangleBehavior(new CaptureEnvelopeListener());
153
        this.mapControl.addBehavior("picker-envelope-capture", captureTool);
154
        this.previosTool = this.mapControl.getCurrentTool();
155
        
156
        if( this.txtEnvelope==null ) {
157
            this.txtUpperLeftX.setText("#########,00000");
158
            Dimension dim = this.txtUpperLeftX.getPreferredSize();
159
            this.txtUpperLeftX.setText("");
160
            
161
            this.txtUpperLeftX.setPreferredSize(dim);
162
            this.txtUpperLeftY.setPreferredSize(dim);
163
            this.txtLowerRightX.setPreferredSize(dim);
164
            this.txtLowerRightY.setPreferredSize(dim);
165
            
166
            DoubleDocumentFilter.install(this.txtUpperLeftX);
167
            DoubleDocumentFilter.install(this.txtUpperLeftY);
168
            DoubleDocumentFilter.install(this.txtLowerRightX);
169
            DoubleDocumentFilter.install(this.txtLowerRightY);
170
        } else {
171
            this.txtEnvelope.setEditable(false);
172
        }
173
    }
174
            
175
    protected void doSetEnvelopeToMapControlEnvelope() {
176
        if( !this.isEditable() ) {
177
            return;
178
        }
179
        try {
180
            Envelope theEnvelope = (Envelope) this.mapControl.getViewPort().getEnvelope().clone();
181
            this.set(theEnvelope);
182
        } catch (Exception ex) {
183
            LOG.warn("Can't get envelope from mapcontrol.", ex);
184
        }
185
    }
186
    
187
    protected void doCaptureEnvelope(boolean enabled) {
188
        if( !this.isEditable() ) {
189
            return;
190
        }
191
        if( enabled ) {
192
            this.previosTool = this.mapControl.getCurrentTool();
193
            this.mapControl.setTool("picker-envelope-capture");
194
        } else {
195
            this.mapControl.setTool(this.previosTool);
196
        }
197
    }
198

    
199
    protected String getEnvelopeAsString(Envelope value) {
200
        String s = String.format(
201
            "%+f, %+f, %+f, %+f",
202
            value.getMinimum(Geometry.DIMENSIONS.X), 
203
            value.getMaximum(Geometry.DIMENSIONS.Y),
204
            value.getMaximum(Geometry.DIMENSIONS.X), 
205
            value.getMinimum(Geometry.DIMENSIONS.Y)
206
        );
207
        return s;
208
    }
209

    
210
    @Override
211
    public boolean isValid() {
212
        if( this.txtEnvelope==null ) {
213
            if( !DoubleDocumentFilter.isValid(this.txtUpperLeftX) ) {
214
                return false;
215
            }
216
            if( !DoubleDocumentFilter.isValid(this.txtUpperLeftY) ) {
217
                return false;
218
            }
219
            if( !DoubleDocumentFilter.isValid(this.txtLowerRightX) ) {
220
                return false;
221
            }
222
            if( !DoubleDocumentFilter.isValid(this.txtLowerRightY) ) {
223
                return false;
224
            }
225
            return true;
226
        } else {
227
            return true;
228
        }
229
    }
230

    
231
    @Override
232
    public boolean isEmpty() {
233
        if( this.txtEnvelope==null ) {
234
            if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) ) {
235
                return true;
236
            }
237
            if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftY) ) {
238
                return true;
239
            }
240
            if( DoubleDocumentFilter.isEmpty(this.txtLowerRightX) ) {
241
                return true;
242
            }
243
            if( DoubleDocumentFilter.isEmpty(this.txtLowerRightY) ) {
244
                return true;
245
            }
246
            return false;
247
        } else {
248
            return this.envelope==null;
249
        }
250
    }
251
    
252
    @Override
253
    public Envelope get() {
254
        if( !this.isValid() ) {
255
            throw new IllegalValueException();
256
        }
257
        if( this.isEmpty() ) {
258
            return null;
259
        }
260
        
261
        if( this.txtEnvelope==null ) {
262
            try {
263
                double upperLeftX = DoubleDocumentFilter.getValue(this.txtUpperLeftX);
264
                double upperLeftY = DoubleDocumentFilter.getValue(this.txtUpperLeftY);
265
                double lowerRightX = DoubleDocumentFilter.getValue(this.txtLowerRightX);
266
                double lowerRightY = DoubleDocumentFilter.getValue(this.txtLowerRightY);
267
                GeometryManager geomManager = GeometryLocator.getGeometryManager();
268
                Point upperLeft = geomManager.createPoint(upperLeftX, upperLeftY, Geometry.SUBTYPES.GEOM2D);
269
                Point lowerRight = geomManager.createPoint(lowerRightX, lowerRightY, Geometry.SUBTYPES.GEOM2D);
270
                this.envelope.setUpperCorner(upperLeft);
271
                this.envelope.setLowerCorner(lowerRight);
272
            } catch (CreateGeometryException ex) {
273
                throw new GeometryRuntimeException("Can't create envelope", ex);
274
            }
275
        }
276
        return this.envelope;
277
    }
278

    
279
    @Override
280
    public void set(Envelope envelope) {
281
        try {
282
            this.envelope = (Envelope) envelope.clone();
283
        } catch (CloneNotSupportedException ex) {
284
            throw new IllegalArgumentException("Can't use the envelope.", ex);
285
        }
286
        if( this.txtEnvelope==null ) {
287
            this.txtUpperLeftX.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.X)));
288
            this.txtUpperLeftY.setText(String.format("%+f", envelope.getMaximum(DIMENSIONS.Y)));
289
            this.txtLowerRightX.setText(String.format("%+f", envelope.getMaximum(DIMENSIONS.X)));
290
            this.txtLowerRightY.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.Y)));
291
        } else {
292
            this.txtEnvelope.setText(getEnvelopeAsString(this.envelope));
293
        }
294
    }
295

    
296
    @Override
297
    public void coerceAndSet(Object value) {
298
        DataType dataType = ToolsLocator.getDataTypesManager()
299
                .get(DataTypes.ENVELOPE);
300
        try {
301
            this.set((Envelope) dataType.coerce(value));
302
        } catch (CoercionException ex) {
303
            LOG.warn("Can't set value.", ex);
304
        }
305
    }
306

    
307
    @Override
308
    public void setEnabled(boolean enabled) {
309
        if( this.txtEnvelope==null ) {
310
            this.txtUpperLeftX.setEnabled(enabled);
311
            this.txtUpperLeftY.setEnabled(enabled);
312
            this.txtLowerRightX.setEnabled(enabled);
313
            this.txtLowerRightY.setEnabled(enabled);
314
        } else {
315
            this.txtEnvelope.setEnabled(enabled);
316
        }
317
        this.btnCapture.setEnabled(enabled);
318
        this.btnMapControlEnvelope.setEnabled(enabled);
319
    }
320

    
321
    @Override
322
    public boolean isEnabled() {
323
        if( this.txtEnvelope!=null ) {
324
            return this.txtEnvelope.isEnabled();
325
        }
326
        return true;
327
    }
328

    
329
    @Override
330
    public void setEditable(boolean editable) {
331
        super.setEditable(editable);
332
        if( this.txtEnvelope==null ) {
333
            this.txtUpperLeftX.setEditable(editable);
334
            this.txtUpperLeftY.setEditable(editable);
335
            this.txtLowerRightX.setEditable(editable);
336
            this.txtLowerRightY.setEditable(editable);
337
        } else {
338
            this.txtEnvelope.setEditable(editable);
339
        }
340
    }
341

    
342
    
343
}