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

History | View | Annotate | Download (9.59 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
6 41248 jjdelcerro
 * 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 40435 jjdelcerro
 *
11 41248 jjdelcerro
 * 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 40435 jjdelcerro
 *
16 41248 jjdelcerro
 * 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 40558 jjdelcerro
 *
20 41248 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40435 jjdelcerro
 */
23
package org.gvsig.app.extension;
24
25
import java.awt.Component;
26 41634 jjdelcerro
import java.io.File;
27 40435 jjdelcerro
import java.lang.reflect.InvocationTargetException;
28
import java.util.ArrayList;
29 41634 jjdelcerro
import java.util.List;
30 40435 jjdelcerro
31
import javax.swing.JOptionPane;
32
33
import org.cresques.cts.ICoordTrans;
34
import org.cresques.cts.IProjection;
35 43152 fdiaz
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37
38 40435 jjdelcerro
import org.gvsig.andami.IconThemeHelper;
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.andami.plugins.Extension;
41 40485 jldominguez
import org.gvsig.app.ApplicationLocator;
42 41248 jjdelcerro
import org.gvsig.app.ApplicationManager;
43 40435 jjdelcerro
import org.gvsig.app.addlayer.AddLayerDialog;
44
import org.gvsig.app.gui.WizardPanel;
45 41248 jjdelcerro
import org.gvsig.app.project.documents.view.ViewDocument;
46 40435 jjdelcerro
import org.gvsig.app.project.documents.view.gui.IView;
47 41634 jjdelcerro
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
48 41434 cmartinez
import org.gvsig.fmap.mapcontext.MapContext;
49 40435 jjdelcerro
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 42896 jjdelcerro
import org.gvsig.tools.ToolsLocator;
55 42573 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
56 42896 jjdelcerro
import org.gvsig.tools.i18n.I18nManager;
57 43636 jjdelcerro
import org.gvsig.tools.util.ArrayUtils;
58 40435 jjdelcerro
59
/**
60 41248 jjdelcerro
 * Extensi�n que abre un di�logo para seleccionar la capa o capas que se
61
 * quieren anadir a la vista.
62 40435 jjdelcerro
 *
63
 */
64
public class AddLayer extends Extension {
65 42573 jjdelcerro
66 41258 jjdelcerro
    private static final Logger logger = LoggerFactory.getLogger(AddLayer.class);
67 42573 jjdelcerro
68 41248 jjdelcerro
    private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
69 40435 jjdelcerro
70 41248 jjdelcerro
    static {
71 42573 jjdelcerro
        AddLayer.wizardStack = new ArrayList<>();
72 43152 fdiaz
        // Anadimos el panel al wizard de cargar capa.
73
        AddLayer.addWizard(FilesystemExplorerWizardPanel.class);
74 41248 jjdelcerro
    }
75 40435 jjdelcerro
76 41248 jjdelcerro
    public static void addWizard(Class<? extends WizardPanel> wpClass) {
77
        AddLayer.wizardStack.add(wpClass);
78
    }
79 40435 jjdelcerro
80 41248 jjdelcerro
    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 40435 jjdelcerro
91 41248 jjdelcerro
        return wp;
92
    }
93 40435 jjdelcerro
94 42573 jjdelcerro
    @Override
95 41248 jjdelcerro
    public boolean isVisible() {
96
        ApplicationManager application = ApplicationLocator.getManager();
97 40485 jldominguez
98 41264 jjdelcerro
        return application.getActiveComponent(ViewDocument.class) != null;
99 41248 jjdelcerro
    }
100 40435 jjdelcerro
101 42573 jjdelcerro
    @Override
102 41248 jjdelcerro
    public void postInitialize() {
103
        super.postInitialize();
104
    }
105 40435 jjdelcerro
106 41248 jjdelcerro
    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 42573 jjdelcerro
                // SUPONEMOS que la capa est� en la proyecci�n que
119 41248 jjdelcerro
                // estamos pidiendo (que ya es mucho suponer, ya).
120
                lyrVect.setProjection(viewPort.getProjection());
121
                return;
122
            }
123 42573 jjdelcerro
            int option;
124 41248 jjdelcerro
            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 40435 jjdelcerro
130 41248 jjdelcerro
                if (option != JOptionPane.OK_OPTION) {
131
                    return;
132
                }
133 40435 jjdelcerro
134 41248 jjdelcerro
                ICoordTrans ct = proj.getCT(viewPort.getProjection());
135
                lyrVect.setCoordTrans(ct);
136
                System.err.println("coordTrans = " + proj.getAbrev() + " "
137
                        + viewPort.getProjection().getAbrev());
138
            }
139
        }
140 40435 jjdelcerro
141 41248 jjdelcerro
    }
142 40435 jjdelcerro
143 42573 jjdelcerro
    @Override
144 41248 jjdelcerro
    public void execute(String actionCommand) {
145 41634 jjdelcerro
        this.execute(actionCommand, null);
146
    }
147
148 42573 jjdelcerro
    @Override
