gvsig-derived-geometries / org.gvsig.derivedgeometries / trunk / org.gvsig.derivedgeometries / org.gvsig.derivedgeometries.swing / org.gvsig.derivedgeometries.swing.impl / src / main / java / org / gvsig / derivedgeometries / swing / impl / panels / DefaultLayerAndProcessSelectionPanel.java @ 42
History | View | Annotate | Download (15 KB)
1 |
/*
|
---|---|
2 |
* Copyright 2014 DiSiD Technologies S.L.L. All rights reserved.
|
3 |
*
|
4 |
* Project : DiSiD org.gvsig.derivedgeometries.swing.impl
|
5 |
* SVN Id : $Id$
|
6 |
*/
|
7 |
package org.gvsig.derivedgeometries.swing.impl.panels; |
8 |
|
9 |
import java.awt.event.ActionEvent; |
10 |
import java.awt.event.ActionListener; |
11 |
import java.io.File; |
12 |
import java.util.Vector; |
13 |
|
14 |
import javax.swing.JComponent; |
15 |
import javax.swing.JFileChooser; |
16 |
import javax.swing.JOptionPane; |
17 |
|
18 |
import org.slf4j.Logger; |
19 |
import org.slf4j.LoggerFactory; |
20 |
|
21 |
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesLocator; |
22 |
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesParameters; |
23 |
import org.gvsig.derivedgeometries.swing.api.DerivedGeometriesProcess.TYPE; |
24 |
import org.gvsig.derivedgeometries.swing.api.exceptions.ValidateSourceLayerException; |
25 |
import org.gvsig.derivedgeometries.swing.api.panels.LayerAndProcessSelectionPanel; |
26 |
import org.gvsig.derivedgeometries.swing.impl.views.LayerAndProcessSelectionPanelView; |
27 |
import org.gvsig.fmap.dal.DALLocator; |
28 |
import org.gvsig.fmap.dal.DataManager; |
29 |
import org.gvsig.fmap.dal.DataServerExplorer; |
30 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
31 |
import org.gvsig.fmap.dal.DataStoreParameters; |
32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
36 |
import org.gvsig.fmap.geom.type.GeometryType; |
37 |
import org.gvsig.fmap.mapcontext.MapContext; |
38 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
39 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
40 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
41 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
42 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener; |
43 |
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent; |
44 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
45 |
import org.gvsig.fmap.mapcontrol.MapControl; |
46 |
import org.gvsig.tools.ToolsLocator; |
47 |
import org.gvsig.tools.i18n.I18nManager; |
48 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
49 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
50 |
import org.gvsig.utils.GenericFileFilter; |
51 |
|
52 |
public class DefaultLayerAndProcessSelectionPanel extends |
53 |
LayerAndProcessSelectionPanelView implements LayerAndProcessSelectionPanel {
|
54 |
|
55 |
private static final long serialVersionUID = 1032294236575442328L; |
56 |
|
57 |
private static final Logger LOG = LoggerFactory |
58 |
.getLogger(DefaultLayerAndProcessSelectionPanel.class); |
59 |
|
60 |
private final String[] SHAPE_EXTENSIONS = { "shp", "SHP", "Shp", "sHp", |
61 |
"SHp", "shP", "ShP", "sHP" }; |
62 |
|
63 |
private MapControl mapControl;
|
64 |
|
65 |
private DerivedGeometriesParameters parameters;
|
66 |
|
67 |
public DefaultLayerAndProcessSelectionPanel(MapControl mapControl) {
|
68 |
super();
|
69 |
|
70 |
this.mapControl = mapControl;
|
71 |
|
72 |
initSourceLayerCombo(); |
73 |
initProcessCombo(); |
74 |
initSelectPathButton(); |
75 |
|
76 |
addVectorialLayers(); |
77 |
|
78 |
initMapControlListeners(); |
79 |
} |
80 |
|
81 |
@SuppressWarnings("unchecked") |
82 |
private void addVectorialLayers() { |
83 |
getLayersComboBox().removeAllItems(); |
84 |
FLayers flayers = mapControl.getMapContext().getLayers(); |
85 |
for (FLyrVect layer : getVectorialLayers(flayers)) {
|
86 |
getLayersComboBox().addItem(layer); |
87 |
} |
88 |
} |
89 |
|
90 |
public JComponent asJComponent() { |
91 |
return this; |
92 |
} |
93 |
|
94 |
public void closePanel() { |
95 |
setVisible(false);
|
96 |
} |
97 |
|
98 |
private void deleteShapeFiles(FLyrVect sourceLayer, String outputLayerPath) { |
99 |
DataManager dataManager = DALLocator.getDataManager(); |
100 |
|
101 |
try {
|
102 |
DataServerExplorerParameters eparams = |
103 |
dataManager |
104 |
.createServerExplorerParameters("FilesystemExplorer");
|
105 |
eparams.setDynValue("initialpath", "/data"); |
106 |
|
107 |
DataServerExplorer serverExplorer = |
108 |
dataManager.openServerExplorer(eparams.getExplorerName(), |
109 |
eparams); |
110 |
|
111 |
DataStoreParameters dataStoreParams = |
112 |
dataManager.createStoreParameters("Shape");
|
113 |
|
114 |
dataStoreParams.setDynValue("shpfile", outputLayerPath);
|
115 |
dataStoreParams.setDynValue("CRS", sourceLayer.getProjection());
|
116 |
dataStoreParams.setDynValue("useNullGeometry", false); |
117 |
dataStoreParams.validate(); |
118 |
|
119 |
serverExplorer.remove(dataStoreParams); |
120 |
} catch (Exception e) { |
121 |
e.printStackTrace(); |
122 |
} |
123 |
} |
124 |
|
125 |
public void doAccept() { |
126 |
FLyrVect sourceLayer = (FLyrVect) getLayersComboBox().getSelectedItem(); |
127 |
String outputLayerName = getOutputLayerName().getText();
|
128 |
String outputLayerPath = getOutputLayerPath().getText();
|
129 |
String type = (String) getOutputShapeTypeCombo().getSelectedItem(); |
130 |
|
131 |
File outputLayer = new File(outputLayerPath); |
132 |
//If file exist, it will delete shape files to overwrite shape.
|
133 |
if (outputLayer.exists()) {
|
134 |
deleteShapeFiles(sourceLayer, outputLayerPath); |
135 |
} |
136 |
|
137 |
int outputLayerType = 0; |
138 |
if (type.equalsIgnoreCase("Multicurve")) { |
139 |
outputLayerType = TYPES.MULTICURVE; |
140 |
} else if (type.equalsIgnoreCase("Surface")) { |
141 |
outputLayerType = TYPES.SURFACE; |
142 |
} |
143 |
|
144 |
TYPE processType = (TYPE) getProcessCombo().getSelectedItem(); |
145 |
|
146 |
parameters = |
147 |
DerivedGeometriesLocator.getManager() |
148 |
.createDerivedGeometriesParameters(mapControl, sourceLayer, |
149 |
outputLayerName, outputLayerPath, outputLayerType, |
150 |
processType); |
151 |
} |
152 |
|
153 |
public void doCancel() { |
154 |
closePanel(); |
155 |
} |
156 |
|
157 |
public DerivedGeometriesParameters getParameters() {
|
158 |
return parameters;
|
159 |
} |
160 |
|
161 |
private Vector<FLyrVect> getVectorialLayers(FLayers flayers) { |
162 |
Vector<FLyrVect> vectorialLayers = new Vector<FLyrVect>(); |
163 |
for (int i = 0; i < flayers.getLayersCount(); i++) { |
164 |
if (flayers.getLayer(i) instanceof FLyrVect) { |
165 |
vectorialLayers.add((FLyrVect) flayers.getLayer(i)); |
166 |
|
167 |
} else if(flayers.getLayer(i) instanceof FLayers ){ |
168 |
FLayers group = ((FLayers) flayers.getLayer(i)); |
169 |
vectorialLayers.addAll(getVectorialLayers(group)); |
170 |
} |
171 |
} |
172 |
|
173 |
return vectorialLayers;
|
174 |
} |
175 |
|
176 |
private void initMapControlListeners() { |
177 |
|
178 |
MapContext context = mapControl.getMapContext(); |
179 |
FLayers layers = context.getLayers(); |
180 |
|
181 |
layers.addLayerCollectionListener(new LayerCollectionListener() {
|
182 |
|
183 |
public void layerAdded(LayerCollectionEvent e) { |
184 |
FLayer layer = e.getAffectedLayer(); |
185 |
if (layer instanceof FLyrVect) { |
186 |
getLayersComboBox().addItem(layer); |
187 |
} |
188 |
} |
189 |
|
190 |
public void layerAdding(LayerCollectionEvent e) |
191 |
throws CancelationException {
|
192 |
} |
193 |
|
194 |
public void layerMoved(LayerPositionEvent e) { |
195 |
} |
196 |
|
197 |
public void layerMoving(LayerPositionEvent e) |
198 |
throws CancelationException {
|
199 |
} |
200 |
|
201 |
public void layerRemoved(LayerCollectionEvent e) { |
202 |
FLayer layer = e.getAffectedLayer(); |
203 |
if (layer instanceof FLyrVect) { |
204 |
getLayersComboBox().removeItem(layer); |
205 |
} |
206 |
} |
207 |
|
208 |
public void layerRemoving(LayerCollectionEvent e) |
209 |
throws CancelationException {
|
210 |
} |
211 |
|
212 |
public void visibilityChanged(LayerCollectionEvent e) |
213 |
throws CancelationException {
|
214 |
} |
215 |
}); |
216 |
} |
217 |
|
218 |
private void initProcessCombo() { |
219 |
getProcessCombo().addActionListener(new ActionListener() { |
220 |
|
221 |
@SuppressWarnings("unchecked") |
222 |
public void actionPerformed(ActionEvent e) { |
223 |
// Update the other comboBox
|
224 |
TYPE processType = (TYPE) getProcessCombo().getSelectedItem(); |
225 |
if (processType != null) { |
226 |
switch (processType) {
|
227 |
case POINTS_TO_LINE:
|
228 |
getOutputShapeTypeCombo().removeAllItems(); |
229 |
getOutputShapeTypeCombo().addItem("Multicurve");
|
230 |
break;
|
231 |
case POINTS_TO_POLYGON:
|
232 |
getOutputShapeTypeCombo().removeAllItems(); |
233 |
getOutputShapeTypeCombo().addItem("Surface");
|
234 |
break;
|
235 |
default:
|
236 |
break;
|
237 |
} |
238 |
} |
239 |
} |
240 |
}); |
241 |
} |
242 |
|
243 |
private void initSelectPathButton() { |
244 |
getJButtonSelectPath().addActionListener( |
245 |
new java.awt.event.ActionListener() {
|
246 |
|
247 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
248 |
ThreadSafeDialogsManager dlgManager = |
249 |
ToolsSwingLocator.getThreadSafeDialogsManager(); |
250 |
|
251 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
252 |
|
253 |
File[] files = |
254 |
dlgManager.showChooserDialog( |
255 |
"test",
|
256 |
JFileChooser.OPEN_DIALOG,
|
257 |
JFileChooser.FILES_ONLY,
|
258 |
false,
|
259 |
new File(System.getProperty("user.home")), |
260 |
new GenericFileFilter(SHAPE_EXTENSIONS, i18nManager
|
261 |
.getTranslation("shape_file") + " (*.shp)"), |
262 |
true);
|
263 |
|
264 |
if (files == null) { |
265 |
return;
|
266 |
} |
267 |
|
268 |
if (files.length == 1) { |
269 |
File file = files[0]; |
270 |
|
271 |
if (file.exists()) {
|
272 |
|
273 |
String title =
|
274 |
i18nManager |
275 |
.getTranslation("_file_already_exist");
|
276 |
String message =
|
277 |
i18nManager |
278 |
.getTranslation("_file_already_exist_override_it");
|
279 |
|
280 |
int answer =
|
281 |
dlgManager.confirmDialog(message, title, |
282 |
JOptionPane.YES_NO_OPTION,
|
283 |
JOptionPane.INFORMATION_MESSAGE);
|
284 |
|
285 |
if (answer == JOptionPane.YES_OPTION) { |
286 |
getOutputLayerPath().setText( |
287 |
file.getAbsolutePath()); |
288 |
} else {
|
289 |
return;
|
290 |
} |
291 |
|
292 |
} else {
|
293 |
|
294 |
// if (FilenameUtils.getExtension(
|
295 |
// file.getAbsolutePath()).equals(SHAPE_EXTENSION))
|
296 |
// {
|
297 |
// getOutputLayerPath().setText(
|
298 |
// file.getAbsolutePath());
|
299 |
// } else {
|
300 |
|
301 |
StringBuilder stb = new StringBuilder(); |
302 |
stb.append(file.getAbsolutePath()); |
303 |
stb.append(".");
|
304 |
stb.append(SHAPE_EXTENSIONS[0]);
|
305 |
|
306 |
getOutputLayerPath().setText(stb.toString()); |
307 |
// }
|
308 |
} |
309 |
} |
310 |
} |
311 |
}); |
312 |
} |
313 |
|
314 |
private void initSourceLayerCombo() { |
315 |
getLayersComboBox().addActionListener(new ActionListener() { |
316 |
|
317 |
@SuppressWarnings("unchecked") |
318 |
public void actionPerformed(ActionEvent e) { |
319 |
FLyrVect selectedLayer = |
320 |
(FLyrVect) getLayersComboBox().getSelectedItem(); |
321 |
GeometryType featureGeomType = null;
|
322 |
|
323 |
getOutputShapeTypeCombo().removeAllItems(); |
324 |
getProcessCombo().removeAllItems(); |
325 |
|
326 |
FeatureStore featureStore = selectedLayer.getFeatureStore(); |
327 |
try {
|
328 |
featureGeomType = |
329 |
featureStore.getDefaultFeatureType() |
330 |
.getDefaultGeometryAttribute().getGeomType(); |
331 |
} catch (DataException ex) {
|
332 |
String message =
|
333 |
String.format("Error getting feature type of %1", |
334 |
featureStore); |
335 |
LOG.info(message, ex); |
336 |
return;
|
337 |
} |
338 |
|
339 |
if (featureGeomType.isTypeOf(Geometry.TYPES.POINT)
|
340 |
|| featureGeomType.isTypeOf(Geometry.TYPES.MULTIPOINT)) { |
341 |
setSelectableComponentsEnabled(true);
|
342 |
getOutputShapeTypeCombo().addItem("Multicurve");
|
343 |
getProcessCombo().addItem(TYPE.POINTS_TO_LINE); |
344 |
getProcessCombo().addItem(TYPE.POINTS_TO_POLYGON); |
345 |
|
346 |
} else if (featureGeomType.isTypeOf(Geometry.TYPES.MULTICURVE)) { |
347 |
setSelectableComponentsEnabled(true);
|
348 |
getOutputShapeTypeCombo().addItem("Surface");
|
349 |
getProcessCombo().addItem(TYPE.LINES_TO_CLOSED_POLYLINE); |
350 |
|
351 |
} else {
|
352 |
setSelectableComponentsEnabled(false);
|
353 |
getOutputLayerName().setText("");
|
354 |
getOutputLayerPath().setText("");
|
355 |
} |
356 |
} |
357 |
}); |
358 |
} |
359 |
|
360 |
public void setMapControl(MapControl mapControl) { |
361 |
this.mapControl = mapControl;
|
362 |
} |
363 |
|
364 |
private void setSelectableComponentsEnabled(boolean b) { |
365 |
getOutputLayerPath().setEnabled(b); |
366 |
getOutputLayerName().setEnabled(b); |
367 |
getOutputShapeTypeCombo().setEnabled(b); |
368 |
getJButtonSelectPath().setEnabled(b); |
369 |
getProcessCombo().setEnabled(b); |
370 |
} |
371 |
|
372 |
public boolean validateFields() { |
373 |
FLyrVect sourceLayer = (FLyrVect) getLayersComboBox().getSelectedItem(); |
374 |
String outputLayerName = getOutputLayerName().getText();
|
375 |
String outputLayerPath = getOutputLayerPath().getText();
|
376 |
|
377 |
if (sourceLayer != null && outputLayerName.length() > 0 |
378 |
&& outputLayerPath.length() > 0) {
|
379 |
return true; |
380 |
} else {
|
381 |
return false; |
382 |
} |
383 |
} |
384 |
|
385 |
public boolean validateSourceLayer() throws ValidateSourceLayerException { |
386 |
FLyrVect sourceLayer = (FLyrVect) getLayersComboBox().getSelectedItem(); |
387 |
FeatureStore featureStore = sourceLayer.getFeatureStore(); |
388 |
GeometryType featureGeomType; |
389 |
try {
|
390 |
featureGeomType = |
391 |
featureStore.getDefaultFeatureType() |
392 |
.getDefaultGeometryAttribute().getGeomType(); |
393 |
} catch (DataException e) {
|
394 |
String message =
|
395 |
String.format(
|
396 |
"Error validating %1. Can't get feature geometry type",
|
397 |
sourceLayer); |
398 |
throw new ValidateSourceLayerException(message, e); |
399 |
} |
400 |
if (featureGeomType.isTypeOf(Geometry.TYPES.POINT)
|
401 |
|| featureGeomType.isTypeOf(Geometry.TYPES.MULTIPOINT) |
402 |
|| featureGeomType.isTypeOf(Geometry.TYPES.MULTICURVE)) { |
403 |
return true; |
404 |
} |
405 |
return false; |
406 |
} |
407 |
} |