KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > AnnotationColumn


1 /*******************************************************************************
2  * Copyright (c) 2006 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.ui.internal.texteditor;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.swt.graphics.Font;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20
21 import org.eclipse.jface.text.source.Annotation;
22 import org.eclipse.jface.text.source.AnnotationRulerColumn;
23 import org.eclipse.jface.text.source.CompositeRuler;
24 import org.eclipse.jface.text.source.IAnnotationHover;
25 import org.eclipse.jface.text.source.IAnnotationModel;
26 import org.eclipse.jface.text.source.IVerticalRulerColumn;
27 import org.eclipse.jface.text.source.IVerticalRulerInfo;
28 import org.eclipse.jface.text.source.IVerticalRulerInfoExtension;
29 import org.eclipse.jface.text.source.IVerticalRulerListener;
30
31 import org.eclipse.ui.editors.text.EditorsUI;
32
33 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
34
35 import org.eclipse.jface.preference.IPreferenceStore;
36 import org.eclipse.jface.util.IPropertyChangeListener;
37 import org.eclipse.jface.util.PropertyChangeEvent;
38 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
39 import org.eclipse.ui.texteditor.AnnotationPreference;
40 import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess;
41 import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
42 import org.eclipse.ui.texteditor.rulers.AbstractContributedRulerColumn;
43
44 /**
45  * The annotation ruler contribution. Encapsulates an {@link AnnotationRulerColumn} as a
46  * contribution to the <code>rulerColumns</code> extension point. Instead of instantiating the
47  * delegate itself, it {@link AbstractDecoratedTextEditor} creates it using its
48  * <code>createAnnotationRulerColumn()</code> method and sets it via
49  * {@link #setDelegate(IVerticalRulerColumn)}.
50  *
51  * @since 3.3
52  */

