Mercurial > projects > dwt-addons
annotate dwtx/jface/text/link/TabStopIterator.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | f70d9508c95c |
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.TabStopIterator; | |
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.LinkedModeUI; // packageimport | |
19 import dwtx.jface.text.link.InclusivePositionUpdater; // packageimport | |
20 import dwtx.jface.text.link.LinkedPositionGroup; // packageimport | |
21 import dwtx.jface.text.link.LinkedModeManager; // packageimport | |
22 import dwtx.jface.text.link.LinkedPositionAnnotations; // packageimport | |
23 import dwtx.jface.text.link.ProposalPosition; // packageimport | |
24 | |
25 | |
129 | 26 import dwt.dwthelper.utils; |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
147
diff
changeset
|
27 import dwtx.dwtxhelper.Collection; |
162 | 28 import tango.core.Exception; |
129 | 29 |
30 import dwtx.core.runtime.Assert; | |
31 import dwtx.jface.text.Position; | |
32 | |
33 | |
34 | |
35 /** | |
36 * Iterator that leaps over the double occurrence of an element when switching from forward | |
37 * to backward iteration that is shown by <code>ListIterator</code>. | |
38 * <p> | |
39 * Package private, only for use by LinkedModeUI. | |
40 * </p> | |
41 * @since 3.0 | |
42 */ | |
43 class TabStopIterator { | |
44 /** | |
45 * Comparator for <code>LinkedPosition</code>s. If the sequence number of two positions is equal, the | |
46 * offset is used. | |
47 */ | |
48 private static class SequenceComparator : Comparator { | |
49 | |
50 /** | |
51 * {@inheritDoc} | |
52 * | |
53 * <p><code>o1</code> and <code>o2</code> are required to be instances | |
54 * of <code>LinkedPosition</code>.</p> | |
55 */ | |
56 public int compare(Object o1, Object o2) { | |
134 | 57 LinkedPosition p1= cast(LinkedPosition)o1; |
58 LinkedPosition p2= cast(LinkedPosition)o2; | |
129 | 59 int i= p1.getSequenceNumber() - p2.getSequenceNumber(); |
60 if (i !is 0) | |
61 return i; | |
62 return p1.getOffset() - p2.getOffset(); | |
63 } | |
64 | |
65 } | |
66 | |
67 /** The comparator to sort the list of positions. */ | |
162 | 68 private static Comparator fComparator_; |
69 private static Comparator fComparator(){ | |
70 if( fComparator_ is null ){ | |
71 synchronized( TabStopIterator.classinfo ){ | |
72 if( fComparator_ is null ){ | |
73 fComparator_ = new SequenceComparator(); | |
74 } | |
75 } | |
76 } | |
77 return fComparator_; | |
78 } | |
129 | 79 |
80 /** The iteration sequence. */ | |
146 | 81 private const ArrayList fList; |
129 | 82 /** The size of <code>fList</code>. */ |
83 private int fSize; | |
84 /** Index of the current element, to the first one initially. */ | |
85 private int fIndex; | |
86 /** Cycling property. */ | |
87 private bool fIsCycling= false; | |
88 | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
89 this(List positionSequence) { |
162 | 90 Assert.isNotNull(cast(Object)positionSequence); |
129 | 91 fList= new ArrayList(positionSequence); |
92 Collections.sort(fList, fComparator); | |
93 fSize= fList.size(); | |
94 fIndex= -1; | |
95 Assert.isTrue(fSize > 0); | |
96 } | |
97 | |
98 bool hasNext(LinkedPosition current) { | |
99 return getNextIndex(current) !is fSize; | |
100 } | |
101 | |
102 private int getNextIndex(LinkedPosition current) { | |
103 if (current !is null && fList.get(fIndex) !is current) | |
104 return findNext(current); | |
105 else if (fIsCycling && fIndex is fSize - 1) | |
106 return 0; | |
107 else | |
108 // default: increase | |
109 return fIndex + 1; | |
110 } | |
111 | |
112 /** | |
113 * Finds the closest position in the iteration set that follows after | |
114 * <code>current</code> and sets <code>fIndex</code> accordingly. If <code>current</code> | |
115 * is in the iteration set, the next in turn is chosen. | |
116 * | |
117 * @param current the current position | |
118 * @return <code>true</code> if there is a next position, <code>false</code> otherwise | |
119 */ | |
120 private int findNext(LinkedPosition current) { | |
121 Assert.isNotNull(current); | |
122 // if the position is in the iteration set, jump to the next one | |
123 int index= fList.indexOf(current); | |
124 if (index !is -1) { | |
125 if (fIsCycling && index is fSize - 1) | |
126 return 0; | |
127 return index + 1; | |
128 } | |
129 | |
130 // index is -1 | |
131 | |
132 // find the position that follows closest to the current position | |
133 LinkedPosition found= null; | |
134 for (Iterator it= fList.iterator(); it.hasNext(); ) { | |
134 | 135 LinkedPosition p= cast(LinkedPosition) it.next(); |
129 | 136 if (p.offset > current.offset) |
137 if (found is null || found.offset > p.offset) | |
138 found= p; | |
139 } | |
140 | |
141 if (found !is null) { | |
142 return fList.indexOf(found); | |
143 } else if (fIsCycling) { | |
144 return 0; | |
145 } else | |
146 return fSize; | |
147 } | |
148 | |
149 bool hasPrevious(LinkedPosition current) { | |
150 return getPreviousIndex(current) !is -1; | |
151 } | |
152 | |
153 private int getPreviousIndex(LinkedPosition current) { | |
154 if (current !is null && fList.get(fIndex) !is current) | |
155 return findPrevious(current); | |
156 else if (fIsCycling && fIndex is 0) | |
157 return fSize - 1; | |
158 else | |
159 return fIndex - 1; | |
160 } | |
161 | |
162 /** | |
163 * Finds the closest position in the iteration set that precedes | |
164 * <code>current</code>. If <code>current</code> | |
165 * is in the iteration set, the previous in turn is chosen. | |
166 * | |
167 * @param current the current position | |
168 * @return the index of the previous position | |
169 */ | |
170 private int findPrevious(LinkedPosition current) { | |
171 Assert.isNotNull(current); | |
172 // if the position is in the iteration set, jump to the next one | |
173 int index= fList.indexOf(current); | |
174 if (index !is -1) { | |
175 if (fIsCycling && index is 0) | |
176 return fSize - 1; | |
177 return index - 1; | |
178 } | |
179 | |
180 // index is -1 | |
181 | |
182 // find the position that follows closest to the current position | |
183 LinkedPosition found= null; | |
184 for (Iterator it= fList.iterator(); it.hasNext(); ) { | |
134 | 185 LinkedPosition p= cast(LinkedPosition) it.next(); |
129 | 186 if (p.offset < current.offset) |
187 if (found is null || found.offset < p.offset) | |
188 found= p; | |
189 } | |
190 if (found !is null) { | |
191 return fList.indexOf(found); | |
192 } else if (fIsCycling) { | |
193 return fSize - 1; | |
194 } else | |
195 return -1; | |
196 } | |
197 | |
198 LinkedPosition next(LinkedPosition current) { | |
199 if (!hasNext(current)) | |
162 | 200 throw new NoSuchElementException(null); |
134 | 201 return cast(LinkedPosition) fList.get(fIndex= getNextIndex(current)); |
129 | 202 } |
203 | |
204 LinkedPosition previous(LinkedPosition current) { | |
205 if (!hasPrevious(current)) | |
162 | 206 throw new NoSuchElementException(null); |
134 | 207 return cast(LinkedPosition) fList.get(fIndex= getPreviousIndex(current)); |
129 | 208 } |
209 | |
210 void setCycling(bool mode) { | |
211 fIsCycling= mode; | |
212 } | |
213 | |
214 void addPosition(Position position) { | |
215 fList.add(fSize++, position); | |
216 Collections.sort(fList, fComparator); | |
217 } | |
218 | |
219 void removePosition(Position position) { | |
220 if (fList.remove(position)) | |
221 fSize--; | |
222 } | |
223 | |
224 /** | |
225 * @return Returns the isCycling. | |
226 */ | |
227 bool isCycling() { | |
228 return fIsCycling; | |
229 } | |
230 | |
231 LinkedPosition[] getPositions() { | |
162 | 232 return arraycast!(LinkedPosition)( fList.toArray()); |
129 | 233 } |
234 } |