fixed bug of duplicate authorities being created
[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.taxeditor.annotatedlineeditor.IEntityPersistenceService;
24 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
25 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
26 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
27 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
28 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
29 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30
31 /**
32 * @author n.hoffmann
33 * @created Mar 11, 2011
34 * @version 1.0
35 */
36 public class DeleteHandler extends AbstractHandler {
37
38 /* (non-Javadoc)
39 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
40 */
41 @Override
42 public Object execute(ExecutionEvent event) throws ExecutionException {
43
44 if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_EXPERIMENTAL_FEATURES)){
45 ISelection selection = HandlerUtil.getCurrentSelection(event);
46
47 IEditorPart editor = HandlerUtil.getActiveEditor(event);
48
49 IEditorInput input = editor.getEditorInput();
50
51 if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
52
53
54 IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
55 LineAnnotationModel model =
56 (LineAnnotationModel) provider.getAnnotationModel(input);
57
58
59 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
60
61 IEntityPersistenceService persistenceService = (IEntityPersistenceService) input;
62
63 for(Object object : structuredSelection.toList()){
64
65 LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object);
66
67 try {
68 persistenceService.delete(object);
69 } catch (Exception e){
70 BulkEditorUtil.errorDialog("Could not delete", getClass(), e.getMessage(), null);
71 }
72 ((BulkEditor) editor).removeAnnotatedLine(annotation);
73
74 }
75 }
76 }else{
77 BulkEditorUtil.warningDialog("Feature not enabled", getClass(), "Deletion is currently an experimental feature." +
78 "Enable it via Preferences at your own risk.");
79 }
80
81
82 return null;
83 }
84
85 }