Revision 46487 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/swing/pickercontroller/impl/EnvelopePickerControllerImpl.java

View differences:

EnvelopePickerControllerImpl.java
4 4
import java.awt.Dimension;
5 5
import java.awt.Image;
6 6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8 7
import java.net.URL;
9 8
import javax.swing.ImageIcon;
10 9
import javax.swing.JButton;
11 10
import javax.swing.JTextField;
12 11
import javax.swing.JToggleButton;
13 12
import javax.swing.SwingConstants;
13
import org.apache.commons.lang.StringUtils;
14 14
import org.gvsig.fmap.dal.DataTypes;
15 15
import org.gvsig.fmap.geom.Geometry;
16 16
import org.gvsig.fmap.geom.Geometry.DIMENSIONS;
17 17
import org.gvsig.fmap.geom.GeometryLocator;
18 18
import org.gvsig.fmap.geom.GeometryManager;
19
import org.gvsig.fmap.geom.exception.CreateGeometryException;
19
import org.gvsig.fmap.geom.GeometryUtils;
20 20
import org.gvsig.fmap.geom.exception.GeometryRuntimeException;
21 21
import org.gvsig.fmap.geom.primitive.Envelope;
22 22
import org.gvsig.fmap.geom.primitive.Point;
......
30 30
import org.gvsig.tools.dataTypes.CoercionException;
31 31
import org.gvsig.tools.dataTypes.DataType;
32 32
import org.gvsig.tools.swing.api.ToolsSwingLocator;
33
import org.gvsig.tools.swing.api.ToolsSwingManager;
33 34
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController;
34 35
import org.gvsig.tools.swing.icontheme.IconTheme;
35 36
import org.gvsig.tools.swing.icontheme.IconThemeManager;
......
38 39
 *
39 40
 * @author jjdelcerro
40 41
 */
