Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / StartEditing.java @ 41081

History | View | Annotate | Download (7.46 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing;
25

    
26
import java.awt.Component;
27
import java.util.List;
28
import java.util.prefs.Preferences;
29

    
30
import javax.swing.JOptionPane;
31

    
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.messages.Messages;
36
import org.gvsig.andami.messages.NotificationManager;
37
import org.gvsig.andami.plugins.Extension;
38
import org.gvsig.andami.plugins.IExtensionExecuteWithArgs;
39
import org.gvsig.app.ApplicationLocator;
40
import org.gvsig.app.gui.preferencespage.GridPage;
41
import org.gvsig.app.project.documents.view.ViewDocument;
42
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
43
import org.gvsig.editing.gui.cad.CADTool;
44
import org.gvsig.editing.gui.tokenmarker.ConsoleToken;
45
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.exception.ReadException;
47
import org.gvsig.fmap.mapcontext.layers.FLayer;
48
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
49
import org.gvsig.fmap.mapcontrol.MapControl;
50
import org.gvsig.utils.console.jedit.KeywordMap;
51
import org.gvsig.utils.console.jedit.Token;
52

    
53

    
54
public class StartEditing extends Extension implements IExtensionExecuteWithArgs {
55
    
56
    private static Logger logger = LoggerFactory.getLogger(StartEditing.class);
57

    
58
        public void initialize() {
59
        }
60

    
61
    public void execute(String actionCommand) {
62
                this.execute(actionCommand, null);
63
        }
64
        
65
        public void execute(String actionCommand, Object[] args) {
66

    
67
        if (actionCommand.compareTo("layer-start-editing") == 0) {
68

    
69
                FLayer[] selectedItems = null;
70
                    // Desde el menu contextual del TOC se le pasan las capas seleccionadas como primer argumento.
71
                if( args!=null && args.length>0 && args[0] instanceof FLayer[] ) {
72
                        selectedItems = (FLayer[]) args[0];
73
                }
74
                
75
            org.gvsig.andami.ui.mdiManager.IWindow f =
76
                PluginServices.getMDIManager().getActiveWindow();
77

    
78
            if (f instanceof DefaultViewPanel) {
79
                DefaultViewPanel vista = (DefaultViewPanel) f;
80

    
81
                MapControl mapControl = vista.getMapControl();
82
                ViewDocument model = vista.getModel();
83
                FLayer[] actives =
84
                    model.getMapContext().getLayers().getActives();
85

    
86
                if (actives.length == 1 && actives[0] instanceof FLyrVect) {
87

    
88
                    FLyrVect lv = (FLyrVect) actives[0];
89

    
90
                    if (!lv.getFeatureStore().getTransforms().isEmpty()) {
91

    
92
                        // cannot edit transformed
93
                        JOptionPane
94
                            .showMessageDialog(
95
                                (Component) f,
96
                                Messages
97
                                    .get("_Cannot_start_edition_in_transformed_layer")
98
                                    + ": '" + lv.getName() + "'",
99
                                PluginServices.getText(this, "warning_title"),
100
                                JOptionPane.INFORMATION_MESSAGE);
101
                        return;
102
                    }
103
                    
104
                    if (!lv.isWritable()) {
105
                        JOptionPane.showMessageDialog((Component) f,
106
                            PluginServices.getText(this,
107
                                "this_layer_is_not_self_editable"),
108
                            PluginServices.getText(this, "warning_title"),
109
                            JOptionPane.WARNING_MESSAGE);
110
                    }
111
                    
112
                    IEditionManager editionManager =
113
                        EditionLocator.getEditionManager();
114
                    
115
                    // ==============================================
116
                    // Set visibility and snapping of grid according to
117
                    // grid preferences.
118
                    Preferences prefs = GridPage.prefs;
119
                    boolean b = prefs.getBoolean("grid.showgrid", false);
120
                    mapControl.setGridVisibility(b);
121
                    b = prefs.getBoolean("grid.adjustgrid", false);
122
                    mapControl.setAdjustGrid(b);
123
                    // Enable snapping to geometries 
124
                    // (this enables snappers which are active
125
                    mapControl.setRefentEnabled(true);
126
                    /*
127
                     * Add if not present in list of layers to snap to:
128
                     */
129
                    List snaplist = mapControl.getMapContext().getLayersToSnap();
130
                    if (!snaplist.contains(lv)) {
131
                            snaplist.add(lv);
132
                    }
133
                    // ==============================================
134
                    
135
                    try {
136
                        editionManager.editLayer(lv, vista);
137
                    } catch ( CancelException e) {
138
                            // Do nothing
139
                    } catch (DataException e) {
140

    
141
                        logger.info(
142
                            "Error while starting edition: " + e.getMessage(), e);
143

    
144
                        ApplicationLocator.getManager().message(
145
                            Messages.get("_Unable_to_start_edition_in_layer")
146
                                + ": " + lv.getName(),
147
                            JOptionPane.ERROR_MESSAGE);
148
                    }
149
                }
150
            }
151
        }
152
    }
153

    
154

    
155

    
156
        public static void startCommandsApplicable(DefaultViewPanel vista,FLyrVect lv) {
157
            if (vista==null)
158
                vista=(DefaultViewPanel)PluginServices.getMDIManager().getActiveWindow();
159

    
160
            CADTool[] cadtools = CADExtension.getCADTools();
161
                KeywordMap keywordMap = new KeywordMap(true);
162
                for (int i = 0; i < cadtools.length; i++) {
163
                        try {
164
                                if (cadtools[i].isApplicable(lv.getShapeType())){
165
                                        keywordMap.add(cadtools[i].getName(), Token.KEYWORD2);
166
                                        keywordMap.add(cadtools[i].toString(), Token.KEYWORD3);
167
                                }
168
                        } catch (ReadException e) {
169
                                NotificationManager.addError(e.getMessage(),e);
170
                        }
171

    
172
                }
173
                ConsoleToken consoletoken = new ConsoleToken(keywordMap);
174
                vista.getConsolePanel().setTokenMarker(consoletoken);
175

    
176
        }
177

    
178

    
179
        public boolean isEnabled() {
180
                return true;
181
        }
182

    
183
        public boolean isVisible() {
184
                org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
185
                                .getActiveWindow();
186

    
187
                if (f == null) {
188
                        return false;
189
                }
190
                if (f instanceof DefaultViewPanel){
191
                        DefaultViewPanel view=(DefaultViewPanel)f;
192
                        FLayer[] selected = view.getModel().getMapContext().getLayers()
193
                        .getActives();
194
                        if (selected.length == 1 && selected[0].isAvailable() && selected[0] instanceof FLyrVect) {
195
                                if (selected[0].isEditing())
196
                                        return false;
197
                                return true;
198
                        }
199
                }
200
                return false;
201
        }
202
}