Statistics
| Revision:

gvsig-vectorediting / 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 @ 69

History | View | Annotate | Download (11.1 KB)

1
/*
2
 * Copyright 2014 DiSiD Technologies S.L.L. All rights reserved.
3
 * 
4
 * Project  : DiSiD org.gvsig.vectorediting.swing.impl 
5
 * SVN Id   : $Id$
6
 */
7
package org.gvsig.vectorediting.swing.impl;
8

    
9
import java.awt.BorderLayout;
10
import java.awt.Component;
11
import java.awt.Font;
12
import java.awt.GridBagConstraints;
13
import java.awt.GridBagLayout;
14
import java.awt.Insets;
15

    
16
import javax.swing.JLabel;
17
import javax.swing.JOptionPane;
18
import javax.swing.JPanel;
19

    
20
import org.gvsig.andami.PluginServices;
21
import org.gvsig.editing.EditingNotification;
22
import org.gvsig.editing.EditingNotificationManager;
23
import org.gvsig.fmap.dal.feature.FeatureStore;
24
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
25
import org.gvsig.fmap.mapcontrol.MapControl;
26
import org.gvsig.fmap.mapcontrol.MapControlLocator;
27
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dynobject.DynObject;
30
import org.gvsig.tools.i18n.I18nManager;
31
import org.gvsig.tools.service.AbstractManager;
32
import org.gvsig.tools.service.Service;
33
import org.gvsig.tools.service.ServiceException;
34
import org.gvsig.vectorediting.lib.api.EditingManager;
35
import org.gvsig.vectorediting.lib.api.exceptions.EndEditingException;
36
import org.gvsig.vectorediting.lib.api.exceptions.StartEditingException;
37
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException;
38
import org.gvsig.vectorediting.lib.impl.DefaultEditingProviderManager;
39
import org.gvsig.vectorediting.swing.api.EditingBehavior;
40
import org.gvsig.vectorediting.swing.api.EditingSwingManager;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
public class DefaultEditingSwingManager extends AbstractManager implements
45
    EditingSwingManager {
46

    
47
  private static final Logger logger = LoggerFactory
48
      .getLogger(EditingManager.class);
49
  
50
  private I18nManager i18nManager = ToolsLocator.getI18nManager();
51

    
52
  private final int CANCEL = 0;
53

    
54
  private final int SAVE_CHANGES = 1;
55

    
56
  private final int DISCARD_CHANGES = 2;
57

    
58
  private final int EXPORT_LAYER = 3;
59

    
60
  public DefaultEditingSwingManager() {
61
    super(new DefaultEditingProviderManager());
62
    // TODO Auto-generated constructor stub
63
  }
64

    
65
  public void activateTool(String name, MapControl mapControl) {
66
    // TODO Auto-generated method stub
67
    if (mapControl != null && mapControl.hasTool("VectorEditing")) {
68
      EditingBehavior behavior = (EditingBehavior) mapControl
69
          .getMapTool("VectorEditing");
70
      mapControl.setTool("VectorEditing");
71
      behavior.activateTool(name);
72
    }
73
  }
74

    
75
  public void beginEdition(FLyrVect layer, MapControl mapControl) {
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
    initFocus(mapControl);
92

    
93
    try {
94
      layer.getFeatureStore().edit();
95
    }
96
    catch (Exception e) {
97
      logger.error("Can't set featureStore in edit mode",
98
          new VectorEditingException(e));
99
    }
100

    
101
    // layer.getFeatureStore().addObserver(view);
102
    layer.getFeatureStore().addObserver(mapControl);
103

    
104
    editingNotificationManager.notifyObservers(this,
105
        EditingNotification.AFTER_ENTER_EDITING_STORE, null, layer);
106
  }
107

    
108
  /**
109
   * @param layer
110
   * @param mapControl
111
   * @throws EndEditingException
112
   */
113
  private void discardChanges(FLyrVect layer, MapControl mapControl)
114
      throws EndEditingException {
115
    FeatureStore featureStore = layer.getFeatureStore();
116
    try {
117
      featureStore.cancelEditing();
118
      EditingBehavior editingBehavior = (EditingBehavior) mapControl
119
          .getMapTool("VectorEditing");
120
      editingBehavior.cleanBehavior();
121
      editingBehavior.hideConsole();
122
    }
123
    catch (Exception e) {
124
      throw new EndEditingException(e);
125
    }
126

    
127
    featureStore.deleteObserver(mapControl);
128

    
129
  }
130

    
131
  /**
132
   * 
133
   * @param layer
134
   * @param mapControl
135
   * @param option
136
   */
137
  private void doAction(FLyrVect layer, MapControl mapControl, int option) {
138
    switch (option) {
139
      case SAVE_CHANGES:
140
        try {
141
          saveChanges(layer, mapControl);
142
        }
143
        catch (VectorEditingException e) {
144
          logger.error("Changes can not be saved in " + layer.getName(), e);
145
        }
146
        break;
147
      case DISCARD_CHANGES:
148
        try {
149
          discardChanges(layer, mapControl);
150
        }
151
        catch (VectorEditingException e) {
152
          logger.error("Changes can not be discared in " + layer.getName(), e);
153
        }
154
        break;
155
      case EXPORT_LAYER:
156
        try {
157
          exportLayer(layer, mapControl);
158
        }
159
        catch (VectorEditingException e) {
160
          logger.error("Changes can not be imported", e);
161
        }
162
        break;
163
      case CANCEL:
164
        break;
165
    }
166

    
167
  }
168

    
169
  public void endEdition(FLyrVect layer, MapControl mapControl) {
170
    EditingNotificationManager editingNotificationManager = MapControlLocator
171
        .getEditingNotificationManager();
172

    
173
    EditingNotification notification = editingNotificationManager
174
        .notifyObservers(this, EditingNotification.BEFORE_EXIT_EDITING_STORE,
175
            null, layer);
176

    
177
    if (notification.isCanceled()) {
178
      logger.info("Stop edit layer '" + layer.getName()
179
          + "' canceled by somme observer.", new EndEditingException(
180
          "Stop edit layer '" + layer.getName() + "' canceled.", null));
181

    
182
    }
183

    
184
    int option;
185
    if (layer.isWritable()) {
186
      option = showPanelSaveOrDiscard(layer.getName());
187
    }
188
    else {
189
      option = showPanelExportOrDiscard(layer.getName());
190
    }
191

    
192
    doAction(layer, mapControl, option);
193

    
194
    editingNotificationManager.notifyObservers(this,
195
        EditingNotification.AFTER_EXIT_EDITING_STORE, null, layer);
196

    
197
  }
198

    
199
  private void exportLayer(FLyrVect layer, MapControl mapCotrol)
200
      throws EndEditingException {
201
    // TODO Export layer
202
  }
203

    
204
  /**
205
   * @param translatedQuestion
206
   * @param translatedFirstTag
207
   * @param translatedFirstDescription
208
   * @return
209
   */
210
  private JPanel getExplanationPanel(String translatedQuestion,
211
                                     String translatedFirstTag,
212
                                     String translatedFirstDescription) {
213

    
214
    JPanel resp = new JPanel(new BorderLayout(10, 10));
215

    
216
    JLabel topLabel = new JLabel(translatedQuestion);
217

    
218
    JPanel mainPanel = new JPanel(new GridBagLayout());
219
    GridBagConstraints cc = new GridBagConstraints();
220

    
221
    cc.gridx = 0;
222
    cc.gridy = 0;
223
    cc.anchor = GridBagConstraints.WEST;
224

    
225
    cc.insets = new Insets(3, 6, 3, 6);
226

    
227
    Font boldf = mainPanel.getFont().deriveFont(Font.BOLD);
228

    
229
    JLabel lbl = new JLabel(translatedFirstTag);
230
    lbl.setFont(boldf);
231
    mainPanel.add(lbl, cc);
232
    cc.gridx = 1;
233
    mainPanel.add(new JLabel(translatedFirstDescription), cc);
234

    
235
    cc.gridx = 0;
236
    cc.gridy = 1;
237
    lbl = new JLabel(i18nManager.getTranslation("discard"));
238
    lbl.setFont(boldf);
239
    mainPanel.add(lbl, cc);
240
    cc.gridx = 1;
241
    mainPanel
242
        .add(new JLabel(i18nManager.getTranslation("discard_and_loose_changes")), cc);
243

    
244
    cc.gridx = 0;
245
    cc.gridy = 2;
246
    lbl = new JLabel(i18nManager.getTranslation("continue"));
247
    lbl.setFont(boldf);
248
    mainPanel.add(lbl, cc);
249
    cc.gridx = 1;
250
    mainPanel.add(
251
        new JLabel(i18nManager.getTranslation("do_not_save_yet_stay_in_editing_mode")),
252
        cc);
253

    
254
    resp.add(mainPanel, BorderLayout.CENTER);
255
    resp.add(topLabel, BorderLayout.NORTH);
256
    return resp;
257
  }
258

    
259
  public Service getService(DynObject parameters) throws ServiceException {
260
    // TODO Auto-generated method stub
261
    return null;
262
  }
263

    
264
  /**
265
   * @param mapControl
266
   */
267
  private void initFocus(MapControl mapControl) {
268

    
269
    EditingBehavior editingBehavior;
270
    if (!mapControl.hasTool("VectorEditing")) {
271
      editingBehavior = new DefaultEditingBehavior(mapControl);
272
      mapControl.addBehavior("VectorEditing", (Behavior) editingBehavior);
273

    
274
    }
275
    else {
276
      editingBehavior = (EditingBehavior) mapControl
277
          .getMapTool("VectorEditing");
278
      editingBehavior.cleanBehavior();
279
    }
280

    
281
    mapControl.setTool("VectorEditing");
282
    editingBehavior.showConsole();
283
  }
284

    
285
  /**
286
   * @param layer
287
   * @param mapControl
288
   * @throws EndEditingException
289
   */
290
  private void saveChanges(FLyrVect layer, MapControl mapControl)
291
      throws EndEditingException {
292
    FeatureStore featureStore = layer.getFeatureStore();
293
    try {
294
      featureStore.finishEditing();
295
      EditingBehavior editingBehavior = (EditingBehavior) mapControl
296
          .getMapTool("VectorEditing");
297
      editingBehavior.cleanBehavior();
298
      editingBehavior.hideConsole();
299
    }
300
    catch (Exception e) {
301
      throw new EndEditingException(e);
302
    }
303

    
304
    featureStore.deleteObserver(mapControl);
305
  }
306

    
307
  /**
308
   * 
309
   * @param name
310
   * @return
311
   */
312
  private int showPanelExportOrDiscard(String name) {
313
    Object[] options = { PluginServices.getText(this, "export"),
314
        "       " + i18nManager.getTranslation("discard") + "       ",
315
        i18nManager.getTranslation("continue") };
316

    
317
    String question = i18nManager.getTranslation("no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer");
318
    String firstLabel = i18nManager.getTranslation("export");
319
    String firstDesc = i18nManager.getTranslation("export_to_another_format");
320

    
321
    JPanel explanation_panel = getExplanationPanel(question, firstLabel,
322
        firstDesc);
323

    
324
    int resp = JOptionPane.showOptionDialog(
325
        (Component) PluginServices.getMainFrame(), explanation_panel,
326
        i18nManager.getTranslation("stop_edition"),
327
        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
328
        options, options[2]);
329

    
330
    if (resp == JOptionPane.NO_OPTION) {
331
      return DISCARD_CHANGES;
332
    }
333
    else if (resp == JOptionPane.YES_OPTION) {
334
      return EXPORT_LAYER;
335
    }
336
    return CANCEL;
337
  }
338
  
339
  /**
340
   * 
341
   * @param layerName
342
   * @return
343
   */
344
  private int showPanelSaveOrDiscard(String layerName) {
345
    Object[] options = { PluginServices.getText(this, "_Guardar"),
346
        "       " + PluginServices.getText(this, "_Descartar") + "       ",
347
        PluginServices.getText(this, "_Continuar") };
348

    
349
    String question = i18nManager.getTranslation("realmente_desea_guardar_la_capa");
350
    question = question + " '" + layerName + "'?";
351
    String firstLabel = i18nManager.getTranslation("guardar");
352
    String firstDesc = i18nManager.getTranslation("save_changes_performed");
353
    JPanel explanation_panel = getExplanationPanel(question, firstLabel,
354
        firstDesc);
355

    
356
    int resp = JOptionPane.showOptionDialog(
357
        (Component) PluginServices.getMainFrame(), explanation_panel,
358
        PluginServices.getText(this, "stop_edition"),
359
        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
360
        options, options[2]);
361

    
362
    if (resp == JOptionPane.YES_OPTION) {
363
      return SAVE_CHANGES;
364
    }
365
    else if (resp == JOptionPane.NO_OPTION) {
366
      return DISCARD_CHANGES;
367
    }
368
    return CANCEL;
369
  }
370
}