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

History | View | Annotate | Download (12 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 45777 jjdelcerro
import java.awt.Dimension;
27 41634 jjdelcerro
import java.io.File;
28 40435 jjdelcerro
import java.lang.reflect.InvocationTargetException;
29
import java.util.ArrayList;
30 41634 jjdelcerro
import java.util.List;
31 40435 jjdelcerro
import javax.swing.JOptionPane;
32
import org.cresques.cts.ICoordTrans;
33
import org.cresques.cts.IProjection;
34
import org.gvsig.andami.IconThemeHelper;
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.plugins.Extension;
37 40485 jldominguez
import org.gvsig.app.ApplicationLocator;
38 41248 jjdelcerro
import org.gvsig.app.ApplicationManager;
39 40435 jjdelcerro
import org.gvsig.app.addlayer.AddLayerDialog;
40
import org.gvsig.app.gui.WizardPanel;
41 41248 jjdelcerro
import org.gvsig.app.project.documents.view.ViewDocument;
42 40435 jjdelcerro
import org.gvsig.app.project.documents.view.gui.IView;
43 41634 jjdelcerro
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
44 41434 cmartinez
import org.gvsig.fmap.mapcontext.MapContext;
45 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.ViewPort;
46
import org.gvsig.fmap.mapcontext.layers.FLayer;
47
import org.gvsig.fmap.mapcontext.layers.FLayers;
48
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
49
import org.gvsig.fmap.mapcontrol.MapControl;
50 42896 jjdelcerro
import org.gvsig.tools.ToolsLocator;
51 46604 fdiaz
import org.gvsig.tools.arguments.Arguments;
52 42573 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
53 42896 jjdelcerro
import org.gvsig.tools.i18n.I18nManager;
54 45777 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingUtils;
55 46604 fdiaz
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
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 42573 jjdelcerro
    @Override
148 41634 jjdelcerro
    public void execute(String command, Object[] args) {
149 46604 fdiaz
        Arguments arguments = Arguments.create(args);
150
151
152
        List<File> files = arguments.getListOf(0,File.class);
153 43152 fdiaz
154 41248 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
155 40435 jjdelcerro
156 41264 jjdelcerro
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
157
        if (view == null) {
158 41248 jjdelcerro
            return;
159
        }
160 41264 jjdelcerro
        ViewDocument document = view.getViewDocument();
161 42573 jjdelcerro
162 41264 jjdelcerro
        MapControl mapControl = view.getMapControl();
163 46604 fdiaz
        this.doAddLayers(mapControl, mapControl.getMapContext(), files, arguments);
164 41248 jjdelcerro
        mapControl.getMapContext().callLegendChanged();
165
        document.setModified(true);
166
    }
167
168 42573 jjdelcerro
    @Override
169 41248 jjdelcerro
    public boolean isEnabled() {
170
        return true;
171
    }
172
173 46601 fdiaz
    public static AddLayerDialog createAddLayerDialog(MapControl mapControl, MapContext mapContext, List<File> files) {
174
        return createAddLayerDialog(mapControl, mapContext, files, null);
175
    }
176
177 41248 jjdelcerro
    /**
178
     * Creates FOpenDialog, and adds file tab, and additional registered tabs
179
     *
180 46604 fdiaz
     * @param mapControl
181
     * @param mapContext
182
     * @param files
183
     * @param args
184 41248 jjdelcerro
     * @return FOpenDialog
185
     */
186 46604 fdiaz
    public static AddLayerDialog createAddLayerDialog(MapControl mapControl, MapContext mapContext, List<File> files, Arguments args) {
187 42896 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
188
        I18nManager i18nManager = ToolsLocator.getI18nManager();
189 41258 jjdelcerro
        AddLayerDialog fopen = new AddLayerDialog();
190 45777 jjdelcerro
        Dimension preferredSize = fopen.getPreferredSize();
191 46601 fdiaz
        String activeTab = null;
192 42573 jjdelcerro
        for (Class<? extends WizardPanel> wpClass : wizardStack) {
193 41248 jjdelcerro
            WizardPanel wp;
194
            try {
195 41434 cmartinez
                Object[] params = {};
196
                wp = wpClass.getConstructor()
197
                        .newInstance(params);
198 42896 jjdelcerro
                application.message(
199 43152 fdiaz
                        i18nManager.getTranslation("Adding tab...") + wp.getTabName(),
200 42896 jjdelcerro
                        JOptionPane.INFORMATION_MESSAGE
201
                );
202 41434 cmartinez
                wp.setMapCtrl(mapControl);
203
                wp.setMapContext(mapContext);
204
                wp.initWizard();
205 42573 jjdelcerro
                if (wp instanceof FilesystemExplorerWizardPanel && files != null && !files.isEmpty()) {
206
                    FilesystemExplorerWizardPanel fswp = (FilesystemExplorerWizardPanel) wp;
207 41634 jjdelcerro
                    fswp.addFiles(files);
208 46601 fdiaz
                    activeTab = wp.getTabName();
209 41634 jjdelcerro
                }
210 46604 fdiaz
                if(wp.addArguments(args)) {
211
                    activeTab = wp.getTabName();
212 46601 fdiaz
                }
213 45777 jjdelcerro
                Dimension dim = wp.getPreferredSize();
214
                if( dim.height>preferredSize.height ) {
215
                    preferredSize.height = dim.height;
216
                }
217
                if( dim.width>preferredSize.width ) {
218
                    preferredSize.width = dim.width;
219
                }
220 41248 jjdelcerro
                fopen.addWizardTab(wp.getTabName(), wp);
221 41258 jjdelcerro
            } catch (Exception e) {
222 42573 jjdelcerro
                logger.warn("Can't create layer open dialog.", e);
223 41248 jjdelcerro
            }
224 41258 jjdelcerro
        }
225 46604 fdiaz
226
        if(args != null && args.contains("activateTab")){
227
            activeTab = (String)args.get("activateTab");
228
        }
229
230 46601 fdiaz
        fopen.setSelectedTab(activeTab);
231 45777 jjdelcerro
        preferredSize.height += ToolsSwingUtils.rows2px(4);
232
        preferredSize.width += ToolsSwingUtils.cols2px(2);
233
234 46708 fdiaz
        fopen.setPreferredSize(preferredSize);
235
236 45778 fdiaz
        ToolsSwingUtils.ensureRowsCols(fopen, -1, -1, 30, 100);
237
238 42896 jjdelcerro
        application.message(null,JOptionPane.INFORMATION_MESSAGE);
239 41662 jjdelcerro
        fopen.updateOkButtonState();
240 41248 jjdelcerro
        return fopen;
241
    }
242 40435 jjdelcerro
243 41248 jjdelcerro
    /**
244 41434 cmartinez
     * Opens the AddLayer dialog, and adds the selected layers to the provided
245
     * MapContext.
246 41248 jjdelcerro
     *
247 41434 cmartinez
     * This method is useful when we want to add a layer but we don't have an
248 42573 jjdelcerro
     * associated View. If a View or a MapControl is available, you will usually
249
     * prefer the {@link #addLayers(MapControl)} method.
250
     *
251 41434 cmartinez
     * @param mapContext The MapContext to add the layers to
252
     * @return <code>true</code> if any layer has been added.
253 41248 jjdelcerro
     */
254 41434 cmartinez
    public boolean addLayers(MapContext mapContext) {
255 42573 jjdelcerro
        return doAddLayers(null, mapContext, null);
256 41434 cmartinez
    }
257
258
    /**
259
     * Opens the AddLayer dialog, and adds the selected layers to the provided
260
     * mapControl.
261 42573 jjdelcerro
     *
262 41434 cmartinez
     * @param mapControl The MapControl to add the layers to
263 42573 jjdelcerro
     *
264 41434 cmartinez
     * @return <code>true</code> if any layer has been added.
265
     */
266 41248 jjdelcerro
    public boolean addLayers(MapControl mapControl) {
267 47691 fdiaz
        return doAddLayers(mapControl, mapControl.getMapContext(), null, false, null);
268 41434 cmartinez
    }
269 42573 jjdelcerro
270 47691 fdiaz
    public boolean addLayers(MapControl mapControl, boolean wait) {
271
        return doAddLayers(mapControl, mapControl.getMapContext(), null, wait, null);
272
    }
273
274 42573 jjdelcerro
    private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files) {
275 46601 fdiaz
        return doAddLayers(mapControl, mapContext, files, null);
276
    }
