Project

General

Profile

Download (3.68 KB) Statistics
| Branch: | Tag: | Revision:
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.annotatedlineeditor;
12

    
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;
24

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

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

    
39
	private int lastLine = 0;
40
	private ILineSelectionSource lineSelectionSource;
41
	
42
	/**
43
	 * <p>Constructor for LineSelectionViewer.</p>
44
	 *
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.
50
	 */
51
	public LineSelectionViewer(Composite parent, IVerticalRuler verticalRuler,
52
			IOverviewRuler overviewRuler, boolean showAnnotationsOverview,
53
			int styles) {
54
		super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
55

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

    
82

    
83
	/* (non-Javadoc)
84
	 * @see org.eclipse.jface.text.TextViewer#firePostSelectionChanged(int, int)
85
	 */
86
	/** {@inheritDoc} */
87
	@Override
88
	protected void firePostSelectionChanged(int offset, int length) {
89
		if (redraws()) {
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);
95
		}
96
	}
97
	
98
	/**
99
	 * Creates a new instance of a LineSelection based on a given ITextSelection
100
	 * 
101
	 * @param textSelection
102
	 * @return
103
	 */
104
	private LineSelection createLineSelection(ITextSelection textSelection) {
105
		if (textSelection instanceof LineSelection ) {
106
			return (LineSelection) textSelection;
107
		}
108
		int line = textSelection.getStartLine();
109
		Object selectionObject = lineSelectionSource.getSelectedObjectAtLine(line);
110
		return new LineSelection(textSelection, getDocument(), selectionObject);
111
	}
112
}
(17-17/18)