comparison dwtx/jface/internal/text/revisions/RevisionSelectionProvider.d @ 129:eb30df5ca28b

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