277
278 46604 fdiaz
    private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files, Arguments args) {
279 47691 fdiaz
        return doAddLayers(mapControl, mapContext, files, false, args);
280
    }
281
282
    private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files, boolean wait, Arguments args) {
283 43636 jjdelcerro
        AddLayerDialog addLayerDialog = null;
284 41258 jjdelcerro
        try {
285 46604 fdiaz
            addLayerDialog = createAddLayerDialog(mapControl, mapContext, files, args);
286 47148 jjdelcerro
            Dimension screensize = ToolsSwingUtils.getCurrentScreenSize();
287
            ToolsSwingUtils.ensureHeightWitdh(
288
                    addLayerDialog,
289
                    3*(screensize.height/4),
290
                    2*(screensize.width/4),
291
                    7*(screensize.height/8),
292
                    3*(screensize.width/4)
293
            );
294 43636 jjdelcerro
            PluginServices.getMDIManager().addWindow(addLayerDialog);
295 40435 jjdelcerro
296 43636 jjdelcerro
            if (addLayerDialog.isAccepted()) {
297
                if (addLayerDialog.getSelectedTab() instanceof WizardPanel) {
298
                    WizardPanel panel = (WizardPanel) addLayerDialog.getSelectedTab();
299
                    panel.executeWizard();
300 47691 fdiaz
                    if(wait) {
301
                        panel.waitTasks(0);
302
                    }
303 41434 cmartinez
                    return true;
304 41258 jjdelcerro
                } else {
305
                    JOptionPane.showMessageDialog((Component) PluginServices
306
                            .getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada"));
307
                }
308 41248 jjdelcerro
            }
309 41258 jjdelcerro
            return false;
310
        } finally {
311 43636 jjdelcerro
            DisposeUtils.disposeQuietly(addLayerDialog);
312 41248 jjdelcerro
        }
313
    }
314 40435 jjdelcerro
315 42573 jjdelcerro
    @Override
316 41248 jjdelcerro
    public void initialize() {
317
        IconThemeHelper.registerIcon("action", "view-layer-add", this);
318
    }
319 40435 jjdelcerro
}