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

History | View | Annotate | Download (10.3 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.awt.Dimension;
27
import java.io.File;
28
import java.lang.reflect.InvocationTargetException;
29
import java.util.ArrayList;
30
import java.util.List;
31

    
32
import javax.swing.JOptionPane;
33

    
34
import org.cresques.cts.ICoordTrans;
35
import org.cresques.cts.IProjection;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

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

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

    
68
    private static final Logger logger = LoggerFactory.getLogger(AddLayer.class);
69

    
70
    private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
71

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

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

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

    
93
        return wp;
94
    }
95

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

    
100
        return application.getActiveComponent(ViewDocument.class) != null;
101
    }
102

    
103
    @Override
104
    public void postInitialize() {
105
        super.postInitialize();
106
    }
107

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

    
132
                if (option != JOptionPane.OK_OPTION) {
133
                    return;
134
                }
135

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

    
143
    }
144

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

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

    
154
        ApplicationManager application = ApplicationLocator.getManager();
155

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

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

    
168
    @Override
169
    public boolean isEnabled() {
170
        return true;
171
    }
172

    
173
    /**
174
     * Creates FOpenDialog, and adds file tab, and additional registered tabs
175
     *
176
     * @return FOpenDialog
177
     */
178
    public static AddLayerDialog createAddLayerDialog(MapControl mapControl, MapContext mapContext, List<File> files) {
179
        ApplicationManager application = ApplicationLocator.getManager();
180
        I18nManager i18nManager = ToolsLocator.getI18nManager();
181
        AddLayerDialog fopen = new AddLayerDialog();
182
        Dimension preferredSize = fopen.getPreferredSize();
183
        for (Class<? extends WizardPanel> wpClass : wizardStack) {
184
            WizardPanel wp;
185
            try {
186
                Object[] params = {};
187
                wp = wpClass.getConstructor()
188
                        .newInstance(params);
189
                application.message(
190
                        i18nManager.getTranslation("Adding tab...") + wp.getTabName(),
191
                        JOptionPane.INFORMATION_MESSAGE
192
                );
193
                wp.setMapCtrl(mapControl);
194
                wp.setMapContext(mapContext);
195
                wp.initWizard();
196
                if (wp instanceof FilesystemExplorerWizardPanel && files != null && !files.isEmpty()) {
197
                    FilesystemExplorerWizardPanel fswp = (FilesystemExplorerWizardPanel) wp;
198
                    fswp.addFiles(files);
199
                }
200
                Dimension dim = wp.getPreferredSize();
201
                if( dim.height>preferredSize.height ) {
202
                    preferredSize.height = dim.height;
203
                }
204
                if( dim.width>preferredSize.width ) {
205
                    preferredSize.width = dim.width;
206
                }
207
                fopen.addWizardTab(wp.getTabName(), wp);
208
            } catch (Exception e) {
209
                logger.warn("Can't create layer open dialog.", e);
210
            }
211
        }
212
        preferredSize.height += ToolsSwingUtils.rows2px(4);
213
        preferredSize.width += ToolsSwingUtils.cols2px(2);
214
        
215
        ToolsSwingUtils.ensureRowsCols(fopen, -1, -1, 30, 100);
216
        
217
        fopen.setPreferredSize(preferredSize);
218
        
219
        application.message(null,JOptionPane.INFORMATION_MESSAGE);
220
        fopen.updateOkButtonState();
221
        return fopen;
222
    }
223

    
224
    /**
225
     * Opens the AddLayer dialog, and adds the selected layers to the provided
226
     * MapContext.
227
     *
228
     * This method is useful when we want to add a layer but we don't have an
229
     * associated View. If a View or a MapControl is available, you will usually
230
     * prefer the {@link #addLayers(MapControl)} method.
231
     *
232
     * @param mapContext The MapContext to add the layers to
233
     * @return <code>true</code> if any layer has been added.
234
     */
235
    public boolean addLayers(MapContext mapContext) {
236
        return doAddLayers(null, mapContext, null);
237
    }
238

    
239
    /**
240
     * Opens the AddLayer dialog, and adds the selected layers to the provided
241
     * mapControl.
242
     *
243
     * @param mapControl The MapControl to add the layers to
244
     *
245
     * @return <code>true</code> if any layer has been added.
246
     */
247
    public boolean addLayers(MapControl mapControl) {
248
        return doAddLayers(mapControl, mapControl.getMapContext(), null);
249
    }
250

    
251
    private boolean doAddLayers(MapControl mapControl, MapContext mapContext, List<File> files) {
252
        AddLayerDialog addLayerDialog = null;
253
        try {
254
            addLayerDialog = createAddLayerDialog(mapControl, mapContext, files);
255
            PluginServices.getMDIManager().addWindow(addLayerDialog);
256

    
257
            if (addLayerDialog.isAccepted()) {
258
                if (addLayerDialog.getSelectedTab() instanceof WizardPanel) {
259
                    WizardPanel panel = (WizardPanel) addLayerDialog.getSelectedTab();
260
                    panel.executeWizard();
261
                    return true;
262
                } else {
263
                    JOptionPane.showMessageDialog((Component) PluginServices
264
                            .getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada"));
265
                }
266
            }
267
            return false;
268
        } finally {
269
            DisposeUtils.disposeQuietly(addLayerDialog);
270
        }
271
    }
272

    
273
    @Override
274
    public void initialize() {
275
        IconThemeHelper.registerIcon("action", "view-layer-add", this);
276
    }
277
}