Mercurial > projects > dwt-addons
annotate dwtx/jface/text/link/LinkedPositionAnnotations.d @ 200:eb3414669eb0 default tip
fix for dmd 1.041 and tango 0.99.8
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 28 Mar 2009 03:09:57 +0100 |
parents | 1a5b8f8129df |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 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.text.link.LinkedPositionAnnotations; | |
14 | |
131 | 15 import dwtx.jface.text.link.LinkedModeModel; // packageimport |
16 import dwtx.jface.text.link.LinkedPosition; // packageimport | |
17 import dwtx.jface.text.link.ILinkedModeListener; // packageimport | |
18 import dwtx.jface.text.link.TabStopIterator; // packageimport | |
19 import dwtx.jface.text.link.LinkedModeUI; // packageimport | |
20 import dwtx.jface.text.link.InclusivePositionUpdater; // packageimport | |
21 import dwtx.jface.text.link.LinkedPositionGroup; // packageimport | |
22 import dwtx.jface.text.link.LinkedModeManager; // packageimport | |
23 import dwtx.jface.text.link.ProposalPosition; // packageimport | |
24 | |
25 | |
129 | 26 import dwt.dwthelper.utils; |
27 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
28 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
29 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
30 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
31 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
32 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
33 |
129 | 34 |
35 import dwtx.core.runtime.Assert; | |
36 import dwtx.jface.text.BadLocationException; | |
37 import dwtx.jface.text.IDocument; | |
38 import dwtx.jface.text.Position; | |
39 import dwtx.jface.text.source.Annotation; | |
40 import dwtx.jface.text.source.AnnotationModel; | |
41 | |
42 /** | |
43 * Internal class. | |
44 * | |
45 * @since 3.0 | |
46 */ | |
47 final class LinkedPositionAnnotations : AnnotationModel { | |
48 | |
49 /* annotation types */ | |
147 | 50 private static const String TARGET_ANNOTATION_TYPE= "dwtx.ui.internal.workbench.texteditor.link.target"; //$NON-NLS-1$ |
51 private static const String SLAVE_ANNOTATION_TYPE= "dwtx.ui.internal.workbench.texteditor.link.slave"; //$NON-NLS-1$ | |
52 private static const String FOCUS_ANNOTATION_TYPE= "dwtx.ui.internal.workbench.texteditor.link.master"; //$NON-NLS-1$ | |
53 private static const String EXIT_ANNOTATION_TYPE= "dwtx.ui.internal.workbench.texteditor.link.exit"; //$NON-NLS-1$ | |
129 | 54 |
55 /* configuration */ | |
56 private bool fMarkTargets= true; | |
57 private bool fMarkSlaves= true; | |
58 private bool fMarkFocus= true; | |
59 private bool fMarkExitTarget= true; | |
60 | |
61 private Annotation fFocusAnnotation= null; | |
62 private Annotation fExitAnnotation= null; | |
162 | 63 private const Map fGroupAnnotations; |
64 private const Map fTargetAnnotations; | |
65 private Position[] fTargets; | |
129 | 66 private LinkedPosition fExitPosition= null; |
67 | |
162 | 68 public this(){ |
69 fGroupAnnotations= new HashMap(); | |
70 fTargetAnnotations= new HashMap(); | |
71 } | |
72 | |
129 | 73 /** |
74 * Sets the position that should be highlighted as the focus position, i.e. | |
75 * as the position whose changes are propagated to all its linked positions | |
76 * by the linked environment. | |
77 * | |
78 * @param position the new focus position, or <code>null</code> if no focus is set. | |
79 * @throws BadLocationException if <code>position</code> is invalid | |
80 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
81 private void setFocusPosition(Position position) { |
129 | 82 if (fMarkFocus && getPosition(fFocusAnnotation) !is position) { |
83 removeAnnotation(fFocusAnnotation, false); | |
84 if (position !is null) { | |
85 fFocusAnnotation= new Annotation(FOCUS_ANNOTATION_TYPE, false, ""); //$NON-NLS-1$ | |
86 addAnnotation(fFocusAnnotation, position, false); | |
87 } else | |
88 fFocusAnnotation= null; | |
89 } | |
90 } | |
91 | |
92 /** | |
93 * Sets the position that should be highlighted as the exit position, i.e. | |
94 * as the position whose changes are propagated to all its linked positions | |
95 * by the linked environment. | |
96 * | |
97 * @param position the new exit position, or <code>null</code> if no focus is set. | |
98 * @throws BadLocationException in case <code>position</code> is invalid | |
99 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
100 private void setExitPosition(Position position) { |
129 | 101 if (fMarkExitTarget && getPosition(fExitAnnotation) !is position) { |
102 removeAnnotation(fExitAnnotation, false); | |
103 if (position !is null) { | |
104 fExitAnnotation= new Annotation(EXIT_ANNOTATION_TYPE, false, ""); //$NON-NLS-1$ | |
105 addAnnotation(fExitAnnotation, position, false); | |
106 } else | |
107 fExitAnnotation= null; | |
108 } | |
109 } | |
110 | |
111 /** | |
112 * Sets the positions that should be highlighted as the slave positions, i.e. | |
113 * as the positions that are linked to the focus position. | |
114 * | |
115 * @param positions the new slave positions, or <code>null</code> if no slave positions are to be set | |
116 * @throws BadLocationException in case any of the given positions is invalid | |
117 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
118 private void setGroupPositions(List positions) { |
129 | 119 if (!fMarkSlaves) |
120 return; | |
121 | |
122 // remove all positions which are already there | |
123 // Algorithm: toRemove contains all mappings at first, but all that are in | |
124 // positions get removed -> toRemove contains the difference set of previous - new | |
125 // toAdd are the new positions, which don't exist in previous = new - previous | |
126 List toRemove= new ArrayList(fGroupAnnotations.values()); | |
127 Map toAdd= new HashMap(); | |
128 if (positions !is null) { | |
129 for (Iterator iter= positions.iterator(); iter.hasNext();) { | |
134 | 130 Position p= cast(Position) iter.next(); |
129 | 131 if (fGroupAnnotations.containsKey(p)) { |
132 toRemove.remove(fGroupAnnotations.get(p)); | |
133 } else { | |
134 Annotation a= new Annotation(SLAVE_ANNOTATION_TYPE, false, ""); //$NON-NLS-1$ | |
135 toAdd.put(a, p); | |
136 fGroupAnnotations.put(p, a); | |
137 } | |
138 } | |
139 } | |
140 fGroupAnnotations.values().removeAll(toRemove); | |
141 | |
162 | 142 replaceAnnotations(arraycast!(Annotation)( toRemove.toArray()), toAdd, false); |
129 | 143 } |
144 | |
145 /** | |
146 * Sets the positions that should be highlighted as the target positions, i.e. | |
147 * as the positions that can be jumped to in a linked set up. | |
148 * | |
149 * @param positions the new target positions, or <code>null</code> if no target positions are to be set | |
150 * @throws BadLocationException in case any of the given positions is invalid | |
151 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
152 private void setTargetPositions(List positions) { |
129 | 153 if (!fMarkTargets) |
154 return; | |
155 | |
156 // remove all positions which are already there | |
157 // Algorithm: toRemove contains all mappings at first, but all that are in | |
158 // positions get removed -> toRemove contains the difference set of previous - new | |
159 // toAdd are the new positions, which don't exist in previous = new - previous | |
160 List toRemove= new ArrayList(fTargetAnnotations.values()); | |
161 Map toAdd= new HashMap(); | |
162 if (positions !is null) { | |
163 for (Iterator iter= positions.iterator(); iter.hasNext();) { | |
134 | 164 Position p= cast(Position) iter.next(); |
129 | 165 if (fTargetAnnotations.containsKey(p)) { |
166 toRemove.remove(fTargetAnnotations.get(p)); | |
167 } else { | |
168 Annotation a= new Annotation(TARGET_ANNOTATION_TYPE, false, ""); //$NON-NLS-1$ | |
169 toAdd.put(a, p); | |
170 fTargetAnnotations.put(p, a); | |
171 } | |
172 } | |
173 } | |
174 fTargetAnnotations.values().removeAll(toRemove); | |
175 | |
162 | 176 replaceAnnotations(arraycast!(Annotation)( toRemove.toArray()), toAdd, false); |
129 | 177 } |
178 | |
179 /** | |
180 * Switches the focus position to <code>position</code> given the | |
181 * <code>LinkedModeModel env</code>. The slave positions for <code>position</code> | |
182 * is extracted from the environment and set accordingly, the target positions | |
183 * are updated as well. | |
184 * | |
185 * @param env | |
186 * @param position | |
187 */ | |
188 public void switchToPosition(LinkedModeModel env, LinkedPosition position) { | |
189 if (fDocument is null || | |
190 (position !is null && getPosition(fFocusAnnotation) is position) || | |
191 (position is null && fFocusAnnotation is null)) | |
192 return; | |
193 | |
194 LinkedPositionGroup linkedGroup= null; | |
195 if (position !is null) | |
196 linkedGroup= env.getGroupForPosition(position); | |
197 | |
198 List targets= new ArrayList(); | |
199 targets.addAll(Arrays.asList(fTargets)); | |
200 | |
201 List group; | |
202 if (linkedGroup !is null) | |
203 group= new ArrayList(Arrays.asList(linkedGroup.getPositions())); | |
204 else | |
205 group= new ArrayList(); | |
206 | |
162 | 207 if (position is null || !(cast(Object)fDocument).opEquals(cast(Object)position.getDocument())) |
129 | 208 // position is not valid if not in this document |
209 position= null; | |
210 | |
211 LinkedPosition exit= fExitPosition; | |
162 | 212 if (exit is null || !(cast(Object)fDocument).opEquals(cast(Object)exit.getDocument())) |
129 | 213 // position is not valid if not in this document |
214 exit= null; | |
215 | |
216 | |
217 if (exit !is null) { | |
218 group.remove(exit); | |
219 targets.remove(exit); | |
220 } | |
221 | |
222 group.removeAll(targets); | |
223 targets.remove(position); | |
224 group.remove(position); | |
225 prune(targets); | |
226 prune(group); | |
227 | |
228 try { | |
229 setFocusPosition(position); | |
230 setExitPosition(exit); | |
231 setGroupPositions(group); | |
232 setTargetPositions(targets); | |
233 } catch (BadLocationException e) { | |
234 // will never happen as we don't actually add/remove positions from the document | |
235 // see the addPosition / removePosition methods | |
236 Assert.isTrue(false); | |
237 } | |
238 fireModelChanged(); | |
239 | |
240 } | |
241 | |
242 /** | |
243 * Prune <code>list</code> of all <code>LinkedPosition</code>s that | |
244 * do not belong to this model's <code>IDocument</code>. | |
245 * | |
246 * @param list the list of positions to prune | |
247 */ | |
248 private void prune(List list) { | |
249 for (Iterator iter= list.iterator(); iter.hasNext();) { | |
134 | 250 LinkedPosition pos= cast(LinkedPosition) iter.next(); |
162 | 251 if (!(cast(Object)pos.getDocument()).opEquals(cast(Object)fDocument)) |
129 | 252 iter.remove(); |
253 } | |
254 } | |
255 | |
256 /** | |
257 * Sets the target positions | |
258 * @param positions | |
259 */ | |
260 public void setTargets(Position[] positions) { | |
261 fTargets= positions; | |
262 } | |
263 | |
264 /** | |
265 * Sets the exit position. | |
266 * | |
267 * @param position the new exit position, or <code>null</code> if no exit position should be set | |
268 */ | |
269 public void setExitTarget(LinkedPosition position) { | |
270 fExitPosition = position; | |
271 } | |
272 | |
273 /* | |
274 * @see dwtx.jface.text.source.AnnotationModel#addPosition(dwtx.jface.text.IDocument, dwtx.jface.text.Position) | |
275 */ | |
276 protected void addPosition(IDocument document, Position position) { | |
277 // don't to anything as our positions are managed by custom | |
278 // position updaters | |
279 } | |
280 | |
281 /* | |
282 * @see dwtx.jface.text.source.AnnotationModel#removePosition(dwtx.jface.text.IDocument, dwtx.jface.text.Position) | |
283 */ | |
284 protected void removePosition(IDocument document, Position pos) { | |
285 // don't to anything as our positions are managed by custom | |
286 // position updaters | |
287 } | |
288 | |
289 /* | |
290 * @see dwtx.jface.text.source.AnnotationModel#fireModelChanged() | |
291 */ | |
292 public void fireModelChanged() { | |
293 super.fireModelChanged(); | |
294 } | |
295 | |
296 /** | |
297 * Sets the drawing state for the exit target. Default is <code>true</code>. | |
298 * | |
299 * @param markExitTargets the new drawing state for exit targets | |
300 */ | |
301 public void markExitTarget(bool markExitTargets) { | |
302 fMarkExitTarget= markExitTargets; | |
303 } | |
304 | |
305 /** | |
306 * Sets the drawing state for the focus position. Default is <code>true</code>. | |
307 * | |
308 * @param markFocus the new drawing state for exit targets | |
309 */ | |
310 public void markFocus(bool markFocus) { | |
311 fMarkFocus= markFocus; | |
312 } | |
313 | |
314 /** | |
315 * Sets the drawing state for slave positions. Default is <code>true</code>. | |
316 * | |
317 * @param markSlaves the new drawing state for slaves | |
318 */ | |
319 public void markSlaves(bool markSlaves) { | |
320 fMarkSlaves= markSlaves; | |
321 } | |
322 | |
323 /** | |
324 * Sets the drawing state for targets. Default is <code>true</code>. | |
325 * | |
326 * @param markTargets the new drawing state for targets | |
327 */ | |
328 public void markTargets(bool markTargets) { | |
329 fMarkTargets= markTargets; | |
330 } | |
331 } |