129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.revisions.RevisionSelectionProvider;
|
|
14
|
131
|
15 import dwtx.jface.internal.text.revisions.HunkComputer; // packageimport
|
|
16 import dwtx.jface.internal.text.revisions.LineIndexOutOfBoundsException; // packageimport
|
|
17 import dwtx.jface.internal.text.revisions.Hunk; // packageimport
|
|
18 import dwtx.jface.internal.text.revisions.Colors; // packageimport
|
|
19 import dwtx.jface.internal.text.revisions.ChangeRegion; // packageimport
|
|
20 import dwtx.jface.internal.text.revisions.Range; // packageimport
|
|
21 import dwtx.jface.internal.text.revisions.RevisionPainter; // packageimport
|
|
22
|
|
23
|
129
|
24 import dwt.dwthelper.utils;
|
|
25
|
|
26
|
|
27
|
|
28 import dwtx.core.runtime.ListenerList;
|
|
29 import dwtx.jface.text.ITextSelection;
|
|
30 import dwtx.jface.text.ITextViewer;
|
|
31 import dwtx.jface.text.revisions.Revision;
|
|
32 import dwtx.jface.viewers.IPostSelectionProvider;
|
|
33 import dwtx.jface.viewers.ISelection;
|
|
34 import dwtx.jface.viewers.ISelectionChangedListener;
|
|
35 import dwtx.jface.viewers.ISelectionProvider;
|
|
36 import dwtx.jface.viewers.IStructuredSelection;
|
|
37 import dwtx.jface.viewers.SelectionChangedEvent;
|
|
38 import dwtx.jface.viewers.StructuredSelection;
|
|
39
|
|
40 /**
|
|
41 * A selection provider for annotate revisions. Selections of a revision can currently happen in
|
|
42 * following ways - note that this list may be changed in the future:
|
|
43 * <ul>
|
|
44 * <li>when the user clicks the revision ruler with the mouse</li>
|
|
45 * <li>when the caret is moved to a revision's line (only on post-selection)</li>
|
|
46 * </ul>
|
|
47 * <p>
|
|
48 * Calling {@link #setSelection(ISelection)} will set the current sticky revision on the ruler.
|
|
49 * </p>
|
|
50 *
|
|
51 * @since 3.2
|
|
52 */
|
|
53 public final class RevisionSelectionProvider : ISelectionProvider {
|
|
54
|
|
55 /**
|
|
56 * Post selection listener on the viewer that remembers the selection provider it is registered
|
|
57 * with.
|
|
58 */
|
|
59 private final class PostSelectionListener : ISelectionChangedListener {
|
|
60 private final IPostSelectionProvider fPostProvider;
|
|
61
|
|
62 public PostSelectionListener(IPostSelectionProvider postProvider) {
|
|
63 postProvider.addPostSelectionChangedListener(this);
|
|
64 fPostProvider= postProvider;
|
|
65 }
|
|
66
|
|
67 public void selectionChanged(SelectionChangedEvent event) {
|
|
68 ISelection selection= event.getSelection();
|
|
69 if (selection instanceof ITextSelection) {
|
|
70 ITextSelection ts= (ITextSelection) selection;
|
|
71 int offset= ts.getOffset();
|
|
72 setSelectedRevision(fPainter.getRevision(offset));
|
|
73 }
|
|
74
|
|
75 }
|
|
76
|
|
77 public void dispose() {
|
|
78 fPostProvider.removePostSelectionChangedListener(this);
|
|
79 }
|
|
80 }
|
|
81
|
|
82 private final RevisionPainter fPainter;
|
|
83 private final ListenerList fListeners= new ListenerList();
|
|
84
|
|
85 /**
|
|
86 * The text viewer once we are installed, <code>null</code> if not installed.
|
|
87 */
|
|
88 private ITextViewer fViewer;
|
|
89 /**
|
|
90 * The selection listener on the viewer, or <code>null</code>.
|
|
91 */
|
|
92 private PostSelectionListener fSelectionListener;
|
|
93 /**
|
|
94 * The last selection, or <code>null</code>.
|
|
95 */
|
|
96 private Revision fSelection;
|
|
97 /**
|
|
98 * Incoming selection changes are ignored while sending out events.
|
|
99 *
|
|
100 * @since 3.3
|
|
101 */
|
|
102 private bool fIgnoreEvents= false;
|
|
103
|
|
104 /**
|
|
105 * Creates a new selection provider.
|
|
106 *
|
|
107 * @param painter the painter that the created provider interacts with
|
|
108 */
|
|
109 RevisionSelectionProvider(RevisionPainter painter) {
|
|
110 fPainter= painter;
|
|
111 }
|
|
112
|
|
113 /*
|
|
114 * @see dwtx.jface.viewers.ISelectionProvider#addSelectionChangedListener(dwtx.jface.viewers.ISelectionChangedListener)
|
|
115 */
|
|
116 public void addSelectionChangedListener(ISelectionChangedListener listener) {
|
|
117 fListeners.add(listener);
|
|
118 }
|
|
119
|
|
120 /*
|
|
121 * @see dwtx.jface.viewers.ISelectionProvider#removeSelectionChangedListener(dwtx.jface.viewers.ISelectionChangedListener)
|
|
122 */
|
|
123 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
|
|
124 fListeners.remove(listener);
|
|
125 }
|
|
126
|
|
127 /*
|
|
128 * @see dwtx.jface.viewers.ISelectionProvider#getSelection()
|
|
129 */
|
|
130 public ISelection getSelection() {
|
|
131 if (fSelection is null)
|
|
132 return StructuredSelection.EMPTY;
|
|
133 return new StructuredSelection(fSelection);
|
|
134 }
|
|
135
|
|
136 /*
|
|
137 * @see dwtx.jface.viewers.ISelectionProvider#setSelection(dwtx.jface.viewers.ISelection)
|
|
138 */
|
|
139 public void setSelection(ISelection selection) {
|
|
140 if (fIgnoreEvents)
|
|
141 return;
|
|
142 if (selection instanceof IStructuredSelection) {
|
|
143 Object first= ((IStructuredSelection) selection).getFirstElement();
|
|
144 if (first instanceof Revision)
|
|
145 fPainter.handleRevisionSelected((Revision) first);
|
|
146 else if (first instanceof String)
|
|
147 fPainter.handleRevisionSelected((String) first);
|
|
148 else if (selection.isEmpty())
|
|
149 fPainter.handleRevisionSelected((Revision) null);
|
|
150 }
|
|
151 }
|
|
152
|
|
153 /**
|
|
154 * Installs the selection provider on the viewer.
|
|
155 *
|
|
156 * @param viewer the viewer on which we listen to for post selection events
|
|
157 */
|
|
158 void install(ITextViewer viewer) {
|
|
159 uninstall();
|
|
160 fViewer= viewer;
|
|
161 if (fViewer !is null) {
|
|
162 ISelectionProvider provider= fViewer.getSelectionProvider();
|
|
163 if (provider instanceof IPostSelectionProvider) {
|
|
164 IPostSelectionProvider postProvider= (IPostSelectionProvider) provider;
|
|
165 fSelectionListener= new PostSelectionListener(postProvider);
|
|
166 }
|
|
167 }
|
|
168 }
|
|
169
|
|
170 /**
|
|
171 * Uninstalls the selection provider.
|
|
172 */
|
|
173 void uninstall() {
|
|
174 fViewer= null;
|
|
175 if (fSelectionListener !is null) {
|
|
176 fSelectionListener.dispose();
|
|
177 fSelectionListener= null;
|
|
178 }
|
|
179 }
|
|
180
|
|
181 /**
|
|
182 * Private protocol used by {@link RevisionPainter} to signal selection of a revision.
|
|
183 *
|
|
184 * @param revision the selected revision, or <code>null</code> for none
|
|
185 */
|
|
186 void revisionSelected(Revision revision) {
|
|
187 setSelectedRevision(revision);
|
|
188 }
|
|
189
|
|
190 /**
|
|
191 * Updates the currently selected revision and sends out an event if it changed.
|
|
192 *
|
|
193 * @param revision the newly selected revision or <code>null</code> for none
|
|
194 */
|
|
195 private void setSelectedRevision(Revision revision) {
|
|
196 if (revision !is fSelection) {
|
|
197 fSelection= revision;
|
|
198 fireSelectionEvent();
|
|
199 }
|
|
200 }
|
|
201
|
|
202 private void fireSelectionEvent() {
|
|
203 fIgnoreEvents= true;
|
|
204 try {
|
|
205 ISelection selection= getSelection();
|
|
206 SelectionChangedEvent event= new SelectionChangedEvent(this, selection);
|
|
207
|
|
208 Object[] listeners= fListeners.getListeners();
|
|
209 for (int i= 0; i < listeners.length; i++)
|
|
210 ((ISelectionChangedListener) listeners[i]).selectionChanged(event);
|
|
211 } finally {
|
|
212 fIgnoreEvents= false;
|
|
213 }
|
|
214 }
|
|
215 }
|