53 public class AnnotationColumn extends AbstractContributedRulerColumn implements IVerticalRulerInfo, IVerticalRulerInfoExtension {
54     /** The contribution id of the annotation ruler. */
55     public static final String JavaDoc ID= "org.eclipse.ui.editors.columns.annotations"; //$NON-NLS-1$
56
/** The width of the vertical ruler. */
57     private final static int VERTICAL_RULER_WIDTH= 12;
58
59     
60     private IVerticalRulerColumn fDelegate;
61     private final MarkerAnnotationPreferences fAnnotationPreferences= EditorsPlugin.getDefault().getMarkerAnnotationPreferences();
62     private IPropertyChangeListener fPropertyListener;
63
64     /*
65      * @see org.eclipse.jface.text.source.IVerticalRulerColumn#createControl(org.eclipse.jface.text.source.CompositeRuler, org.eclipse.swt.widgets.Composite)
66      */

67     public Control createControl(CompositeRuler parentRuler, Composite parentControl) {
68         initialize();
69         Control control= fDelegate.createControl(parentRuler, parentControl);
70         return control;
71     }
72
73     /*
74      * @see org.eclipse.jface.text.source.IVerticalRulerColumn#getControl()
75      */

76     public Control getControl() {
77         return fDelegate.getControl();
78     }
79
80     /*
81      * @see org.eclipse.jface.text.source.IVerticalRulerColumn#getWidth()
82      */

83     public int getWidth() {
84         return fDelegate.getWidth();
85     }
86
87     /*
88      * @see org.eclipse.jface.text.source.IVerticalRulerColumn#redraw()
89      */

90     public void redraw() {
91         fDelegate.redraw();
92     }
93
94     /*
95      * @see org.eclipse.jface.text.source.IVerticalRulerColumn#setFont(org.eclipse.swt.graphics.Font)
96      */

97     public void setFont(Font font) {
98         fDelegate.setFont(font);
99     }
100
101     /*
102      * @see org.eclipse.jface.text.source.IVerticalRulerColumn#setModel(org.eclipse.jface.text.source.IAnnotationModel)
103      */

104     public void setModel(IAnnotationModel model) {
105         fDelegate.setModel(model);
106     }
107
108     /*
109      * @see org.eclipse.ui.texteditor.rulers.AbstractContributedRulerColumn#columnRemoved()
110      */

111     public void columnRemoved() {
112         if (fPropertyListener != null) {
113             IPreferenceStore store= getPreferenceStore();
114             if (store != null)
115                 store.removePropertyChangeListener(fPropertyListener);
116             fPropertyListener= null;
117         }
118     }
119
120     /**
121      * Initializes the given line number ruler column from the preference store.
122      */

123     private void initialize() {
124         if (fDelegate == null)
125             fDelegate= new AnnotationRulerColumn(VERTICAL_RULER_WIDTH, new DefaultMarkerAnnotationAccess());
126         IPreferenceStore store= getPreferenceStore();
127         if (store != null && fDelegate instanceof AnnotationRulerColumn) {
128             final AnnotationRulerColumn column= (AnnotationRulerColumn) fDelegate;
129             // initial set up
130
for (Iterator JavaDoc iter2= fAnnotationPreferences.getAnnotationPreferences().iterator(); iter2.hasNext();) {
131                 AnnotationPreference preference= (AnnotationPreference)iter2.next();
132                 String JavaDoc key= preference.getVerticalRulerPreferenceKey();
133                 boolean showAnnotation= true;
134                 if (key != null && store.contains(key))
135                     showAnnotation= store.getBoolean(key);
136                 if (showAnnotation)
137                     column.addAnnotationType(preference.getAnnotationType());
138             }
139             column.addAnnotationType(Annotation.TYPE_UNKNOWN);
140             
141             // link to preference store
142
fPropertyListener= new IPropertyChangeListener() {
143                 public void propertyChange(PropertyChangeEvent event) {
144                     String JavaDoc property= event.getProperty();
145                     AnnotationPreference annotationPreference= getVerticalRulerAnnotationPreference(property);
146                     if (annotationPreference != null && event.getNewValue() instanceof Boolean JavaDoc) {
147                         Object JavaDoc type= annotationPreference.getAnnotationType();
148                         if (((Boolean JavaDoc)event.getNewValue()).booleanValue())
149                             column.addAnnotationType(type);
150                         else
151                             column.removeAnnotationType(type);
152                         column.redraw();
153                     }
154                 }
155             };
156             store.addPropertyChangeListener(fPropertyListener);
157         }
158     }
159     
160     /**
161      * Returns the annotation preference for which the given
162      * preference matches a vertical ruler preference key.
163      *
164      * @param preferenceKey the preference key string
165      * @return the annotation preference or <code>null</code> if none
166      */

167     private AnnotationPreference getVerticalRulerAnnotationPreference(String JavaDoc preferenceKey) {
168         if (preferenceKey == null)
169             return null;
170
171         Iterator JavaDoc e= fAnnotationPreferences.getAnnotationPreferences().iterator();
172         while (e.hasNext()) {
173             AnnotationPreference info= (AnnotationPreference) e.next();
174             if (info != null && preferenceKey.equals(info.getVerticalRulerPreferenceKey()))
175                 return info;
176         }
177         return null;
178     }
179
180     private IPreferenceStore getPreferenceStore() {
181         return EditorsUI.getPreferenceStore();
182     }
183
184     /**
185      * Sets the compatibility delegate. Called by {@link AbstractDecoratedTextEditor}.
186      *
187      * @param column the delegate column implementation
188      */

189     public void setDelegate(IVerticalRulerColumn column) {
190         Assert.isLegal(fDelegate == null);
191         Assert.isLegal(column != null);
192         fDelegate= column;
193     }
194
195     /*
196      * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#addVerticalRulerListener(org.eclipse.jface.text.source.IVerticalRulerListener)
197      */

198     public void addVerticalRulerListener(IVerticalRulerListener listener) {
199         if (fDelegate instanceof IVerticalRulerInfoExtension)
200             ((IVerticalRulerInfoExtension) fDelegate).addVerticalRulerListener(listener);
201     }
202
203     /*
204      * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#getHover()
205      */

206     public IAnnotationHover getHover() {
207         if (fDelegate instanceof IVerticalRulerInfoExtension)
208             return ((IVerticalRulerInfoExtension) fDelegate).getHover();
209         return null;
210     }
211
212     /*
213      * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#getModel()
214      */

215     public IAnnotationModel getModel() {
216         if (fDelegate instanceof IVerticalRulerInfoExtension)
217             return ((IVerticalRulerInfoExtension) fDelegate).getModel();
218         return null;
219     }
220
221     /*
222      * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#removeVerticalRulerListener(org.eclipse.jface.text.source.IVerticalRulerListener)
223      */

224     public void removeVerticalRulerListener(IVerticalRulerListener listener) {
225         if (fDelegate instanceof IVerticalRulerInfoExtension)
226             ((IVerticalRulerInfoExtension) fDelegate).removeVerticalRulerListener(listener);
227     }
228
229     /*
230      * @see org.eclipse.jface.text.source.IVerticalRulerInfo#getLineOfLastMouseButtonActivity()
231      */

232     public int getLineOfLastMouseButtonActivity() {
233         if (fDelegate instanceof IVerticalRulerInfo)
234             ((IVerticalRulerInfo) fDelegate).getLineOfLastMouseButtonActivity();
235         return -1;
236     }
237
238     /*
239      * @see org.eclipse.jface.text.source.IVerticalRulerInfo#toDocumentLineNumber(int)
240      */

241     public int toDocumentLineNumber(int y_coordinate) {
242         if (fDelegate instanceof IVerticalRulerInfo)
243             ((IVerticalRulerInfo) fDelegate).toDocumentLineNumber(y_coordinate);
244         return -1;
245     }
246 }
247
Popular Tags