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 @ 44259

History | View | Annotate | Download (9.59 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.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.andami.IconThemeHelper;
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.andami.plugins.Extension;
41
import org.gvsig.app.ApplicationLocator;
42
import org.gvsig.app.ApplicationManager;
43
import org.gvsig.app.addlayer.AddLayerDialog;
44
import org.gvsig.app.gui.WizardPanel;
45
import org.gvsig.app.project.documents.view.ViewDocument;
46
import org.gvsig.app.project.documents.view.gui.IView;
47
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
48
import org.gvsig.fmap.mapcontext.MapContext;
49
import org.gvsig.fmap.mapcontext.ViewPort;
50
import org.gvsig.fmap.mapcontext.layers.FLayer;
51
import org.gvsig.fmap.mapcontext.layers.FLayers;
52
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
53
import org.gvsig.fmap.mapcontrol.MapControl;
54
import org.gvsig.tools.ToolsLocator;
55
import org.gvsig.tools.dispose.DisposeUtils;
56
import org.gvsig.tools.i18n.I18nManager;
57
import org.gvsig.tools.util.ArrayUtils;
58

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

    
66
    private static final Logger logger = LoggerFactory.getLogger(AddLayer.class);
67

    
68
    private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
69

    
70
    static {
71
        AddLayer.wizardStack = new ArrayList<>();
72
        // Anadimos el panel al wizard de cargar capa.
73
        AddLayer.addWizard(FilesystemExplorerWizardPanel.class);
74
    }
75

    
76
    public static void addWizard(Class<? extends WizardPanel> wpClass) {
77
        AddLayer.wizardStack.add(wpClass);
78
    }
79

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

    
91
        return wp;
92
    }
93

    
94
    @Override
95
    public boolean isVisible() {
96
        ApplicationManager application = ApplicationLocator.getManager();
97

    
98
        return application.getActiveComponent(ViewDocument.class) != null;
99
    }
100

    
101
    @Override
102
    public void postInitialize() {
103
        super.postInitialize();
104
    }
105

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

    
130
                if (option != JOptionPane.OK_OPTION) {
131
                    return;
132
                }
133

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

    
141
    }
142

    
143
    @Override
144
    public void execute(String actionCommand) {
145
        this.execute(actionCommand, null);
146
    }
147

    
148
    @Override
149
    public void execute(String command, Object[] args) {
150
        List<File> files = ArrayUtils.getListOf(args,0,File.class);
151

    
152
        ApplicationManager application = ApplicationLocator.getManager();
153

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

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

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

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

    
207
    /**
208
     * Opens the AddLayer dialog, and adds the selected layers to the provided
209
     * MapContext.
210
     *
211
     * This method is useful when we want to add a layer but we don't have an
212
     * associated View. If a View or a MapControl is available, you will usually
213
     * prefer the {@link #addLayers(MapControl)} method.
214
     *
215
     * @param mapContext The MapContext to add the layers to
216
     * @return <code>true</code> if any layer has been added.
217
     */
218
    public boolean addLayers(MapContext mapContext) {
219
        return doAddLayers(null, mapContext, null);
220
    }
221

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

    
234
    private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files) {
235
        AddLayerDialog addLayerDialog = null;
236
        try {
237
            addLayerDialog = createAddLayerDialog(mapControl, mapContext, files);
238
            PluginServices.getMDIManager().addWindow(addLayerDialog);
239

    
240
            if (addLayerDialog.isAccepted()) {
241
                if (addLayerDialog.getSelectedTab() instanceof WizardPanel) {
242
                    WizardPanel panel = (WizardPanel) addLayerDialog.getSelectedTab();
243
                    panel.executeWizard();
244
                    return true;
245
                } else {
246
                    JOptionPane.showMessageDialog((Component) PluginServices
247
                            .getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada"));
248
                }
249
            }
250
            return false;
251
        } finally {
252
            DisposeUtils.disposeQuietly(addLayerDialog);
253
        }
254
    }
255

    
256
    @Override
257
    public void initialize() {
258
        IconThemeHelper.registerIcon("action", "view-layer-add", this);
259
    }
260
}