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

History | View | Annotate | Download (11.9 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 40435 jjdelcerro
58
/**
59 41248 jjdelcerro
 * Extensi�n que abre un di�logo para seleccionar la capa o capas que se
60
 * quieren anadir a la vista.
61 40435 jjdelcerro
 *
62
 */
63
public class AddLayer extends Extension {
64 42573 jjdelcerro
65 41258 jjdelcerro
    private static final Logger logger = LoggerFactory.getLogger(AddLayer.class);
66 42573 jjdelcerro
67 41248 jjdelcerro
    private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
68 40435 jjdelcerro
69 41248 jjdelcerro
    static {
70 42573 jjdelcerro
        AddLayer.wizardStack = new ArrayList<>();
71 43152 fdiaz
        // Anadimos el panel al wizard de cargar capa.
72
        AddLayer.addWizard(FilesystemExplorerWizardPanel.class);
73 41248 jjdelcerro
    }
74 40435 jjdelcerro
75 41248 jjdelcerro
    public static void addWizard(Class<? extends WizardPanel> wpClass) {
76
        AddLayer.wizardStack.add(wpClass);
77
    }
78 40435 jjdelcerro
79 41248 jjdelcerro
    private static WizardPanel getInstance(int i, MapControl mapControl)
80
            throws IllegalArgumentException, SecurityException,
81
            InstantiationException, IllegalAccessException,
82
            InvocationTargetException, NoSuchMethodException {
83
        Class<? extends WizardPanel> wpClass = AddLayer.wizardStack.get(i);
84
        Object[] params = {};
85
        WizardPanel wp = wpClass.getConstructor()
86
                .newInstance(params);
87
        wp.setMapCtrl(mapControl);
88
        wp.initWizard();
89 40435 jjdelcerro
90 41248 jjdelcerro
        return wp;
91
    }
92 40435 jjdelcerro
93 42573 jjdelcerro
    @Override
94 41248 jjdelcerro
    public boolean isVisible() {
95
        ApplicationManager application = ApplicationLocator.getManager();
96 40485 jldominguez
97 41264 jjdelcerro
        return application.getActiveComponent(ViewDocument.class) != null;
98 41248 jjdelcerro
    }
99 40435 jjdelcerro
100 42573 jjdelcerro
    @Override
101 41248 jjdelcerro
    public void postInitialize() {
102
        super.postInitialize();
103
    }
104 40435 jjdelcerro
105 41248 jjdelcerro
    public static void checkProjection(FLayer lyr, ViewPort viewPort) {
106
        if (lyr instanceof FLayers) {
107
            FLayers layers = (FLayers) lyr;
108
            for (int i = 0; i < layers.getLayersCount(); i++) {
109
                checkProjection(layers.getLayer(i), viewPort);
110
            }
111
        }
112
        if (lyr instanceof FLyrVect) {
113
            FLyrVect lyrVect = (FLyrVect) lyr;
114
            IProjection proj = lyr.getProjection();
115
            // Comprobar que la projecci�n es la misma que la vista
116
            if (proj == null) {
117 42573 jjdelcerro
                // SUPONEMOS que la capa est� en la proyecci�n que
118 41248 jjdelcerro
                // estamos pidiendo (que ya es mucho suponer, ya).
119
                lyrVect.setProjection(viewPort.getProjection());
120
                return;
121
            }
122 42573 jjdelcerro
            int option;
123 41248 jjdelcerro
            if (proj != viewPort.getProjection()) {
124
                option = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(), PluginServices
125
                        .getText(AddLayer.class, "reproyectar_aviso") + "\n" + PluginServices.getText(AddLayer.class, "Capa") + ": " + lyrVect.getName(), PluginServices
126
                        .getText(AddLayer.class, "reproyectar_pregunta"),
127
                        JOptionPane.YES_NO_OPTION);
128 40435 jjdelcerro
129 41248 jjdelcerro
                if (option != JOptionPane.OK_OPTION) {
130
                    return;
131
                }
132 40435 jjdelcerro
133 41248 jjdelcerro
                ICoordTrans ct = proj.getCT(viewPort.getProjection());
134
                lyrVect.setCoordTrans(ct);
135
                System.err.println("coordTrans = " + proj.getAbrev() + " "
136
                        + viewPort.getProjection().getAbrev());
137
            }
138
        }
139 40435 jjdelcerro
140 41248 jjdelcerro
    }
141 40435 jjdelcerro
142 42573 jjdelcerro
    @Override
143 41248 jjdelcerro
    public void execute(String actionCommand) {
144 41634 jjdelcerro
        this.execute(actionCommand, null);
145
    }
146
147 43100 jjdelcerro
    public static class ArrayUtils {
148 43152 fdiaz
149 43100 jjdelcerro
        // FIXME: Use the class in org.gvsig.tools when this is available and remove this.
150
151
        /**
152
         * Return the element of the array. If the array is null or don't have
153
         * the element indicated return null.
154
         *
155
         * @param array
156
         * @param n
157
         * @return
158
         */
159
        public static Object get(Object[] array, int n) {
160
            if (array == null) {
161
                return null;
162
            }
163
            if (array.length <= n || n < 0) {
164
                return null;
165
            }
166
            return array[n];
167
        }
168
169
        /**
170
         * Return the element of the array if exists and if of the type
171
         * indicated. If the array is null, don't have the element indicated of
172
         * is not of the type required return null.
173
         *
174
         * @param array
175
         * @param n
176
         * @param elementClass
177
         * @return
178
         */
179
        public static Object get(Object[] array, int n, Class elementClass) {
180
            Object o = get(array, n);
181
            if (elementClass.isInstance(o)) {
182
                return o;
183
            }
184
            return null;
185
        }
186
187
        /**
188
         * Return the element of the array if this is a list of the indiqued
189
         * type. If the array don't have elements, is null, not is a list or the
190
         * elements of the list is not the indicated return null.
191
         *
192
         * @param array
193
         * @param n
194
         * @param elementClass
195
         * @return
196
         */
197
        public static List getListOf(Object[] array, int n, Class elementClass) {
198
            List list = (List) get(array, n, List.class);
199
            if (list == null) {
200
                return null;
201
            }
202
            if (list.isEmpty()) {
203
                return null;
204
            }
205
            if (elementClass.isInstance(list.get(0))) {
206
                return list;
207
            }
208
            return null;
209
        }
210
211
    }
212
213 42573 jjdelcerro
    @Override
214 41634 jjdelcerro
    public void execute(String command, Object[] args) {
215 43100 jjdelcerro
        List<File> files = ArrayUtils.getListOf(args,0,File.class);
216 43152 fdiaz
217 41248 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
218 40435 jjdelcerro
219 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
220
        if (view == null) {
221 41248 jjdelcerro
            return;
222
        }
223 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
224 42573 jjdelcerro
225 41264 jjdelcerro
        MapControl mapControl = view.getMapControl();
226 41634 jjdelcerro
        this.doAddLayers(mapControl, mapControl.getMapContext(), files);
227 41248 jjdelcerro
        mapControl.getMapContext().callLegendChanged();
228
        document.setModified(true);
229
    }
230
231 42573 jjdelcerro
    @Override
232 41248 jjdelcerro
    public boolean isEnabled() {
233
        return true;
234
    }
235
236
    /**
237
     * Creates FOpenDialog, and adds file tab, and additional registered tabs
238
     *
239
     * @return FOpenDialog
240
     */
241 41634 jjdelcerro
    private AddLayerDialog createFOpenDialog(MapControl mapControl, MapContext mapContext, List<File> files) {
242 42896 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
243
        I18nManager i18nManager = ToolsLocator.getI18nManager();
244 41258 jjdelcerro
        AddLayerDialog fopen = new AddLayerDialog();
245 42573 jjdelcerro
        for (Class<? extends WizardPanel> wpClass : wizardStack) {
246 41248 jjdelcerro
            WizardPanel wp;
247
            try {
248 41434 cmartinez
                Object[] params = {};
249
                wp = wpClass.getConstructor()
250
                        .newInstance(params);
251 42896 jjdelcerro
                application.message(
252 43152 fdiaz
                        i18nManager.getTranslation("Adding tab...") + wp.getTabName(),
253 42896 jjdelcerro
                        JOptionPane.INFORMATION_MESSAGE
254
                );
255 41434 cmartinez
                wp.setMapCtrl(mapControl);
256
                wp.setMapContext(mapContext);
257
                wp.initWizard();
258 42573 jjdelcerro
                if (wp instanceof FilesystemExplorerWizardPanel && files != null && !files.isEmpty()) {
259
                    FilesystemExplorerWizardPanel fswp = (FilesystemExplorerWizardPanel) wp;
260 41634 jjdelcerro
                    fswp.addFiles(files);
261
                }
262 41248 jjdelcerro
                fopen.addWizardTab(wp.getTabName(), wp);
263 41258 jjdelcerro
            } catch (Exception e) {
264 42573 jjdelcerro
                logger.warn("Can't create layer open dialog.", e);
265 41248 jjdelcerro
            }
266 41258 jjdelcerro
        }
267 42896 jjdelcerro
        application.message(null,JOptionPane.INFORMATION_MESSAGE);
268 41662 jjdelcerro
        fopen.updateOkButtonState();
269 41248 jjdelcerro
        return fopen;
270
    }
271 40435 jjdelcerro
272 41248 jjdelcerro
    /**
273
     * Adds to mapcontrol all layers selected by user in the specified
274
     * WizardPanel.
275
     *
276
     * @param mapControl MapControl on which we want to load user selected
277
     * layers.
278
     * @param wizardPanel WizardPanel where user selected the layers to load
279
     * @return
280
     */
281 42573 jjdelcerro
//    private boolean loadGenericWizardPanelLayers(MapContext mapContext, WizardPanel wp, boolean mapControlAvailable) {
282
//        wp.setMapContext(mapContext);
283
//        wp.executeWizard();
284
//        return true;
285
//    }
286 41248 jjdelcerro
    /**
287 41434 cmartinez
     * Opens the AddLayer dialog, and adds the selected layers to the provided
288
     * MapContext.
289 41248 jjdelcerro
     *
290 41434 cmartinez
     * This method is useful when we want to add a layer but we don't have an
291 42573 jjdelcerro
     * associated View. If a View or a MapControl is available, you will usually
292
     * prefer the {@link #addLayers(MapControl)} method.
293
     *
294 41434 cmartinez
     * @param mapContext The MapContext to add the layers to
295
     * @return <code>true</code> if any layer has been added.
296 41248 jjdelcerro
     */
297 41434 cmartinez
    public boolean addLayers(MapContext mapContext) {
298 42573 jjdelcerro
        return doAddLayers(null, mapContext, null);
299 41434 cmartinez
    }
300
301
    /**
302
     * Opens the AddLayer dialog, and adds the selected layers to the provided
303
     * mapControl.
304 42573 jjdelcerro
     *
305 41434 cmartinez
     * @param mapControl The MapControl to add the layers to
306 42573 jjdelcerro
     *
307 41434 cmartinez
     * @return <code>true</code> if any layer has been added.
308
     */
309 41248 jjdelcerro
    public boolean addLayers(MapControl mapControl) {
310 42573 jjdelcerro
        return doAddLayers(mapControl, mapControl.getMapContext(), null);
311 41434 cmartinez
    }
312 42573 jjdelcerro
313
    private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files) {
314 41258 jjdelcerro
        AddLayerDialog fopen = null;
315
        try {
316 41634 jjdelcerro
            fopen = createFOpenDialog(mapControl, mapContext, files);
317 41258 jjdelcerro
            PluginServices.getMDIManager().addWindow(fopen);
318 40435 jjdelcerro
319 41258 jjdelcerro
            if (fopen.isAccepted()) {
320
                if (fopen.getSelectedTab() instanceof WizardPanel) {
321
                    WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
322 41434 cmartinez
                    wp.execute();
323
                    return true;
324 41258 jjdelcerro
                } else {
325
                    JOptionPane.showMessageDialog((Component) PluginServices
326
                            .getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada"));
327
                }
328 41248 jjdelcerro
            }
329 41258 jjdelcerro
            return false;
330
        } finally {
331 42573 jjdelcerro
            DisposeUtils.disposeQuietly(fopen);
332 41248 jjdelcerro
        }
333
    }
334 40435 jjdelcerro
335 42573 jjdelcerro
    @Override
336 41248 jjdelcerro
    public void initialize() {
337
        IconThemeHelper.registerIcon("action", "view-layer-add", this);
338
    }
339 40435 jjdelcerro
}