KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > source > projection > ProjectionAnnotationModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.jface.text.source.projection;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.Position;
18 import org.eclipse.jface.text.source.Annotation;
19 import org.eclipse.jface.text.source.AnnotationModel;
20
21
22 /**
23  * A projection annotation model. It provides methods for modifying the
24  * expansion state of the managed projection annotations.
25  * <p>
26  * Do not subclass. Use it as is.
27  * </p>
28  *
29  * @since 3.0
30  */

31 public class ProjectionAnnotationModel extends AnnotationModel {
32
33
34     /**
35      * Creates a new, empty projection annotation model.
36      */

37     public ProjectionAnnotationModel() {
38     }
39
40     /**
41      * Changes the state of the given annotation to collapsed. An appropriate
42      * annotation model change event is sent out.
43      *
44      * @param annotation the annotation
45      */

46     public void collapse(Annotation annotation) {
47         if (annotation instanceof ProjectionAnnotation) {
48             ProjectionAnnotation projection= (ProjectionAnnotation) annotation;
49             if (!projection.isCollapsed()) {
50                 projection.markCollapsed();
51                 modifyAnnotation(projection, true);
52             }
53         }
54     }
55
56     /**
57      * Changes the state of the given annotation to expanded. An appropriate
58      * annotation model change event is sent out.
59      *
60      * @param annotation the annotation
61      */

62     public void expand(Annotation annotation) {
63         if (annotation instanceof ProjectionAnnotation) {
64             ProjectionAnnotation projection= (ProjectionAnnotation) annotation;
65             if (projection.isCollapsed()) {
66                 projection.markExpanded();
67                 modifyAnnotation(projection, true);
68             }
69         }
70     }
71
72     /**
73      * Toggles the expansion state of the given annotation. An appropriate
74      * annotation model change event is sent out.
75      *
76      * @param annotation the annotation
77      */

78     public void toggleExpansionState(Annotation annotation) {
79         if (annotation instanceof ProjectionAnnotation) {
80             ProjectionAnnotation projection= (ProjectionAnnotation) annotation;
81
82             if (projection.isCollapsed())
83                 projection.markExpanded();
84             else
85                 projection.markCollapsed();
86
87             modifyAnnotation(projection, true);
88         }
89     }
90
91     /**
92      * Expands all annotations that overlap with the given range and are collapsed.
93      *
94      * @param offset the range offset
95      * @param length the range length
96      * @return <code>true</code> if any annotation has been expanded, <code>false</code> otherwise
97      */

98     public boolean expandAll(int offset, int length) {
99         return expandAll(offset, length, true);
100     }
101
102     /**
103      * Collapses all annotations that overlap with the given range and are collapsed.
104      *
105      * @param offset the range offset
106      * @param length the range length
107      * @return <code>true</code> if any annotation has been collapse, <code>false</code>
108      * otherwise
109      * @since 3.2
110      */

111     public boolean collapseAll(int offset, int length) {
112
113         boolean collapsing= false;
114
115         Iterator JavaDoc iterator= getAnnotationIterator();
116         while (iterator.hasNext()) {
117             ProjectionAnnotation annotation= (ProjectionAnnotation) iterator.next();
118             if (!annotation.isCollapsed()) {
119                 Position position= getPosition(annotation);
120                 if (position != null && position.overlapsWith(offset, length) /* || is a delete at the boundary */ ) {
121                     annotation.markCollapsed();
122                     modifyAnnotation(annotation, false);
123                     collapsing= true;
124                 }
125             }
126         }
127
128         if (collapsing)
129             fireModelChanged();
130
131         return collapsing;
132     }
133     
134     /**
135      * Expands all annotations that overlap with the given range and are collapsed. Fires a model change event if
136      * requested.
137      *
138      * @param offset the offset of the range
139      * @param length the length of the range
140      * @param fireModelChanged <code>true</code> if a model change event
141      * should be fired, <code>false</code> otherwise
142      * @return <code>true</code> if any annotation has been expanded, <code>false</code> otherwise
143      */

144     protected boolean expandAll(int offset, int length, boolean fireModelChanged) {
145
146         boolean expanding= false;
147
148         Iterator JavaDoc iterator= getAnnotationIterator();
149         while (iterator.hasNext()) {
150             ProjectionAnnotation annotation= (ProjectionAnnotation) iterator.next();
151             if (annotation.isCollapsed()) {
152                 Position position= getPosition(annotation);
153                 if (position != null && position.overlapsWith(offset, length) /* || is a delete at the boundary */ ) {
154                     annotation.markExpanded();
155                     modifyAnnotation(annotation, false);
156                     expanding= true;
157                 }
158             }
159         }
160
161         if (expanding && fireModelChanged)
162             fireModelChanged();
163
164         return expanding;
165     }
166
167     /**
168      * Modifies the annotation model.
169      *
170      * @param deletions the list of deleted annotations
171      * @param additions the set of annotations to add together with their associated position
172      * @param modifications the list of modified annotations
173      */

174     public void modifyAnnotations(Annotation[] deletions, Map JavaDoc additions, Annotation[] modifications) {
175         try {
176             replaceAnnotations(deletions, additions, false);
177             if (modifications != null) {
178                 for (int i= 0; i < modifications.length; i++)
179                     modifyAnnotation(modifications[i], false);
180             }
181         } catch (BadLocationException x) {
182         }
183         fireModelChanged();
184     }
185 }
186
Popular Tags