KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > source > VisualAnnotationModel


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;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.Position;
19
20
21
22 /**
23  * Annotation model for visual annotations. Assume a viewer's input element is annotated with
24  * some semantic annotation such as a breakpoint and that it is simultaneously shown in multiple
25  * viewers. A source viewer, e.g., supports visual range indication for which it utilizes
26  * annotations. The range indicating annotation is specific to the visual presentation
27  * of the input element in this viewer and thus should only be visible in this viewer. The
28  * breakpoints however are independent from the input element's presentation and thus should
29  * be shown in all viewers in which the element is shown. As a viewer supports one vertical
30  * ruler which is based on one annotation model, there must be a visual annotation model for
31  * each viewer which all wrap the same element specific model annotation model.
32  */

33 class VisualAnnotationModel extends AnnotationModel implements IAnnotationModelListener {
34
35     /** The wrapped model annotation model */
36     private IAnnotationModel fModel;
37
38     /**
39      * Constructs a visual annotation model which wraps the given
40      * model based annotation model
41      *
42      * @param modelAnnotationModel the model based annotation model
43      */

44     public VisualAnnotationModel(IAnnotationModel modelAnnotationModel) {
45         fModel= modelAnnotationModel;
46     }
47
48     /**
49      * Returns the visual annotation model's wrapped model based annotation model.
50      *
51      * @return the model based annotation model
52      */

53     public IAnnotationModel getModelAnnotationModel() {
54         return fModel;
55     }
56
57     /*
58      * @see IAnnotationModel#addAnnotationModelListener(IAnnotationModelListener)
59      */

60     public void addAnnotationModelListener(IAnnotationModelListener listener) {
61
62         if (fModel != null && fAnnotationModelListeners.isEmpty())
63             fModel.addAnnotationModelListener(this);
64
65         super.addAnnotationModelListener(listener);
66     }
67
68     /*
69      * @see IAnnotationModel#connect(IDocument)
70      */

71     public void connect(IDocument document) {
72         super.connect(document);
73         if (fModel != null)
74             fModel.connect(document);
75     }
76
77     /*
78      * @see IAnnotationModel#disconnect(IDocument)
79      */

80     public void disconnect(IDocument document) {
81         super.disconnect(document);
82         if (fModel != null)
83             fModel.disconnect(document);
84     }
85
86     /*
87      * @see IAnnotationModel#getAnnotationIterator()
88      */

89     public Iterator JavaDoc getAnnotationIterator() {
90
91         if (fModel == null)
92             return super.getAnnotationIterator();
93
94         ArrayList JavaDoc a= new ArrayList JavaDoc(20);
95
96         Iterator JavaDoc e= fModel.getAnnotationIterator();
97         while (e.hasNext())
98             a.add(e.next());
99
100         e= super.getAnnotationIterator();
101         while (e.hasNext())
102             a.add(e.next());
103
104         return a.iterator();
105     }
106
107     /*
108      * @see IAnnotationModel#getPosition(Annotation)
109      */

110     public Position getPosition(Annotation annotation) {
111
112         Position p= (Position) getAnnotationMap().get(annotation);
113         if (p != null)
114             return p;
115
116         if (fModel != null)
117             return fModel.getPosition(annotation);
118
119         return null;
120     }
121
122     /*
123      * @see IAnnotationModelListener#modelChanged(IAnnotationModel)
124      */

125     public void modelChanged(IAnnotationModel model) {
126         if (model == fModel) {
127             Iterator JavaDoc iter= new ArrayList JavaDoc(fAnnotationModelListeners).iterator();
128             while (iter.hasNext()) {
129                 IAnnotationModelListener l= (IAnnotationModelListener)iter.next();
130                 l.modelChanged(this);
131             }
132         }
133     }
134
135     /*
136      * @see IAnnotationModel#removeAnnotationModelListener(IAnnotationModelListener)
137      */

138     public void removeAnnotationModelListener(IAnnotationModelListener listener) {
139         super.removeAnnotationModelListener(listener);
140
141         if (fModel != null && fAnnotationModelListeners.isEmpty())
142             fModel.removeAnnotationModelListener(this);
143     }
144 }
145
Popular Tags