42
@SuppressWarnings("UseSpecificCatch")
41 43
public class EnvelopePickerControllerImpl extends AbstractPickerController<Envelope>{
42 44

  
43 45
        public static void selfRegister() {
......
94 96
    private ImageIcon captureCursor;    
95 97
    private RectangleBehavior captureTool;
96 98
    private String previosTool;
97
    private Envelope envelope;
98 99
    
99 100
    public EnvelopePickerControllerImpl(
100 101
            MapControl mapControl,
......
125 126
        ) {
126 127
        this.mapControl = mapControl;
127 128
        this.txtEnvelope = txtEnvelope;
128
        this.txtUpperLeftX = null;
129
        this.txtUpperLeftY = null;
130
        this.txtLowerRightX = null;
131
        this.txtLowerRightY = null;
129
        this.txtUpperLeftX = new JTextField();
130
        this.txtUpperLeftY = new JTextField();
131
        this.txtLowerRightX = new JTextField();
132
        this.txtLowerRightY = new JTextField();
132 133
        this.btnMapControlEnvelope = btnMapControlEnvelope;
133 134
        this.btnCapture = btnCapture;
134 135
        
......
136 137
    }
137 138

  
138 139
    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"));
140
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
141
        this.captureCursor = new ImageIcon(this.getIcon("picker-envelope-cursor-capture").getImage());
142 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
            
143
        if( this.btnCapture!=null ) {
144
            this.btnCapture.setIcon(this.getIcon("picker-envelope-capture"));
145
            if( this.btnCapture.getText().equals("...") ) {
146
                this.btnCapture.setText("");
147
            }
148
            this.btnCapture.addActionListener((ActionEvent e) -> {
149
                doCaptureEnvelope(btnCapture.isSelected());
150
            });
151
            this.btnCapture.setEnabled(this.mapControl!=null);
152
        }
148 153
        if( this.btnMapControlEnvelope!=null ) {
149
            this.btnMapControlEnvelope.addActionListener(new ActionListener() {
150
                @Override
151
                public void actionPerformed(ActionEvent e) {
152
                    doSetEnvelopeToMapControlEnvelope();
153
                }
154
            this.btnMapControlEnvelope.setIcon(this.getIcon("picker-envelope-from-mapcontrol"));
155
            if( this.btnMapControlEnvelope.getText().equals("...") ) {
156
                this.btnMapControlEnvelope.setText("");
157
            }
158
            this.btnMapControlEnvelope.addActionListener((ActionEvent e) -> {
159
                doSetEnvelopeToMapControlEnvelope();
154 160
            });
161
            this.btnMapControlEnvelope.setEnabled(this.mapControl!=null);
155 162
        }
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();
163
        if( this.mapControl!=null ) {
164
            this.captureTool = new RectangleBehavior(new CaptureEnvelopeListener());
165
            this.mapControl.addBehavior("picker-envelope-capture", captureTool);
166
            this.previosTool = this.mapControl.getCurrentTool();
167
        }
166 168
        
167 169
        if( this.txtEnvelope==null ) {
170
            toolsSwingManager.addClearButton(this.txtUpperLeftX);
171
            toolsSwingManager.addClearButton(this.txtUpperLeftY);
172
            toolsSwingManager.addClearButton(this.txtLowerRightX);
173
            toolsSwingManager.addClearButton(this.txtLowerRightY);
174
            
175
            toolsSwingManager.setDefaultPopupMenu(this.txtUpperLeftX);
176
            toolsSwingManager.setDefaultPopupMenu(this.txtUpperLeftY);
177
            toolsSwingManager.setDefaultPopupMenu(this.txtLowerRightX);
178
            toolsSwingManager.setDefaultPopupMenu(this.txtLowerRightY);
179

  
180
            this.txtUpperLeftX.setHorizontalAlignment(SwingConstants.RIGHT);
181
            this.txtUpperLeftY.setHorizontalAlignment(SwingConstants.RIGHT);
182
            this.txtLowerRightX.setHorizontalAlignment(SwingConstants.RIGHT);
183
            this.txtLowerRightY.setHorizontalAlignment(SwingConstants.RIGHT);
184
            
168 185
            this.txtUpperLeftX.setText("#########,00000");
169 186
            Dimension dim = this.txtUpperLeftX.getPreferredSize();
170 187
            this.txtUpperLeftX.setText("");
......
179 196
            DoubleDocumentFilter.install(this.txtLowerRightX);
180 197
            DoubleDocumentFilter.install(this.txtLowerRightY);
181 198
        } else {
182
            this.txtEnvelope.setEditable(false);
199
            toolsSwingManager.addClearButton(this.txtEnvelope);
200
            toolsSwingManager.setDefaultPopupMenu(this.txtEnvelope);
201
//            this.txtEnvelope.setEditable(false);
183 202
        }
184 203
    }
185 204
            
186 205
    protected void doSetEnvelopeToMapControlEnvelope() {
187
        if( !this.isEditable() ) {
206
        if( !this.isEditable() || mapControl==null ) {
188 207
            return;
189 208
        }
190 209
        try {
191
            Envelope theEnvelope = (Envelope) this.mapControl.getViewPort().getEnvelope().clone();
210
            Envelope env = this.mapControl.getViewPort().getEnvelope();
211
            if( env == null ) {
212
                return;
213
            }
214
            Envelope theEnvelope = (Envelope) env.clone();
192 215
            this.set(theEnvelope);
193 216
        } catch (Exception ex) {
194 217
            LOG.warn("Can't get envelope from mapcontrol.", ex);
......
196 219
    }
197 220
    
198 221
    protected void doCaptureEnvelope(boolean enabled) {
199
        if( !this.isEditable() ) {
222
        if( !this.isEditable() || mapControl==null ) {
200 223
            return;
201 224
        }
225
        Envelope env = this.mapControl.getViewPort().getEnvelope();
226
        if( env == null ) {
227
            return;
228
        }
202 229
        if( enabled ) {
203 230
            this.previosTool = this.mapControl.getCurrentTool();
204 231
            this.mapControl.setTool("picker-envelope-capture");
......
221 248
    @Override
222 249
    public boolean isValid() {
223 250
        if( this.txtEnvelope==null ) {
251
            if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) 
252
                    && DoubleDocumentFilter.isEmpty(this.txtUpperLeftY)
253
                    && DoubleDocumentFilter.isEmpty(this.txtLowerRightX)
254
                    && DoubleDocumentFilter.isEmpty(this.txtLowerRightY) ) {
255
                return true;
256
            }
257
            if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) 
258
                    || DoubleDocumentFilter.isEmpty(this.txtUpperLeftY)
259
                    || DoubleDocumentFilter.isEmpty(this.txtLowerRightX)
260
                    || DoubleDocumentFilter.isEmpty(this.txtLowerRightY) ) {
261
                return false;
262
            }
224 263
            if( !DoubleDocumentFilter.isValid(this.txtUpperLeftX) ) {
225 264
                return false;
226 265
            }
......
235 274
            }
236 275
            return true;
237 276
        } else {
277
            if( StringUtils.isBlank(this.txtEnvelope.getText()) ) {
278
                return true;
279
            }
280
            try {
281
                GeometryUtils.createFrom(this.txtEnvelope.getText());
282
            } catch(Exception ex) {
283
                return false;
284
            }
238 285
            return true;
239 286
        }
240 287
    }
......
242 289
    @Override
243 290
    public boolean isEmpty() {
244 291
        if( this.txtEnvelope==null ) {
245
            if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) ) {
292
            if( DoubleDocumentFilter.isEmpty(this.txtUpperLeftX) 
293
                    && DoubleDocumentFilter.isEmpty(this.txtUpperLeftY)
294
                    && DoubleDocumentFilter.isEmpty(this.txtLowerRightX)
295
                    && DoubleDocumentFilter.isEmpty(this.txtLowerRightY) ) {
246 296
                return true;
247 297
            }
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 298
            return false;
258 299
        } else {
259
            return this.envelope==null;
300
            return StringUtils.isBlank(this.txtEnvelope.getText());
260 301
        }
261 302
    }
262 303
    
263 304
    @Override
264 305
    public Envelope get() {
306
        if( this.isEmpty() ) {
307
            return null;
308
        }
265 309
        if( !this.isValid() ) {
266 310
            throw new IllegalValueException();
267 311
        }
268
        if( this.isEmpty() ) {
269
            return null;
270
        }
271 312
        
272 313
        if( this.txtEnvelope==null ) {
273 314
            try {
......
276 317
                double lowerRightX = DoubleDocumentFilter.getValue(this.txtLowerRightX);
277 318
                double lowerRightY = DoubleDocumentFilter.getValue(this.txtLowerRightY);
278 319
                GeometryManager geomManager = GeometryLocator.getGeometryManager();
320
                Envelope envelope = geomManager.createEnvelope(Geometry.SUBTYPES.GEOM2D);
279 321
                Point upperLeft = geomManager.createPoint(upperLeftX, upperLeftY, Geometry.SUBTYPES.GEOM2D);
280 322
                Point lowerRight = geomManager.createPoint(lowerRightX, lowerRightY, Geometry.SUBTYPES.GEOM2D);
281
                this.envelope.setUpperCorner(upperLeft);
282
                this.envelope.setLowerCorner(lowerRight);
283
            } catch (CreateGeometryException ex) {
323
                envelope.setUpperCorner(upperLeft);
324
                envelope.setLowerCorner(lowerRight);
325
                return envelope;
326
            } catch (Exception ex) {
284 327
                throw new GeometryRuntimeException("Can't create envelope", ex);
285 328
            }
329
        } else {
330
            try {
331
                Geometry geom = GeometryUtils.createFrom(this.txtEnvelope.getText());
332
                return geom.getEnvelope();
333
            } catch(Exception ex) {
334
                return null;
335
            }
286 336
        }
287
        return this.envelope;
288 337
    }
289 338

  
290 339
    @Override
291 340
    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);
341
        if( envelope == null ) {
342
            this.clear();
343
            return;
296 344
        }
297 345
        if( this.txtEnvelope==null ) {
298 346
            this.txtUpperLeftX.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.X)));
