Revision 89 org.gvsig.vectorediting/trunk/org.gvsig.vectorediting/org.gvsig.vectorediting.swing/org.gvsig.vectorediting.swing.impl/src/main/java/org/gvsig/vectorediting/swing/impl/DefaultEditingSwingManager.java
DefaultEditingSwingManager.java | ||
---|---|---|
12 | 12 |
import java.awt.GridBagConstraints; |
13 | 13 |
import java.awt.GridBagLayout; |
14 | 14 |
import java.awt.Insets; |
15 |
import java.util.HashMap; |
|
16 |
import java.util.Map; |
|
15 | 17 |
|
16 | 18 |
import javax.swing.JLabel; |
17 | 19 |
import javax.swing.JOptionPane; |
18 | 20 |
import javax.swing.JPanel; |
19 | 21 |
|
20 |
import org.gvsig.editing.EditingNotification; |
|
21 |
import org.gvsig.editing.EditingNotificationManager; |
|
22 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
23 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
|
24 | 22 |
import org.gvsig.fmap.mapcontrol.MapControl; |
25 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
|
26 |
import org.gvsig.fmap.mapcontrol.tools.CompoundBehavior; |
|
27 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
|
28 | 23 |
import org.gvsig.tools.ToolsLocator; |
29 | 24 |
import org.gvsig.tools.dynobject.DynObject; |
30 | 25 |
import org.gvsig.tools.i18n.I18nManager; |
31 | 26 |
import org.gvsig.tools.service.AbstractManager; |
32 | 27 |
import org.gvsig.tools.service.Service; |
33 | 28 |
import org.gvsig.tools.service.ServiceException; |
34 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
|
35 |
import org.gvsig.vectorediting.lib.api.exceptions.CreateEditingBehaviorException; |
|
36 |
import org.gvsig.vectorediting.lib.api.exceptions.EndEditingException; |
|
37 |
import org.gvsig.vectorediting.lib.api.exceptions.StartEditingException; |
|
38 |
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException; |
|
39 | 29 |
import org.gvsig.vectorediting.lib.impl.DefaultEditingProviderManager; |
40 |
import org.gvsig.vectorediting.swing.api.EditingBehavior;
|
|
30 |
import org.gvsig.vectorediting.swing.api.EditingContext;
|
|
41 | 31 |
import org.gvsig.vectorediting.swing.api.EditingSwingManager; |
42 |
import org.slf4j.Logger; |
|
43 |
import org.slf4j.LoggerFactory; |
|
44 | 32 |
|
45 | 33 |
public class DefaultEditingSwingManager extends AbstractManager implements |
46 | 34 |
EditingSwingManager { |
47 |
|
|
48 |
private static final Logger logger = LoggerFactory |
|
49 |
.getLogger(EditingManager.class); |
|
50 |
|
|
35 |
|
|
51 | 36 |
private I18nManager i18nManager = ToolsLocator.getI18nManager(); |
37 |
|
|
38 |
Map<MapControl, EditingContext> editingContexts = new HashMap<MapControl, EditingContext>(); |
|
52 | 39 |
|
53 |
private final int CANCEL = 0; |
|
54 |
|
|
55 |
private final int SAVE_CHANGES = 1; |
|
56 |
|
|
57 |
private final int DISCARD_CHANGES = 2; |
|
58 |
|
|
59 |
private final int EXPORT_LAYER = 3; |
|
60 |
|
|
61 | 40 |
public DefaultEditingSwingManager() { |
62 | 41 |
super(new DefaultEditingProviderManager()); |
63 | 42 |
} |
64 | 43 |
|
65 |
public void activateService(String name, MapControl mapControl) {
|
|
66 |
if (mapControl != null && mapControl.hasTool("VectorEditing")) {
|
|
67 |
CompoundBehavior editingCompoundBehavior = getEditingCompoundBehavior(mapControl);
|
|
68 |
mapControl.setTool("VectorEditing");
|
|
69 |
EditingBehavior editingBehavior = (EditingBehavior) editingCompoundBehavior.getBehavior(EditingCompoundBehavior.EDITING_INDEX);
|
|
70 |
editingCompoundBehavior.setDrawnBehavior(EditingCompoundBehavior.EDITING_INDEX, true);
|
|
71 |
editingBehavior.activateService(name);
|
|
44 |
public EditingContext getEditingContext(MapControl mapControl) {
|
|
45 |
if(editingContexts.containsKey(mapControl)){
|
|
46 |
return editingContexts.get(mapControl);
|
|
47 |
} else{
|
|
48 |
EditingContext editingContext = new DefaultEditingContext(mapControl);
|
|
49 |
editingContexts.put(mapControl, editingContext);
|
|
50 |
return editingContext;
|
|
72 | 51 |
} |
73 | 52 |
} |
74 |
|
|
75 |
public void beginEdition(FLyrVect layer, MapControl mapControl, Behavior[] additionalBehaviors) { |
|
76 |
|
|
77 |
EditingNotificationManager editingNotificationManager = MapControlLocator |
|
78 |
.getEditingNotificationManager(); |
|
79 |
|
|
80 |
EditingNotification notification = editingNotificationManager |
|
81 |
.notifyObservers(this, EditingNotification.BEFORE_ENTER_EDITING_STORE, |
|
82 |
null, layer); |
|
83 |
|
|
84 |
if (notification.isCanceled()) { |
|
85 |
logger.info("Edit layer '" + layer.getName() |
|
86 |
+ "' canceled by somme observer.", new StartEditingException( |
|
87 |
"Edit layer '" + layer.getName() + "' canceled.", null)); |
|
88 |
|
|
89 |
} |
|
90 |
|
|
91 |
try { |
|
92 |
addBehaviors(mapControl, additionalBehaviors); |
|
93 |
} catch (CreateEditingBehaviorException e1) { |
|
94 |
logger.error(e1.getMessage(), e1); |
|
95 |
} |
|
96 |
|
|
97 |
try { |
|
98 |
layer.getFeatureStore().edit(); |
|
99 |
} |
|
100 |
catch (Exception e) { |
|
101 |
logger.error("Can't set featureStore in edit mode", |
|
102 |
new VectorEditingException(e)); } |
|
103 |
|
|
104 |
// layer.getFeatureStore().addObserver(view); |
|
105 |
layer.getFeatureStore().addObserver(mapControl); |
|
106 |
|
|
107 |
editingNotificationManager.notifyObservers(this, |
|
108 |
EditingNotification.AFTER_ENTER_EDITING_STORE, null, layer); |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* @param layer |
|
113 |
* @param mapControl |
|
114 |
* @throws EndEditingException |
|
115 |
*/ |
|
116 |
private void discardChanges(FLyrVect layer, MapControl mapControl) |
|
117 |
throws EndEditingException { |
|
118 |
FeatureStore featureStore = layer.getFeatureStore(); |
|
119 |
try { |
|
120 |
featureStore.cancelEditing(); |
|
121 |
EditingCompoundBehavior editingCompoundBehavior = getEditingCompoundBehavior(mapControl); |
|
122 |
EditingBehavior editingBehavior = (EditingBehavior) editingCompoundBehavior.getBehavior(EditingCompoundBehavior.EDITING_INDEX); |
|
123 |
editingBehavior.cleanBehavior(); |
|
124 |
editingBehavior.hideConsole(); |
|
125 |
} |
|
126 |
catch (Exception e) { |
|
127 |
throw new EndEditingException(e); |
|
128 |
} |
|
129 |
|
|
130 |
featureStore.deleteObserver(mapControl); |
|
131 |
|
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* @param layer |
|
136 |
* @param mapControl |
|
137 |
* @param option |
|
138 |
*/ |
|
139 |
private void doAction(FLyrVect layer, MapControl mapControl, int option) { |
|
140 |
switch (option) { |
|
141 |
case SAVE_CHANGES: |
|
142 |
try { |
|
143 |
saveChanges(layer, mapControl); |
|
144 |
} |
|
145 |
catch (VectorEditingException e) { |
|
146 |
logger.error("Changes can not be saved in " + layer.getName(), e); |
|
147 |
} |
|
148 |
break; |
|
149 |
case DISCARD_CHANGES: |
|
150 |
try { |
|
151 |
discardChanges(layer, mapControl); |
|
152 |
} |
|
153 |
catch (VectorEditingException e) { |
|
154 |
logger.error("Changes can not be discared in " + layer.getName(), e); |
|
155 |
} |
|
156 |
break; |
|
157 |
case EXPORT_LAYER: |
|
158 |
try { |
|
159 |
exportLayer(layer, mapControl); |
|
160 |
} |
|
161 |
catch (VectorEditingException e) { |
|
162 |
logger.error("Changes can not be imported", e); |
|
163 |
} |
|
164 |
break; |
|
165 |
case CANCEL: |
|
166 |
break; |
|
167 |
} |
|
168 |
|
|
169 |
} |
|
170 |
|
|
171 |
public void endEdition(FLyrVect layer, MapControl mapControl) { |
|
172 |
if (layer.isEditing()) { |
|
173 |
EditingNotificationManager editingNotificationManager = MapControlLocator |
|
174 |
.getEditingNotificationManager(); |
|
175 |
|
|
176 |
EditingNotification notification = editingNotificationManager |
|
177 |
.notifyObservers(this, EditingNotification.BEFORE_EXIT_EDITING_STORE, |
|
178 |
null, layer); |
|
179 |
|
|
180 |
if (notification.isCanceled()) { |
|
181 |
logger.info("Stop edit layer '" + layer.getName() |
|
182 |
+ "' canceled by somme observer.", new EndEditingException( |
|
183 |
"Stop edit layer '" + layer.getName() + "' canceled.", null)); |
|
184 |
|
|
185 |
} |
|
186 |
mapControl.getCanceldraw().setCanceled(true); |
|
187 |
int option; |
|
188 |
if (layer.isWritable()) { |
|
189 |
option = showPanelSaveOrDiscard(mapControl, layer.getName()); |
|
190 |
} |
|
191 |
else { |
|
192 |
option = showPanelExportOrDiscard(mapControl, layer.getName()); |
|
193 |
} |
|
194 |
|
|
195 |
doAction(layer, mapControl, option); |
|
196 |
|
|
197 |
editingNotificationManager.notifyObservers(this, |
|
198 |
EditingNotification.AFTER_EXIT_EDITING_STORE, null, layer); |
|
199 |
|
|
200 |
} |
|
201 |
} |
|
202 |
|
|
203 |
private void exportLayer(FLyrVect layer, MapControl mapCotrol) |
|
204 |
throws EndEditingException { |
|
205 |
// TODO Export layer |
|
206 |
} |
|
207 |
|
|
208 |
/** |
|
209 |
* @param translatedQuestion |
|
210 |
* @param translatedFirstTag |
|
211 |
* @param translatedFirstDescription |
|
212 |
* @return |
|
213 |
*/ |
|
214 |
private JPanel getExplanationPanel(String translatedQuestion, |
|
215 |
String translatedFirstTag, |
|
216 |
String translatedFirstDescription) { |
|
217 |
|
|
53 |
|
|
54 |
private JPanel getExplanationPanel(String question, String action, |
|
55 |
String description) { |
|
218 | 56 |
JPanel resp = new JPanel(new BorderLayout(10, 10)); |
219 | 57 |
|
220 |
JLabel topLabel = new JLabel(translatedQuestion);
|
|
58 |
JLabel topLabel = new JLabel(question);
|
|
221 | 59 |
|
222 | 60 |
JPanel mainPanel = new JPanel(new GridBagLayout()); |
223 | 61 |
GridBagConstraints cc = new GridBagConstraints(); |
... | ... | |
230 | 68 |
|
231 | 69 |
Font boldf = mainPanel.getFont().deriveFont(Font.BOLD); |
232 | 70 |
|
233 |
JLabel lbl = new JLabel(translatedFirstTag);
|
|
71 |
JLabel lbl = new JLabel(action);
|
|
234 | 72 |
lbl.setFont(boldf); |
235 | 73 |
mainPanel.add(lbl, cc); |
236 | 74 |
cc.gridx = 1; |
237 |
mainPanel.add(new JLabel(translatedFirstDescription), cc);
|
|
75 |
mainPanel.add(new JLabel(description), cc);
|
|
238 | 76 |
|
239 | 77 |
cc.gridx = 0; |
240 | 78 |
cc.gridy = 1; |
... | ... | |
261 | 99 |
resp.add(topLabel, BorderLayout.NORTH); |
262 | 100 |
return resp; |
263 | 101 |
} |
264 |
|
|
102 |
|
|
265 | 103 |
public Service getService(DynObject parameters) throws ServiceException { |
266 |
// TODO Auto-generated method stub |
|
267 | 104 |
return null; |
268 | 105 |
} |
269 | 106 |
|
270 |
/** |
|
271 |
* @param mapControl |
|
272 |
* @throws CreateEditingBehaviorException |
|
273 |
*/ |
|
274 |
private void addBehaviors(MapControl mapControl, Behavior[] additionalBehavior) throws CreateEditingBehaviorException { |
|
275 |
|
|
276 |
EditingBehavior editingBehavior; |
|
277 |
EditingCompoundBehavior editingCompoundBehavior; |
|
278 |
if (!mapControl.hasTool("VectorEditing")) { |
|
279 |
editingBehavior = new DefaultEditingBehavior(mapControl); |
|
280 |
editingCompoundBehavior = new EditingCompoundBehavior(editingBehavior); |
|
281 |
((DefaultEditingBehavior)editingBehavior).setCompoundBehavior(editingCompoundBehavior); |
|
282 |
if(additionalBehavior != null){ |
|
283 |
Behavior[] behaviors = new Behavior[additionalBehavior.length+1]; |
|
284 |
behaviors[0] = editingCompoundBehavior; |
|
285 |
for (int i = 0; i < additionalBehavior.length; i++) { |
|
286 |
behaviors[i+1]= additionalBehavior[i]; |
|
287 |
} |
|
288 |
mapControl.addBehavior("VectorEditing",behaviors); |
|
289 |
} else { |
|
290 |
mapControl.addBehavior("VectorEditing",editingCompoundBehavior); |
|
291 |
} |
|
292 |
} |
|
293 |
else { |
|
294 |
editingCompoundBehavior = getEditingCompoundBehavior(mapControl); |
|
295 |
editingBehavior = (EditingBehavior) editingCompoundBehavior.getBehavior(EditingCompoundBehavior.EDITING_INDEX); |
|
296 |
((DefaultEditingBehavior)editingBehavior).setCompoundBehavior(editingCompoundBehavior); |
|
297 |
editingBehavior.cleanBehavior(); |
|
298 |
} |
|
299 |
|
|
300 |
|
|
301 |
mapControl.setTool("VectorEditing"); |
|
302 |
editingBehavior.showConsole(); |
|
303 |
} |
|
304 |
|
|
305 |
/** |
|
306 |
* @param layer |
|
307 |
* @param mapControl |
|
308 |
* @throws EndEditingException |
|
309 |
*/ |
|
310 |
private void saveChanges(FLyrVect layer, MapControl mapControl) |
|
311 |
throws EndEditingException { |
|
312 |
FeatureStore featureStore = layer.getFeatureStore(); |
|
313 |
try { |
|
314 |
featureStore.finishEditing(); |
|
315 |
EditingBehavior editingBehavior; |
|
316 |
|
|
317 |
EditingCompoundBehavior editingCompoundBehavior = getEditingCompoundBehavior(mapControl); |
|
318 |
|
|
319 |
editingBehavior = (EditingBehavior) editingCompoundBehavior.getBehavior(EditingCompoundBehavior.EDITING_INDEX); |
|
320 |
|
|
321 |
editingBehavior.cleanBehavior(); |
|
322 |
editingBehavior.hideConsole(); |
|
323 |
} |
|
324 |
catch (Exception e) { |
|
325 |
throw new EndEditingException(e); |
|
326 |
} |
|
327 |
|
|
328 |
featureStore.deleteObserver(mapControl); |
|
329 |
} |
|
330 |
|
|
331 |
private EditingCompoundBehavior getEditingCompoundBehavior(MapControl mapControl) { |
|
332 |
EditingCompoundBehavior editingCompoundBehavior; |
|
333 |
|
|
334 |
CompoundBehavior compoundBehavior = (CompoundBehavior) mapControl.getMapTool("VectorEditing"); |
|
335 |
if(compoundBehavior instanceof EditingCompoundBehavior){ |
|
336 |
editingCompoundBehavior = (EditingCompoundBehavior) compoundBehavior; |
|
337 |
} else { |
|
338 |
editingCompoundBehavior = (EditingCompoundBehavior) compoundBehavior.getBehavior(0); |
|
339 |
} |
|
340 |
return editingCompoundBehavior; |
|
341 |
} |
|
342 |
|
|
343 |
/** |
|
344 |
* @param name |
|
345 |
* @return |
|
346 |
*/ |
|
347 |
private int showPanelExportOrDiscard(Component root, String name) { |
|
107 |
public int showPanelExportOrDiscard(MapControl parent, String name) { |
|
348 | 108 |
Object[] options = { i18nManager.getTranslation("export"), |
349 | 109 |
" " + i18nManager.getTranslation("discard") + " ", |
350 | 110 |
i18nManager.getTranslation("continue") }; |
... | ... | |
358 | 118 |
firstDesc); |
359 | 119 |
|
360 | 120 |
int resp = JOptionPane.showOptionDialog( |
361 |
root, explanation_panel,
|
|
121 |
(Component) parent, explanation_panel,
|
|
362 | 122 |
i18nManager.getTranslation("stop_edition"), |
363 | 123 |
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, |
364 | 124 |
options, options[2]); |
365 | 125 |
|
366 | 126 |
if (resp == JOptionPane.NO_OPTION) { |
367 |
return DISCARD_CHANGES; |
|
127 |
return EditingContext.DISCARD_CHANGES;
|
|
368 | 128 |
} |
369 | 129 |
else if (resp == JOptionPane.YES_OPTION) { |
370 |
return EXPORT_LAYER; |
|
130 |
return EditingContext.EXPORT_LAYER;
|
|
371 | 131 |
} |
372 |
return CANCEL; |
|
132 |
return EditingContext.CANCEL;
|
|
373 | 133 |
} |
374 | 134 |
|
375 |
/** |
|
376 |
* @param layerName |
|
377 |
* @return |
|
378 |
*/ |
|
379 |
private int showPanelSaveOrDiscard(Component root, String layerName) { |
|
135 |
public int showPanelSaveOrDiscard(MapControl parent, String name) { |
|
380 | 136 |
Object[] options = { i18nManager.getTranslation("_Guardar"), |
381 | 137 |
" " + i18nManager.getTranslation("_Descartar") + " ", |
382 | 138 |
i18nManager.getTranslation("_Continuar") }; |
383 | 139 |
|
384 | 140 |
String question = i18nManager |
385 | 141 |
.getTranslation("realmente_desea_guardar_la_capa"); |
386 |
question = question + " '" + layerName + "'?";
|
|
142 |
question = question + " '" + name + "'?";
|
|
387 | 143 |
String firstLabel = i18nManager.getTranslation("guardar"); |
388 | 144 |
String firstDesc = i18nManager.getTranslation("save_changes_performed"); |
389 | 145 |
JPanel explanation_panel = getExplanationPanel(question, firstLabel, |
390 | 146 |
firstDesc); |
391 | 147 |
|
392 | 148 |
int resp = JOptionPane.showOptionDialog( |
393 |
root, explanation_panel,
|
|
149 |
(Component) parent, explanation_panel,
|
|
394 | 150 |
i18nManager.getTranslation("stop_edition"), |
395 | 151 |
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, |
396 | 152 |
options, options[2]); |
397 | 153 |
|
398 | 154 |
if (resp == JOptionPane.YES_OPTION) { |
399 |
return SAVE_CHANGES; |
|
155 |
return EditingContext.SAVE_CHANGES;
|
|
400 | 156 |
} |
401 | 157 |
else if (resp == JOptionPane.NO_OPTION) { |
402 |
return DISCARD_CHANGES; |
|
158 |
return EditingContext.DISCARD_CHANGES;
|
|
403 | 159 |
} |
404 |
return CANCEL; |
|
160 |
return EditingContext.CANCEL;
|
|
405 | 161 |
} |
406 | 162 |
} |
Also available in: Unified diff