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