KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > MarginPainter


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jface.text;
12
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.StyledText;
16 import org.eclipse.swt.events.PaintEvent;
17 import org.eclipse.swt.events.PaintListener;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.GC;
20 import org.eclipse.swt.graphics.Rectangle;
21
22
23 /**
24  * Paints a vertical line (margin line) after a given column respecting the text
25  * viewer's font.
26  * <p>
27  * Clients usually instantiate and configure objects of this class.</p>
28  * <p>
29  * This class is not intended to be subclassed.</p>
30  *
31  * @since 2.1
32  */

33 public class MarginPainter implements IPainter, PaintListener {
34
35     /** The widget of the text viewer */
36     private StyledText fTextWidget;
37
38     /** The column after which to paint the line, default value <code>80</code> */
39     private int fMarginWidth= 80;
40     /** The color in which to paint the line */
41     private Color fColor;
42     /** The line style of the line to be painted, default value <code>SWT.LINE_SOLID</code> */
43     private int fLineStyle= SWT.LINE_SOLID;
44     /** The line width of the line to be painted, default value <code>1</code> */
45     private int fLineWidth= 0; // NOTE: 0 means width is 1 but with optimized performance
46
/** The cached x-offset of the <code>fMarginWidth</code> for the current font */
47     private int fCachedWidgetX= -1;
48     /** The active state of this painter */
49     private boolean fIsActive= false;
50
51     /**
52      * Creates a new painter for the given text viewer.
53      *
54      * @param textViewer the text viewer
55      */

56     public MarginPainter(ITextViewer textViewer) {
57         fTextWidget= textViewer.getTextWidget();
58     }
59
60     /**
61      * Sets the column after which to draw the margin line.
62      *
63      * @param width the column
64      */

65     public void setMarginRulerColumn(int width) {
66         fMarginWidth= width;
67         initialize();
68     }
69
70     /**
71      * Sets the line style of the margin line.
72      *
73      * @param lineStyle a <code>SWT</code> style constant describing the line style
74      */

75     public void setMarginRulerStyle(int lineStyle) {
76         fLineStyle= lineStyle;
77     }
78
79     /**
80      * Sets the line width of the margin line.
81      *
82      * @param lineWidth the line width
83      */

84     public void setMarginRulerWidth(int lineWidth) {
85         if (lineWidth == 1)
86             lineWidth= 0; // NOTE: 0 means width is 1 but with optimized performance
87
fLineWidth= lineWidth;
88     }
89
90     /**
91      * Sets the color of the margin line. Must be called before <code>paint</code> is called the first time.
92      *
93      * @param color the color
94      */

95     public void setMarginRulerColor(Color color) {
96         fColor= color;
97     }
98
99     /**
100      * Initializes this painter, by flushing and recomputing all caches and causing
101      * the widget to be redrawn. Must be called explicitly when font of text widget changes.
102      */

103     public void initialize() {
104         computeWidgetX();
105         fTextWidget.redraw();
106     }
107
108     /**
109      * Computes and remembers the x-offset of the margin column for the
110      * current widget font.
111      */

112     private void computeWidgetX() {
113         GC gc= new GC(fTextWidget);
114         int pixels= gc.getFontMetrics().getAverageCharWidth();
115         gc.dispose();
116
117         fCachedWidgetX= pixels * fMarginWidth;
118     }
119
120     /*
121      * @see IPainter#deactivate(boolean)
122      */

123     public void deactivate(boolean redraw) {
124         if (fIsActive) {
125             fIsActive= false;
126             fCachedWidgetX= -1;
127             fTextWidget.removePaintListener(this);
128             if (redraw)
129                 fTextWidget.redraw();
130         }
131     }
132
133     /*
134      * @see IPainter#dispose()
135      */

136     public void dispose() {
137         fTextWidget= null;
138     }
139
140     /*
141      * @see IPainter#paint(int)
142      */

143     public void paint(int reason) {
144         if (!fIsActive) {
145             fIsActive= true;
146             fTextWidget.addPaintListener(this);
147             if (fCachedWidgetX == -1)
148                 computeWidgetX();
149             fTextWidget.redraw();
150         } else if (CONFIGURATION == reason || INTERNAL == reason)
151             fTextWidget.redraw();
152     }
153
154     /*
155      * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
156      */

157     public void paintControl(PaintEvent e) {
158         if (fTextWidget != null) {
159             int x= fCachedWidgetX - fTextWidget.getHorizontalPixel();
160             if (x >= 0) {
161                 Rectangle area= fTextWidget.getClientArea();
162                 e.gc.setForeground(fColor);
163                 e.gc.setLineStyle(fLineStyle);
164                 e.gc.setLineWidth(fLineWidth);
165                 e.gc.drawLine(x, 0, x, area.height);
166             }
167         }
168     }
169
170     /*
171      * @see org.eclipse.jface.text.IPainter#setPositionManager(org.eclipse.jface.text.IPaintPositionManager)
172      */

173     public void setPositionManager(IPaintPositionManager manager) {
174     }
175 }
176
Popular Tags