1 11 package org.eclipse.jface.text.link; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.NoSuchElementException ; 19 20 import org.eclipse.core.runtime.Assert; 21 22 import org.eclipse.jface.text.Position; 23 import org.eclipse.jface.text.link.LinkedPosition; 24 25 26 27 35 class TabStopIterator { 36 40 private static class SequenceComparator implements Comparator { 41 42 48 public int compare(Object o1, Object o2) { 49 LinkedPosition p1= (LinkedPosition)o1; 50 LinkedPosition p2= (LinkedPosition)o2; 51 int i= p1.getSequenceNumber() - p2.getSequenceNumber(); 52 if (i != 0) 53 return i; 54 return p1.getOffset() - p2.getOffset(); 55 } 56 57 } 58 59 60 private static final Comparator fComparator= new SequenceComparator(); 61 62 63 private final ArrayList fList; 64 65 private int fSize; 66 67 private int fIndex; 68 69 private boolean fIsCycling= false; 70 71 TabStopIterator(List positionSequence) { 72 Assert.isNotNull(positionSequence); 73 fList= new ArrayList (positionSequence); 74 Collections.sort(fList, fComparator); 75 fSize= fList.size(); 76 fIndex= -1; 77 Assert.isTrue(fSize > 0); 78 } 79 80 boolean hasNext(LinkedPosition current) { 81 return getNextIndex(current) != fSize; 82 } 83 84 private int getNextIndex(LinkedPosition current) { 85 if (current != null && fList.get(fIndex) != current) 86 return findNext(current); 87 else if (fIsCycling && fIndex == fSize - 1) 88 return 0; 89 else 90 return fIndex + 1; 92 } 93 94 102 private int findNext(LinkedPosition current) { 103 Assert.isNotNull(current); 104 int index= fList.indexOf(current); 106 if (index != -1) { 107 if (fIsCycling && index == fSize - 1) 108 return 0; 109 return index + 1; 110 } 111 112 114 LinkedPosition found= null; 116 for (Iterator it= fList.iterator(); it.hasNext(); ) { 117 LinkedPosition p= (LinkedPosition) it.next(); 118 if (p.offset > current.offset) 119 if (found == null || found.offset > p.offset) 120 found= p; 121 } 122 123 if (found != null) { 124 return fList.indexOf(found); 125 } else if (fIsCycling) { 126 return 0; 127 } else 128 return fSize; 129 } 130 131 boolean hasPrevious(LinkedPosition current) { 132 return getPreviousIndex(current) != -1; 133 } 134 135 private int getPreviousIndex(LinkedPosition current) { 136 if (current != null && fList.get(fIndex) != current) 137 return findPrevious(current); 138 else if (fIsCycling && fIndex == 0) 139 return fSize - 1; 140 else 141 return fIndex - 1; 142 } 143 144 152 private int findPrevious(LinkedPosition current) { 153 Assert.isNotNull(current); 154 int index= fList.indexOf(current); 156 if (index != -1) { 157 if (fIsCycling && index == 0) 158 return fSize - 1; 159 return index - 1; 160 } 161 162 164 LinkedPosition found= null; 166 for (Iterator it= fList.iterator(); it.hasNext(); ) { 167 LinkedPosition p= (LinkedPosition) it.next(); 168 if (p.offset < current.offset) 169 if (found == null || found.offset < p.offset) 170 found= p; 171 } 172 if (found != null) { 173 return fList.indexOf(found); 174 } else if (fIsCycling) { 175 return fSize - 1; 176 } else 177 return -1; 178 } 179 180 LinkedPosition next(LinkedPosition current) { 181 if (!hasNext(current)) 182 throw new NoSuchElementException (); 183 return (LinkedPosition) fList.get(fIndex= getNextIndex(current)); 184 } 185 186 LinkedPosition previous(LinkedPosition current) { 187 if (!hasPrevious(current)) 188 throw new NoSuchElementException (); 189 return (LinkedPosition) fList.get(fIndex= getPreviousIndex(current)); 190 } 191 192 void setCycling(boolean mode) { 193 fIsCycling= mode; 194 } 195 196 void addPosition(Position position) { 197 fList.add(fSize++, position); 198 Collections.sort(fList, fComparator); 199 } 200 201 void removePosition(Position position) { 202 if (fList.remove(position)) 203 fSize--; 204 } 205 206 209 boolean isCycling() { 210 return fIsCycling; 211 } 212 213 LinkedPosition[] getPositions() { 214 return (LinkedPosition[]) fList.toArray(new LinkedPosition[fSize]); 215 } 216 } 217 | Popular Tags |