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 / ViewControls.java @ 43521

History | View | Annotate | Download (10.5 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.util.ArrayList;
26
import java.util.List;
27
import javax.swing.JOptionPane;
28

    
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
import org.gvsig.andami.IconThemeHelper;
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.andami.plugins.Extension;
35
import org.gvsig.andami.preferences.IPreference;
36
import org.gvsig.andami.preferences.IPreferenceExtension;
37
import org.gvsig.app.ApplicationLocator;
38
import org.gvsig.app.ApplicationManager;
39
import org.gvsig.app.gui.preferencespage.ViewPage;
40
import org.gvsig.app.gui.preferencespage.dal.DALPage;
41
import org.gvsig.app.gui.preferencespage.dialogreminder.DialogReminderPage;
42
import org.gvsig.app.project.Project;
43
import org.gvsig.app.project.ProjectManager;
44
import org.gvsig.app.project.documents.view.Encuadrator;
45
import org.gvsig.app.project.documents.view.ViewDocument;
46
import org.gvsig.app.project.documents.view.ViewManager;
47
import org.gvsig.app.project.documents.view.gui.ExtentListSelectorModel;
48
import org.gvsig.app.project.documents.view.gui.FPanelExtentSelector;
49
import org.gvsig.app.project.documents.view.gui.FPanelLocConfig;
50
import org.gvsig.app.project.documents.view.gui.IView;
51
import org.gvsig.app.project.documents.view.toc.gui.FPopupMenu;
52
import org.gvsig.fmap.geom.primitive.Envelope;
53
import org.gvsig.fmap.mapcontext.MapContext;
54
import org.gvsig.fmap.mapcontext.layers.FLayer;
55
import org.gvsig.fmap.mapcontext.layers.FLayers;
56
import org.gvsig.fmap.mapcontrol.MapControl;
57
import org.gvsig.tools.ToolsLocator;
58
import org.gvsig.tools.dataTypes.DataTypes;
59

    
60
/**
61
 * Extensi?n que controla las operaciones b?sicas realizadas sobre la vista.
62
 *
63
 */
64
public class ViewControls extends Extension implements IPreferenceExtension {
65

    
66
    private static final Logger logger = LoggerFactory
67
            .getLogger(ViewInvertSelection.class);
68

    
69
    private static ViewPage viewPropertiesPage = null;
70

    
71
    public void execute(String s) {
72
        execute(s, null);
73
    }
74

    
75
    public void execute(String command, Object[] args) {
76
        ApplicationManager application = ApplicationLocator.getManager();
77

    
78
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
79
        if (view == null) {
80
            return;
81
        }
82
        ViewDocument document = view.getViewDocument();
83

    
84
        MapContext mapa = document.getMapContext();
85
        MapControl mapCtrl = view.getMapControl();
86

    
87
        if (command.equalsIgnoreCase("view-navigation-zoom-all")) {
88
            Envelope all = mapa.getLayers().getFullEnvelope();
89
            if( all!=null && !all.isEmpty() ) {
90
                mapa.getViewPort().setEnvelope(all);
91
                document.setModified(true);
92
            }
93

    
94
        } else if (command.equalsIgnoreCase("view-navigation-frame-manager")) {
95
            FPanelExtentSelector l = new FPanelExtentSelector();
96

    
97
            ProjectExtension p = (ProjectExtension) PluginServices.getExtension(org.gvsig.app.extension.ProjectExtension.class);
98
            Project project = p.getProject();
99
            ExtentListSelectorModel modelo = new ExtentListSelectorModel(project);
100
            project.addPropertyChangeListener(modelo);
101
            l.setModel(modelo);
102
            l.addSelectionListener(new Encuadrator(project, mapa, view));
103
            document.setModified(true);
104
            PluginServices.getMDIManager().addWindow(l);
105

    
106
        } else if (command.equalsIgnoreCase("view-locator-setup")) {
107
            FPanelLocConfig m_panelLoc = new FPanelLocConfig(view.getMapOverview());
108
            PluginServices.getMDIManager().addCentredWindow(m_panelLoc);
109
            document.setModified(true);
110

    
111
        } else if (command.equalsIgnoreCase("view-navigation-pan")) {
112
            mapCtrl.setTool("pan");
113
            document.setModified(true);
114

    
115
        } else if (command.equalsIgnoreCase("view-navigation-zoom-in-topoint")) {
116
            mapCtrl.setTool("zoomIn");
117
            document.setModified(true);
118

    
119
        } else if (command.equalsIgnoreCase("view-navigation-zoom-out-topoint")) {
120
            mapCtrl.setTool("zoomOut");
121
            document.setModified(true);
122

    
123
        } else if (command.equalsIgnoreCase("layer-set-visible")) {
124
            setVisibles(true, mapa.getLayers());
125
            document.setModified(true);
126
            application.refreshMenusAndToolBars();
127

    
128
        } else if (command.equalsIgnoreCase("layer-set-hide")) {
129
            setVisibles(false, mapa.getLayers());
130
            document.setModified(true);
131
            application.refreshMenusAndToolBars();
132

    
133
        } else if (command.equalsIgnoreCase("layer-set-active")) {
134
            setActives(true, mapa.getLayers());
135
            document.setModified(true);
136
            application.refreshMenusAndToolBars();
137

    
138
        } else if (command.equalsIgnoreCase("layer-set-inactive")) {
139
            setActives(false, mapa.getLayers());
140
            document.setModified(true);
141
            application.refreshMenusAndToolBars();
142

    
143
        } else if (command.equalsIgnoreCase("view-navigation-zoom-in-center")) {
144
            mapCtrl.zoomIn();
145
            document.setModified(true);
146

    
147
        } else if (command.equalsIgnoreCase("view-navigation-zoom-out-center")) {
148
            mapCtrl.zoomOut();
149
            document.setModified(true);
150

    
151
        } else if (command.equalsIgnoreCase("view-change-scale")) {
152
            try {
153
                Long scale = (Long) ToolsLocator.getDataTypesManager().coerce(DataTypes.LONG, args[0]);
154
                mapa.setScaleView(scale.longValue());
155
                document.setModified(true);
156
            } catch (Throwable ex) {
157
                logger.info("Can't change scale of view.", ex);
158
                application.message("Can't change scale of view.", JOptionPane.ERROR_MESSAGE);
159
            }
160
        }
161
    }
162

    
163
    private ViewPage getViewPropertiesPage() {
164
        if (viewPropertiesPage == null) {
165
            viewPropertiesPage = new ViewPage();
166
        }
167
        return viewPropertiesPage;
168
    }
169

    
170
    /**
171
     * Pone todas las capas visibles o no visibles.
172
     *
173
     * @param visible true si que quieren poner a visibles.
174
     * @param mapa FMap sobre el que actuar.
175
     */
176
    private void setVisibles(boolean visible, FLayers layers) {
177
        int layerCount = layers.getLayersCount();
178
        for (int i = 0; i < layerCount; i++) {
179
            FLayer layer = layers.getLayer(i);
180
            layer.setVisible(visible);
181
            if (layer instanceof FLayers) {
182
                setVisibles(visible, (FLayers) layer);
183
            }
184
        }
185
    }
186

    
187
    /**
188
     * Pone todas las capas activas o no activas.
189
     *
190
     * @param active true si que quieren poner a activas.
191
     * @param mapa FMap sobre el que actuar.
192
     */
193
    private void setActives(boolean active, FLayers layers) {
194
        int layerCount = layers.getLayersCount();
195
        for (int i = 0; i < layerCount; i++) {
196
            FLayer layer = layers.getLayer(i);
197
            layer.setActive(active);
198
            if (layer instanceof FLayers) {
199
                setActives(active, (FLayers) layer);
200
            }
201
        }
202
    }
203

    
204
    public boolean isVisible() {
205
        ApplicationManager application = ApplicationLocator.getManager();
206

    
207
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
208
        if (view == null) {
209
            return false;
210
        }
211
        ViewDocument document = view.getViewDocument();
212
        MapContext mapa = document.getMapContext();
213
        return mapa.getLayers().getLayersCount() > 0;
214
    }
215

    
216
    public void initialize() {
217
        registerIcons();
218
    }
219

    
220
    public void postInitialize() {
221
        FPopupMenu.registerExtensionPoint();
222
        ProjectManager projectManager = ApplicationLocator.getProjectManager();
223
        ViewManager viewManager = (ViewManager) projectManager.getDocumentManager(ViewManager.TYPENAME);
224
        viewManager.addTOCContextAction("view-layer-add");
225
    }
226

    
227
    private void registerIcons() {
228
        IconThemeHelper.registerIcon("action", "view-navigation-zoom-in-topoint", this);
229
        IconThemeHelper.registerIcon("action", "view-navigation-zoom-out-topoint", this);
230
        IconThemeHelper.registerIcon("action", "view-navigation-zoom-all", this);
231
        IconThemeHelper.registerIcon("action", "view-navigation-zoom-in-center", this);
232
        IconThemeHelper.registerIcon("action", "view-navigation-zoom-out-center", this);
233
        IconThemeHelper.registerIcon("action", "view-navigation-pan", this);
234
        IconThemeHelper.registerIcon("action", "view-navigation-frame-manager", this);
235

    
236
        IconThemeHelper.registerIcon("action", "edit-undo-redo-actions-modify", this);
237
        IconThemeHelper.registerIcon("action", "edit-undo-redo-actions-add", this);
238
        IconThemeHelper.registerIcon("action", "edit-undo-redo-actions-delete", this);
239

    
240
        IconThemeHelper.registerIcon("preferences", "dialog-remember-preferences", this);
241
        IconThemeHelper.registerIcon("preferences", "dal-preferences", this);
242
    }
243

    
244
    public boolean isEnabled() {
245
        ApplicationManager application = ApplicationLocator.getManager();
246

    
247
        IView view = (IView) application.getActiveComponent(ViewDocument.class);
248
        if (view == null) {
249
            return false;
250
        }
251
        ViewDocument document = view.getViewDocument();
252
        MapContext mapa = document.getMapContext();
253
        FLayers layers = mapa.getLayers();
254
        for (int i = 0; i < layers.getLayersCount(); i++) {
255
            if (layers.getLayer(i).isAvailable()) {
256
                return true;
257
            }
258
        }
259
        return false;
260
    }
261

    
262
    @Override
263
    public IPreference[] getPreferencesPages() {
264
        List<IPreference> prefs = new ArrayList<>();
265
        prefs.add(getViewPropertiesPage());
266
        prefs.add(new DialogReminderPage());
267
        prefs.add(new DALPage());
268
        
269
        return prefs.toArray(new IPreference[prefs.size()]);
270
    }
271
}