149 41634 jjdelcerro
    public void execute(String command, Object[] args) {
150 43100 jjdelcerro
        List<File> files = ArrayUtils.getListOf(args,0,File.class);
151 43152 fdiaz
152 41248 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
153 40435 jjdelcerro
154 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
155
        if (view == null) {
156 41248 jjdelcerro
            return;
157
        }
158 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
159 42573 jjdelcerro
160 41264 jjdelcerro
        MapControl mapControl = view.getMapControl();
161 41634 jjdelcerro
        this.doAddLayers(mapControl, mapControl.getMapContext(), files);
162 41248 jjdelcerro
        mapControl.getMapContext().callLegendChanged();
163
        document.setModified(true);
164
    }
165
166 42573 jjdelcerro
    @Override
167 41248 jjdelcerro
    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 43636 jjdelcerro
    private AddLayerDialog createAddLayerDialog(MapControl mapControl, MapContext mapContext, List<File> files) {
177 42896 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
178
        I18nManager i18nManager = ToolsLocator.getI18nManager();
179 41258 jjdelcerro
        AddLayerDialog fopen = new AddLayerDialog();
180 42573 jjdelcerro
        for (Class<? extends WizardPanel> wpClass : wizardStack) {
181 41248 jjdelcerro
            WizardPanel wp;
182
            try {
183 41434 cmartinez
                Object[] params = {};
184
                wp = wpClass.getConstructor()
185
                        .newInstance(params);
186 42896 jjdelcerro
                application.message(
187 43152 fdiaz
                        i18nManager.getTranslation("Adding tab...") + wp.getTabName(),
188 42896 jjdelcerro
                        JOptionPane.INFORMATION_MESSAGE
189
                );
190 41434 cmartinez
                wp.setMapCtrl(mapControl);
191
                wp.setMapContext(mapContext);
192
                wp.initWizard();
193 42573 jjdelcerro
                if (wp instanceof FilesystemExplorerWizardPanel && files != null && !files.isEmpty()) {
194
                    FilesystemExplorerWizardPanel fswp = (FilesystemExplorerWizardPanel) wp;
195 41634 jjdelcerro
                    fswp.addFiles(files);
196
                }
197 41248 jjdelcerro
                fopen.addWizardTab(wp.getTabName(), wp);
198 41258 jjdelcerro
            } catch (Exception e) {
199 42573 jjdelcerro
                logger.warn("Can't create layer open dialog.", e);
200 41248 jjdelcerro
            }
201 41258 jjdelcerro
        }
202 42896 jjdelcerro
        application.message(null,JOptionPane.INFORMATION_MESSAGE);
203 41662 jjdelcerro
        fopen.updateOkButtonState();
204 41248 jjdelcerro
        return fopen;
205
    }
206 40435 jjdelcerro
207 41248 jjdelcerro
    /**
208 41434 cmartinez
     * Opens the AddLayer dialog, and adds the selected layers to the provided
209
     * MapContext.
210 41248 jjdelcerro
     *
211 41434 cmartinez
     * This method is useful when we want to add a layer but we don't have an
212 42573 jjdelcerro
     * associated View. If a View or a MapControl is available, you will usually
213
     * prefer the {@link #addLayers(MapControl)} method.
214
     *
215 41434 cmartinez
     * @param mapContext The MapContext to add the layers to
216
     * @return <code>true</code> if any layer has been added.
217 41248 jjdelcerro
     */
218 41434 cmartinez
    public boolean addLayers(MapContext mapContext) {
219 42573 jjdelcerro
        return doAddLayers(null, mapContext, null);
220 41434 cmartinez
    }
221
222
    /**
223
     * Opens the AddLayer dialog, and adds the selected layers to the provided
224
     * mapControl.
225 42573 jjdelcerro
     *
226 41434 cmartinez
     * @param mapControl The MapControl to add the layers to
227 42573 jjdelcerro
     *
228 41434 cmartinez
     * @return <code>true</code> if any layer has been added.
229
     */
230 41248 jjdelcerro
    public boolean addLayers(MapControl mapControl) {
231 42573 jjdelcerro
        return doAddLayers(mapControl, mapControl.getMapContext(), null);
232 41434 cmartinez
    }
233 42573 jjdelcerro
234
    private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files) {
235 43636 jjdelcerro
        AddLayerDialog addLayerDialog = null;
236 41258 jjdelcerro
        try {
237 43636 jjdelcerro
            addLayerDialog = createAddLayerDialog(mapControl, mapContext, files);
238
            PluginServices.getMDIManager().addWindow(addLayerDialog);
239 40435 jjdelcerro
240 43636 jjdelcerro
            if (addLayerDialog.isAccepted()) {
241
                if (addLayerDialog.getSelectedTab() instanceof WizardPanel) {
242
                    WizardPanel panel = (WizardPanel) addLayerDialog.getSelectedTab();
243
                    panel.executeWizard();
244 41434 cmartinez
                    return true;
245 41258 jjdelcerro
                } else {
246
                    JOptionPane.showMessageDialog((Component) PluginServices
247
                            .getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada"));
248
                }
249 41248 jjdelcerro
            }
250 41258 jjdelcerro
            return false;
251
        } finally {
252 43636 jjdelcerro
            DisposeUtils.disposeQuietly(addLayerDialog);
253 41248 jjdelcerro
        }
254
    }
255 40435 jjdelcerro
256 42573 jjdelcerro
    @Override
257 41248 jjdelcerro
    public void initialize() {
258
        IconThemeHelper.registerIcon("action", "view-layer-add", this);
259
    }
260 40435 jjdelcerro
}