129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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.source.projection.ProjectionAnnotationModel;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.Iterator;
|
|
18 import java.util.Map;
|
|
19
|
|
20 import dwtx.jface.text.BadLocationException;
|
|
21 import dwtx.jface.text.Position;
|
|
22 import dwtx.jface.text.source.Annotation;
|
|
23 import dwtx.jface.text.source.AnnotationModel;
|
|
24
|
|
25
|
|
26 /**
|
|
27 * A projection annotation model. It provides methods for modifying the
|
|
28 * expansion state of the managed projection annotations.
|
|
29 * <p>
|
|
30 * Do not subclass. Use it as is.
|
|
31 * </p>
|
|
32 *
|
|
33 * @since 3.0
|
|
34 * @noextend This class is not intended to be subclassed by clients.
|
|
35 */
|
|
36 public class ProjectionAnnotationModel : AnnotationModel {
|
|
37
|
|
38
|
|
39 /**
|
|
40 * Creates a new, empty projection annotation model.
|
|
41 */
|
|
42 public ProjectionAnnotationModel() {
|
|
43 }
|
|
44
|
|
45 /**
|
|
46 * Changes the state of the given annotation to collapsed. An appropriate
|
|
47 * annotation model change event is sent out.
|
|
48 *
|
|
49 * @param annotation the annotation
|
|
50 */
|
|
51 public void collapse(Annotation annotation) {
|
|
52 if (annotation instanceof ProjectionAnnotation) {
|
|
53 ProjectionAnnotation projection= (ProjectionAnnotation) annotation;
|
|
54 if (!projection.isCollapsed()) {
|
|
55 projection.markCollapsed();
|
|
56 modifyAnnotation(projection, true);
|
|
57 }
|
|
58 }
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * Changes the state of the given annotation to expanded. An appropriate
|
|
63 * annotation model change event is sent out.
|
|
64 *
|
|
65 * @param annotation the annotation
|
|
66 */
|
|
67 public void expand(Annotation annotation) {
|
|
68 if (annotation instanceof ProjectionAnnotation) {
|
|
69 ProjectionAnnotation projection= (ProjectionAnnotation) annotation;
|
|
70 if (projection.isCollapsed()) {
|
|
71 projection.markExpanded();
|
|
72 modifyAnnotation(projection, true);
|
|
73 }
|
|
74 }
|
|
75 }
|
|
76
|
|
77 /**
|
|
78 * Toggles the expansion state of the given annotation. An appropriate
|
|
79 * annotation model change event is sent out.
|
|
80 *
|
|
81 * @param annotation the annotation
|
|
82 */
|
|
83 public void toggleExpansionState(Annotation annotation) {
|
|
84 if (annotation instanceof ProjectionAnnotation) {
|
|
85 ProjectionAnnotation projection= (ProjectionAnnotation) annotation;
|
|
86
|
|
87 if (projection.isCollapsed())
|
|
88 projection.markExpanded();
|
|
89 else
|
|
90 projection.markCollapsed();
|
|
91
|
|
92 modifyAnnotation(projection, true);
|
|
93 }
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * Expands all annotations that overlap with the given range and are collapsed.
|
|
98 *
|
|
99 * @param offset the range offset
|
|
100 * @param length the range length
|
|
101 * @return <code>true</code> if any annotation has been expanded, <code>false</code> otherwise
|
|
102 */
|
|
103 public bool expandAll(int offset, int length) {
|
|
104 return expandAll(offset, length, true);
|
|
105 }
|
|
106
|
|
107 /**
|
|
108 * Collapses all annotations that overlap with the given range and are collapsed.
|
|
109 *
|
|
110 * @param offset the range offset
|
|
111 * @param length the range length
|
|
112 * @return <code>true</code> if any annotation has been collapse, <code>false</code>
|
|
113 * otherwise
|
|
114 * @since 3.2
|
|
115 */
|
|
116 public bool collapseAll(int offset, int length) {
|
|
117
|
|
118 bool collapsing= false;
|
|
119
|
|
120 Iterator iterator= getAnnotationIterator();
|
|
121 while (iterator.hasNext()) {
|
|
122 ProjectionAnnotation annotation= (ProjectionAnnotation) iterator.next();
|
|
123 if (!annotation.isCollapsed()) {
|
|
124 Position position= getPosition(annotation);
|
|
125 if (position !is null && position.overlapsWith(offset, length) /* || is a delete at the boundary */ ) {
|
|
126 annotation.markCollapsed();
|
|
127 modifyAnnotation(annotation, false);
|
|
128 collapsing= true;
|
|
129 }
|
|
130 }
|
|
131 }
|
|
132
|
|
133 if (collapsing)
|
|
134 fireModelChanged();
|
|
135
|
|
136 return collapsing;
|
|
137 }
|
|
138
|
|
139 /**
|
|
140 * Expands all annotations that overlap with the given range and are collapsed. Fires a model change event if
|
|
141 * requested.
|
|
142 *
|
|
143 * @param offset the offset of the range
|
|
144 * @param length the length of the range
|
|
145 * @param fireModelChanged <code>true</code> if a model change event
|
|
146 * should be fired, <code>false</code> otherwise
|
|
147 * @return <code>true</code> if any annotation has been expanded, <code>false</code> otherwise
|
|
148 */
|
|
149 protected bool expandAll(int offset, int length, bool fireModelChanged) {
|
|
150
|
|
151 bool expanding= false;
|
|
152
|
|
153 Iterator iterator= getAnnotationIterator();
|
|
154 while (iterator.hasNext()) {
|
|
155 ProjectionAnnotation annotation= (ProjectionAnnotation) iterator.next();
|
|
156 if (annotation.isCollapsed()) {
|
|
157 Position position= getPosition(annotation);
|
|
158 if (position !is null && position.overlapsWith(offset, length) /* || is a delete at the boundary */ ) {
|
|
159 annotation.markExpanded();
|
|
160 modifyAnnotation(annotation, false);
|
|
161 expanding= true;
|
|
162 }
|
|
163 }
|
|
164 }
|
|
165
|
|
166 if (expanding && fireModelChanged)
|
|
167 fireModelChanged();
|
|
168
|
|
169 return expanding;
|
|
170 }
|
|
171
|
|
172 /**
|
|
173 * Modifies the annotation model.
|
|
174 *
|
|
175 * @param deletions the list of deleted annotations
|
|
176 * @param additions the set of annotations to add together with their associated position
|
|
177 * @param modifications the list of modified annotations
|
|
178 */
|
|
179 public void modifyAnnotations(Annotation[] deletions, Map additions, Annotation[] modifications) {
|
|
180 try {
|
|
181 replaceAnnotations(deletions, additions, false);
|
|
182 if (modifications !is null) {
|
|
183 for (int i= 0; i < modifications.length; i++)
|
|
184 modifyAnnotation(modifications[i], false);
|
|
185 }
|
|
186 } catch (BadLocationException x) {
|
|
187 }
|
|
188 fireModelChanged();
|
|
189 }
|
|
190 }
|