KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.graphics.GC;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.jface.text.revisions.IRevisionListener;
21 import org.eclipse.jface.text.revisions.IRevisionRulerColumn;
22 import org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension;
23 import org.eclipse.jface.text.revisions.RevisionInformation;
24
25 import org.eclipse.jface.internal.text.revisions.RevisionPainter;
26 import org.eclipse.jface.internal.text.source.DiffPainter;
27 import org.eclipse.jface.viewers.ISelectionProvider;
28
29 /**
30  * A vertical ruler column displaying line numbers and serving as a UI for quick diff.
31  * Clients usually instantiate and configure object of this class.
32  *
33  * @since 3.0
34  */

35 public final class LineNumberChangeRulerColumn extends LineNumberRulerColumn implements IVerticalRulerInfo, IVerticalRulerInfoExtension, IChangeRulerColumn, IRevisionRulerColumn, IRevisionRulerColumnExtension {
36     /** The ruler's annotation model. */
37     private IAnnotationModel fAnnotationModel;
38     /** <code>true</code> if changes should be displayed using character indications instead of background colors. */
39     private boolean fCharacterDisplay;
40     /**
41      * The revision painter strategy.
42      *
43      * @since 3.2
44      */

45     private final RevisionPainter fRevisionPainter;
46     /**
47      * The diff information painter strategy.
48      *
49      * @since 3.2
50      */

51     private final DiffPainter fDiffPainter;
52     /**
53      * Whether to show number or to behave like a change ruler column.
54      * @since 3.3
55      */

56     private boolean fShowNumbers= true;
57
58     /**
59      * Creates a new instance.
60      *
61      * @param sharedColors the shared colors provider to use
62      */

63     public LineNumberChangeRulerColumn(ISharedTextColors sharedColors) {
64         Assert.isNotNull(sharedColors);
65         fRevisionPainter= new RevisionPainter(this, sharedColors);
66         fDiffPainter= new DiffPainter(this, sharedColors);
67     }
68     
69     /*
70      * @see org.eclipse.jface.text.source.LineNumberRulerColumn#createControl(org.eclipse.jface.text.source.CompositeRuler, org.eclipse.swt.widgets.Composite)
71      */

72     public Control createControl(CompositeRuler parentRuler, Composite parentControl) {
73         Control control= super.createControl(parentRuler, parentControl);
74         fRevisionPainter.setParentRuler(parentRuler);
75         fDiffPainter.setParentRuler(parentRuler);
76         return control;
77     }
78     
79     /*
80      * @see org.eclipse.jface.text.source.IVerticalRulerInfo#getLineOfLastMouseButtonActivity()
81      */

82     public int getLineOfLastMouseButtonActivity() {
83         return getParentRuler().getLineOfLastMouseButtonActivity();
84     }
85
86     /*
87      * @see org.eclipse.jface.text.source.IVerticalRulerInfo#toDocumentLineNumber(int)
88      */

89     public int toDocumentLineNumber(int y_coordinate) {
90         return getParentRuler().toDocumentLineNumber(y_coordinate);
91     }
92
93     /*
94      * @see IVerticalRulerColumn#setModel(IAnnotationModel)
95      */

96     public void setModel(IAnnotationModel model) {
97         setAnnotationModel(model);
98         fRevisionPainter.setModel(model);
99         fDiffPainter.setModel(model);
100         updateNumberOfDigits();
101         computeIndentations();
102         layout(true);
103         postRedraw();
104     }
105     
106     private void setAnnotationModel(IAnnotationModel model) {
107         if (fAnnotationModel != model)
108             fAnnotationModel= model;
109     }
110
111
112     /**
113      * Sets the display mode of the ruler. If character mode is set to <code>true</code>, diff
114      * information will be displayed textually on the line number ruler.
115      *
116      * @param characterMode <code>true</code> if diff information is to be displayed textually.
117      */

118     public void setDisplayMode(boolean characterMode) {
119         if (characterMode != fCharacterDisplay) {
120             fCharacterDisplay= characterMode;
121             updateNumberOfDigits();
122             computeIndentations();
123             layout(true);
124         }
125     }
126
127     /*
128      * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#getModel()
129      */

130     public IAnnotationModel getModel() {
131         return fAnnotationModel;
132     }
133
134     /*
135      * @see org.eclipse.jface.text.source.LineNumberRulerColumn#createDisplayString(int)
136      */

137     protected String JavaDoc createDisplayString(int line) {
138         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
139         if (fShowNumbers)
140             buffer.append(super.createDisplayString(line));
141         if (fCharacterDisplay && getModel() != null)
142             buffer.append(fDiffPainter.getDisplayCharacter(line));
143         return buffer.toString();
144     }
145
146     /*
147      * @see org.eclipse.jface.text.source.LineNumberRulerColumn#computeNumberOfDigits()
148      */

149     protected int computeNumberOfDigits() {
150         int digits;
151         if (fCharacterDisplay && getModel() != null) {
152             if (fShowNumbers)
153                 digits= super.computeNumberOfDigits() + 1;
154             else
155                 digits= 1;
156         } else {
157             if (fShowNumbers)
158                 digits= super.computeNumberOfDigits();
159             else
160                 digits= 0;
161         }
162         if (fRevisionPainter.hasInformation())
163             digits+= fRevisionPainter.getRequiredWidth();
164         return digits;
165     }
166
167     /*
168      * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#addVerticalRulerListener(org.eclipse.jface.text.source.IVerticalRulerListener)
169      */

170     public void addVerticalRulerListener(IVerticalRulerListener listener) {
171         throw new UnsupportedOperationException JavaDoc();
172     }
173
174     /*
175      * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#removeVerticalRulerListener(org.eclipse.jface.text.source.IVerticalRulerListener)
176      */

177     public void removeVerticalRulerListener(IVerticalRulerListener listener) {
178         throw new UnsupportedOperationException JavaDoc();
179     }
180     
181     /*
182      * @see org.eclipse.jface.text.source.LineNumberRulerColumn#doPaint(org.eclipse.swt.graphics.GC)
183      */

184     void doPaint(GC gc, ILineRange visibleLines) {
185         Color foreground= gc.getForeground();
186         if (visibleLines != null) {
187             if (fRevisionPainter.hasInformation())
188                 fRevisionPainter.paint(gc, visibleLines);
189             else if (fDiffPainter.hasInformation()) // don't paint quick diff colors if revisions are painted
190
fDiffPainter.paint(gc, visibleLines);
191         }
192         gc.setForeground(foreground);
193         if (fShowNumbers || fCharacterDisplay)
194             super.doPaint(gc, visibleLines);
195     }
196     
197     /*
198      * @see org.eclipse.jface.text.source.IVerticalRulerInfoExtension#getHover()
199      */

200     public IAnnotationHover getHover() {
201         int activeLine= getParentRuler().getLineOfLastMouseButtonActivity();
202         if (fRevisionPainter.hasHover(activeLine))
203             return fRevisionPainter.getHover();
204         if (fDiffPainter.hasHover(activeLine))
205             return fDiffPainter.getHover();
206         return null;
207     }
208
209     /*
210      * @see org.eclipse.jface.text.source.IChangeRulerColumn#setHover(org.eclipse.jface.text.source.IAnnotationHover)
211      */

212     public void setHover(IAnnotationHover hover) {
213         fRevisionPainter.setHover(hover);
214         fDiffPainter.setHover(hover);
215     }
216
217     /*
218      * @see org.eclipse.jface.text.source.IChangeRulerColumn#setBackground(org.eclipse.swt.graphics.Color)
219      */

220     public void setBackground(Color background) {
221         super.setBackground(background);
222         fRevisionPainter.setBackground(background);
223         fDiffPainter.setBackground(background);
224     }
225
226     /*
227      * @see org.eclipse.jface.text.source.IChangeRulerColumn#setAddedColor(org.eclipse.swt.graphics.Color)
228      */

229     public void setAddedColor(Color addedColor) {
230         fDiffPainter.setAddedColor(addedColor);
231     }
232
233     /*
234      * @see org.eclipse.jface.text.source.IChangeRulerColumn#setChangedColor(org.eclipse.swt.graphics.Color)
235      */

236     public void setChangedColor(Color changedColor) {
237         fDiffPainter.setChangedColor(changedColor);
238     }
239
240     /*
241      * @see org.eclipse.jface.text.source.IChangeRulerColumn#setDeletedColor(org.eclipse.swt.graphics.Color)
242      */

243     public void setDeletedColor(Color deletedColor) {
244         fDiffPainter.setDeletedColor(deletedColor);
245     }
246     
247     /*
248      * @see org.eclipse.jface.text.revisions.IRevisionRulerColumn#setRevisionInformation(org.eclipse.jface.text.revisions.RevisionInformation)
249      */

250     public void setRevisionInformation(RevisionInformation info) {
251         fRevisionPainter.setRevisionInformation(info);
252         updateNumberOfDigits();
253         computeIndentations();
254         layout(true);
255         postRedraw();
256     }
257
258     /*
259      * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#getRevisionSelectionProvider()
260      * @since 3.2
261      */

262     public ISelectionProvider getRevisionSelectionProvider() {
263         return fRevisionPainter.getRevisionSelectionProvider();
264     }
265
266     /*
267      * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#setRenderingMode(org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension.RenderingMode)
268      * @since 3.3
269      */

270     public void setRevisionRenderingMode(RenderingMode renderingMode) {
271         fRevisionPainter.setRenderingMode(renderingMode);
272     }
273
274     /**
275      * Sets the line number display mode.
276      *
277      * @param showNumbers <code>true</code> to show numbers, <code>false</code> to only show
278      * diff / revision info.
279      * @since 3.3
280      */

281     public void showLineNumbers(boolean showNumbers) {
282         if (fShowNumbers != showNumbers) {
283             fShowNumbers= showNumbers;
284             updateNumberOfDigits();
285             computeIndentations();
286             layout(true);
287         }
288     }
289
290     /*
291      * @see org.eclipse.jface.text.source.LineNumberRulerColumn#getWidth()
292      * @since 3.3
293      */

294     public int getWidth() {
295         int width= super.getWidth();
296         return width > 0 ? width : 8; // minimal width to display quick diff / revisions if no textual info is shown
297
}
298
299     /**
300      * Returns <code>true</code> if the ruler is showing line numbers, <code>false</code>
301      * otherwise
302      *
303      * @return <code>true</code> if line numbers are shown, <code>false</code> otherwise
304      * @since 3.3
305      */

306     public boolean isShowingLineNumbers() {
307         return fShowNumbers;
308     }
309
310     /**
311      * Returns <code>true</code> if the ruler is showing revision information, <code>false</code>
312      * otherwise
313      *
314      * @return <code>true</code> if revision information is shown, <code>false</code> otherwise
315      * @since 3.3
316      */

317     public boolean isShowingRevisionInformation() {
318         return fRevisionPainter.hasInformation();
319     }
320
321     /**
322      * Returns <code>true</code> if the ruler is showing change information, <code>false</code>
323      * otherwise
324      *
325      * @return <code>true</code> if change information is shown, <code>false</code> otherwise
326      * @since 3.3
327      */

328     public boolean isShowingChangeInformation() {
329         return fDiffPainter.hasInformation();
330     }
331
332     /*
333      * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#showRevisionAuthor(boolean)
334      * @since 3.3
335      */

336     public void showRevisionAuthor(boolean show) {
337         fRevisionPainter.showRevisionAuthor(show);
338         updateNumberOfDigits();
339         computeIndentations();
340         layout(true);
341         postRedraw();
342     }
343
344     /*
345      * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#showRevisionId(boolean)
346      * @since 3.3
347      */

348     public void showRevisionId(boolean show) {
349         fRevisionPainter.showRevisionId(show);
350         updateNumberOfDigits();
351         computeIndentations();
352         layout(true);
353         postRedraw();
354     }
355
356     /*
357      * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#addRevisionListener(org.eclipse.jface.text.revisions.IRevisionListener)
358      * @since 3.3
359      */

360     public void addRevisionListener(IRevisionListener listener) {
361         fRevisionPainter.addRevisionListener(listener);
362     }
363
364     /*
365      * @see org.eclipse.jface.text.revisions.IRevisionRulerColumnExtension#removeRevisionListener(org.eclipse.jface.text.revisions.IRevisionListener)
366      * @since 3.3
367      */

368     public void removeRevisionListener(IRevisionListener listener) {
369         fRevisionPainter.removeRevisionListener(listener);
370     }
371     
372     /*
373      * @see org.eclipse.jface.text.source.LineNumberRulerColumn#handleDispose()
374      * @since 3.3
375      */

376     protected void handleDispose() {
377         fRevisionPainter.setParentRuler(null);
378         fRevisionPainter.setModel(null);
379         fDiffPainter.setParentRuler(null);
380         fDiffPainter.setModel(null);
381         super.handleDispose();
382     }
383 }
384
Popular Tags