Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.snapping.app / org.gvsig.snapping.app.mainplugin / src / main / java / org / gvsig / app / extension / SnappingExtension.java @ 47426

History | View | Annotate | Download (6.22 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.prefs.Preferences;
26

    
27
import org.gvsig.andami.IconThemeHelper;
28
import org.gvsig.andami.PluginServices;
29
import org.gvsig.andami.plugins.Extension;
30
import org.gvsig.andami.preferences.IPreference;
31
import org.gvsig.andami.preferences.IPreferenceExtension;
32
import org.gvsig.app.gui.preferencespage.GridPage;
33
import org.gvsig.app.gui.preferencespage.SnapConfigPage;
34
import org.gvsig.app.project.documents.view.ViewDocument;
35
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
36
import org.gvsig.app.project.documents.view.gui.ViewSnappingPropertiesPageFactory;
37
import org.gvsig.app.project.documents.view.toolListeners.snapping.Snapping;
38
import org.gvsig.fmap.mapcontext.MapContext;
39
import org.gvsig.fmap.mapcontext.layers.FLayer;
40
import org.gvsig.fmap.mapcontext.layers.FLayers;
41
import org.gvsig.fmap.mapcontext.layers.LayersIterator;
42
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
43
import org.gvsig.fmap.mapcontrol.MapControlLocator;
44
import org.gvsig.fmap.mapcontrol.MapControlManager;
45
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper;
46
import org.gvsig.propertypage.PropertiesPageManager;
47
import org.gvsig.tools.swing.api.ToolsSwingUtils;
48

    
49

    
50
/**
51
 * Extension for config the snapping
52
 *
53
 * @author fdiaz
54
 *
55
 */
56
public class SnappingExtension extends Extension implements IPreferenceExtension {
57

    
58
    private IPreference[] preferencePages = null;
59

    
60
    public void initialize() {
61
        Snapping.register();
62

    
63
        registerIcons();
64

    
65
        MapControlManager mapControlManager = MapControlLocator.getMapControlManager();
66
        for (int n = 0; n < mapControlManager.getSnapperCount(); n++) {
67
            ISnapper snp = (ISnapper) mapControlManager.getSnapperAt(n);
68
            String nameClass = snp.getClass().getName();
69
            nameClass = nameClass.substring(nameClass.lastIndexOf('.') + 1);
70
            snp.setEnabled((Boolean) getPlugin().getPluginProperties().getDynValue("snapperActivated" + nameClass));
71
            snp.setPriority((Integer) getPlugin().getPluginProperties().getDynValue("snapperPriority" + nameClass));
72
        }
73

    
74
        Integer snapTolerance
75
                = (Integer) getPlugin().getPluginProperties().getDynValue("snapTolerance");
76
        mapControlManager.setTolerance(snapTolerance.intValue());
77

    
78
        Boolean applySnappers
79
                = (Boolean) getPlugin().getPluginProperties().getDynValue("applySnappers");
80

    
81
        Preferences prefs = Preferences.userRoot().node("snappers");
82
        prefs.putBoolean("apply-snappers", applySnappers);
83

    
84
        //TODO: si se a?ade al MapControlManager para habilitar/deshabilitar de manera global los snappers activos, invocarlo aqu?
85
//        mapControlManager.applySnappers(applySnappers);
86

    
87
        PropertiesPageManager manager = MapControlLocator.getPropertiesPageManager();
88
        manager.registerFactory(new ViewSnappingPropertiesPageFactory());
89

    
90
    }
91

    
92
    @Override
93
    public void postInitialize() {
94

    
95
    }
96

    
97
    private void registerIcons() {
98
        ToolsSwingUtils.registerIcons(
99
                this.getClass(), 
100
                "/images/action",
101
                this.getPlugin().getPluginName(),
102
                new String[] { "action", "enable-snapping","snapping" },
103
                new String[] { "action", "disable-snapping","snapping" }
104
        ); 
105
        ToolsSwingUtils.registerIcons(
106
                this.getClass(), 
107
                "/images/preferences",
108
                this.getPlugin().getPluginName(),
109
                new String[] { "preferences", "preferences-snapper","snapping" },
110
                new String[] { "preferences", "grid-preferences","snapping" }
111
        ); 
112

    
113
    }
114

    
115
    public void execute(String actionCommand) {
116
    }
117

    
118
    public boolean isEnabled() {
119
        return true;
120
    }
121

    
122
    public boolean isVisible() {
123

    
124
        org.gvsig.andami.ui.mdiManager.IWindow f
125
                = PluginServices.getMDIManager().getActiveWindow();
126

    
127
        if (f != null && f instanceof DefaultViewPanel) {
128
            DefaultViewPanel vista = (DefaultViewPanel) f;
129
            ViewDocument model = vista.getViewDocument();
130
            MapContext mapa = model.getMapContext();
131

    
132
            FLayers capas = mapa.getLayers();
133

    
134
            int numActiveVectorial = 0;
135
            int numActiveVectorialEditable = 0;
136

    
137
            LayersIterator iter = new LayersIterator(capas);
138

    
139
            FLayer capa;
140
            while (iter.hasNext()) {
141
                capa = iter.nextLayer();
142
                if (capa instanceof FLyrVect && capa.isActive()
143
                        && capa.isAvailable()) {
144
                    numActiveVectorial++;
145
                    if (capa.isEditing()) {
146
                        numActiveVectorialEditable++;
147
                    }
148
                }
149

    
150
            }
151

    
152
            if (numActiveVectorialEditable == 1 && numActiveVectorial == 1) {
153
                return true;
154
            }
155
        }
156
        return false;
157
    }
158

    
159
    @Override
160
    public IPreference[] getPreferencesPages() {
161
        if( this.preferencePages == null ) {
162
            this.preferencePages = new IPreference[] {
163
                new SnapConfigPage(),
164
                new GridPage()
165
            };
166
        }
167
        return this.preferencePages;
168
    }
169
}