Revision 41857 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.editing.app/org.gvsig.editing.app.mainplugin/src/main/java/org/gvsig/editing/StopEditing.java

View differences:

StopEditing.java
10 10
 *
11 11
 * This program is distributed in the hope that it will be useful,
12 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14
 * GNU General Public License for more details.
15 15
 *
16 16
 * You should have received a copy of the GNU General Public License
17 17
 * along with this program; if not, write to the Free Software
18 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
19
 * MA 02110-1301, USA.
20 20
 *
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
......
79 79

  
80 80
/**
81 81
 * @author Francisco Jos?
82
 * 
83
 *         Cuando un tema se pone en edici?n, puede que su driver implemente
84
 *         ISpatialWriter. En ese caso, es capaz de guardarse sobre s? mismo. Si
85
 *         no lo implementa, esta opci?n estar? deshabilitada y la ?nica
86
 *         posibilidad de guardar este tema ser? "Guardando como..."
87
 *         
82
 *
83
 * Cuando un tema se pone en edici?n, puede que su driver implemente
84
 * ISpatialWriter. En ese caso, es capaz de guardarse sobre s? mismo. Si
85
 * no lo implementa, esta opci?n estar? deshabilitada y la ?nica
86
 * posibilidad de guardar este tema ser? "Guardando como..."
87
 *
88 88
 */
