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 @ 70

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

    
166
  }
167

    
168
  public void endEdition(FLyrVect layer, MapControl mapControl) {
169
    if (layer.isEditing()) {
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
      mapControl.getCanceldraw().setCanceled(true);
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

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

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

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

    
217
    JLabel topLabel = new JLabel(translatedQuestion);
218

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

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

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

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

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

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

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

    
257
    resp.add(mainPanel, BorderLayout.CENTER);
258
    resp.add(topLabel, BorderLayout.NORTH);
259
    return resp;
260
  }
261

    
262
  public Service getService(DynObject parameters) throws ServiceException {
263
    // TODO Auto-generated method stub
264
    return null;
265
  }
266

    
267
  /**
268
   * @param mapControl
269
   */
270
  private void initFocus(MapControl mapControl) {
271

    
272
    EditingBehavior editingBehavior;
273
    if (!mapControl.hasTool("VectorEditing")) {
274
      editingBehavior = new DefaultEditingBehavior(mapControl);
275
      mapControl.addBehavior("VectorEditing", (Behavior) editingBehavior);
276

    
277
    }
278
    else {
279
      editingBehavior = (EditingBehavior) mapControl
280
          .getMapTool("VectorEditing");
281
      editingBehavior.cleanBehavior();
282
    }
283

    
284
    mapControl.setTool("VectorEditing");
285
    editingBehavior.showConsole();
286
  }
287

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

    
307
    featureStore.deleteObserver(mapControl);
308
  }
309

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

    
319
    String question = i18nManager
320
        .getTranslation("no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer");
321
    String firstLabel = i18nManager.getTranslation("export");
322
    String firstDesc = i18nManager.getTranslation("export_to_another_format");
323

    
324
    JPanel explanation_panel = getExplanationPanel(question, firstLabel,
325
        firstDesc);
326

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

    
333
    if (resp == JOptionPane.NO_OPTION) {
334
      return DISCARD_CHANGES;
335
    }
336
    else if (resp == JOptionPane.YES_OPTION) {
337
      return EXPORT_LAYER;
338
    }
339
    return CANCEL;
340
  }
341

    
342
  /**
343
   * @param layerName
344
   * @return
345
   */
346
  private int showPanelSaveOrDiscard(String layerName) {
347
    Object[] options = { PluginServices.getText(this, "_Guardar"),
348
        "       " + PluginServices.getText(this, "_Descartar") + "       ",
349
        PluginServices.getText(this, "_Continuar") };
350

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

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

    
365
    if (resp == JOptionPane.YES_OPTION) {
366
      return SAVE_CHANGES;
367
    }
368
    else if (resp == JOptionPane.NO_OPTION) {
369
      return DISCARD_CHANGES;
370
    }
371
    return CANCEL;
372
  }
373
}