bc11b149bc1ac9d4d2df51f66e414fce1893fd00
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / handler / SetMergeTargetHandler.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 package eu.etaxonomy.taxeditor.bulkeditor.handler;
11
12 import org.eclipse.core.commands.AbstractHandler;
13 import org.eclipse.core.commands.ExecutionEvent;
14 import org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.TextSelection;
17 import org.eclipse.jface.text.source.Annotation;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.handlers.HandlerUtil;
22 import org.eclipse.ui.texteditor.IDocumentProvider;
23
24 import eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor;
25 import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
26 import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
27 import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
28
29 /**
30 * <p>SetMergeTargetHandler class.</p>
31 *
32 * @author p.ciardelli
33 * @created 25.06.2009
34 * @version 1.0
35 */
36 public class SetMergeTargetHandler extends AbstractHandler {
37
38 /* (non-Javadoc)
39 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
40 */
41 /** {@inheritDoc} */
42 public Object execute(ExecutionEvent event) throws ExecutionException {
43
44 IEditorPart editor = HandlerUtil.getActiveEditor(event);
45 IEditorInput input = editor.getEditorInput();
46
47 if (editor instanceof AnnotatedLineEditor && input instanceof AbstractBulkEditorInput) {
48
49 IDocumentProvider provider = ((AnnotatedLineEditor) editor).getDocumentProvider();
50 LineAnnotationModel model =
51 (LineAnnotationModel) provider.getAnnotationModel(input);
52 IDocument document = provider.getDocument(input);
53
54 ISelection selection = HandlerUtil.getActiveMenuSelection(event);
55 if (selection instanceof TextSelection) {
56 Annotation annotation = null;
57
58 // Get annotation in current line
59 int line = ((TextSelection) selection).getStartLine();
60 annotation = model.getAnnotationAtLine(line, document);
61
62 // Remove existing merge target
63 model.removeTypeFromAllAnnotations(IBulkEditorConstants.TYPE_MERGE_TARGET);
64
65 // Set annotation in current line to merge target
66 if (annotation != null) {
67 model.changeAnnotationType(
68 annotation, IBulkEditorConstants.TYPE_MERGE_TARGET);
69 }
70 }
71 }
72 return null;
73 }
74 }