89 89
public class StopEditing extends Extension implements IExtensionExecuteWithArgs {
90
    
90

  
91 91
    private final static Logger logger = LoggerFactory.getLogger(StopEditing.class);
92
    
93
	private DefaultViewPanel vista;
94 92

  
95
	/**
96
	 * @see org.gvsig.andami.plugins.IExtension#initialize()
97
	 */
98
	public void initialize() {
99
	}
93
    private DefaultViewPanel vista;
100 94

  
101
	public void execute(String s) {
102
		this.execute(s, null);
103
	}
95
    /**
96
     * @see org.gvsig.andami.plugins.IExtension#initialize()
97
     */
98
    public void initialize() {
99
    }
104 100

  
105
	public void execute(String s, Object[] args) {
106
		org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices
107
				.getMDIManager().getActiveWindow();
101
    public void execute(String s) {
102
        this.execute(s, null);
103
    }
108 104

  
109
		vista = (DefaultViewPanel) f;
110
		boolean isStop = false;
111
		ViewDocument model = vista.getModel();
112
		MapContext mapa = model.getMapContext();
113
		FLayers layers = mapa.getLayers();
114
		IEditionManager edMan = EditionLocator.getEditionManager();
105
    public void execute(String s, Object[] args) {
106
        org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices
107
                .getMDIManager().getActiveWindow();
115 108

  
116
		if (s.equals("layer-stop-editing")) {
117
			
118
        	FLayer[] selectedItems = null;
119
    		// Desde el menu contextual del TOC se le pasan las capas seleccionadas como primer argumento.
120
        	if( args!=null && args.length>0 && args[0] instanceof FLayer[] ) {
121
        		selectedItems = (FLayer[]) args[0];
122
        	}
109
        vista = (DefaultViewPanel) f;
110
        boolean isStop = false;
111
        ViewDocument model = vista.getModel();
112
        MapContext mapa = model.getMapContext();
113
        FLayers layers = mapa.getLayers();
114
        IEditionManager edMan = EditionLocator.getEditionManager();
123 115

  
124
			vista.getMapControl().getCanceldraw().setCanceled(true);
125
			FLayer[] actives = layers.getActives();
116
        if ( s.equals("layer-stop-editing") ) {
117

  
118
            FLayer[] selectedItems = null;
119
            // Desde el menu contextual del TOC se le pasan las capas seleccionadas como primer argumento.
120
            if ( args != null && args.length > 0 && args[0] instanceof FLayer[] ) {
121
                selectedItems = (FLayer[]) args[0];
122
            }
123

  
124
            vista.getMapControl().getCanceldraw().setCanceled(true);
125
            FLayer[] actives = layers.getActives();
126 126
			// TODO: Comprobar que solo hay una activa, o al menos
127
			// que solo hay una en edici?n que est? activa, etc, etc
128
			for (int i = 0; i < actives.length; i++) {
129
				if (actives[i] instanceof FLyrVect && actives[i].isEditing()) {
130
					FLyrVect lv = (FLyrVect) actives[i];
131
					MapControl mapControl = vista.getMapControl();
127
            // que solo hay una en edici?n que est? activa, etc, etc
128
            for ( int i = 0; i < actives.length; i++ ) {
129
                if ( actives[i] instanceof FLyrVect && actives[i].isEditing() ) {
130
                    FLyrVect lv = (FLyrVect) actives[i];
131
                    MapControl mapControl = vista.getMapControl();
132 132

  
133
					int user_opt = confirmStop(lv, mapControl);
134
					if (user_opt != IEditionManager.CONTINUE_EDITING) {
135
					    isStop = true;
136
					}
137
					
138
					try {
139
	                    edMan.stopEditLayer(vista, lv, user_opt);
140
					} catch (CancelException ex) {
141
						// Do nothing
142
		            	isStop = false;
143
					} catch (Exception ex) {
144
						
145
						/*
146
						 * Unable to end editing, inform user
147
						 * of the problem. Editing session will continue.
148
						 */
149
		            	showUnableToEndEditingDialog(ex);
150
		            	isStop = false;
151
					    logger.info("While stopping layer editing.", ex);
152
					}
153
				}
154
			}
155
			
156
			if (isStop) {
157
				CADExtension.clearView();
158
				/*
159
				 * Make grid not visible and remove snapping to grid
160
				 * and disable snappers
161
				 */
162
				disableGridAndSnappers(vista.getMapControl());
163
			}
164
		}
165
		PluginServices.getMainFrame().enableControls();
166
	}
167
	
168
	
169
	private void disableGridAndSnappers(MapControl mco) {
170
		mco.setGridVisibility(false);
171
		mco.setAdjustGrid(false);
133
                    int user_opt = confirmStop(lv, mapControl);
134
                    if ( user_opt != IEditionManager.CONTINUE_EDITING ) {
135
                        isStop = true;
136
                    }
137

  
138
                    try {
139
                        edMan.stopEditLayer(vista, lv, user_opt);
140
                    } catch (CancelException ex) {
141
                        // Do nothing
142
                        isStop = false;
143
                    } catch (Exception ex) {
144

  
145
                        /*
146
                         * Unable to end editing, inform user
147
                         * of the problem. Editing session will continue.
148
                         */
149
                        showUnableToEndEditingDialog(ex);
150
                        isStop = false;
151
                        logger.info("While stopping layer editing.", ex);
152
                    }
153
                }
154
            }
155

  
156
            if ( isStop ) {
157
                CADExtension.clearView();
158
                /*
159
                 * Make grid not visible and remove snapping to grid
160
                 * and disable snappers
161
                 */
162
                disableGridAndSnappers(vista.getMapControl());
163
            }
164
        }
165
        PluginServices.getMainFrame().enableControls();
166
    }
167

  
168
    private void disableGridAndSnappers(MapControl mco) {
169
        mco.setGridVisibility(false);
170
        mco.setAdjustGrid(false);
172 171
        Preferences prefs = GridPage.prefs;
173 172
        prefs.putBoolean("grid.showgrid", false);
174 173
        prefs.getBoolean("grid.adjustgrid", false);
......
182 181
        List removelist = new ArrayList();
183 182
        FLyrVect item = null;
184 183
        Iterator snapiter = mco.getMapContext().getLayersToSnap().iterator();
185
        while (snapiter.hasNext()) {
186
        	item = (FLyrVect) snapiter.next();
187
        	if (!item.isEditing()) {
188
        		/*
189
        		 * Get list of layers to be removed from the list
190
        		 */
191
        		removelist.add(item);
192
        	}
184
        while ( snapiter.hasNext() ) {
185
            item = (FLyrVect) snapiter.next();
186
            if ( !item.isEditing() ) {
187
                /*
188
                 * Get list of layers to be removed from the list
189
                 */
190
                removelist.add(item);
191
            }
193 192
        }
194 193
        /*
195 194
         * Remove layers from list
196 195
         */
197 196
        mco.getMapContext().getLayersToSnap().removeAll(removelist);
198
        
199
        
200
	}
201 197

  
202
	
198
    }
203 199

  
204
	private void showUnableToEndEditingDialog(Exception ex) {
205
		
206
		String msg = Messages.getText(
207
				"_Unable_to_save_edits_The_cause_is");
208
		msg = msg + ":\n\n" + EditionUtilities.getLastMessage(ex);
209
		JOptionPane.showMessageDialog(
210
				ApplicationLocator.getManager().getRootComponent(),
211
				msg,
212
				Messages.getText("_Guardar"),
213
				JOptionPane.ERROR_MESSAGE);
214
		
215
	}
200
    private void showUnableToEndEditingDialog(Exception ex) {
216 201

  
217
	/**
218
	 * @see org.gvsig.andami.plugins.IExtension#isEnabled()
219
	 */
220
	public boolean isEnabled() {
221
		FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
222
		if (lyrs == null) {
223
			return false;
224
		}
225
		FLyrVect lyrVect = (FLyrVect) lyrs[0];
226
		if (lyrVect.isEditing()) {
227
			return true;
228
		}
229
		return false;
230
	}
202
        String msg = Messages.getText(
203
                "_Unable_to_save_edits_The_cause_is");
204
        msg = msg + ":\n\n" + EditionUtilities.getLastMessage(ex);
205
        JOptionPane.showMessageDialog(
206
                ApplicationLocator.getManager().getRootComponent(),
207
                msg,
208
                Messages.getText("_Guardar"),
209
                JOptionPane.ERROR_MESSAGE);
231 210

  
232
	/**
233
	 * DOCUMENT ME!
234
	 */
235
	
236
	/*
237
	public boolean stopEditing(FLyrVect layer, MapControl mapControl) {
238
		int resp = JOptionPane.CANCEL_OPTION;
211
    }
239 212

  
240
		try {
241
			if (layer.isWritable()) {
242
				Object[] options = {
243
						PluginServices.getText(this, "_Guardar"),
244
						"       " + PluginServices.getText(this, "_Descartar") + "       ",
245
						PluginServices.getText(this, "_Continuar") };
213
    /**
214
     * @see org.gvsig.andami.plugins.IExtension#isEnabled()
215
     */
216
    public boolean isEnabled() {
217
        FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
218
        if ( lyrs == null ) {
219
            return false;
220
        }
221
        FLyrVect lyrVect = (FLyrVect) lyrs[0];
222
        if ( lyrVect.isEditing() ) {
223
            return true;
224
        }
225
        return false;
226
    }
227

  
228
    /**
229
     * DOCUMENT ME!
230
     */
231
    /*
232
     public boolean stopEditing(FLyrVect layer, MapControl mapControl) {
233
     int resp = JOptionPane.CANCEL_OPTION;
234

  
235
     try {
236
     if (layer.isWritable()) {
237
     Object[] options = {
238
     PluginServices.getText(this, "_Guardar"),
239
     "       " + PluginServices.getText(this, "_Descartar") + "       ",
240
     PluginServices.getText(this, "_Continuar") };
246 241
				
247
				JPanel explanation_panel = getExplanationPanel(layer.getName());
242
     JPanel explanation_panel = getExplanationPanel(layer.getName());
248 243

  
249
				resp = JOptionPane
250
						.showOptionDialog(
251
								(Component) PluginServices.getMainFrame(),
252
								explanation_panel,
253
								PluginServices.getText(this, "stop_edition"),
254
								JOptionPane.YES_NO_CANCEL_OPTION,
255
								JOptionPane.QUESTION_MESSAGE, null, options,
256
								options[2]);
244
     resp = JOptionPane
245
     .showOptionDialog(
246
     (Component) PluginServices.getMainFrame(),
247
     explanation_panel,
248
     PluginServices.getText(this, "stop_edition"),
249
     JOptionPane.YES_NO_CANCEL_OPTION,
250
     JOptionPane.QUESTION_MESSAGE, null, options,
251
     options[2]);
257 252
				
258
				if (resp == JOptionPane.YES_OPTION) {
259
				    // SAVE
260
					saveLayer(layer);
261
					// layer.setEditing(false);
262
                    return true;
263
				} else if (resp == JOptionPane.NO_OPTION) {
264
				    // CANCEL EDITING
265
					cancelEdition(layer);
266
					// layer.setEditing(false);
267
					return true;
268
				} else if (resp == JOptionPane.CANCEL_OPTION) {
269
				    // CONTINUE EDITING
270
				    return false;
271
				} else {
272
				    // This happens when user clicks on [x]
273
				    // to abruptly close previous JOptionPane dialog
274
				    // We make it equivalent to CONTINUE EDITING
275
                    return false;
276
				}
253
     if (resp == JOptionPane.YES_OPTION) {
254
     // SAVE
255
     saveLayer(layer);
256
     // layer.setEditing(false);
257
     return true;
258
     } else if (resp == JOptionPane.NO_OPTION) {
259
     // CANCEL EDITING
260
     cancelEdition(layer);
261
     // layer.setEditing(false);
262
     return true;
263
     } else if (resp == JOptionPane.CANCEL_OPTION) {
264
     // CONTINUE EDITING
265
     return false;
266
     } else {
267
     // This happens when user clicks on [x]
268
     // to abruptly close previous JOptionPane dialog
269
     // We make it equivalent to CONTINUE EDITING
270
     return false;
271
     }
277 272
				
278
			}
279
			// ========================================
280
			// Layer cannot save changes:
273
     }
274
     // ========================================
275
     // Layer cannot save changes:
281 276
			
282
			Object[] options = {
283
					PluginServices.getText(this, "export"),
284
					PluginServices.getText(this,
285
							"finish_editing_without_saving_changes"),
286
					PluginServices.getText(this, "continue_editing") };
277
     Object[] options = {
278
     PluginServices.getText(this, "export"),
279
     PluginServices.getText(this,
280
     "finish_editing_without_saving_changes"),
281
     PluginServices.getText(this, "continue_editing") };
287 282

  
288
			resp = JOptionPane
289
					.showOptionDialog(
290
							(Component) PluginServices.getMainFrame(),
291
							PluginServices
292
									.getText(
293
											this,
294
											"no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer")
295
									+ " : " + layer.getName(), PluginServices
296
									.getText(this, "stop_edition"),
297
							JOptionPane.YES_NO_CANCEL_OPTION,
298
							JOptionPane.QUESTION_MESSAGE, null, options,
299
							options[2]);
300
			if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
301
				cancelEdition(layer);
302
				// layer.setEditing(false);
303
				return true;
304
			} else if (resp == JOptionPane.YES_OPTION) {
305
				int status = exportLayer(layer);
306
				if (status == JOptionPane.OK_OPTION) {
307
					cancelEdition(layer);
308
					// layer.setEditing(false);
309
				}
310
			}
283
     resp = JOptionPane
284
     .showOptionDialog(
285
     (Component) PluginServices.getMainFrame(),
286
     PluginServices
287
     .getText(
288
     this,
289
     "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer")
290
     + " : " + layer.getName(), PluginServices
291
     .getText(this, "stop_edition"),
292
     JOptionPane.YES_NO_CANCEL_OPTION,
293
     JOptionPane.QUESTION_MESSAGE, null, options,
294
     options[2]);
295
     if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
296
     cancelEdition(layer);
297
     // layer.setEditing(false);
298
     return true;
299
     } else if (resp == JOptionPane.YES_OPTION) {
300
     int status = exportLayer(layer);
301
     if (status == JOptionPane.OK_OPTION) {
302
     cancelEdition(layer);
303
     // layer.setEditing(false);
304
     }
305
     }
311 306

  
312
		} catch (ReadException e) {
313
			NotificationManager.addError(e);
314
		} catch (CancelEditingLayerException e) {
315
			NotificationManager.addError(e);
316
		}
317
		return false;
307
     } catch (ReadException e) {
308
     NotificationManager.addError(e);
309
     } catch (CancelEditingLayerException e) {
310
     NotificationManager.addError(e);
311
     }
312
     return false;
318 313

  
319
	}
320
	*/
314
     }
315
     */
316
    public int confirmStop(FLyrVect layer, MapControl mapControl) {
317
        int resp = JOptionPane.CANCEL_OPTION;
321 318

  
322
	
323
	   public int confirmStop(FLyrVect layer, MapControl mapControl) {
324
	        int resp = JOptionPane.CANCEL_OPTION;
319
        try {
320
            if ( layer.isWritable() ) {
321
                Object[] options = {
322
                    PluginServices.getText(this, "_Guardar"),
323
                    "       " + PluginServices.getText(this, "_Descartar") + "       ",
324
                    PluginServices.getText(this, "_Continuar")};
325 325

  
326
	        try {
327
	            if (layer.isWritable()) {
328
	                Object[] options = {
329
	                        PluginServices.getText(this, "_Guardar"),
330
	                        "       " + PluginServices.getText(this, "_Descartar") + "       ",
331
	                        PluginServices.getText(this, "_Continuar") };
332
	                
333
	                
334
	                String question = Messages.getText("realmente_desea_guardar_la_capa");
335
	                question = question + " '" + layer.getName() + "'?";
336
	                String firstLabel = Messages.getText("_Guardar");
337
	                String firstDesc = Messages.getText("_Save_changes_performed");
338
	                
339
	                JPanel explanation_panel = getExplanationPanel(
340
	                    question, firstLabel, firstDesc);
326
                String question = Messages.getText("realmente_desea_guardar_la_capa");
327
                question = question + " '" + layer.getName() + "'?";
328
                String firstLabel = Messages.getText("_Guardar");
329
                String firstDesc = Messages.getText("_Save_changes_performed");
341 330

  
342
	                resp = JOptionPane
343
	                        .showOptionDialog(
344
	                                (Component) PluginServices.getMainFrame(),
345
	                                explanation_panel,
346
	                                PluginServices.getText(this, "stop_edition"),
347
	                                JOptionPane.YES_NO_CANCEL_OPTION,
348
	                                JOptionPane.QUESTION_MESSAGE, null, options,
349
	                                options[2]);
350
	                
351
	                if (resp == JOptionPane.YES_OPTION) {
352
	                    return IEditionManager.ACCEPT_EDITING;
353
	                } else if (resp == JOptionPane.NO_OPTION) {
354
                        return IEditionManager.CANCEL_EDITING;
355
	                } else if (resp == JOptionPane.CANCEL_OPTION) {
356
                        return IEditionManager.CONTINUE_EDITING;
357
	                } else {
331
                JPanel explanation_panel = getExplanationPanel(
332
                        question, firstLabel, firstDesc);
333

  
334
                resp = JOptionPane
335
                        .showOptionDialog(
336
                                (Component) PluginServices.getMainFrame(),
337
                                explanation_panel,
338
                                PluginServices.getText(this, "stop_edition"),
339
                                JOptionPane.YES_NO_CANCEL_OPTION,
340
                                JOptionPane.QUESTION_MESSAGE, null, options,
341
                                options[2]);
342

  
343
                if ( resp == JOptionPane.YES_OPTION ) {
344
                    return IEditionManager.ACCEPT_EDITING;
345
                } else if ( resp == JOptionPane.NO_OPTION ) {
346
                    return IEditionManager.CANCEL_EDITING;
347
                } else if ( resp == JOptionPane.CANCEL_OPTION ) {
348
                    return IEditionManager.CONTINUE_EDITING;
349
                } else {
358 350
	                    // This happens when user clicks on [x]
359
	                    // to abruptly close previous JOptionPane dialog
360
	                    // We make it equivalent to CONTINUE EDITING
361
                        return IEditionManager.CONTINUE_EDITING;
362
	                }
363
	                
364
	            }
351
                    // to abruptly close previous JOptionPane dialog
352
                    // We make it equivalent to CONTINUE EDITING
353
                    return IEditionManager.CONTINUE_EDITING;
354
                }
355

  
356
            }
365 357
	            // ========================================
366
	            // Layer cannot save changes:
367
	            
368
                Object[] options = {
369
                    PluginServices.getText(this, "export"),
370
                    "       " + PluginServices.getText(this, "_Descartar") + "       ",
371
                    PluginServices.getText(this, "_Continuar") };
372
                
373
                String question = Messages.getText(
358
            // Layer cannot save changes:
359

  
360
            Object[] options = {
361
                PluginServices.getText(this, "export"),
362
                "       " + PluginServices.getText(this, "_Descartar") + "       ",
363
                PluginServices.getText(this, "_Continuar")};
364

  
365
            String question = Messages.getText(
374 366
                    "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer");
375
                String firstLabel = Messages.getText("export");
376
                String firstDesc = Messages.getText("_Export_to_another_format");
377
                
378
                JPanel explanation_panel = getExplanationPanel(
367
            String firstLabel = Messages.getText("export");
368
            String firstDesc = Messages.getText("_Export_to_another_format");
369

  
370
            JPanel explanation_panel = getExplanationPanel(
379 371
                    question, firstLabel, firstDesc);
380 372

  
381
	            resp = JOptionPane
382
	                    .showOptionDialog(
383
	                            (Component) PluginServices.getMainFrame(),
384
	                            explanation_panel,
385
	                            PluginServices.getText(this, "stop_edition"),
386
	                            JOptionPane.YES_NO_CANCEL_OPTION,
387
	                            JOptionPane.QUESTION_MESSAGE, null, options,
388
	                            options[2]);
389
	            if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
373
            resp = JOptionPane
374
                    .showOptionDialog(
375
                            (Component) PluginServices.getMainFrame(),
376
                            explanation_panel,
377
                            PluginServices.getText(this, "stop_edition"),
378
                            JOptionPane.YES_NO_CANCEL_OPTION,
379
                            JOptionPane.QUESTION_MESSAGE, null, options,
380
                            options[2]);
381
            if ( resp == JOptionPane.NO_OPTION ) { // CANCEL EDITING
382
                return IEditionManager.CANCEL_EDITING;
383
            } else if ( resp == JOptionPane.YES_OPTION ) {
384
                int status = exportLayer(layer);
385
                if ( status == JOptionPane.OK_OPTION ) {
390 386
                    return IEditionManager.CANCEL_EDITING;
391
	            } else if (resp == JOptionPane.YES_OPTION) {
392
	                int status = exportLayer(layer);
393
	                if (status == JOptionPane.OK_OPTION) {
394
	                    return IEditionManager.CANCEL_EDITING;
395
	                }
396
	            }
387
                }
388
            }
397 389

  
398
	        } catch (ReadException e) {
399
	            NotificationManager.addError(e);
400
	        }
401
            return IEditionManager.CONTINUE_EDITING;
390
        } catch (ReadException e) {
391
            NotificationManager.addError(e);
392
        }
393
        return IEditionManager.CONTINUE_EDITING;
402 394

  
395
    }
403 396

  
404
	    }
405

  
406

  
407 397
    private JPanel getExplanationPanel(
408
        String translatedQuestion,
409
        String translatedFirstTag,
410
        String translatedFirstDescription) {
411
        
398
            String translatedQuestion,
399
            String translatedFirstTag,
400
            String translatedFirstDescription) {
401

  
412 402
        JPanel resp = new JPanel(new BorderLayout(10, 10));
413
        
403

  
414 404
        JLabel topLabel = new JLabel(translatedQuestion);
415
        
405

  
416 406
        JPanel mainPanel = new JPanel(new GridBagLayout());
417 407
        GridBagConstraints cc = new GridBagConstraints();
418
        
408

  
419 409
        cc.gridx = 0;
420 410
        cc.gridy = 0;
421 411
        cc.anchor = GridBagConstraints.WEST;
422
        
412

  
423 413
        cc.insets = new Insets(3, 6, 3, 6);
424
        
414

  
425 415
        Font boldf = mainPanel.getFont().deriveFont(Font.BOLD);
426
        
416

  
427 417
        JLabel lbl = new JLabel(translatedFirstTag);
428 418
        lbl.setFont(boldf);
429 419
        mainPanel.add(lbl, cc);
430 420
        cc.gridx = 1;
431 421
        mainPanel.add(new JLabel(translatedFirstDescription), cc);
432
        
422

  
433 423
        cc.gridx = 0;
434 424
        cc.gridy = 1;
435 425
        lbl = new JLabel(Messages.getText("_Descartar"));
......
450 440
        resp.add(topLabel, BorderLayout.NORTH);
451 441
        return resp;
452 442
    }
453
    
454 443

  
444

  
455 445
    /*
456
    private void saveLayer(FLyrVect layer) throws ReadException {
457
		FeatureStore featureStore = layer.getFeatureStore();
458
		try {
459
			featureStore.finishEditing();
460
		} catch (WriteException e) {
461
			throw new ReadException(featureStore.getName(), e);
462
		} catch (DataException e) {
463
			throw new ReadException(featureStore.getName(), e);
464
		}
465
	}
446
     private void saveLayer(FLyrVect layer) throws ReadException {
447
     FeatureStore featureStore = layer.getFeatureStore();
448
     try {
449
     featureStore.finishEditing();
450
     } catch (WriteException e) {
451
     throw new ReadException(featureStore.getName(), e);
452
     } catch (DataException e) {
453
     throw new ReadException(featureStore.getName(), e);
454
     }
455
     }
466 456

  
467
	private void cancelEdition(FLyrVect layer)
468
			throws CancelEditingLayerException {
469
		FeatureStore featureStore = null;
470
		try {
471
			featureStore = layer.getFeatureStore();
457
     private void cancelEdition(FLyrVect layer)
458
     throws CancelEditingLayerException {
459
     FeatureStore featureStore = null;
460
     try {
461
     featureStore = layer.getFeatureStore();
472 462

  
473
			featureStore.cancelEditing();
474
		} catch (ReadException e) {
475
			throw new CancelEditingLayerException(layer.getName(), e);
476
		} catch (DataException e) {
477
			throw new CancelEditingLayerException(layer.getName(), e);
478
		}
479
	}
480
	*/
463
     featureStore.cancelEditing();
464
     } catch (ReadException e) {
465
     throw new CancelEditingLayerException(layer.getName(), e);
466
     } catch (DataException e) {
467
     throw new CancelEditingLayerException(layer.getName(), e);
468
     }
469
     }
470
     */
471
    private int exportLayer(FLyrVect layer) throws ReadException {
472
        ViewDocument model = vista.getModel();
473
        MapContext mapContext = model.getMapContext();
474
        IProjection projection = mapContext.getProjection();
481 475

  
482
	private int exportLayer(FLyrVect layer) throws ReadException {
483
		ViewDocument model = vista.getModel();
484
		MapContext mapContext = model.getMapContext();
485
		IProjection projection = mapContext.getProjection();
476
        ExporttoLayerExtension extension = (ExporttoLayerExtension) PluginsLocator
477
                .getManager().getExtension(ExporttoLayerExtension.class);
478
        return extension.showExportto(layer, projection, mapContext, WindowManager.MODE.DIALOG);
479
    }
486 480

  
487
		ExporttoLayerExtension extension = (ExporttoLayerExtension) PluginsLocator
488
				.getManager().getExtension(ExporttoLayerExtension.class);
489
		return extension.showExportto(layer, projection, mapContext, WindowManager.MODE.DIALOG);
490
	}
481
    /**
482
     * @see org.gvsig.andami.plugins.IExtension#isVisible()
483
     */
484
    public boolean isVisible() {
485
        if ( EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE ) {
486
            return true;
487
        }
488
        return false;
491 489

  
492
	/**
493
	 * @see org.gvsig.andami.plugins.IExtension#isVisible()
494
	 */
495
	public boolean isVisible() {
496
		if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
497
			return true;
498
		}
499
		return false;
490
    }
500 491

  
501
	}
