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