......
300 348
            this.txtLowerRightX.setText(String.format("%+f", envelope.getMaximum(DIMENSIONS.X)));
301 349
            this.txtLowerRightY.setText(String.format("%+f", envelope.getMinimum(DIMENSIONS.Y)));
302 350
        } else {
303
            this.txtEnvelope.setText(getEnvelopeAsString(this.envelope));
351
            this.txtEnvelope.setText(envelope.getGeometry().convertToWKTQuietly());
304 352
        }
305 353
    }
306 354

  
307 355
    @Override
308 356
    public void coerceAndSet(Object value) {
357
        if( value == null ) {
358
            this.clear();
359
            return;
360
        }
309 361
        DataType dataType = ToolsLocator.getDataTypesManager()
310 362
                .get(DataTypes.ENVELOPE);
311 363
        try {
......
325 377
        } else {
326 378
            this.txtEnvelope.setEnabled(enabled);
327 379
        }
328
        this.btnCapture.setEnabled(enabled);
329
        this.btnMapControlEnvelope.setEnabled(enabled);
380
        if( this.mapControl == null ) {
381
            this.btnCapture.setEnabled(false);
382
            this.btnMapControlEnvelope.setEnabled(false);
383
        } else {
384
            this.btnCapture.setEnabled(enabled);
385
            this.btnMapControlEnvelope.setEnabled(enabled);
386
        }
330 387
    }
331 388

  
332 389
    @Override
......
350 407
        }
351 408
    }
352 409

  
410
    public void clear() {
411
        if( this.txtEnvelope==null ) {
412
            this.txtUpperLeftX.setText("");
413
            this.txtUpperLeftY.setText("");
414
            this.txtLowerRightX.setText("");
415
            this.txtLowerRightY.setText("");
416
        } else {
417
            this.txtEnvelope.setText("");
418
        }
419
    }
420

  
353 421
    
354 422
}

Also available in: Unified diff