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