Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / AddLayer.java @ 42573

History | View | Annotate | Download (9.7 KB)

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

    
25
import java.awt.Component;
26
import java.io.File;
27
import java.lang.reflect.InvocationTargetException;
28
import java.util.ArrayList;
29
import java.util.List;
30

    
31
import javax.swing.JOptionPane;
32

    
33
import org.cresques.cts.ICoordTrans;
34
import org.cresques.cts.IProjection;
35
import org.gvsig.andami.IconThemeHelper;
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.andami.plugins.Extension;
38
import org.gvsig.app.ApplicationLocator;
39
import org.gvsig.app.ApplicationManager;
40
import org.gvsig.app.addlayer.AddLayerDialog;
41
import org.gvsig.app.gui.WizardPanel;
42
import org.gvsig.app.project.documents.view.ViewDocument;
43
import org.gvsig.app.project.documents.view.gui.IView;
44
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerAddLayerWizardPanel;
45
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
46
import org.gvsig.fmap.mapcontext.MapContext;
47
import org.gvsig.fmap.mapcontext.ViewPort;
48
import org.gvsig.fmap.mapcontext.layers.FLayer;
49
import org.gvsig.fmap.mapcontext.layers.FLayers;
50
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
51
import org.gvsig.fmap.mapcontrol.MapControl;
52
import org.gvsig.tools.dispose.DisposeUtils;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55

    
56
/**
57
 * Extensi�n que abre un di�logo para seleccionar la capa o capas que se
58
 * quieren anadir a la vista.
59
 *
60
 */
61
public class AddLayer extends Extension {
62

    
63
    private static final Logger logger = LoggerFactory.getLogger(AddLayer.class);
64

    
65
    private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
66

    
67
    static {
68
        AddLayer.wizardStack = new ArrayList<>();
69
        // Anadimos el panel al wizard de cargar capa. 
70
        AddLayer.addWizard(FilesystemExplorerAddLayerWizardPanel.class);
71
    }
72

    
73
    public static void addWizard(Class<? extends WizardPanel> wpClass) {
74
        AddLayer.wizardStack.add(wpClass);
75
    }
76

    
77
    private static WizardPanel getInstance(int i, MapControl mapControl)
78
            throws IllegalArgumentException, SecurityException,
79
            InstantiationException, IllegalAccessException,
80
            InvocationTargetException, NoSuchMethodException {
81
        Class<? extends WizardPanel> wpClass = AddLayer.wizardStack.get(i);
82
        Object[] params = {};
83
        WizardPanel wp = wpClass.getConstructor()
84
                .newInstance(params);
85
        wp.setMapCtrl(mapControl);
86
        wp.initWizard();
87

    
88
        return wp;
89
    }
90

    
91
    @Override
92
    public boolean isVisible() {
93
        ApplicationManager application = ApplicationLocator.getManager();
94

    
95
        return application.getActiveComponent(ViewDocument.class) != null;
96
    }
97

    
98
    @Override
99
    public void postInitialize() {
100
        super.postInitialize();
101
    }
102

    
103
    public static void checkProjection(FLayer lyr, ViewPort viewPort) {
104
        if (lyr instanceof FLayers) {
105
            FLayers layers = (FLayers) lyr;
106
            for (int i = 0; i < layers.getLayersCount(); i++) {
107
                checkProjection(layers.getLayer(i), viewPort);
108
            }
109
        }
110
        if (lyr instanceof FLyrVect) {
111
            FLyrVect lyrVect = (FLyrVect) lyr;
112
            IProjection proj = lyr.getProjection();
113
            // Comprobar que la projecci�n es la misma que la vista
114
            if (proj == null) {
115
                // SUPONEMOS que la capa est� en la proyecci�n que
116
                // estamos pidiendo (que ya es mucho suponer, ya).
117
                lyrVect.setProjection(viewPort.getProjection());
118
                return;
119
            }
120
            int option;
121
            if (proj != viewPort.getProjection()) {
122
                option = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(), PluginServices
123
                        .getText(AddLayer.class, "reproyectar_aviso") + "\n" + PluginServices.getText(AddLayer.class, "Capa") + ": " + lyrVect.getName(), PluginServices
124
                        .getText(AddLayer.class, "reproyectar_pregunta"),
125
                        JOptionPane.YES_NO_OPTION);
126

    
127
                if (option != JOptionPane.OK_OPTION) {
128
                    return;
129
                }
130

    
131
                ICoordTrans ct = proj.getCT(viewPort.getProjection());
132
                lyrVect.setCoordTrans(ct);
133
                System.err.println("coordTrans = " + proj.getAbrev() + " "
134
                        + viewPort.getProjection().getAbrev());
135
            }
136
        }
137

    
138
    }
