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

History | View | Annotate | Download (13.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.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
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
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.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
import org.gvsig.vectorediting.lib.impl.DefaultEditingProviderManager;
40
import org.gvsig.vectorediting.swing.api.EditingBehavior;
41
import org.gvsig.vectorediting.swing.api.EditingSwingManager;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
public class DefaultEditingSwingManager extends AbstractManager implements
46
    EditingSwingManager {
47

    
48
  private static final Logger logger = LoggerFactory
49
      .getLogger(EditingManager.class);
50

    
51
  private I18nManager i18nManager = ToolsLocator.getI18nManager();
52

    
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
  public DefaultEditingSwingManager() {
62
    super(new DefaultEditingProviderManager());
63
  }
64

    
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);
72
    }
73
  }
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

    
218
    JPanel resp = new JPanel(new BorderLayout(10, 10));
219

    
220
    JLabel topLabel = new JLabel(translatedQuestion);
221

    
222
    JPanel mainPanel = new JPanel(new GridBagLayout());
223
    GridBagConstraints cc = new GridBagConstraints();
224

    
225
    cc.gridx = 0;
226
    cc.gridy = 0;
227
    cc.anchor = GridBagConstraints.WEST;
228

    
229
    cc.insets = new Insets(3, 6, 3, 6);
230

    
231
    Font boldf = mainPanel.getFont().deriveFont(Font.BOLD);
232

    
233
    JLabel lbl = new JLabel(translatedFirstTag);
234
    lbl.setFont(boldf);
235
    mainPanel.add(lbl, cc);
236
    cc.gridx = 1;
237
    mainPanel.add(new JLabel(translatedFirstDescription), cc);
238

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

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

    
260
    resp.add(mainPanel, BorderLayout.CENTER);
261
    resp.add(topLabel, BorderLayout.NORTH);
262
    return resp;
263
  }
264

    
265
  public Service getService(DynObject parameters) throws ServiceException {
266
    // TODO Auto-generated method stub
267
    return null;
268
  }
269

    
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) {
348
    Object[] options = { i18nManager.getTranslation("export"),
349
        "       " + i18nManager.getTranslation("discard") + "       ",
350
        i18nManager.getTranslation("continue") };
351

    
352
    String question = i18nManager
353
        .getTranslation("no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer");
354
    String firstLabel = i18nManager.getTranslation("export");
355
    String firstDesc = i18nManager.getTranslation("export_to_another_format");
356

    
357
    JPanel explanation_panel = getExplanationPanel(question, firstLabel,
358
        firstDesc);
359

    
360
    int resp = JOptionPane.showOptionDialog(
361
        root, explanation_panel,
362
        i18nManager.getTranslation("stop_edition"),
363
        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
364
        options, options[2]);
365

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

    
375
  /**
376
   * @param layerName
377
   * @return
378
   */
379
  private int showPanelSaveOrDiscard(Component root, String layerName) {
380
    Object[] options = { i18nManager.getTranslation("_Guardar"),
381
        "       " + i18nManager.getTranslation("_Descartar") + "       ",
382
        i18nManager.getTranslation("_Continuar") };
383

    
384
    String question = i18nManager
385
        .getTranslation("realmente_desea_guardar_la_capa");
386
    question = question + " '" + layerName + "'?";
387
    String firstLabel = i18nManager.getTranslation("guardar");
388
    String firstDesc = i18nManager.getTranslation("save_changes_performed");
389
    JPanel explanation_panel = getExplanationPanel(question, firstLabel,
390
        firstDesc);
391

    
392
    int resp = JOptionPane.showOptionDialog(
393
        root, explanation_panel,
394
        i18nManager.getTranslation("stop_edition"),
395
        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
396
        options, options[2]);
397

    
398
    if (resp == JOptionPane.YES_OPTION) {
399
      return SAVE_CHANGES;
400
    }
401
    else if (resp == JOptionPane.NO_OPTION) {
402
      return DISCARD_CHANGES;
403
    }
404
    return CANCEL;
405
  }
406
}