gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.app / org.gvsig.vectorediting.app.mainplugin / src / main / java / org / gvsig / vectorediting / app / mainplugin / EditingExtension.java @ 400
History | View | Annotate | Download (8.17 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2014 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 2
|
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.vectorediting.app.mainplugin; |
25 |
|
26 |
import java.io.File; |
27 |
|
28 |
import org.slf4j.Logger; |
29 |
import org.slf4j.LoggerFactory; |
30 |
|
31 |
import org.gvsig.andami.IconThemeHelper; |
32 |
import org.gvsig.andami.PluginServices; |
33 |
import org.gvsig.andami.plugins.Extension; |
34 |
import org.gvsig.app.ApplicationLocator; |
35 |
import org.gvsig.app.ApplicationManager; |
36 |
import org.gvsig.app.project.documents.view.ViewDocument; |
37 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
38 |
import org.gvsig.app.project.documents.view.gui.IView; |
39 |
import org.gvsig.app.project.documents.view.toolListeners.StatusBarListener; |
40 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
41 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
42 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
43 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
44 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException; |
45 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
46 |
import org.gvsig.fmap.mapcontrol.MapControl; |
47 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
48 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.MouseMovementBehavior; |
49 |
import org.gvsig.tools.observer.Notification; |
50 |
import org.gvsig.tools.observer.Observable; |
51 |
import org.gvsig.tools.observer.Observer; |
52 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
53 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
54 |
import org.gvsig.vectorediting.swing.api.EditingContext; |
55 |
import org.gvsig.vectorediting.swing.api.EditingSwingLocator; |
56 |
import org.gvsig.vectorediting.swing.api.EditingSwingManager; |
57 |
|
58 |
public class EditingExtension extends Extension implements Observer { |
59 |
|
60 |
private static Logger logger = LoggerFactory.getLogger(EditingExtension.class); |
61 |
|
62 |
public void execute(String actionCommand) { |
63 |
|
64 |
IView view = getActiveView(); |
65 |
EditingSwingManager swingManager = |
66 |
EditingSwingLocator.getSwingManager(); |
67 |
|
68 |
if (view != null) { |
69 |
|
70 |
FLyrVect layer = getActiveLayer(view); |
71 |
EditingContext editingContext = |
72 |
swingManager.getEditingContext(view.getMapControl()); |
73 |
|
74 |
if ("start-editing".equals(actionCommand)) { |
75 |
|
76 |
if (canBeEdited(layer)) {
|
77 |
MapControl mapControl = view.getMapControl(); |
78 |
StatusBarListener sbl = new StatusBarListener(mapControl);
|
79 |
editingContext.beginEdition(layer, |
80 |
new Behavior[] { new MouseMovementBehavior(sbl) }); |
81 |
editingContext.addObserver(this);
|
82 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
83 |
} |
84 |
|
85 |
} else if ("end-editing".equals(actionCommand)) { |
86 |
|
87 |
if ((layer != null) && layer.isEditing()) { |
88 |
editingContext.endEdition(layer); |
89 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
90 |
} |
91 |
|
92 |
} |
93 |
} |
94 |
} |
95 |
|
96 |
public void initialize() { |
97 |
registerIcons(); |
98 |
|
99 |
// Disable default view panel console. Uses editing context console.
|
100 |
DefaultViewPanel.setDisableConsole(true);
|
101 |
} |
102 |
|
103 |
|
104 |
private void registerIcons() { |
105 |
IconThemeHelper.registerIcon("vectorediting", "vector-editing", this); |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public void postInitialize() { |
110 |
super.postInitialize();
|
111 |
registerSymbols(); |
112 |
|
113 |
} |
114 |
|
115 |
/**
|
116 |
* Register all symbols in the plugin symbols folder in the providerManager.
|
117 |
* The description of the symbols must be unique because the key used for registration is the proper description of the symbol.
|
118 |
*
|
119 |
*/
|
120 |
private void registerSymbols() { |
121 |
|
122 |
EditingProviderManager providerManager = |
123 |
EditingProviderLocator.getProviderManager(); |
124 |
|
125 |
SymbolManager symbolManager = MapContextLocator.getSymbolManager(); |
126 |
File pluginFolder = getPlugin().getPluginDirectory();
|
127 |
String pathSeparator = System.getProperty("file.separator"); |
128 |
String symbolsPath = pluginFolder.getAbsolutePath()+pathSeparator+"symbols"+pathSeparator+"editing"; |
129 |
File symbolsFolder = new File(symbolsPath); |
130 |
ISymbol[] symbols = null; |
131 |
try {
|
132 |
symbols = symbolManager.loadSymbols(symbolsFolder); |
133 |
} catch (SymbolException e) {
|
134 |
logger.warn("No symbols loaded from "+symbolsPath, e);
|
135 |
} |
136 |
|
137 |
if (symbols != null) { |
138 |
for (int i = 0; i < symbols.length; i++) { |
139 |
ISymbol symbol = symbols[i]; |
140 |
String description = symbol.getDescription();
|
141 |
providerManager.registerSymbol(description, symbol); |
142 |
} |
143 |
} |
144 |
} |
145 |
|
146 |
public boolean isEnabled() { |
147 |
return true; |
148 |
} |
149 |
|
150 |
public boolean isVisible() { |
151 |
return true; |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public boolean isVisible(String action) { |
156 |
IView view = getActiveView(); |
157 |
FLyrVect activeLayer = getActiveLayer(view); |
158 |
|
159 |
if ("start-editing".equals(action)) { |
160 |
return ((view != null) && (activeLayer != null) && !activeLayer |
161 |
.isEditing()); |
162 |
|
163 |
} else {
|
164 |
return ((view != null) && (activeLayer != null) && activeLayer |
165 |
.isEditing()); |
166 |
|
167 |
} |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public boolean isEnabled(String action) { |
172 |
|
173 |
IView vista = getActiveView(); |
174 |
FLyrVect activeLayer = getActiveLayer(vista); |
175 |
|
176 |
if ("start-editing".equals(action)) { |
177 |
return (canBeEdited(activeLayer));
|
178 |
|
179 |
} else if ("end-editing".equals(action)) { |
180 |
return activeLayer.isEditing();
|
181 |
|
182 |
} |
183 |
|
184 |
return false; |
185 |
|
186 |
} |
187 |
|
188 |
@Override
|
189 |
public boolean canQueryByAction() { |
190 |
return true; |
191 |
} |
192 |
|
193 |
private IView getActiveView() {
|
194 |
ApplicationManager application = ApplicationLocator.getManager(); |
195 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
196 |
return view;
|
197 |
} |
198 |
|
199 |
private boolean canBeEdited(FLyrVect layer) { |
200 |
if (layer != null && layer.isAvailable()) { |
201 |
|
202 |
boolean isWritable = layer.isWritable();
|
203 |
boolean isNotTransformed =
|
204 |
layer.getFeatureStore().getTransforms().isEmpty(); |
205 |
|
206 |
return isWritable && isNotTransformed && !layer.isEditing();
|
207 |
} |
208 |
|
209 |
return false; |
210 |
} |
211 |
|
212 |
private FLyrVect getActiveLayer(IView vista) {
|
213 |
if (vista != null) { |
214 |
ViewDocument viewDocument = vista.getViewDocument(); |
215 |
FLayer[] actives =
|
216 |
viewDocument.getMapContext().getLayers().getActives(); |
217 |
|
218 |
if ((actives.length == 1) && (actives[0] instanceof FLyrVect)) { |
219 |
return (FLyrVect) actives[0]; |
220 |
} |
221 |
} |
222 |
return null; |
223 |
} |
224 |
|
225 |
public void update(Observable observable, Object notification) { |
226 |
|
227 |
if (notification instanceof Notification){ |
228 |
ApplicationManager appManager = |
229 |
ApplicationLocator.getManager(); |
230 |
Notification n = (Notification)notification; |
231 |
if (n.getType().equalsIgnoreCase(EditingContext.CHANGE_SELECTED_TOOL_NOTIFICATION)){
|
232 |
String name = (String)n.getValue(); |
233 |
PluginServices.getMainFrame().setSelectedTool(name); |
234 |
appManager.refreshMenusAndToolBars(); |
235 |
|
236 |
} else if (n.getType().equalsIgnoreCase(EditingContext.REFRESH_TOOLS_NOTIFICATION)){ |
237 |
appManager.refreshMenusAndToolBars(); |
238 |
} |
239 |
} |
240 |
} |
241 |
} |