915349fc0cd143d116c89c7d1a5d2ad5a84f8621
[taxeditor.git] / taxeditor-bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / BulkEditor.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;
11
12 import org.eclipse.jface.text.IDocument;
13 import org.eclipse.jface.text.source.Annotation;
14 import org.eclipse.jface.text.source.ISourceViewer;
15 import org.eclipse.jface.text.source.IVerticalRuler;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.StyledText;
18 import org.eclipse.swt.events.MouseAdapter;
19 import org.eclipse.swt.events.MouseEvent;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorSite;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.editors.text.TextEditor;
26
27 /**
28 * A document provider is responsible not just for providing the document for a
29 * given editor input but for the complete translation between the domain model
30 * and the editor's internal document-based model. Document provider can be shared
31 * between editors.
32 *
33 * @author p.ciardelli
34 * @created 25.06.2009
35 * @version 1.0
36 */
37 public class BulkEditor extends TextEditor {
38
39 public static final String ID = "bulkeditor.editor";
40
41 private ISourceViewer viewer;
42
43 public BulkEditor() {
44 super();
45
46 /**
47 * see AbstractTextEditor javadoc for explanation of context menu ids
48 */
49 setEditorContextMenuId("#BulkEditorContext");
50 }
51
52 @Override
53 public void init(IEditorSite site, IEditorInput input)
54 throws PartInitException {
55 setDocumentProvider(new BulkEditorDocumentProvider());
56 super.init(site, input);
57 }
58
59 @Override
60 protected ISourceViewer createSourceViewer(Composite parent,
61 IVerticalRuler ruler, int styles) {
62 viewer = super.createSourceViewer(parent, ruler, styles | SWT.WRAP);
63 setSourceViewerConfiguration(new BulkEditorSourceViewerConfiguration());
64
65 addToggleMergeCandidateListener(ruler.getControl());
66 return viewer;
67 }
68
69 private void addToggleMergeCandidateListener(Control control) {
70 control.addMouseListener(new MouseAdapter() {
71 @Override
72 public void mouseDoubleClick(MouseEvent e) {
73 StyledText textWidget = viewer.getTextWidget();
74 int line = textWidget.getLineIndex(e.y);
75 toggleMergeCandidateAnnotation(line);
76 }
77 });
78 }
79
80 private void toggleMergeCandidateAnnotation(int line) {
81 IDocument document = viewer.getDocument();
82 BulkEditorAnnotationModel model =
83 (BulkEditorAnnotationModel) viewer.getAnnotationModel();
84
85 Annotation annotation = model.getAnnotationAtLine(line, document);
86
87 if (annotation != null) {
88 if (annotation.getType().equals(BulkEditorAnnotation.TYPE_MERGE_CANDIDATE)) {
89 model.changeAnnotationType(
90 annotation, BulkEditorAnnotation.TYPE_GENERIC);
91 } else {
92 model.changeAnnotationType(
93 annotation, BulkEditorAnnotation.TYPE_MERGE_CANDIDATE);
94 }
95 }
96 }
97 }