Project

General

Profile

Download (3.67 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.annotatedlineeditor;
11

    
12
import org.apache.log4j.Logger;
13
import org.eclipse.jface.text.IRegion;
14
import org.eclipse.jface.text.ITextSelection;
15
import org.eclipse.jface.text.Region;
16
import org.eclipse.jface.text.TextSelection;
17
import org.eclipse.jface.text.source.IOverviewRuler;
18
import org.eclipse.jface.text.source.IVerticalRuler;
19
import org.eclipse.jface.text.source.SourceViewer;
20
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.SelectionChangedEvent;
22
import org.eclipse.swt.widgets.Composite;
23

    
24
import eu.etaxonomy.taxeditor.model.LineSelection;
25

    
26
/**
27
 * Notifies selection service every time the user changes focus to a new line.
28
 *
29
 * @author p.ciardelli
30
 * @created 03.07.2009
31
 * @version 1.0
32
 */
33
public class LineSelectionViewer extends SourceViewer {
34
	@SuppressWarnings("unused")
35
	private static final Logger logger = Logger
36
			.getLogger(LineSelectionViewer.class);
37

    
38
	private int lastLine = 0;
39
	private ILineSelectionSource lineSelectionSource;
40
	
41
	/**
42
	 * <p>Constructor for LineSelectionViewer.</p>
43
	 *
44
	 * @param parent a {@link org.eclipse.swt.widgets.Composite} object.
45
	 * @param verticalRuler a {@link org.eclipse.jface.text.source.IVerticalRuler} object.
46
	 * @param overviewRuler a {@link org.eclipse.jface.text.source.IOverviewRuler} object.
47
	 * @param showAnnotationsOverview a boolean.
48
	 * @param styles a int.
49
	 */
50
	public LineSelectionViewer(Composite parent, IVerticalRuler verticalRuler,
51
			IOverviewRuler overviewRuler, boolean showAnnotationsOverview,
52
			int styles) {
53
		super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
54

    
55
//		createLineChangeListeners();
56
		setLineSelectionSource(new LineSelectionSource(this));
57
	}
58
	
59
	/* (non-Javadoc)
60
	 * @see org.eclipse.jface.text.TextViewer#getSelection()
61
	 */
62
	/** {@inheritDoc} */
63
	@Override
64
	public ISelection getSelection() {
65
		ISelection selection = super.getSelection();
66
		if (getDocument() != null) {
67
			selection = createLineSelection((TextSelection) selection);
68
		}
69
		return selection;
70
	}
71
	
72
	/**
73
	 * <p>Setter for the field <code>lineSelectionSource</code>.</p>
74
	 *
75
	 * @param lineSelectionSource a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineSelectionSource} object.
76
	 */
77
	public void setLineSelectionSource(ILineSelectionSource lineSelectionSource) {
78
		this.lineSelectionSource = lineSelectionSource;
79
	}
80

    
81

    
82
	/* (non-Javadoc)
83
	 * @see org.eclipse.jface.text.TextViewer#firePostSelectionChanged(int, int)
84
	 */
85
	/** {@inheritDoc} */
86
	@Override
87
	protected void firePostSelectionChanged(int offset, int length) {
88
		if (redraws()) {
89
			IRegion r= widgetRange2ModelRange(new Region(offset, length));
90
			ITextSelection textSelection= r != null ? new TextSelection(getDocument(), r.getOffset(), r.getLength()) : TextSelection.emptySelection();
91
			LineSelection lineSelection = createLineSelection(textSelection);
92
			SelectionChangedEvent event= new SelectionChangedEvent(this, lineSelection);
93
			fireSelectionChanged(event);
94
		}
95
	}
96
	
97
	/**
98
	 * Creates a new instance of a LineSelection based on a given ITextSelection
99
	 * 
100
	 * @param textSelection
101
	 * @return
102
	 */
103
	private LineSelection createLineSelection(ITextSelection textSelection) {
104
		if (textSelection instanceof LineSelection ) {
105
			return (LineSelection) textSelection;
106
		}
107
		int line = textSelection.getStartLine();
108
		Object selectionObject = lineSelectionSource.getSelectedObjectAtLine(line);
109
		return new LineSelection(textSelection, getDocument(), selectionObject);
110
	}
111
}
(15-15/16)