Statistics
| Revision:

gvsig-3d / 2.1 / branches / extrusion / org.gvsig.view3d.main / src / main / java / org / gvsig / view3d / main / PaletteActions.java @ 657

History | View | Annotate | Download (11.8 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.View3DSwingLocator;
56
import org.gvsig.view3d.swing.api.View3DSwingManager;
57
import org.gvsig.view3d.swing.api.View3DSwingManager.TYPE;
58
import org.gvsig.view3d.swing.api.View3DPanel;
59

    
60
/**
61
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
62
 *
63
 */
64
public class PaletteActions {
65

    
66
    Map<String, Action> actions;
67
    Main main;
68

    
69
    public PaletteActions(Main main) {
70

    
71
        this.main = main;
72

    
73
        actions = new HashMap<String, Action>();
74

    
75
        actions.put("exit", getExitAction());
76
        actions.put("add-vectorial-layer", getAddVectorialLayerAction());
77
        actions.put("add-raster-layer", getAddRasterLayerAction());
78
        actions.put("remove-layer", getRemoveLayerAction());
79
        actions.put("create-sphere-view-3d", getCreateSphereViewAction());
80
        actions.put("create-flat-view-3d", getCreateFlatViewAction());
81
        actions.put("create-reload-view-3d", getCreateReloadLayersAction());
82
        actions.put("create-synchronize-view-3d", getCreateSynchronizeAction());
83

    
84
    }
85

    
86
    public Action getAction(String actionCommand) {
87
        return actions.get(actionCommand);
88
    }
89

    
90
    private AbstractAction getAddVectorialLayerAction() {
91
        return new AbstractAction("Add Vectorial layer") {
92

    
93
            /**
94
             *
95
             */
96
            private static final long serialVersionUID = 7506436995278783992L;
97

    
98
            public void actionPerformed(ActionEvent e) {
99
                ThreadSafeDialogsManager dlgManager =
100
                    ToolsSwingLocator.getThreadSafeDialogsManager();
101

    
102
                File[] file =
103
                    dlgManager.showChooserDialog(
104
                        "Seleccione una capa vectorial",
105
                        JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY,
106
                        false, new File(System.getProperty("user.home")),
107
                        new GenericFileFilter(new String[] { ".shp", ".kml",
108
                            ".kmz" }, "(*.shp),(*.kml),(*.kmz)"), true);
109

    
110
                if (file != null) {
111
                    try {
112
                        for (int i = 0; i < file.length; i++) {
113
                            addVectorialLayer(file[i].getAbsolutePath());
114
                        }
115
                    } catch (Exception ex) {
116
                        ex.printStackTrace();
117
                    }
118
                }
119
            }
120
        };
121
    }
122

    
123
    public void addRasterLayer(String layerPath) throws InitializeException,
124
        ProviderNotRegisteredException, ValidateDataParametersException,
125
        LoadLayerException {
126

    
127
        DataStoreParameters params;
128
        DataManager dataManager = DALLocator.getDataManager();
129
        MapContextManager mapContextManager =
130
            MapContextLocator.getMapContextManager();
131

    
132
        params = dataManager.createStoreParameters("Gdal Store");
133
        params.setDynValue("uri", layerPath);
134
        params.validate();
135

    
136
        DefaultRasterStore store =
137
            (DefaultRasterStore) dataManager.openStore(
138
                params.getDataStoreName(), params);
139

    
140
        FLyrRaster layer =
141
            (FLyrRaster) mapContextManager.createLayer(store.getName(), store);
142

    
143
        this.main.getMapContext().beginAtomicEvent();
144
        this.main.getMapContext().getLayers().addLayer(layer);
145
        this.main.getMapContext().invalidate();
146
        this.main.getMapContext().endAtomicEvent();
147

    
148
        // XXX Forces component repaint.
149
        SwingUtilities.updateComponentTreeUI(this.main.mainFrame);
150
    }
151

    
152
    private AbstractAction getAddRasterLayerAction() {
153
        return new AbstractAction("Add Raster layer") {
154

    
155
            /**
156
             *
157
             */
158
            private static final long serialVersionUID = 7506436995278783992L;
159

    
160
            public void actionPerformed(ActionEvent e) {
161
                ThreadSafeDialogsManager dlgManager =
162
                    ToolsSwingLocator.getThreadSafeDialogsManager();
163

    
164
                File[] file =
165
                    dlgManager.showChooserDialog("Seleccione una capa raster",
166
                        JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY,
167
                        false, new File(System.getProperty("user.home")),
168
                        new GenericFileFilter(new String[] { "bmp", "gif",
169
                            "tif", "tiff", "jpg", "jpeg", "png", "vrt", "dat",
170
                            "lan", "gis", "img", "pix", "aux", "adf", "mpr",
171
                            "mpl", "map", "asc", "pgm", "ppm", "rst", "rmf",
172
                            "nos", "kap", "hdr", "raw", "ers", "xml", "grd",
173
                            "txt" }, "Gdal format"), true);
174

    
175
                if (file != null) {
176
                    try {
177
                        for (int i = 0; i < file.length; i++) {
178
                            addRasterLayer(file[i].getAbsolutePath());
179
                        }
180
                    } catch (Exception ex) {
181
                        ex.printStackTrace();
182
                    }
183
                }
184
            }
185
        };
186
    }
187

    
188
    public void addVectorialLayer(String layerPath) throws InitializeException,
189
        ProviderNotRegisteredException, ValidateDataParametersException,
190
        LoadLayerException {
191

    
192
        DataStoreParameters params = null;
193
        DataManager dataManager = DALLocator.getDataManager();
194
        MapContextManager mapContextManager =
195
            MapContextLocator.getMapContextManager();
196

    
197
        if (layerPath.endsWith(".shp")) {
198
            params = dataManager.createStoreParameters("Shape");
199
            params.setDynValue("shpFile", layerPath);
200

    
201
        } else if (layerPath.endsWith(".kml") || layerPath.endsWith(".kmz")) {
202
            params = dataManager.createStoreParameters("GPE");
203
            params.setDynValue("File", layerPath);
204
        }
205

    
206
        params.setDynValue("CRS", this.main.getDefaultProjection());
207
        params.validate();
208

    
209
        FeatureStore store =
210
            (FeatureStore) dataManager.openStore(params.getDataStoreName(),
211
                params);
212

    
213
        FLayer layer = mapContextManager.createLayer(store.getName(), store);
214
        this.main.getMapContext().beginAtomicEvent();
215
        this.main.getMapContext().getLayers().addLayer(layer);
216
        this.main.getMapContext().invalidate();
217
        this.main.getMapContext().endAtomicEvent();
218

    
219
        // XXX Forces component repaint.
220
        SwingUtilities.updateComponentTreeUI(this.main.mainFrame);
221
    }
222

    
223
    private AbstractAction getExitAction() {
224
        return new AbstractAction("Exit") {
225

    
226
            /**
227
             *
228
             */
229
            private static final long serialVersionUID = 7506436995278783992L;
230

    
231
            public void actionPerformed(ActionEvent e) {
232
                System.exit(0);
233
            }
234
        };
235
    }
236

    
237
    private Action getCreateSphereViewAction() {
238
        return new AbstractAction("Create Sphere 3D visor") {
239

    
240
            /**
241
             *
242
             */
243
            private static final long serialVersionUID = 7506436995278783992L;
244

    
245
            public void actionPerformed(ActionEvent e) {
246
                View3DSwingManager manager = View3DSwingLocator.getManager();
247
                View3DPanel view3dPanel =
248
                    manager.createView3DPanel(main.getMapContext(),
249
                        "Main",TYPE.SPHERE, new DummieExtendedPropertiesSupport());
250
                view3dPanel.show();
251
            }
252
        };
253
    }
254

    
255
    private Action getCreateSynchronizeAction() {
256
        return new AbstractAction("Synchronize views") {
257

    
258
            /**
259
             *
260
             */
261
            private static final long serialVersionUID = 7506436995278783992L;
262

    
263
            public void actionPerformed(ActionEvent e) {
264
                // TODO
265
            }
266
        };
267
    }
268

    
269
    private Action getCreateReloadLayersAction() {
270
        return new AbstractAction("Reload layers") {
271

    
272
            /**
273
             *
274
             */
275
            private static final long serialVersionUID = 7506436995278783992L;
276

    
277
            public void actionPerformed(ActionEvent e) {
278
                // TODO
279
            }
280
        };
281
    }
282

    
283
    private Action getCreateFlatViewAction() {
284
        return new AbstractAction("Create Flat 3D visor") {
285

    
286
            /**
287
             *
288
             */
289
            private static final long serialVersionUID = 7506436995278783992L;
290

    
291
            public void actionPerformed(ActionEvent e) {
292
                View3DSwingManager manager = View3DSwingLocator.getManager();
293
                View3DPanel view3dPanel =
294
                    manager.createView3DPanel(main.getMapContext(),"Main", TYPE.FLAT,
295
                        new DummieExtendedPropertiesSupport());
296
                // Change value for constant key
297
                view3dPanel.show();
298
            }
299
        };
300
    }
301

    
302
    private Action getRemoveLayerAction() {
303
        return new AbstractAction("Remove selected layer") {
304

    
305
            /**
306
             *
307
             */
308
            private static final long serialVersionUID = 7506436995278783992L;
309

    
310
            public void actionPerformed(ActionEvent e) {
311
                TreePath selectionPath = main.getTree().getSelectionPath();
312

    
313
                if (selectionPath == null) {
314
                    return;
315
                }
316

    
317
                DefaultMutableTreeNode selectedNode =
318
                    (DefaultMutableTreeNode) selectionPath
319
                        .getLastPathComponent();
320
                selectedNode.removeFromParent();
321
                main.getTreeModel().reload();
322

    
323
                main.getMapContext().beginAtomicEvent();
324
                main.getMapContext().getLayers()
325
                    .removeLayer(selectedNode.toString());
326
                main.getMapContext().invalidate();
327
                main.getMapContext().endAtomicEvent();
328

    
329
                // XXX Forces component repaint.
330
                SwingUtilities.updateComponentTreeUI(main.mainFrame);
331
            }
332
        };
333
    }
334
}