gvsig-3d / 2.1 / trunk / org.gvsig.view3d / org.gvsig.view3d.main / src / main / java / org / gvsig / view3d / main / PaletteActions.java @ 411
History | View | Annotate | Download (11.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2015 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 |
package org.gvsig.view3d.main; |
26 |
|
27 |
import java.awt.event.ActionEvent; |
28 |
import java.io.File; |
29 |
import java.util.HashMap; |
30 |
import java.util.Map; |
31 |
|
32 |
import javax.swing.AbstractAction; |
33 |
import javax.swing.Action; |
34 |
import javax.swing.JFileChooser; |
35 |
import javax.swing.SwingUtilities; |
36 |
import javax.swing.tree.DefaultMutableTreeNode; |
37 |
import javax.swing.tree.TreePath; |
38 |
|
39 |
import org.gvsig.fmap.dal.DALLocator; |
40 |
import org.gvsig.fmap.dal.DataManager; |
41 |
import org.gvsig.fmap.dal.DataStoreParameters; |
42 |
import org.gvsig.fmap.dal.exception.InitializeException; |
43 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
44 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
47 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
48 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
49 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
50 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
51 |
import org.gvsig.raster.impl.store.DefaultRasterStore; |
52 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
53 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
54 |
import org.gvsig.utils.GenericFileFilter; |
55 |
import org.gvsig.view3d.swing.api.View3DLocator; |
56 |
import org.gvsig.view3d.swing.api.View3DManager; |
57 |
import org.gvsig.view3d.swing.api.View3DPanel; |
58 |
|
59 |
/**
|
60 |
* @author llmarques
|
61 |
*
|
62 |
*/
|
63 |
public class PaletteActions { |
64 |
|
65 |
Map<String, Action> actions; |
66 |
Main main; |
67 |
|
68 |
public PaletteActions(Main main) {
|
69 |
|
70 |
this.main = main;
|
71 |
|
72 |
actions = new HashMap<String, Action>(); |
73 |
|
74 |
actions.put("exit", getExitAction());
|
75 |
actions.put("add-vectorial-layer", getAddVectorialLayerAction());
|
76 |
actions.put("add-raster-layer", getAddRasterLayerAction());
|
77 |
actions.put("remove-layer", getRemoveLayerAction());
|
78 |
actions.put("create-sphere-view-3d", getCreateSphereViewAction());
|
79 |
actions.put("create-flat-view-3d", getCreateFlatViewAction());
|
80 |
actions.put("create-reload-view-3d", getCreateReloadLayersAction());
|
81 |
actions.put("create-synchronize-view-3d", getCreateSynchronizeAction());
|
82 |
|
83 |
} |
84 |
|
85 |
public Action getAction(String actionCommand) { |
86 |
return actions.get(actionCommand);
|
87 |
} |
88 |
|
89 |
private AbstractAction getAddVectorialLayerAction() { |
90 |
return new AbstractAction("Add Vectorial layer") { |
91 |
|
92 |
/**
|
93 |
*
|
94 |
*/
|
95 |
private static final long serialVersionUID = 7506436995278783992L; |
96 |
|
97 |
public void actionPerformed(ActionEvent e) { |
98 |
ThreadSafeDialogsManager dlgManager = |
99 |
ToolsSwingLocator.getThreadSafeDialogsManager(); |
100 |
|
101 |
File[] file = |
102 |
dlgManager.showChooserDialog( |
103 |
"Seleccione una capa vectorial",
|
104 |
JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, |
105 |
false, new File(System.getProperty("user.home")), |
106 |
new GenericFileFilter(new String[] { ".shp", ".kml", |
107 |
".kmz" }, "(*.shp),(*.kml),(*.kmz)"), true); |
108 |
|
109 |
if (file != null) { |
110 |
try {
|
111 |
for (int i = 0; i < file.length; i++) { |
112 |
addVectorialLayer(file[i].getAbsolutePath()); |
113 |
} |
114 |
} catch (Exception ex) { |
115 |
ex.printStackTrace(); |
116 |
} |
117 |
} |
118 |
} |
119 |
}; |
120 |
} |
121 |
|
122 |
public void addRasterLayer(String layerPath) throws InitializeException, |
123 |
ProviderNotRegisteredException, ValidateDataParametersException, |
124 |
LoadLayerException { |
125 |
|
126 |
DataStoreParameters params; |
127 |
DataManager dataManager = DALLocator.getDataManager(); |
128 |
MapContextManager mapContextManager = |
129 |
MapContextLocator.getMapContextManager(); |
130 |
|
131 |
params = dataManager.createStoreParameters("Gdal Store");
|
132 |
params.setDynValue("uri", layerPath);
|
133 |
params.validate(); |
134 |
|
135 |
DefaultRasterStore store = |
136 |
(DefaultRasterStore) dataManager.openStore( |
137 |
params.getDataStoreName(), params); |
138 |
|
139 |
FLyrRaster layer = |
140 |
(FLyrRaster) mapContextManager.createLayer(store.getName(), store); |
141 |
|
142 |
this.main.getMapContext().beginAtomicEvent();
|
143 |
this.main.getMapContext().getLayers().addLayer(layer);
|
144 |
this.main.getMapContext().invalidate();
|
145 |
this.main.getMapContext().endAtomicEvent();
|
146 |
|
147 |
// XXX Forces component repaint.
|
148 |
SwingUtilities.updateComponentTreeUI(this.main.mainFrame); |
149 |
} |
150 |
|
151 |
private AbstractAction getAddRasterLayerAction() { |
152 |
return new AbstractAction("Add Raster layer") { |
153 |
|
154 |
/**
|
155 |
*
|
156 |
*/
|
157 |
private static final long serialVersionUID = 7506436995278783992L; |
158 |
|
159 |
public void actionPerformed(ActionEvent e) { |
160 |
ThreadSafeDialogsManager dlgManager = |
161 |
ToolsSwingLocator.getThreadSafeDialogsManager(); |
162 |
|
163 |
File[] file = |
164 |
dlgManager.showChooserDialog("Seleccione una capa raster",
|
165 |
JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, |
166 |
false, new File(System.getProperty("user.home")), |
167 |
new GenericFileFilter(new String[] { "bmp", "gif", |
168 |
"tif", "tiff", "jpg", "jpeg", "png", "vrt", "dat", |
169 |
"lan", "gis", "img", "pix", "aux", "adf", "mpr", |
170 |
"mpl", "map", "asc", "pgm", "ppm", "rst", "rmf", |
171 |
"nos", "kap", "hdr", "raw", "ers", "xml", "grd", |
172 |
"txt" }, "Gdal format"), true); |
173 |
|
174 |
if (file != null) { |
175 |
try {
|
176 |
for (int i = 0; i < file.length; i++) { |
177 |
addRasterLayer(file[i].getAbsolutePath()); |
178 |
} |
179 |
} catch (Exception ex) { |
180 |
ex.printStackTrace(); |
181 |
} |
182 |
} |
183 |
} |
184 |
}; |
185 |
} |
186 |
|
187 |
public void addVectorialLayer(String layerPath) throws InitializeException, |
188 |
ProviderNotRegisteredException, ValidateDataParametersException, |
189 |
LoadLayerException { |
190 |
|
191 |
DataStoreParameters params = null;
|
192 |
DataManager dataManager = DALLocator.getDataManager(); |
193 |
MapContextManager mapContextManager = |
194 |
MapContextLocator.getMapContextManager(); |
195 |
|
196 |
if (layerPath.endsWith(".shp")) { |
197 |
params = dataManager.createStoreParameters("Shape");
|
198 |
params.setDynValue("shpFile", layerPath);
|
199 |
|
200 |
} else if (layerPath.endsWith(".kml") || layerPath.endsWith(".kmz")) { |
201 |
params = dataManager.createStoreParameters("GPE");
|
202 |
params.setDynValue("File", layerPath);
|
203 |
} |
204 |
|
205 |
params.setDynValue("CRS", this.main.getDefaultProjection()); |
206 |
params.validate(); |
207 |
|
208 |
FeatureStore store = |
209 |
(FeatureStore) dataManager.openStore(params.getDataStoreName(), |
210 |
params); |
211 |
|
212 |
FLayer layer = mapContextManager.createLayer(store.getName(), store); |
213 |
this.main.getMapContext().beginAtomicEvent();
|
214 |
this.main.getMapContext().getLayers().addLayer(layer);
|
215 |
this.main.getMapContext().invalidate();
|
216 |
this.main.getMapContext().endAtomicEvent();
|
217 |
|
218 |
// XXX Forces component repaint.
|
219 |
SwingUtilities.updateComponentTreeUI(this.main.mainFrame); |
220 |
} |
221 |
|
222 |
private AbstractAction getExitAction() { |
223 |
return new AbstractAction("Exit") { |
224 |
|
225 |
/**
|
226 |
*
|
227 |
*/
|
228 |
private static final long serialVersionUID = 7506436995278783992L; |
229 |
|
230 |
public void actionPerformed(ActionEvent e) { |
231 |
System.exit(0); |
232 |
} |
233 |
}; |
234 |
} |
235 |
|
236 |
private Action getCreateSphereViewAction() { |
237 |
return new AbstractAction("Create Sphere 3D visor") { |
238 |
|
239 |
/**
|
240 |
*
|
241 |
*/
|
242 |
private static final long serialVersionUID = 7506436995278783992L; |
243 |
|
244 |
public void actionPerformed(ActionEvent e) { |
245 |
View3DManager manager = View3DLocator.getManager(); |
246 |
View3DPanel view3dPanel = |
247 |
manager.createView3DPanel(main.getMapContext(), 0);
|
248 |
// Change value for constant key
|
249 |
view3dPanel.show(); |
250 |
} |
251 |
}; |
252 |
} |
253 |
|
254 |
private Action getCreateSynchronizeAction() { |
255 |
return new AbstractAction("Synchronize views") { |
256 |
|
257 |
/**
|
258 |
*
|
259 |
*/
|
260 |
private static final long serialVersionUID = 7506436995278783992L; |
261 |
|
262 |
public void actionPerformed(ActionEvent e) { |
263 |
// TODO
|
264 |
} |
265 |
}; |
266 |
} |
267 |
|
268 |
private Action getCreateReloadLayersAction() { |
269 |
return new AbstractAction("Reload layers") { |
270 |
|
271 |
/**
|
272 |
*
|
273 |
*/
|
274 |
private static final long serialVersionUID = 7506436995278783992L; |
275 |
|
276 |
public void actionPerformed(ActionEvent e) { |
277 |
// TODO
|
278 |
} |
279 |
}; |
280 |
} |
281 |
|
282 |
private Action getCreateFlatViewAction() { |
283 |
return new AbstractAction("Create Flat 3D visor") { |
284 |
|
285 |
/**
|
286 |
*
|
287 |
*/
|
288 |
private static final long serialVersionUID = 7506436995278783992L; |
289 |
|
290 |
public void actionPerformed(ActionEvent e) { |
291 |
View3DManager manager = View3DLocator.getManager(); |
292 |
View3DPanel view3dPanel = |
293 |
manager.createView3DPanel(main.getMapContext(), 1);
|
294 |
// Change value for constant key
|
295 |
view3dPanel.show(); |
296 |
} |
297 |
}; |
298 |
} |
299 |
|
300 |
private Action getRemoveLayerAction() { |
301 |
return new AbstractAction("Remove selected layer") { |
302 |
|
303 |
/**
|
304 |
*
|
305 |
*/
|
306 |
private static final long serialVersionUID = 7506436995278783992L; |
307 |
|
308 |
public void actionPerformed(ActionEvent e) { |
309 |
TreePath selectionPath = main.getTree().getSelectionPath();
|
310 |
|
311 |
if (selectionPath == null) { |
312 |
return;
|
313 |
} |
314 |
|
315 |
DefaultMutableTreeNode selectedNode =
|
316 |
(DefaultMutableTreeNode) selectionPath
|
317 |
.getLastPathComponent(); |
318 |
selectedNode.removeFromParent(); |
319 |
main.getTreeModel().reload(); |
320 |
|
321 |
main.getMapContext().beginAtomicEvent(); |
322 |
main.getMapContext().getLayers() |
323 |
.removeLayer(selectedNode.toString()); |
324 |
main.getMapContext().invalidate(); |
325 |
main.getMapContext().endAtomicEvent(); |
326 |
|
327 |
// XXX Forces component repaint.
|
328 |
SwingUtilities.updateComponentTreeUI(main.mainFrame);
|
329 |
} |
330 |
}; |
331 |
} |
332 |
} |