merge from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / DeleteHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.bulkeditor.handler;
12
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.ui.IEditorInput;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.handlers.HandlerUtil;
21 import org.eclipse.ui.texteditor.IDocumentProvider;
22
23 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
24 import eu.etaxonomy.cdm.api.service.IReferenceService;
25 import eu.etaxonomy.cdm.api.service.ITaxonService;
26 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
27 import eu.etaxonomy.cdm.model.reference.Reference;
28 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
29 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
30 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
31 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
32 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
33 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
34 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
35 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
36 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38
39 /**
40 * @author n.hoffmann
41 * @created Mar 11, 2011
42 * @version 1.0
43 */
44 public class DeleteHandler extends AbstractHandler {
45
46 /* (non-Javadoc)
47 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
48 */
49 @Override
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51
52 if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_EXPERIMENTAL_FEATURES)){
53 ISelection selection = HandlerUtil.getCurrentSelection(event);
54
55 IEditorPart editor = HandlerUtil.getActiveEditor(event);
56
57 IEditorInput input = editor.getEditorInput();
58
59 if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
60
61
62 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
63 LineAnnotationModel model =
64 (LineAnnotationModel) provider.getAnnotationModel(input);
65
66
67 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
68
69 IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
70
71 for(Object object : structuredSelection.toList()){
72
73 LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
74
75 try {
76 ICdmApplicationConfiguration controller;
77 controller = (ICdmApplicationConfiguration) CdmStore.getCurrentApplicationConfiguration();
78 //persistenceService.delete(object);
79 if (object instanceof SpecimenOrObservationBase){
80
81 IOccurrenceService service = controller.getOccurrenceService();
82 service.delete((SpecimenOrObservationBase)object);
83 } else if (object instanceof Reference){
84 IReferenceService service = controller.getReferenceService();
85 service.delete((Reference)object);
86 }
87
88 } catch (Exception e){
89 BulkEditorUtil.errorDialog("Could not delete", getClass(), e.getMessage(), null);
90 }
91 ((BulkEditor) editor).removeAnnotatedLine(annotation);
92
93 }
94 }
95 }else{
96 BulkEditorUtil.warningDialog("Feature not enabled", getClass(), "Deletion is currently an experimental feature." +
97 "Enable it via Preferences at your own risk.");
98 }
99
100
101 return null;
102 }
103
104 }