492
    public IExtensionStatus getStatus() {
493
        return new StopEditingStatus();
494
    }
502 495

  
503
	public IExtensionStatus getStatus() {
504
		return new StopEditingStatus();
505
	}
496
    /**
497
     * Show the dialogs to save the layer without ask if don't like to save.
498
     *
499
     * @param layer
500
     * Layer to save.
501
     */
502
    public boolean executeSaveLayer(FLyrVect layer) {
506 503

  
507
	/**
508
	 * Show the dialogs to save the layer without ask if don't like to save.
509
	 * 
510
	 * @param layer
511
	 *            Layer to save.
512
	 */
513
	public boolean executeSaveLayer(FLyrVect layer) {
514
	    
515
	    IEditionManager edMan = EditionLocator.getEditionManager(layer);
504
        IEditionManager edMan = EditionLocator.getEditionManager(layer);
516 505

  
517
		VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan
518
				.getLayerEdited(layer);
519
		boolean isStop = false;
520
		try {
521
			lyrEd.clearSelection();
506
        VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan
507
                .getLayerEdited(layer);
508
        boolean isStop = false;
509
        try {
510
            lyrEd.clearSelection();
522 511

  
523
			if (layer.isWritable()) {
512
            if ( layer.isWritable() ) {
524 513

  
525
			    try {
526
			        edMan.stopEditLayer(null, layer, IEditionManager.ACCEPT_EDITING);
527
					isStop = true;
528
			    } catch (CancelException ex) {
529
					isStop = false;
530
			    	
531
			    } catch (Exception ex) {
532
			        logger.error("While stopping layer editing.", ex);
533
					isStop = false;
534
			    }
514
                try {
515
                    edMan.stopEditLayer(null, layer, IEditionManager.ACCEPT_EDITING);
516
                    isStop = true;
517
                } catch (CancelException ex) {
518
                    isStop = false;
535 519

  
536
			} else {
537
				// Si no existe writer para la capa que tenemos en edici?n
538
				int resp = JOptionPane
539
						.showConfirmDialog(
540
								(Component) PluginServices.getMainFrame(),
541
								PluginServices
542
										.getText(
543
												this,
544
												"no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
545
										+ " : " + layer.getName(),
546
								PluginServices
547
										.getText(this, "cancelar_edicion"),
548
								JOptionPane.YES_NO_OPTION);
549
				if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING
520
                } catch (Exception ex) {
521
                    logger.error("While stopping layer editing.", ex);
522
                    isStop = false;
523
                }
550 524

  
551
				    try {
552
				        edMan.stopEditLayer(null, layer, IEditionManager.CANCEL_EDITING);
553
						isStop = true;
554
				    } catch(CancelException ex) {
555
						isStop = false;
556
				    	
557
				    } catch (Exception ex) {
558
						isStop = false;
559
				        logger.error("While stopping layer editing.", ex);
560
				    }
525
            } else {
526
                // Si no existe writer para la capa que tenemos en edici?n
527
                int resp = JOptionPane
528
                        .showConfirmDialog(
529
                                (Component) PluginServices.getMainFrame(),
530
                                PluginServices
531
                                .getText(
532
                                        this,
533
                                        "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
534
                                + " : " + layer.getName(),
535
                                PluginServices
536
                                .getText(this, "cancelar_edicion"),
537
                                JOptionPane.YES_NO_OPTION);
538
                if ( resp == JOptionPane.YES_OPTION ) { // CANCEL EDITING
561 539

  
562
				}
540
                    try {
541
                        edMan.stopEditLayer(null, layer, IEditionManager.CANCEL_EDITING);
542
                        isStop = true;
543
                    } catch (CancelException ex) {
544
                        isStop = false;
563 545

  
564
			}
565
		} catch (ReadException e1) {
566
			NotificationManager.showMessageError(e1.getMessage(), e1);
567
		} catch (DataException e) {
568
			NotificationManager.showMessageError(e.getMessage(), e);
569
		}
570
		return isStop;
571
	}
546
                    } catch (Exception ex) {
547
                        isStop = false;
548
                        logger.error("While stopping layer editing.", ex);
549
                    }
572 550

  
573
	private class UnsavedLayer extends UnsavedData {
551
                }
574 552

  
575
		private FLayer layer;
553
            }
554
        } catch (ReadException e1) {
555
            NotificationManager.showMessageError(e1.getMessage(), e1);
556
        } catch (DataException e) {
557
            NotificationManager.showMessageError(e.getMessage(), e);
558
        }
559
        return isStop;
560
    }
576 561

  
577
		public UnsavedLayer(IExtension extension) {
578
			super(extension);
579
		}
562
    private class UnsavedLayer extends UnsavedData {
580 563

  
581
		public String getDescription() {
582
			return PluginServices.getText(this, "editing_layer_unsaved");
583
		}
564
        private FLayer layer;
584 565

  
585
		public String getResourceName() {
586
			return layer.getName();
587
		}
566
        public UnsavedLayer(IExtension extension) {
567
            super(extension);
568
        }
588 569

  
589
		public boolean saveData() {
590
			return executeSaveLayer((FLyrVect) layer);
591
		}
570
        public String getDescription() {
571
            return PluginServices.getText(this, "editing_layer_unsaved");
572
        }
592 573

  
593
		public void setLayer(FLayer layer) {
594
			this.layer = layer;
574
        public String getResourceName() {
575
            return layer.getName();
576
        }
595 577

  
596
		}
578
        public boolean saveData() {
579
            return executeSaveLayer((FLyrVect) layer);
580
        }
597 581

  
598
		public String getIcon() {
599
			return layer.getTocImageIcon();
600
		}
582
        public void setLayer(FLayer layer) {
583
            this.layer = layer;
601 584

  
602
	}
585
        }
603 586

  
604
	/**
605
	 * <p>
606
	 * This class provides the status of extensions. If this extension has some
607
	 * unsaved editing layer (and save them), and methods to check if the
608
	 * extension has some associated background tasks.
609
	 * 
610
	 * @author Vicente Caballero Navarro
611
	 * 
612
	 */
613
	private class StopEditingStatus implements IExtensionStatus {
614
		/**
615
		 * This method is used to check if this extension has some unsaved
616
		 * editing layer.
617
		 * 
618
		 * @return true if the extension has some unsaved editing layer, false
619
		 *         otherwise.
620
		 */
621
		public boolean hasUnsavedData() {
622
			Project project = ProjectManager.getInstance().getCurrentProject();
623
			DefaultViewDocument[] views = project.getDocuments(
624
					ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
625
			for (int i = 0; i < views.length; i++) {
626
				FLayers layers = views[i].getMapContext().getLayers();
627
				LayersIterator iter = getEditingLayer(layers);
628
				if (iter.hasNext()) {
629
					return true;
630
				}
631
			}
632
			return false;
633
		}
587
        public String getIcon() {
588
            return layer.getTocImageIcon();
589
        }
634 590

  
635
		/**
636
		 * This method is used to check if the extension has some associated
637
		 * background process which is currently running.
638
		 * 
639
		 * @return true if the extension has some associated background process,
640
		 *         false otherwise.
641
		 */
642
		public boolean hasRunningProcesses() {
643
			return false;
644
		}
591
    }
645 592

  
646
		/**
647
		 * <p>
648
		 * Gets an array of the traceable background tasks associated with this
649
		 * extension. These tasks may be tracked, canceled, etc.
650
		 * </p>
651
		 * 
652
		 * @return An array of the associated background tasks, or null in case
653
		 *         there is no associated background tasks.
654
		 */
655
		public IMonitorableTask[] getRunningProcesses() {
656
			return null;
657
		}
593
    /**
594
     * <p>
595
     * This class provides the status of extensions. If this extension has some
596
     * unsaved editing layer (and save them), and methods to check if the
597
     * extension has some associated background tasks.
598
     *
599
     * @author Vicente Caballero Navarro
600
     *
601
     */
602
    private class StopEditingStatus implements IExtensionStatus {
658 603

  
659
		/**
660
		 * <p>
661
		 * Gets an array of the UnsavedData objects, which contain information
662
		 * about the unsaved editing layers and allows to save it.
663
		 * </p>
664
		 * 
665
		 * @return An array of the associated unsaved editing layers, or null in
666
		 *         case the extension has not unsaved editing layers.
667
		 */
668
		public IUnsavedData[] getUnsavedData() {
669
			Project project = ProjectManager.getInstance().getCurrentProject();
670
			DefaultViewDocument[] views = project.getDocuments(
671
					ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
672
			ArrayList unsavedLayers = new ArrayList();
673
			for (int i = 0; i < views.length; i++) {
674
				FLayers layers = views[i].getMapContext().getLayers();
675
				LayersIterator iter = getEditingLayer(layers);
676
				while (iter.hasNext()) {
677
					UnsavedLayer ul = new UnsavedLayer(StopEditing.this);
678
					ul.setLayer(iter.nextLayer());
679
					unsavedLayers.add(ul);
680
				}
681
			}
682
			return (IUnsavedData[]) unsavedLayers.toArray(new IUnsavedData[0]);
683
		}
684
	}
604
        /**
605
         * This method is used to check if this extension has some unsaved
606
         * editing layer.
607
         *
608
         * @return true if the extension has some unsaved editing layer, false
609
         * otherwise.
610
         */
611
        public boolean hasUnsavedData() {
612
            Project project = ProjectManager.getInstance().getCurrentProject();
613
            DefaultViewDocument[] views = project.getDocuments(
614
                    ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
615
            for ( int i = 0; i < views.length; i++ ) {
616
                FLayers layers = views[i].getMapContext().getLayers();
617
                LayersIterator iter = getEditingLayer(layers);
618
                if ( iter.hasNext() ) {
619
                    return true;
620
                }
621
            }
622
            return false;
623
        }
685 624

  
686
	private LayersIterator getEditingLayer(FLayers layers) {
687
		return new LayersIterator(layers) {
688
			public boolean evaluate(FLayer layer) {
689
				return layer.isEditing();
690
			}
691
		};
692
	}
625
        /**
626
         * This method is used to check if the extension has some associated
627
         * background process which is currently running.
628
         *
629
         * @return true if the extension has some associated background process,
630
         * false otherwise.
631
         */
632
        public boolean hasRunningProcesses() {
633
            return false;
634
        }
635

  
636
        /**
637
         * <p>
638
         * Gets an array of the traceable background tasks associated with this
639
         * extension. These tasks may be tracked, canceled, etc.
640
         * </p>
641
         *
642
         * @return An array of the associated background tasks, or null in case
643
         * there is no associated background tasks.
644
         */
645
        public IMonitorableTask[] getRunningProcesses() {
646
            return null;
647
        }
648

  
649
        /**
650
         * <p>
651
         * Gets an array of the UnsavedData objects, which contain information
652
         * about the unsaved editing layers and allows to save it.
653
         * </p>
654
         *
655
         * @return An array of the associated unsaved editing layers, or null in
656
         * case the extension has not unsaved editing layers.
657
         */
658
        public IUnsavedData[] getUnsavedData() {
659
            Project project = ProjectManager.getInstance().getCurrentProject();
660
            DefaultViewDocument[] views = project.getDocuments(
661
                    ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
662
            ArrayList unsavedLayers = new ArrayList();
663
            for ( int i = 0; i < views.length; i++ ) {
664
                FLayers layers = views[i].getMapContext().getLayers();
665
                LayersIterator iter = getEditingLayer(layers);
666
                while ( iter.hasNext() ) {
667
                    UnsavedLayer ul = new UnsavedLayer(StopEditing.this);
668
                    ul.setLayer(iter.nextLayer());
669
                    unsavedLayers.add(ul);
670
                }
671
            }
672
            return (IUnsavedData[]) unsavedLayers.toArray(new IUnsavedData[0]);
673
        }
674
    }
675

  
676
    private LayersIterator getEditingLayer(FLayers layers) {
677
        return new LayersIterator(layers) {
678
            public boolean evaluate(FLayer layer) {
679
                return layer.isEditing();
680
            }
681
        };
682
    }
693 683
}

Also available in: Unified diff