Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/revisions/RevisionSelectionProvider.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | 75302ef3f92f |
children |
rev | line source |
---|---|
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> | |
162 | 50 * |
129 | 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 { | |
146 | 60 private const IPostSelectionProvider fPostProvider; |
129 | 61 |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
62 public this(IPostSelectionProvider postProvider) { |
129 | 63 postProvider.addPostSelectionChangedListener(this); |
64 fPostProvider= postProvider; | |
65 } | |
66 | |
67 public void selectionChanged(SelectionChangedEvent event) { | |
68 ISelection selection= event.getSelection(); | |
138 | 69 if ( cast(ITextSelection)selection ) { |
134 | 70 ITextSelection ts= cast(ITextSelection) selection; |
129 | 71 int offset= ts.getOffset(); |
72 setSelectedRevision(fPainter.getRevision(offset)); | |
73 } | |
162 | 74 |
129 | 75 } |
162 | 76 |
129 | 77 public void dispose() { |
78 fPostProvider.removePostSelectionChangedListener(this); | |
79 } | |
80 } | |
81 | |
146 | 82 private const RevisionPainter fPainter; |
162 | 83 private const ListenerList fListeners; |
84 | |
129 | 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. | |
162 | 106 * |
129 | 107 * @param painter the painter that the created provider interacts with |
108 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
109 this(RevisionPainter painter) { |
162 | 110 fListeners= new ListenerList(); |
129 | 111 fPainter= painter; |
112 } | |
113 | |
114 /* | |
115 * @see dwtx.jface.viewers.ISelectionProvider#addSelectionChangedListener(dwtx.jface.viewers.ISelectionChangedListener) | |
116 */ | |
117 public void addSelectionChangedListener(ISelectionChangedListener listener) { | |
162 | 118 fListeners.add(cast(Object)listener); |
129 | 119 } |
120 | |
121 /* | |
122 * @see dwtx.jface.viewers.ISelectionProvider#removeSelectionChangedListener(dwtx.jface.viewers.ISelectionChangedListener) | |
123 */ | |
124 public void removeSelectionChangedListener(ISelectionChangedListener listener) { | |
162 | 125 fListeners.remove(cast(Object)listener); |
129 | 126 } |
162 | 127 |
129 | 128 /* |
129 * @see dwtx.jface.viewers.ISelectionProvider#getSelection() | |
130 */ | |
131 public ISelection getSelection() { | |
132 if (fSelection is null) | |
133 return StructuredSelection.EMPTY; | |
134 return new StructuredSelection(fSelection); | |
135 } | |
136 | |
137 /* | |
138 * @see dwtx.jface.viewers.ISelectionProvider#setSelection(dwtx.jface.viewers.ISelection) | |
139 */ | |
140 public void setSelection(ISelection selection) { | |
141 if (fIgnoreEvents) | |
142 return; | |
138 | 143 if ( cast(IStructuredSelection)selection ) { |
134 | 144 Object first= (cast(IStructuredSelection) selection).getFirstElement(); |
138 | 145 if ( cast(Revision)first ) |
134 | 146 fPainter.handleRevisionSelected(cast(Revision) first); |
162 | 147 else if ( auto str = cast(ArrayWrapperString)first ) |
148 fPainter.handleRevisionSelected(str.array); | |
129 | 149 else if (selection.isEmpty()) |
134 | 150 fPainter.handleRevisionSelected(cast(Revision) null); |
129 | 151 } |
152 } | |
153 | |
154 /** | |
155 * Installs the selection provider on the viewer. | |
162 | 156 * |
129 | 157 * @param viewer the viewer on which we listen to for post selection events |
158 */ | |
159 void install(ITextViewer viewer) { | |
160 uninstall(); | |
161 fViewer= viewer; | |
162 if (fViewer !is null) { | |
163 ISelectionProvider provider= fViewer.getSelectionProvider(); | |
138 | 164 if ( cast(IPostSelectionProvider)provider ) { |
134 | 165 IPostSelectionProvider postProvider= cast(IPostSelectionProvider) provider; |
129 | 166 fSelectionListener= new PostSelectionListener(postProvider); |
167 } | |
168 } | |
169 } | |
162 | 170 |
129 | 171 /** |
172 * Uninstalls the selection provider. | |
173 */ | |
174 void uninstall() { | |
175 fViewer= null; | |
176 if (fSelectionListener !is null) { | |
177 fSelectionListener.dispose(); | |
178 fSelectionListener= null; | |
179 } | |
180 } | |
181 | |
182 /** | |
183 * Private protocol used by {@link RevisionPainter} to signal selection of a revision. | |
162 | 184 * |
129 | 185 * @param revision the selected revision, or <code>null</code> for none |
186 */ | |
187 void revisionSelected(Revision revision) { | |
188 setSelectedRevision(revision); | |
189 } | |
190 | |
191 /** | |
192 * Updates the currently selected revision and sends out an event if it changed. | |
162 | 193 * |
129 | 194 * @param revision the newly selected revision or <code>null</code> for none |
195 */ | |
196 private void setSelectedRevision(Revision revision) { | |
197 if (revision !is fSelection) { | |
198 fSelection= revision; | |
199 fireSelectionEvent(); | |
200 } | |
201 } | |
202 | |
203 private void fireSelectionEvent() { | |
204 fIgnoreEvents= true; | |
205 try { | |
206 ISelection selection= getSelection(); | |
207 SelectionChangedEvent event= new SelectionChangedEvent(this, selection); | |
208 | |
209 Object[] listeners= fListeners.getListeners(); | |
210 for (int i= 0; i < listeners.length; i++) | |
134 | 211 (cast(ISelectionChangedListener) listeners[i]).selectionChanged(event); |
129 | 212 } finally { |
213 fIgnoreEvents= false; | |
214 } | |
215 } | |
216 } |