3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.annotatedlineeditor
;
13 import org
.apache
.log4j
.Logger
;
14 import org
.eclipse
.jface
.text
.IRegion
;
15 import org
.eclipse
.jface
.text
.ITextSelection
;
16 import org
.eclipse
.jface
.text
.Region
;
17 import org
.eclipse
.jface
.text
.TextSelection
;
18 import org
.eclipse
.jface
.text
.source
.IOverviewRuler
;
19 import org
.eclipse
.jface
.text
.source
.IVerticalRuler
;
20 import org
.eclipse
.jface
.text
.source
.SourceViewer
;
21 import org
.eclipse
.jface
.viewers
.ISelection
;
22 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
25 import eu
.etaxonomy
.taxeditor
.model
.LineSelection
;
28 * Notifies selection service every time the user changes focus to a new line.
34 public class LineSelectionViewer
extends SourceViewer
{
35 @SuppressWarnings("unused")
36 private static final Logger logger
= Logger
37 .getLogger(LineSelectionViewer
.class);
39 private int lastLine
= 0;
40 private ILineSelectionSource lineSelectionSource
;
43 * <p>Constructor for LineSelectionViewer.</p>
45 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
46 * @param verticalRuler a {@link org.eclipse.jface.text.source.IVerticalRuler} object.
47 * @param overviewRuler a {@link org.eclipse.jface.text.source.IOverviewRuler} object.
48 * @param showAnnotationsOverview a boolean.
49 * @param styles a int.
51 public LineSelectionViewer(Composite parent
, IVerticalRuler verticalRuler
,
52 IOverviewRuler overviewRuler
, boolean showAnnotationsOverview
,
54 super(parent
, verticalRuler
, overviewRuler
, showAnnotationsOverview
, styles
);
56 // createLineChangeListeners();
57 setLineSelectionSource(new LineSelectionSource(this));
61 * @see org.eclipse.jface.text.TextViewer#getSelection()
65 public ISelection
getSelection() {
66 ISelection selection
= super.getSelection();
67 if (getDocument() != null) {
68 selection
= createLineSelection((TextSelection
) selection
);
74 * <p>Setter for the field <code>lineSelectionSource</code>.</p>
76 * @param lineSelectionSource a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineSelectionSource} object.
78 public void setLineSelectionSource(ILineSelectionSource lineSelectionSource
) {
79 this.lineSelectionSource
= lineSelectionSource
;
84 * @see org.eclipse.jface.text.TextViewer#firePostSelectionChanged(int, int)
88 protected void firePostSelectionChanged(int offset
, int length
) {
90 IRegion r
= widgetRange2ModelRange(new Region(offset
, length
));
91 ITextSelection textSelection
= r
!= null ?
new TextSelection(getDocument(), r
.getOffset(), r
.getLength()) : TextSelection
.emptySelection();
92 LineSelection lineSelection
= createLineSelection(textSelection
);
93 SelectionChangedEvent event
= new SelectionChangedEvent(this, lineSelection
);
94 fireSelectionChanged(event
);
99 * Creates a new instance of a LineSelection based on a given ITextSelection
101 * @param textSelection
104 private LineSelection
createLineSelection(ITextSelection textSelection
) {
105 if (textSelection
instanceof LineSelection
) {
106 return (LineSelection
) textSelection
;
108 int line
= textSelection
.getStartLine();
109 Object selectionObject
= lineSelectionSource
.getSelectedObjectAtLine(line
);
110 return new LineSelection(textSelection
, getDocument(), selectionObject
);