Revision 1907 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
EditingExtension.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import java.io.File; |
27 | 27 |
import java.util.ArrayList; |
28 |
import java.util.Collections; |
|
28 | 29 |
import java.util.List; |
30 |
import java.util.function.Predicate; |
|
29 | 31 |
import javax.swing.JOptionPane; |
30 | 32 |
import org.apache.commons.io.FileUtils; |
31 | 33 |
|
... | ... | |
83 | 85 |
|
84 | 86 |
public class EditingExtension extends Extension implements Observer { |
85 | 87 |
|
86 |
private static final Logger logger = LoggerFactory.getLogger(EditingExtension.class);
|
|
88 |
private static final Logger LOGGER = LoggerFactory.getLogger(EditingExtension.class);
|
|
87 | 89 |
|
88 |
@Override
|
|
89 |
public void execute(String actionCommand) {
|
|
90 |
@Override |
|
91 |
public void execute(String actionCommand) { |
|
90 | 92 |
|
91 |
ApplicationManager application = ApplicationLocator.getManager();
|
|
92 |
IView view = getActiveView();
|
|
93 |
EditingSwingManager swingManager = EditingSwingLocator.getSwingManager();
|
|
93 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
94 |
IView view = getActiveView(); |
|
95 |
EditingSwingManager swingManager = EditingSwingLocator.getSwingManager(); |
|
94 | 96 |
|
95 |
if (view != null) {
|
|
97 |
if (view != null) { |
|
96 | 98 |
|
97 |
FLyrVect layer = getActiveLayer(view); |
|
98 |
MapControl mapControl = view.getMapControl(); |
|
99 |
EditingContext editingContext = swingManager.getEditingContext(mapControl); |
|
99 |
MapControl mapControl = view.getMapControl(); |
|
100 |
EditingContext editingContext = swingManager.getEditingContext(mapControl); |
|
100 | 101 |
|
101 |
if ("start-editing".equals(actionCommand)) {
|
|
102 |
if ("start-editing".equals(actionCommand)) { |
|
102 | 103 |
|
103 |
if (canBeEdited(layer)) { |
|
104 |
if( !mapControl.getProjection().equals(layer.getProjection()) ) { |
|
105 |
String msg = "_The_layer_is_reproyected_on_the_fly" |
|
106 |
+ "_Not_all_editing_tools_work_properly" |
|
107 |
+ "_When_you_finish_editing" |
|
108 |
+ "_will_only_export_the_changes_to_another_layer"; |
|
109 |
application.messageDialog(msg, "_Warning", JOptionPane.WARNING_MESSAGE); |
|
110 |
} else if (!isWritable(layer)){ |
|
111 |
String msg = "_The_layer_can_not_be_written" |
|
112 |
+ "_When_you_finish_editing" |
|
113 |
+ "_will_only_export_the_changes_to_another_layer"; |
|
114 |
application.messageDialog(msg, "_Warning", JOptionPane.WARNING_MESSAGE); |
|
115 |
} |
|
116 |
editingContext.beginEdition(layer); |
|
117 |
editingContext.addObserver(this); |
|
118 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
|
119 |
} |
|
104 |
List<FLyrVect> reprojectedLayers = getActiveLayers( |
|
105 |
view, |
|
106 |
(FLyrVect layer) -> canBeEdited(layer) && |
|
107 |
!mapControl.getProjection().equals(layer.getProjection()) |
|
108 |
); |
|
109 |
if (!reprojectedLayers.isEmpty()) { |
|
110 |
String msg = "_The_layer_is_reproyected_on_the_fly" |
|
111 |
+ "_Not_all_editing_tools_work_properly" |
|
112 |
+ "_When_you_finish_editing" |
|
113 |
+ "_will_only_export_the_changes_to_another_layer"; |
|
114 |
application.messageDialog(msg, "_Warning", JOptionPane.WARNING_MESSAGE); |
|
115 |
} else { |
|
116 |
List<FLyrVect> nonWritableLayers = getActiveLayers( |
|
117 |
view, |
|
118 |
(FLyrVect layer) -> canBeEdited(layer) && !isWritable(layer) |
|
119 |
); |
|
120 |
if (!nonWritableLayers.isEmpty()) { |
|
121 |
String msg = "_The_layer_can_not_be_written" |
|
122 |
+ "_When_you_finish_editing" |
|
123 |
+ "_will_only_export_the_changes_to_another_layer"; |
|
124 |
application.messageDialog(msg, "_Warning", JOptionPane.WARNING_MESSAGE); |
|
125 |
} |
|
126 |
} |
|
127 |
List<FLyrVect> activeLayers = getActiveLayers( |
|
128 |
view, |
|
129 |
(FLyrVect layer) -> canBeEdited(layer) |
|
130 |
); |
|
131 |
for (FLyrVect layer : activeLayers) { |
|
132 |
editingContext.beginEdition(layer); |
|
133 |
editingContext.addObserver(this); |
|
134 |
} |
|
135 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
|
120 | 136 |
|
121 |
} else if ("end-editing".equals(actionCommand)) { |
|
137 |
} else if ("end-editing".equals(actionCommand)) { |
|
138 |
List<FLyrVect> activeLayers = getActiveLayers( |
|
139 |
view, |
|
140 |
(FLyrVect layer) -> layer.isEditing() |
|
141 |
); |
|
142 |
for (FLyrVect layer : activeLayers) { |
|
143 |
editingContext.endEdition(layer); |
|
144 |
} |
|
145 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
|
122 | 146 |
|
123 |
if ((layer != null) && layer.isEditing()) { |
|
124 |
editingContext.endEdition(layer); |
|
125 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
|
126 |
} |
|
127 |
|
|
128 |
} |
|
129 |
} |
|
147 |
} |
|
130 | 148 |
} |
149 |
} |
|
131 | 150 |
|
132 |
@Override
|
|
133 |
public void initialize() {
|
|
134 |
registerIcons();
|
|
151 |
@Override |
|
152 |
public void initialize() { |
|
153 |
registerIcons(); |
|
135 | 154 |
|
136 |
// Disable default view panel console. Uses editing context console.
|
|
137 |
DefaultViewPanel.setDisableConsole(true);
|
|
155 |
// Disable default view panel console. Uses editing context console. |
|
156 |
DefaultViewPanel.setDisableConsole(true); |
|
138 | 157 |
|
139 |
// Adding TOC menu entry
|
|
140 |
ExtensionPoint exPoint = ToolsLocator.getExtensionPointManager().add(
|
|
141 |
"View_TocActions", "");
|
|
142 |
exPoint.append(
|
|
158 |
// Adding TOC menu entry |
|
159 |
ExtensionPoint exPoint = ToolsLocator.getExtensionPointManager().add( |
|
160 |
"View_TocActions", ""); |
|
161 |
exPoint.append( |
|
143 | 162 |
StartEditingTocMenuEntry.EXTENSION_POINT_NAME, |
144 |
"TOC popup menu to start vector layer's editing",
|
|
145 |
new StartEditingTocMenuEntry());
|
|
146 |
exPoint.append(
|
|
163 |
"TOC popup menu to start vector layer's editing", |
|
164 |
new StartEditingTocMenuEntry()); |
|
165 |
exPoint.append( |
|
147 | 166 |
EndEditingTocMenuEntry.EXTENSION_POINT_NAME, |
148 |
"TOC popup menu to end vector layer's editing",
|
|
149 |
new EndEditingTocMenuEntry());
|
|
150 |
}
|
|
167 |
"TOC popup menu to end vector layer's editing", |
|
168 |
new EndEditingTocMenuEntry()); |
|
169 |
} |
|
151 | 170 |
|
171 |
private void registerIcons() { |
|
172 |
IconThemeHelper.registerIcon("vectorediting", "vector-editing", this); |
|
173 |
} |
|
152 | 174 |
|
153 |
private void registerIcons() { |
|
154 |
IconThemeHelper.registerIcon("vectorediting", "vector-editing", this); |
|
155 |
} |
|
175 |
@Override |
|
176 |
public void postInitialize() { |
|
177 |
super.postInitialize(); |
|
178 |
registerSymbols(); |
|
156 | 179 |
|
157 |
@Override |
|
158 |
public void postInitialize() { |
|
159 |
super.postInitialize(); |
|
160 |
registerSymbols(); |
|
180 |
MapControlLocator.getMapControlManager().addMapControlCreationListener(new MapControlCreationListener() { |
|
161 | 181 |
|
162 |
MapControlLocator.getMapControlManager().addMapControlCreationListener(new MapControlCreationListener() { |
|
182 |
@Override |
|
183 |
public MapControl mapControlCreated(MapControl mapControl) { |
|
184 |
EditingContext editingContext = EditingSwingLocator.getSwingManager().getEditingContext(mapControl); |
|
185 |
StatusBarListener sbl = new StatusBarListener(mapControl); |
|
186 |
editingContext.setDefaultBehaviors(new Behavior[]{new MouseMovementBehavior(sbl)}); |
|
187 |
editingContext.addObserver(EditingExtension.this); |
|
188 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
|
189 |
return mapControl; |
|
190 |
} |
|
191 |
}); |
|
192 |
} |
|
163 | 193 |
|
164 |
@Override |
|
165 |
public MapControl mapControlCreated(MapControl mapControl) { |
|
166 |
EditingContext editingContext = EditingSwingLocator.getSwingManager().getEditingContext(mapControl); |
|
167 |
StatusBarListener sbl = new StatusBarListener(mapControl); |
|
168 |
editingContext.setDefaultBehaviors(new Behavior[] { new MouseMovementBehavior(sbl) }); |
|
169 |
editingContext.addObserver(EditingExtension.this); |
|
170 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
|
171 |
return mapControl; |
|
172 |
} |
|
173 |
}); |
|
194 |
/** |
|
195 |
* Register all symbols in the plugin symbols folder in the providerManager. |
|
196 |
* The description of the symbols must be unique because the key used for |
|
197 |
* registration is the proper description of the symbol. |
|
198 |
* |
|
199 |
*/ |
|
200 |
private void registerSymbols() { |
|
201 |
|
|
202 |
EditingProviderManager providerManager = EditingProviderLocator.getProviderManager(); |
|
203 |
|
|
204 |
SymbolManager symbolManager = MapContextLocator.getSymbolManager(); |
|
205 |
File symbolsFolder = FileUtils.getFile(getPlugin().getPluginDirectory(), "symbols", "editing"); |
|
206 |
ISymbol[] symbols = null; |
|
207 |
try { |
|
208 |
symbols = symbolManager.loadSymbols(symbolsFolder); |
|
209 |
} catch (SymbolException e) { |
|
210 |
LOGGER.warn("No symbols loaded from " + symbolsFolder.getAbsolutePath(), e); |
|
174 | 211 |
} |
175 | 212 |
|
176 |
/** |
|
177 |
* Register all symbols in the plugin symbols folder in the providerManager. |
|
178 |
* The description of the symbols must be unique because the key used for registration is the proper description of the symbol. |
|
179 |
* |
|
180 |
*/ |
|
181 |
private void registerSymbols() { |
|
213 |
if (symbols != null) { |
|
214 |
for (ISymbol symbol : symbols) { |
|
215 |
String description = symbol.getDescription(); |
|
216 |
providerManager.registerSymbol(description, symbol); |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
182 | 220 |
|
183 |
EditingProviderManager providerManager = EditingProviderLocator.getProviderManager(); |
|
221 |
@Override |
|
222 |
public boolean isEnabled() { |
|
223 |
return true; |
|
224 |
} |
|
184 | 225 |
|
185 |
SymbolManager symbolManager = MapContextLocator.getSymbolManager(); |
|
186 |
File symbolsFolder = FileUtils.getFile(getPlugin().getPluginDirectory(), "symbols","editing"); |
|
187 |
ISymbol[] symbols = null; |
|
188 |
try { |
|
189 |
symbols = symbolManager.loadSymbols(symbolsFolder); |
|
190 |
} catch (SymbolException e) { |
|
191 |
logger.warn("No symbols loaded from "+symbolsFolder.getAbsolutePath(), e); |
|
192 |
} |
|
226 |
@Override |
|
227 |
public boolean isVisible() { |
|
228 |
return true; |
|
229 |
} |
|
193 | 230 |
|
194 |
if (symbols != null) { |
|
195 |
for (ISymbol symbol : symbols) { |
|
196 |
String description = symbol.getDescription(); |
|
197 |
providerManager.registerSymbol(description, symbol); |
|
198 |
} |
|
199 |
} |
|
231 |
@Override |
|
232 |
public boolean isVisible(String action) { |
|
233 |
IView view = getActiveView(); |
|
234 |
if( view == null ) { |
|
235 |
return false; |
|
200 | 236 |
} |
237 |
if ("start-editing".equals(action)) { |
|
238 |
List<FLyrVect> activeLayers = getActiveLayers(view, (FLyrVect layer) -> !layer.isEditing()); |
|
239 |
return !activeLayers.isEmpty(); |
|
240 |
} else { |
|
241 |
List<FLyrVect> activeLayers = getActiveLayers(view, (FLyrVect layer) -> layer.isEditing()); |
|
242 |
return !activeLayers.isEmpty(); |
|
201 | 243 |
|
202 |
@Override |
|
203 |
public boolean isEnabled() { |
|
204 |
return true; |
|
205 | 244 |
} |
245 |
} |
|
206 | 246 |
|
207 |
@Override |
|
208 |
public boolean isVisible() { |
|
209 |
return true; |
|
247 |
@Override |
|
248 |
public boolean isEnabled(String action) { |
|
249 |
IView view = getActiveView(); |
|
250 |
if( view == null ) { |
|
251 |
return false; |
|
210 | 252 |
} |
253 |
if ("start-editing".equals(action)) { |
|
254 |
List<FLyrVect> activeLayers = getActiveLayers(view, (FLyrVect layer) -> canBeEdited(layer)); |
|
255 |
return !activeLayers.isEmpty(); |
|
211 | 256 |
|
212 |
@Override |
|
213 |
public boolean isVisible(String action) { |
|
214 |
IView view = getActiveView(); |
|
215 |
FLyrVect activeLayer = getActiveLayer(view); |
|
257 |
} else if ("end-editing".equals(action)) { |
|
258 |
List<FLyrVect> activeLayers = getActiveLayers(view, (FLyrVect layer) -> layer.isEditing()); |
|
259 |
return !activeLayers.isEmpty(); |
|
216 | 260 |
|
217 |
if ("start-editing".equals(action)) { |
|
218 |
return ((view != null) && (activeLayer != null) && !activeLayer |
|
219 |
.isEditing()); |
|
220 |
|
|
221 |
} else { |
|
222 |
return ((view != null) && (activeLayer != null) && activeLayer |
|
223 |
.isEditing()); |
|
224 |
|
|
225 |
} |
|
226 | 261 |
} |
227 | 262 |
|
228 |
@Override |
|
229 |
public boolean isEnabled(String action) { |
|
263 |
return false; |
|
230 | 264 |
|
231 |
IView vista = getActiveView(); |
|
232 |
FLyrVect activeLayer = getActiveLayer(vista); |
|
265 |
} |
|
233 | 266 |
|
234 |
if ("start-editing".equals(action)) { |
|
235 |
return (canBeEdited(activeLayer)); |
|
267 |
@Override |
|
268 |
public boolean canQueryByAction() { |
|
269 |
return true; |
|
270 |
} |
|
236 | 271 |
|
237 |
} else if ("end-editing".equals(action)) { |
|
238 |
return activeLayer.isEditing(); |
|
272 |
private IView getActiveView() { |
|
273 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
274 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
|
275 |
return view; |
|
276 |
} |
|
239 | 277 |
|
240 |
} |
|
278 |
private boolean isWritable(FLyrVect layer) { |
|
279 |
if (layer != null && layer.isAvailable()) { |
|
280 |
return layer.getFeatureStore().allowWrite(); |
|
281 |
} |
|
282 |
return false; |
|
283 |
} |
|
241 | 284 |
|
242 |
return false; |
|
285 |
private boolean canBeEdited(FLyrVect layer) { |
|
286 |
if (layer != null && layer.isAvailable()) { |
|
243 | 287 |
|
244 |
} |
|
288 |
boolean isNotTransformed |
|
289 |
= layer.getFeatureStore().getTransforms().isEmpty(); |
|
245 | 290 |
|
246 |
@Override |
|
247 |
public boolean canQueryByAction() { |
|
248 |
return true; |
|
291 |
return isNotTransformed && !layer.isEditing(); |
|
249 | 292 |
} |
250 | 293 |
|
251 |
private IView getActiveView() { |
|
252 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
253 |
IView view = (IView) application.getActiveComponent(ViewDocument.class); |
|
254 |
return view; |
|
255 |
} |
|
294 |
return false; |
|
295 |
} |
|
256 | 296 |
|
257 |
private boolean isWritable(FLyrVect layer) { |
|
258 |
if (layer != null && layer.isAvailable()) { |
|
259 |
return layer.getFeatureStore().allowWrite(); |
|
260 |
} |
|
261 |
return false; |
|
297 |
// private FLyrVect getActiveLayer(IView vista) { |
|
298 |
// if (vista != null) { |
|
299 |
// ViewDocument viewDocument = vista.getViewDocument(); |
|
300 |
// FLayer[] actives |
|
301 |
// = viewDocument.getMapContext().getLayers().getActives(); |
|
302 |
// |
|
303 |
// if ((actives.length == 1) && (actives[0] instanceof FLyrVect)) { |
|
304 |
// return (FLyrVect) actives[0]; |
|
305 |
// } |
|
306 |
// } |
|
307 |
// return null; |
|
308 |
// } |
|
309 |
|
|
310 |
private List<FLyrVect> getActiveLayers(IView view, Predicate<FLyrVect> valid) { |
|
311 |
if (view == null) { |
|
312 |
return Collections.EMPTY_LIST; |
|
262 | 313 |
} |
314 |
List<FLyrVect> layers = new ArrayList<>(); |
|
315 |
ViewDocument viewDocument = view.getViewDocument(); |
|
316 |
FLayer[] actives = viewDocument.getMapContext().getLayers().getActives(); |
|
317 |
for (FLayer layer : actives) { |
|
318 |
if (layer instanceof FLyrVect && valid.test((FLyrVect) layer)) { |
|
319 |
layers.add((FLyrVect) layer); |
|
320 |
} |
|
321 |
} |
|
322 |
return layers; |
|
323 |
} |
|
263 | 324 |
|
264 |
private boolean canBeEdited(FLyrVect layer) {
|
|
265 |
if (layer != null && layer.isAvailable()) {
|
|
325 |
@Override
|
|
326 |
public void update(Observable observable, Object notification) {
|
|
266 | 327 |
|
267 |
boolean isNotTransformed = |
|
268 |
layer.getFeatureStore().getTransforms().isEmpty(); |
|
328 |
if (notification instanceof Notification) { |
|
329 |
ApplicationManager appManager = ApplicationLocator.getManager(); |
|
330 |
Notification n = (Notification) notification; |
|
331 |
if (n.isOfType(EditingContext.CHANGE_SELECTED_TOOL_NOTIFICATION)) { |
|
332 |
String name = (String) n.getValue(); |
|
333 |
LOGGER.trace("Changed selected tool to '{}'", name); |
|
334 |
PluginServices.getMainFrame().setSelectedTool(name); |
|
335 |
appManager.refreshMenusAndToolBars(); |
|
269 | 336 |
|
270 |
return isNotTransformed && !layer.isEditing();
|
|
271 |
}
|
|
337 |
} else if (n.isOfType(EditingContext.EXPORT_LAYER_NOTIFICATION)) {
|
|
338 |
exportLayer((FLyrVect) (n.getValue()));
|
|
272 | 339 |
|
273 |
return false; |
|
340 |
} else if (n.isOfType(EditingContext.REFRESH_TOOLS_NOTIFICATION)) { |
|
341 |
appManager.refreshMenusAndToolBars(); |
|
342 |
} |
|
274 | 343 |
} |
344 |
} |
|
275 | 345 |
|
276 |
private FLyrVect getActiveLayer(IView vista) { |
|
277 |
if (vista != null) { |
|
278 |
ViewDocument viewDocument = vista.getViewDocument(); |
|
279 |
FLayer[] actives = |
|
280 |
viewDocument.getMapContext().getLayers().getActives(); |
|
346 |
private void exportLayer(FLyrVect layer) { |
|
347 |
ApplicationManager appManager = ApplicationLocator.getManager(); |
|
348 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
|
349 |
ActionInfo action = PluginsLocator.getActionInfoManager().getAction("layer-exportto"); |
|
350 |
action.execute(new Object[]{layer, MODE.toInteger(MODE.DIALOG)}); |
|
351 |
String msg = "_Do_you_want_to_finish_editing_If_not_exported_the_data_the_changes_will_be_lost"; |
|
352 |
if (appManager.confirmDialog( |
|
353 |
i18nManager.getTranslation(msg), |
|
354 |
i18nManager.getTranslation("end_editing"), |
|
355 |
JOptionPane.YES_NO_OPTION, |
|
356 |
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) { |
|
357 |
try { |
|
358 |
layer.getFeatureStore().cancelEditing(); |
|
359 |
} catch (Exception e) { |
|
360 |
LOGGER.warn("Can't abort editing of layer '" + layer.getName() + "'.", e); |
|
361 |
msg = "_Cant_finish_editing_See_the_error_log_for_more_information"; |
|
362 |
appManager.messageDialog(msg, "_Warning", JOptionPane.WARNING_MESSAGE); |
|
363 |
} |
|
364 |
} |
|
365 |
} |
|
281 | 366 |
|
282 |
if ((actives.length == 1) && (actives[0] instanceof FLyrVect)) { |
|
283 |
return (FLyrVect) actives[0]; |
|
367 |
private List<FLyrVect> getEditedLayers() { |
|
368 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
369 |
List<FLyrVect> editedLayers = new ArrayList<>(); |
|
370 |
|
|
371 |
Project project = application.getCurrentProject(); |
|
372 |
for (Document document : project.getDocuments(ViewManager.TYPENAME)) { |
|
373 |
ViewDocument view = (ViewDocument) document; |
|
374 |
MapContext mapContext = view.getMapContext(); |
|
375 |
if (mapContext != null) { |
|
376 |
FLayers layers = mapContext.getLayers(); |
|
377 |
for (int j = 0; j < layers.getLayersCount(); j++) { |
|
378 |
FLayer lyr = layers.getLayer(j); |
|
379 |
if (lyr instanceof FLyrVect) { |
|
380 |
FLyrVect layerVect = (FLyrVect) lyr; |
|
381 |
if (layerVect.isEditing()) { |
|
382 |
editedLayers.add(layerVect); |
|
284 | 383 |
} |
384 |
} |
|
285 | 385 |
} |
286 |
return null;
|
|
386 |
}
|
|
287 | 387 |
} |
388 |
return editedLayers; |
|
389 |
} |
|
288 | 390 |
|
289 |
@Override |
|
290 |
public void update(Observable observable, Object notification) { |
|
391 |
private List<IUnsavedData> getUnsavedData(List<FLyrVect> editedLayers) { |
|
392 |
List<IUnsavedData> unsavedData = new ArrayList<>(); |
|
393 |
for (FLyrVect editedLayer : editedLayers) { |
|
394 |
IUnsavedData unsavedDataLayer = new UnsavedLayer(this, editedLayer); |
|
395 |
unsavedData.add(unsavedDataLayer); |
|
396 |
} |
|
397 |
return unsavedData; |
|
291 | 398 |
|
292 |
if (notification instanceof Notification){ |
|
293 |
ApplicationManager appManager = ApplicationLocator.getManager(); |
|
294 |
Notification n = (Notification)notification; |
|
295 |
if (n.isOfType(EditingContext.CHANGE_SELECTED_TOOL_NOTIFICATION)){ |
|
296 |
String name = (String)n.getValue(); |
|
297 |
logger.trace("Changed selected tool to '{}'", name); |
|
298 |
PluginServices.getMainFrame().setSelectedTool(name); |
|
299 |
appManager.refreshMenusAndToolBars(); |
|
399 |
} |
|
300 | 400 |
|
301 |
} else if (n.isOfType(EditingContext.EXPORT_LAYER_NOTIFICATION)){ |
|
302 |
exportLayer((FLyrVect)(n.getValue())); |
|
401 |
private static class UnsavedLayer extends UnsavedData { |
|
303 | 402 |
|
304 |
} else if (n.isOfType(EditingContext.REFRESH_TOOLS_NOTIFICATION)){ |
|
305 |
appManager.refreshMenusAndToolBars(); |
|
306 |
} |
|
307 |
} |
|
403 |
private final FLyrVect layer; |
|
404 |
|
|
405 |
public UnsavedLayer(IExtension extension, FLyrVect layer) { |
|
406 |
super(extension); |
|
407 |
this.layer = layer; |
|
308 | 408 |
} |
309 | 409 |
|
310 |
private void exportLayer(FLyrVect layer) { |
|
311 |
ApplicationManager appManager = ApplicationLocator.getManager(); |
|
312 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
|
313 |
ActionInfo action = PluginsLocator.getActionInfoManager().getAction("layer-exportto"); |
|
314 |
action.execute(new Object[] { layer, MODE.toInteger(MODE.DIALOG) }); |
|
315 |
String msg = "_Do_you_want_to_finish_editing_If_not_exported_the_data_the_changes_will_be_lost"; |
|
316 |
if( appManager.confirmDialog( |
|
317 |
i18nManager.getTranslation(msg), |
|
318 |
i18nManager.getTranslation("end_editing"), |
|
319 |
JOptionPane.YES_NO_OPTION, |
|
320 |
JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION ) { |
|
321 |
try { |
|
322 |
layer.getFeatureStore().cancelEditing(); |
|
323 |
} catch (Exception e) { |
|
324 |
logger.warn("Can't abort editing of layer '"+layer.getName()+"'.",e); |
|
325 |
msg = "_Cant_finish_editing_See_the_error_log_for_more_information"; |
|
326 |
appManager.messageDialog(msg, "_Warning", JOptionPane.WARNING_MESSAGE); |
|
327 |
} |
|
328 |
} |
|
410 |
@Override |
|
411 |
public String getDescription() { |
|
412 |
return layer.getName(); |
|
329 | 413 |
} |
330 | 414 |
|
331 |
private List<FLyrVect> getEditedLayers(){ |
|
332 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
333 |
List<FLyrVect> editedLayers = new ArrayList<>(); |
|
334 |
|
|
335 |
Project project = application.getCurrentProject(); |
|
336 |
for( Document document : project.getDocuments(ViewManager.TYPENAME) ) { |
|
337 |
ViewDocument view = (ViewDocument) document; |
|
338 |
MapContext mapContext = view.getMapContext(); |
|
339 |
if (mapContext != null) { |
|
340 |
FLayers layers = mapContext.getLayers(); |
|
341 |
for (int j = 0; j < layers.getLayersCount(); j++) { |
|
342 |
FLayer lyr = layers.getLayer(j); |
|
343 |
if (lyr instanceof FLyrVect) { |
|
344 |
FLyrVect layerVect = (FLyrVect) lyr; |
|
345 |
if (layerVect.isEditing()) { |
|
346 |
editedLayers.add(layerVect); |
|
347 |
} |
|
348 |
} |
|
349 |
} |
|
350 |
} |
|
351 |
} |
|
352 |
return editedLayers; |
|
415 |
@Override |
|
416 |
public String getResourceName() { |
|
417 |
return layer.getFeatureStore().getFullName(); |
|
353 | 418 |
} |
354 | 419 |
|
355 |
private List<IUnsavedData> getUnsavedData(List<FLyrVect> editedLayers){ |
|
356 |
List<IUnsavedData> unsavedData = new ArrayList<>(); |
|
357 |
for (FLyrVect editedLayer : editedLayers) { |
|
358 |
IUnsavedData unsavedDataLayer = new UnsavedLayer(this, editedLayer); |
|
359 |
unsavedData.add(unsavedDataLayer); |
|
420 |
@Override |
|
421 |
public boolean saveData() { |
|
422 |
FeatureStore featureStore = layer.getFeatureStore(); |
|
423 |
if (featureStore.isEditing()) { |
|
424 |
try { |
|
425 |
featureStore.finishEditing(); |
|
426 |
return true; |
|
427 |
} catch (DataException e) { |
|
428 |
throw new RuntimeException(e); |
|
360 | 429 |
} |
361 |
return unsavedData;
|
|
362 |
|
|
430 |
}
|
|
431 |
return true; |
|
363 | 432 |
} |
433 |
} |
|
364 | 434 |
|
365 |
private static class UnsavedLayer extends UnsavedData { |
|
366 |
private final FLyrVect layer; |
|
435 |
@Override |
|
436 |
public IExtensionStatus getStatus() { |
|
437 |
List<FLyrVect> editedLayers = getEditedLayers(); |
|
438 |
final List<IUnsavedData> unsavedData = getUnsavedData(editedLayers); |
|
367 | 439 |
|
368 |
public UnsavedLayer(IExtension extension, FLyrVect layer) { |
|
369 |
super(extension); |
|
370 |
this.layer = layer; |
|
371 |
} |
|
440 |
return new IExtensionStatus() { |
|
372 | 441 |
|
373 |
@Override |
|
374 |
public String getDescription() { |
|
375 |
return layer.getName(); |
|
442 |
@Override |
|
443 |
public boolean hasUnsavedData() { |
|
444 |
if (unsavedData == null) { |
|
445 |
return false; |
|
376 | 446 |
} |
447 |
return !unsavedData.isEmpty(); |
|
448 |
} |
|
377 | 449 |
|
378 |
@Override
|
|
379 |
public String getResourceName() {
|
|
380 |
return layer.getFeatureStore().getFullName();
|
|
381 |
}
|
|
450 |
@Override |
|
451 |
public boolean hasRunningProcesses() {
|
|
452 |
return false;
|
|
453 |
} |
|
382 | 454 |
|
383 |
@Override |
|
384 |
public boolean saveData() { |
|
385 |
FeatureStore featureStore = layer.getFeatureStore(); |
|
386 |
if(featureStore.isEditing()){ |
|
387 |
try { |
|
388 |
featureStore.finishEditing(); |
|
389 |
return true; |
|
390 |
} catch (DataException e) { |
|
391 |
throw new RuntimeException(e); |
|
392 |
} |
|
393 |
} |
|
394 |
return true; |
|
455 |
@Override |
|
456 |
public IUnsavedData[] getUnsavedData() { |
|
457 |
if (unsavedData == null) { |
|
458 |
return null; |
|
395 | 459 |
} |
396 |
} |
|
460 |
return unsavedData.toArray(new IUnsavedData[0]); |
|
461 |
} |
|
397 | 462 |
|
398 |
@Override |
|
399 |
public IExtensionStatus getStatus() { |
|
400 |
List<FLyrVect> editedLayers = getEditedLayers(); |
|
401 |
final List<IUnsavedData> unsavedData = getUnsavedData(editedLayers); |
|
402 |
|
|
403 |
return new IExtensionStatus() { |
|
404 |
|
|
405 |
@Override |
|
406 |
public boolean hasUnsavedData() { |
|
407 |
if(unsavedData == null){ |
|
408 |
return false; |
|
409 |
} |
|
410 |
return !unsavedData.isEmpty(); |
|
411 |
} |
|
412 |
|
|
413 |
@Override |
|
414 |
public boolean hasRunningProcesses() { |
|
415 |
return false; |
|
416 |
} |
|
417 |
|
|
418 |
@Override |
|
419 |
public IUnsavedData[] getUnsavedData() { |
|
420 |
if(unsavedData == null){ |
|
421 |
return null; |
|
422 |
} |
|
423 |
return unsavedData.toArray(new IUnsavedData[0]); |
|
424 |
} |
|
425 |
|
|
426 |
@Override |
|
427 |
public IMonitorableTask[] getRunningProcesses() { |
|
428 |
return null; |
|
429 |
} |
|
430 |
}; |
|
431 |
} |
|
463 |
@Override |
|
464 |
public IMonitorableTask[] getRunningProcesses() { |
|
465 |
return null; |
|
466 |
} |
|
467 |
}; |
|
468 |
} |
|
432 | 469 |
} |
Also available in: Unified diff