Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.swing / org.gvsig.vectorediting.swing.impl / src / main / java / org / gvsig / vectorediting / swing / impl / contextmenu / DefaultDynamicEditingPointPanel.java @ 1575

History | View | Annotate | Download (10.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
/* gvSIG. Desktop Geographic Information System.
26
 *
27
 * Copyright ? 2007-2014 gvSIG Association
28
 *
29
 * This program is free software; you can redistribute it and/or
30
 * modify it under the terms of the GNU General Public License
31
 * as published by the Free Software Foundation; either version 2
32
 * of the License, or (at your option) any later version.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
 * GNU General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU General Public License
40
 * along with this program; if not, write to the Free Software
41
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
42
 * MA  02110-1301, USA.
43
 *
44
 * For any additional information, do not hesitate to contact us
45
 * at info AT gvsig.com, or visit our website www.gvsig.com.
46
 */
47
package org.gvsig.vectorediting.swing.impl.contextmenu;
48

    
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.awt.event.ItemEvent;
52
import java.awt.event.ItemListener;
53
import java.util.Objects;
54
import javax.swing.DefaultComboBoxModel;
55
import javax.swing.JComponent;
56
import org.apache.commons.lang3.StringUtils;
57
import org.apache.commons.lang3.tuple.Pair;
58
import org.gvsig.expressionevaluator.swing.CalculatorController;
59
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator;
60
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager;
61
import org.gvsig.fmap.dal.DataTypes;
62
import org.gvsig.fmap.geom.Geometry;
63
import org.gvsig.fmap.geom.GeometryLocator;
64

    
65
import org.gvsig.fmap.geom.primitive.Point;
66
import org.gvsig.fmap.geom.type.GeometryType;
67
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
68
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
69
import org.gvsig.temporarystorage.TemporaryStorageGroup;
70
import org.gvsig.temporarystorage.TemporaryStorageLocator;
71
import org.gvsig.tools.locator.LocatorException;
72
import org.gvsig.tools.swing.api.ListElement;
73
import org.gvsig.tools.swing.api.ToolsSwingLocator;
74
import org.gvsig.tools.swing.api.ToolsSwingManager;
75

    
76
/**
77
 * @author fdiaz
78
 *
79
 */
80
public class DefaultDynamicEditingPointPanel extends DynamicEditingPointPanelView {
81

    
82
    /**
83
     *
84
     */
85
    private static final long serialVersionUID = 2398169399771503091L;
86
    private final Point defaultPoint;
87
    private final CalculatorController pickers[];
88
    private CalculatorController pickerX;
89
    private CalculatorController pickerY;
90
    private CalculatorController pickerZ;
91
    private CalculatorController pickerM;
92
    private final GeometryType geomType;
93

    
94
    /**
95
     * @param geomType
96
     * @param point
97
     */
98
    public DefaultDynamicEditingPointPanel(GeometryType geomType, Point point)  {
99
        super();
100
        this.defaultPoint = point;
101
        if( geomType==null ) {
102
            if( this.defaultPoint==null ) {
103
                try {
104
                    this.geomType = GeometryLocator.getGeometryManager().getGeometryType(
105
                            Geometry.TYPES.POINT,
106
                            Geometry.SUBTYPES.GEOM2D
107
                    );
108
                } catch (GeometryTypeNotSupportedException | GeometryTypeNotValidException | LocatorException ex) {
109
                    throw new IllegalArgumentException("Can't create the geometry type, and params are null.");
110
                }
111
            } else {
112
                this.geomType = this.defaultPoint.getGeometryType();
113
            }
114
        } else {
115
            this.geomType = geomType;
116
        }
117
        this.pickers = new CalculatorController[this.geomType.getDimension()];
118
        this.initComponents();
119
    }
120

    
121
    private void initComponents() {
122
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
123
        ExpressionEvaluatorSwingManager evaluatorManager = ExpressionEvaluatorSwingLocator.getManager();
124

    
125
        toolsSwingManager.translate(this.rdbEnterTheCoordinatesManually);
126
        toolsSwingManager.translate(this.rdbUseAPointPreviouslyCaptured);
127

    
128
        toolsSwingManager.addClearButton(this.txtX);
129
        toolsSwingManager.addClearButton(this.txtY);
130
        toolsSwingManager.addClearButton(this.txtZ);
131
        toolsSwingManager.addClearButton(this.txtM);
132

    
133
        toolsSwingManager.setDefaultPopupMenu(this.txtX);
134
        toolsSwingManager.setDefaultPopupMenu(this.txtY);
135
        toolsSwingManager.setDefaultPopupMenu(this.txtZ);
136
        toolsSwingManager.setDefaultPopupMenu(this.txtM);
137

    
138
        this.pickerX = evaluatorManager.createCalculatorController(
139
                this.txtX, DataTypes.DOUBLE
140
        );
141
        this.pickerY = evaluatorManager.createCalculatorController(
142
                this.txtY, DataTypes.DOUBLE
143
        );
144
        this.pickerZ = evaluatorManager.createCalculatorController(
145
                this.txtZ, DataTypes.DOUBLE
146
        );
147
        this.pickerM = evaluatorManager.createCalculatorController(
148
                this.txtM, DataTypes.DOUBLE
149
        );
150
        this.pickers[0] = this.pickerX;
151
        this.pickers[1] = this.pickerY;
152
        if( this.geomType.hasZ() && this.geomType.hasM() ) {
153
            this.pickers[2] = this.pickerZ;
154
            this.pickers[3] = this.pickerM;                
155
        } else if( this.geomType.hasZ() ) {
156
            this.pickers[2] = this.pickerZ;
157
        } else if( this.geomType.hasM() ) {
158
            this.pickers[2] = this.pickerM;
159
        }
160
        
161
        addHierarchyChanged();
162
        
163
        TemporaryStorageGroup points = TemporaryStorageLocator.getTemporaryStorageManager()
164
                .create("Points",Point.class);
165
        
166
        DefaultComboBoxModel model = new DefaultComboBoxModel();
167
        model.addElement(new ListElement("", null));
168
        for (Pair<String, Object> pair : points.asList()) {
169
            model.addElement(new ListElement(pair.getKey(), (Point)(pair.getValue())));
170
        }
171
        this.cboCapturedPoints.setModel(model);
172
        this.cboCapturedPoints.addItemListener(new ItemListener() {
173
            @Override
174
            public void itemStateChanged(ItemEvent e) {
175
                if( e.getStateChange()==ItemEvent.SELECTED ) {
176
                    setPoint(((ListElement<Point>)e.getItem()).getValue());
177
                }
178
            }
179
        });
180
        this.rdbEnterTheCoordinatesManually.addActionListener(new ActionListener() {
181
            @Override
182
            public void actionPerformed(ActionEvent e) {
183
                updateEnabledState();
184
            }
185
        });
186
        this.rdbUseAPointPreviouslyCaptured.addActionListener(new ActionListener() {
187
            @Override
188
            public void actionPerformed(ActionEvent e) {
189
                updateEnabledState();
190
            }
191
        });
192
        this.rdbEnterTheCoordinatesManually.setSelected(true);
193
        this.updateEnabledState();
194
    }
195
    
196
    private void updateEnabledState() {
197
        
198
        if( this.rdbEnterTheCoordinatesManually.isSelected()) {
199
            this.cboCapturedPoints.setEnabled(false);
200
            
201
            this.txtX.setEnabled(true);
202
            this.txtY.setEnabled(true);
203
            this.txtZ.setEnabled(false);
204
            this.txtM.setEnabled(false);
205

    
206
            this.lblX.setEnabled(true);
207
            this.lblY.setEnabled(true);
208
            this.lblZ.setEnabled(false);
209
            this.lblM.setEnabled(false);
210
            if( geomType.hasZ() && geomType.hasM() ) {
211
                this.txtZ.setEnabled(true);
212
                this.lblZ.setEnabled(true);
213
                this.txtM.setEnabled(true);
214
                this.lblM.setEnabled(true);
215
            } else if( geomType.hasZ() ) {
216
                this.txtZ.setEnabled(true);
217
                this.lblZ.setEnabled(true);
218
            } else if( geomType.hasM() ) {
219
                this.txtM.setEnabled(true);
220
                this.lblM.setEnabled(true);
221
            }
222
        }
223
        if( this.rdbUseAPointPreviouslyCaptured.isSelected()) {
224
            this.cboCapturedPoints.setEnabled(true);
225
            
226
            this.txtX.setEnabled(false);
227
            this.txtY.setEnabled(false);
228
            this.txtZ.setEnabled(false);
229
            this.txtM.setEnabled(false);
230

    
231
            this.lblX.setEnabled(false);
232
            this.lblY.setEnabled(false);
233
            this.lblZ.setEnabled(false);
234
            this.lblM.setEnabled(false);
235
        }
236
    }
237

    
238
    public void setPoint(Point point) {
239
        if( point == null ) {
240
            this.pickerX.set(null);
241
            this.pickerY.set(null);
242
            this.pickerZ.set(null);
243
            this.pickerM.set(null);
244
            return;
245
        }
246
        this.pickerX.set( point.getX() );
247
        this.pickerY.set( point.getY() );
248
        GeometryType theGeomType = point.getGeometryType();
249
        if( theGeomType.hasZ() ) {
250
            this.pickerZ.set(point.getCoordinateAt(Geometry.DIMENSIONS.Z));
251
        } 
252
        if( theGeomType.hasM() ) {
253
            this.pickerM.set(point.getCoordinateAt(point.getDimension()-1));
254
        }
255
    }
256
    
257
    private void addHierarchyChanged() {
258
        this.txtX.addHierarchyListener(new DefaultHierarchyListener());
259
    }
260

    
261
    /**
262
     * @return this as JComponent
263
     */
264
    public JComponent asJComponent() {
265
        return this;
266
    }
267

    
268
    /**
269
     * @param i
270
     * @return String value of coordinate i
271
     */
272
    public String getValue(int i) {
273
        return Objects.toString(this.pickers[i].get(),"");
274
    }
275

    
276
    /**
277
     * @param i
278
     * @param value
279
     */
280
    public void setValue(int i, String value) {
281
        if( StringUtils.isBlank(value) ) {
282
            this.pickers[i].set(null);
283
            return;
284
        } 
285
        this.pickers[i].coerceAndSet(value);
286
    }
287
}