comparison dwtx/jface/text/source/AnnotationModel.d @ 145:02cd5f1224d3

...
author Frank Benoit <benoit@tionex.de>
date Sun, 24 Aug 2008 22:31:00 +0200
parents 26688fec6d23
children 75302ef3f92f
comparison
equal deleted inserted replaced
144:16a71f577815 145:02cd5f1224d3
107 public class AnnotationModel : IAnnotationModel, IAnnotationModelExtension, IAnnotationModelExtension2, ISynchronizable { 107 public class AnnotationModel : IAnnotationModel, IAnnotationModelExtension, IAnnotationModelExtension2, ISynchronizable {
108 108
109 109
110 /** 110 /**
111 * Iterator that returns the annotations for a given region. 111 * Iterator that returns the annotations for a given region.
112 * 112 *
113 * @since 3.4 113 * @since 3.4
114 * @see AnnotationModel.RegionIterator#RegionIterator(Iterator, IAnnotationModel, int, int, bool, bool) 114 * @see AnnotationModel.RegionIterator#RegionIterator(Iterator, IAnnotationModel, int, int, bool, bool)
115 */ 115 */
116 private static final class RegionIterator : Iterator { 116 private static final class RegionIterator : Iterator {
117 117
118 private final Iterator fParentIterator; 118 private final Iterator fParentIterator;
119 private final bool fCanEndAfter; 119 private final bool fCanEndAfter;
120 private final bool fCanStartBefore; 120 private final bool fCanStartBefore;
121 private final IAnnotationModel fModel; 121 private final IAnnotationModel fModel;
122 private Object fNext; 122 private Object fNext;
123 private Position fRegion; 123 private Position fRegion;
124 124
125 /** 125 /**
126 * Iterator that returns all annotations from the parent iterator which 126 * Iterator that returns all annotations from the parent iterator which
127 * have a position in the given model inside the given region. 127 * have a position in the given model inside the given region.
128 * <p> 128 * <p>
129 * See {@link IAnnotationModelExtension2} for a definition of inside. 129 * See {@link IAnnotationModelExtension2} for a definition of inside.
130 * </p> 130 * </p>
131 * 131 *
132 * @param parentIterator iterator containing all annotations 132 * @param parentIterator iterator containing all annotations
133 * @param model the model to use to retrieve positions from for each 133 * @param model the model to use to retrieve positions from for each
134 * annotation 134 * annotation
135 * @param offset start position of the region 135 * @param offset start position of the region
136 * @param length length of the region 136 * @param length length of the region
144 fRegion= new Position(offset, length); 144 fRegion= new Position(offset, length);
145 fCanEndAfter= canEndAfter; 145 fCanEndAfter= canEndAfter;
146 fCanStartBefore= canStartBefore; 146 fCanStartBefore= canStartBefore;
147 fNext= findNext(); 147 fNext= findNext();
148 } 148 }
149 149
150 /* 150 /*
151 * @see java.util.Iterator#hasNext() 151 * @see java.util.Iterator#hasNext()
152 */ 152 */
153 public bool hasNext() { 153 public bool hasNext() {
154 return fNext !is null; 154 return fNext !is null;
155 } 155 }
156 156
157 /* 157 /*
158 * @see java.util.Iterator#next() 158 * @see java.util.Iterator#next()
159 */ 159 */
160 public Object next() { 160 public Object next() {
161 if (!hasNext()) 161 if (!hasNext())
162 throw new NoSuchElementException(); 162 throw new NoSuchElementException();
163 163
164 Object result= fNext; 164 Object result= fNext;
165 fNext= findNext(); 165 fNext= findNext();
166 return result; 166 return result;
167 } 167 }
168 168
169 /* 169 /*
170 * @see java.util.Iterator#remove() 170 * @see java.util.Iterator#remove()
171 */ 171 */
172 public void remove() { 172 public void remove() {
173 throw new UnsupportedOperationException(); 173 throw new UnsupportedOperationException();
174 } 174 }
175 175
176 private Object findNext() { 176 private Object findNext() {
177 while (fParentIterator.hasNext()) { 177 while (fParentIterator.hasNext()) {
178 Annotation next= cast(Annotation) fParentIterator.next(); 178 Annotation next= cast(Annotation) fParentIterator.next();
179 Position position= fModel.getPosition(next); 179 Position position= fModel.getPosition(next);
180 if (position !is null) { 180 if (position !is null) {
195 return fRegion.includes(start); 195 return fRegion.includes(start);
196 else 196 else
197 return fRegion.includes(start) && fRegion.includes(start + length - 1); 197 return fRegion.includes(start) && fRegion.includes(start + length - 1);
198 } 198 }
199 } 199 }
200 200
201 /** 201 /**
202 * An iterator iteration over a Positions and mapping positions to 202 * An iterator iteration over a Positions and mapping positions to
203 * annotations using a provided map if the provided map contains the element. 203 * annotations using a provided map if the provided map contains the element.
204 * 204 *
205 * @since 3.4 205 * @since 3.4
206 */ 206 */
207 private static final class AnnotationsInterator : Iterator { 207 private static final class AnnotationsInterator : Iterator {
208 208
209 private Object fNext; 209 private Object fNext;
210 private final Position[] fPositions; 210 private final Position[] fPositions;
211 private int fIndex; 211 private int fIndex;
212 private final Map fMap; 212 private final Map fMap;
213 213
214 /** 214 /**
215 * @param positions positions to iterate over 215 * @param positions positions to iterate over
216 * @param map a map to map positions to annotations 216 * @param map a map to map positions to annotations
217 */ 217 */
218 public this(Position[] positions, Map map) { 218 public this(Position[] positions, Map map) {
219 fPositions= positions; 219 fPositions= positions;
220 fIndex= 0; 220 fIndex= 0;
221 fMap= map; 221 fMap= map;
222 fNext= findNext(); 222 fNext= findNext();
223 } 223 }
224 224
225 /* (non-Javadoc) 225 /* (non-Javadoc)
226 * @see java.util.Iterator#hasNext() 226 * @see java.util.Iterator#hasNext()
227 */ 227 */
228 public bool hasNext() { 228 public bool hasNext() {
229 return fNext !is null; 229 return fNext !is null;
230 } 230 }
231 231
232 /* (non-Javadoc) 232 /* (non-Javadoc)
233 * @see java.util.Iterator#next() 233 * @see java.util.Iterator#next()
234 */ 234 */
235 public Object next() { 235 public Object next() {
236 Object result= fNext; 236 Object result= fNext;
237 fNext= findNext(); 237 fNext= findNext();
238 return result; 238 return result;
239 } 239 }
240 240
241 /* (non-Javadoc) 241 /* (non-Javadoc)
242 * @see java.util.Iterator#remove() 242 * @see java.util.Iterator#remove()
243 */ 243 */
244 public void remove() { 244 public void remove() {
245 throw new UnsupportedOperationException(); 245 throw new UnsupportedOperationException();
246 } 246 }
247 247
248 private Object findNext() { 248 private Object findNext() {
249 while (fIndex < fPositions.length) { 249 while (fIndex < fPositions.length) {
250 Position position= fPositions[fIndex]; 250 Position position= fPositions[fIndex];
251 fIndex++; 251 fIndex++;
252 if (fMap.containsKey(position)) 252 if (fMap.containsKey(position))
253 return fMap.get(position); 253 return fMap.get(position);
254 } 254 }
255 255
256 return null; 256 return null;
257 } 257 }
258 } 258 }
259 259
260 /** 260 /**
738 738
739 if (fireModelChanged && forkNotification) { 739 if (fireModelChanged && forkNotification) {
740 removeAnnotations(deleted, false, false); 740 removeAnnotations(deleted, false, false);
741 synchronized (getLockObject()) { 741 synchronized (getLockObject()) {
742 if (fModelEvent !is null) 742 if (fModelEvent !is null)
743 new class() Thread { 743 (new Thread ( &fireModelChanged() )).start();
744 public void run() {
745 fireModelChanged();
746 }
747 }.start();
748 } 744 }
749 } else 745 } else
750 removeAnnotations(deleted, fireModelChanged, false); 746 removeAnnotations(deleted, fireModelChanged, false);
751 } 747 }
752 } 748 }
758 return getAnnotationIterator(true, true); 754 return getAnnotationIterator(true, true);
759 } 755 }
760 756
761 /** 757 /**
762 * {@inheritDoc} 758 * {@inheritDoc}
763 * 759 *
764 * @since 3.4 760 * @since 3.4
765 */ 761 */
766 public Iterator getAnnotationIterator(int offset, int length, bool canStartBefore, bool canEndAfter) { 762 public Iterator getAnnotationIterator(int offset, int length, bool canStartBefore, bool canEndAfter) {
767 Iterator regionIterator= getRegionAnnotationIterator(offset, length, canStartBefore, canEndAfter); 763 Iterator regionIterator= getRegionAnnotationIterator(offset, length, canStartBefore, canEndAfter);
768 764
769 if (fAttachments.isEmpty()) 765 if (fAttachments.isEmpty())
770 return regionIterator; 766 return regionIterator;
771 767
772 List iterators= new ArrayList(fAttachments.size() + 1); 768 List iterators= new ArrayList(fAttachments.size() + 1);
773 iterators.add(regionIterator); 769 iterators.add(regionIterator);
774 Iterator it= fAttachments.keySet().iterator(); 770 Iterator it= fAttachments.keySet().iterator();
775 while (it.hasNext()) { 771 while (it.hasNext()) {
776 IAnnotationModel attachment= cast(IAnnotationModel) fAttachments.get(it.next()); 772 IAnnotationModel attachment= cast(IAnnotationModel) fAttachments.get(it.next());
777 if ( cast(IAnnotationModelExtension2)attachment ) 773 if ( cast(IAnnotationModelExtension2)attachment )
778 iterators.add((cast(IAnnotationModelExtension2) attachment).getAnnotationIterator(offset, length, canStartBefore, canEndAfter)); 774 iterators.add((cast(IAnnotationModelExtension2) attachment).getAnnotationIterator(offset, length, canStartBefore, canEndAfter));
779 else 775 else
780 iterators.add(new RegionIterator(attachment.getAnnotationIterator(), attachment, offset, length, canStartBefore, canEndAfter)); 776 iterators.add(new RegionIterator(attachment.getAnnotationIterator(), attachment, offset, length, canStartBefore, canEndAfter));
781 } 777 }
782 778
783 return new MetaIterator(iterators.iterator()); 779 return new MetaIterator(iterators.iterator());
784 } 780 }
785 781
786 /** 782 /**
787 * Returns an iterator as specified in {@link IAnnotationModelExtension2#getAnnotationIterator(int, int, bool, bool)} 783 * Returns an iterator as specified in {@link IAnnotationModelExtension2#getAnnotationIterator(int, int, bool, bool)}
788 * 784 *
789 * @param offset region start 785 * @param offset region start
790 * @param length region length 786 * @param length region length
791 * @param canStartBefore position can start before region 787 * @param canStartBefore position can start before region
792 * @param canEndAfter position can end after region 788 * @param canEndAfter position can end after region
793 * @return an iterator to iterate over annotations in region 789 * @return an iterator to iterate over annotations in region
795 * @since 3.4 791 * @since 3.4
796 */ 792 */
797 private Iterator getRegionAnnotationIterator(int offset, int length, bool canStartBefore, bool canEndAfter) { 793 private Iterator getRegionAnnotationIterator(int offset, int length, bool canStartBefore, bool canEndAfter) {
798 if (!( cast(AbstractDocument)fDocument )) 794 if (!( cast(AbstractDocument)fDocument ))
799 return new RegionIterator(getAnnotationIterator(true), this, offset, length, canStartBefore, canEndAfter); 795 return new RegionIterator(getAnnotationIterator(true), this, offset, length, canStartBefore, canEndAfter);
800 796
801 AbstractDocument document= cast(AbstractDocument) fDocument; 797 AbstractDocument document= cast(AbstractDocument) fDocument;
802 cleanup(true); 798 cleanup(true);
803 799
804 try { 800 try {
805 Position[] positions= document.getPositions(IDocument.DEFAULT_CATEGORY, offset, length, canStartBefore, canEndAfter); 801 Position[] positions= document.getPositions(IDocument.DEFAULT_CATEGORY, offset, length, canStartBefore, canEndAfter);
806 return new AnnotationsInterator(positions, fPositions); 802 return new AnnotationsInterator(positions, fPositions);
807 } catch (BadPositionCategoryException e) { 803 } catch (BadPositionCategoryException e) {
808 //can not happen 804 //can not happen