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

History | View | Annotate | Download (7.95 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.lang.reflect.InvocationTargetException;
27
import java.util.ArrayList;
28

    
29
import javax.swing.JOptionPane;
30

    
31
import org.cresques.cts.ICoordTrans;
32
import org.cresques.cts.IProjection;
33
import org.gvsig.andami.IconThemeHelper;
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.plugins.Extension;
36
import org.gvsig.app.ApplicationLocator;
37
import org.gvsig.app.ApplicationManager;
38
import org.gvsig.app.addlayer.AddLayerDialog;
39
import org.gvsig.app.gui.WizardPanel;
40
import org.gvsig.app.project.documents.view.ViewDocument;
41
import org.gvsig.app.project.documents.view.gui.IView;
42
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerAddLayerWizardPanel;
43
import org.gvsig.fmap.mapcontext.ViewPort;
44
import org.gvsig.fmap.mapcontext.layers.FLayer;
45
import org.gvsig.fmap.mapcontext.layers.FLayers;
46
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
47
import org.gvsig.fmap.mapcontrol.MapControl;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
/**
52
 * Extensi�n que abre un di�logo para seleccionar la capa o capas que se
53
 * quieren anadir a la vista.
54
 *
55
 */
56
public class AddLayer extends Extension {
57
    private static final Logger logger = LoggerFactory.getLogger(AddLayer.class);
58
    
59
    private static ArrayList<Class<? extends WizardPanel>> wizardStack = null;
60

    
61
    static {
62
        AddLayer.wizardStack = new ArrayList<Class<? extends WizardPanel>>();
63
                // Anadimos el panel al wizard de cargar capa. (Esto es temporal hasta que
64
        // el actual sea totalmente extensible)
65
        AddLayer.addWizard(FilesystemExplorerAddLayerWizardPanel.class);
66
    }
67

    
68
    public static void addWizard(Class<? extends WizardPanel> wpClass) {
69
        AddLayer.wizardStack.add(wpClass);
70
    }
71

    
72
    private static WizardPanel getInstance(int i, MapControl mapControl)
73
            throws IllegalArgumentException, SecurityException,
74
            InstantiationException, IllegalAccessException,
75
            InvocationTargetException, NoSuchMethodException {
76
        Class<? extends WizardPanel> wpClass = AddLayer.wizardStack.get(i);
77
        Object[] params = {};
78
        WizardPanel wp = wpClass.getConstructor()
79
                .newInstance(params);
80
        wp.setMapCtrl(mapControl);
81
        wp.initWizard();
82

    
83
        return wp;
84
    }
85

    
86
    public boolean isVisible() {
87
        ApplicationManager application = ApplicationLocator.getManager();
88

    
89
        return application.getActiveDocument(ViewDocument.class) != null;
90
    }
91

    
92
    public void postInitialize() {
93
        super.postInitialize();
94
    }
95

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

    
120
                if (option != JOptionPane.OK_OPTION) {
121
                    return;
122
                }
123

    
124
                ICoordTrans ct = proj.getCT(viewPort.getProjection());
125
                lyrVect.setCoordTrans(ct);
126
                System.err.println("coordTrans = " + proj.getAbrev() + " "
127
                        + viewPort.getProjection().getAbrev());
128
            }
129
        }
130

    
131
    }
132

    
133
    public void execute(String actionCommand) {
134
        ApplicationManager application = ApplicationLocator.getManager();
135

    
136
        ViewDocument document = (ViewDocument) application.getActiveDocument(ViewDocument.class);
137
        if (document == null) {
138
            return;
139
        }
140
        IView theView = (IView) document.getMainComponent();
141

    
142
        MapControl mapControl = theView.getMapControl();
143
        this.addLayers(mapControl);
144
        mapControl.getMapContext().callLegendChanged();
145
        document.setModified(true);
146
    }
147

    
148
    public boolean isEnabled() {
149
        return true;
150
    }
151

    
152
    /**
153
     * Creates FOpenDialog, and adds file tab, and additional registered tabs
154
     *
155
     * @return FOpenDialog
156
     */
157
    private AddLayerDialog createFOpenDialog(MapControl mapControl) {
158
        AddLayerDialog fopen = new AddLayerDialog();
159
        for (int i = 0; i < wizardStack.size(); i++) {
160
            WizardPanel wp;
161
            try {
162
                wp = AddLayer.getInstance(i, mapControl);
163

    
164
                fopen.addWizardTab(wp.getTabName(), wp);
165
            } catch (Exception e) {
166
                logger.warn("Can't create layer open dialog.",e);
167
            }
168
        }
169
        return fopen;
170
    }
171

    
172
    /**
173
     * Adds to mapcontrol all layers selected by user in the specified
174
     * WizardPanel.
175
     *
176
     * @param mapControl MapControl on which we want to load user selected
177
     * layers.
178
     * @param wizardPanel WizardPanel where user selected the layers to load
179
     * @return
180
     */
181
    private boolean loadGenericWizardPanelLayers(MapControl mapControl, WizardPanel wp) {
182
        wp.setMapCtrl(mapControl);
183
        wp.execute();
184
        return true;
185
    }
186

    
187
    /**
188
     * Abre dialogo para a�adir capas y las a�ade en mapControl
189
     *
190
     * Devuelve true si se han a�adido capas.
191
     * @param mapControl
192
     * @return 
193
     */
194
    public boolean addLayers(MapControl mapControl) {
195
        // create and show the modal fopen dialog
196
        AddLayerDialog fopen = null;
197
        try {
198
            fopen = createFOpenDialog(mapControl);
199
            PluginServices.getMDIManager().addWindow(fopen);
200

    
201
            if (fopen.isAccepted()) {
202
                if (fopen.getSelectedTab() instanceof WizardPanel) {
203
                    WizardPanel wp = (WizardPanel) fopen.getSelectedTab();
204
                    return loadGenericWizardPanelLayers(mapControl, wp);
205
                } else {
206
                    JOptionPane.showMessageDialog((Component) PluginServices
207
                            .getMainFrame(), PluginServices.getText(this, "ninguna_capa_seleccionada"));
208
                }
209
            }
210
            return false;
211
        } finally {
212
            if (fopen != null) {
213
                fopen.dispose();
214
                fopen = null;
215
            }
216
        }
217
    }
218

    
219
    public void initialize() {
220
        IconThemeHelper.registerIcon("action", "view-layer-add", this);
221
    }
222
}