139

    
140
    @Override
141
    public void execute(String actionCommand) {
142
        this.execute(actionCommand, null);
143
    }
144

    
145
    @Override
146
    public void execute(String command, Object[] args) {
147
        List<File> files = null;
148
        if (args != null && args.length >= 1 && args[0] instanceof List) {
149
            files = (List<File>) args[0];
150
        }
151
        ApplicationManager application = ApplicationLocator.getManager();
152

    
153
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
154
        if (view == null) {
155
            return;
156
        }
157
        ViewDocument document = view.getViewDocument();
158

    
159
        MapControl mapControl = view.getMapControl();
160
        this.doAddLayers(mapControl, mapControl.getMapContext(), files);
161
        mapControl.getMapContext().callLegendChanged();
162
        document.setModified(true);
163
    }
164

    
165
    @Override
166
    public boolean isEnabled() {
167
        return true;
168
    }
169

    
170
    /**
171
     * Creates FOpenDialog, and adds file tab, and additional registered tabs
172
     *
173
     * @return FOpenDialog
174
     */
175
    private AddLayerDialog createFOpenDialog(MapControl mapControl, MapContext mapContext, List<File> files) {
176
        AddLayerDialog fopen = new AddLayerDialog();
177
        for (Class<? extends WizardPanel> wpClass : wizardStack) {
178
            WizardPanel wp;
179
            try {
180
                Object[] params = {};
181
                wp = wpClass.getConstructor()
182
                        .newInstance(params);
183
                wp.setMapCtrl(mapControl);
184
                wp.setMapContext(mapContext);
185
                wp.initWizard();
186
                if (wp instanceof FilesystemExplorerWizardPanel && files != null && !files.isEmpty()) {
187
                    FilesystemExplorerWizardPanel fswp = (FilesystemExplorerWizardPanel) wp;
188
                    fswp.addFiles(files);
189
                }
190
                fopen.addWizardTab(wp.getTabName(), wp);
191
            } catch (Exception e) {
192
                logger.warn("Can't create layer open dialog.", e);
193
            }
194
        }
195
        fopen.updateOkButtonState();
196
        return fopen;
197
    }
198

    
199
    /**
200
     * Adds to mapcontrol all layers selected by user in the specified
201
     * WizardPanel.
202
     *
203
     * @param mapControl MapControl on which we want to load user selected
204
     * layers.
205
     * @param wizardPanel WizardPanel where user selected the layers to load
206
     * @return
207
     */
208
//    private boolean loadGenericWizardPanelLayers(MapContext mapContext, WizardPanel wp, boolean mapControlAvailable) {
209
//        wp.setMapContext(mapContext);
210
//        wp.executeWizard();
211
//        return true;
212
//    }
213
    /**
214
     * Opens the AddLayer dialog, and adds the selected layers to the provided
215
     * MapContext.
216
     *
217
     * This method is useful when we want to add a layer but we don't have an
218
     * associated View. If a View or a MapControl is available, you will usually
219
     * prefer the {@link #addLayers(MapControl)} method.
220
     *
221
     * @param mapContext The MapContext to add the layers to
222
     * @return <code>true</code> if any layer has been added.
223
     */
224
    public boolean addLayers(MapContext mapContext) {
225
        return doAddLayers(null, mapContext, null);
226
    }
227

    
228
    /**
229
     * Opens the AddLayer dialog, and adds the selected layers to the provided
230
     * mapControl.
231
     *
232
     * @param mapControl The MapControl to add the layers to
233
     *
234
     * @return <code>true</code> if any layer has been added.
235
     */
236
    public boolean addLayers(MapControl mapControl) {
237
        return doAddLayers(mapControl, mapControl.getMapContext(), null);
238
    }
239

    
240
    private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files) {
241
        AddLayerDialog fopen = null;
242
        try {
243
            fopen = createFOpenDialog(mapControl, mapContext, files);
244
            PluginServices.getMDIManager().addWindow(fopen);
245

    
246
            if (fopen.isAccepted()) {
247
                if (fopen.getSelectedTab() instanceof WizardPanel) {
248
                    WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
249
                    wp.execute();
250
                    return true;
251
                } else {
252
                    JOptionPane.showMessageDialog((Component) PluginServices
253
                            .getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada"));
254
                }
255
            }
256
            return false;
257
        } finally {
258
            DisposeUtils.disposeQuietly(fopen);
259
        }
260
    }
261

    
262
    @Override
263
    public void initialize() {
264
        IconThemeHelper.registerIcon("action", "view-layer-add", this);
265
    }
266
}