Added state reset
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PrintMarginPainter.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */
7
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.custom.StyledText;
10 import org.eclipse.swt.events.PaintEvent;
11 import org.eclipse.swt.events.PaintListener;
12 import org.eclipse.swt.graphics.Color;
13 import org.eclipse.swt.graphics.GC;
14 import org.eclipse.swt.graphics.Rectangle;
15
16 import org.eclipse.jface.text.source.ISourceViewer;
17
18
19
20 public class PrintMarginPainter  implements IPainter, PaintListener {
21         
22
23         private StyledText fTextWidget;
24         
25         private int fMarginWidth= 80;
26         private Color fColor;
27         private int fLineStyle= SWT.LINE_SOLID;
28         private int fLineWidth= 1;
29         
30         private int fCachedWidgetX= -1;
31         private boolean fIsActive= false;
32         
33         public PrintMarginPainter(ISourceViewer sourceViewer) {
34                 fTextWidget= sourceViewer.getTextWidget();
35         }
36         
37         public void setMarginRulerColumn(int width) {
38                 fMarginWidth= width;
39                 intialize();
40         }
41         
42         public void setMarginRulerStyle(int lineStyle) {
43                 fLineStyle= lineStyle;
44         }
45         
46         public void setMarginRulerWidth(int lineWidth) {
47                 fLineWidth= lineWidth;
48         }
49         
50         /**
51          * Must be called before <code>paint</code> is called the first time.
52          */
53         public void setMarginRulerColor(Color color) {
54                 fColor= color;
55         }
56         
57         /**
58          * Must be called explicitly when font of text widget changes.
59          */
60         public void intialize() {
61                 computeWidgetX();
62                 fTextWidget.redraw();
63         }
64         
65         private void computeWidgetX() {
66                 GC gc= new GC(fTextWidget);
67                 int pixels= gc.getFontMetrics().getAverageCharWidth();
68                 gc.dispose();
69                 
70                 fCachedWidgetX= pixels * fMarginWidth;
71         }
72         
73         /*
74          * @see IPainter#deactivate(boolean)
75          */
76         public void deactivate(boolean redraw) {
77                 if (fIsActive) {
78                         fIsActive= false;
79                         fTextWidget.removePaintListener(this);
80                         if (redraw)
81                                 fTextWidget.redraw();
82                 }       
83         }
84
85         /*
86          * @see IPainter#dispose()
87          */
88         public void dispose() {
89                 fTextWidget= null;
90         }
91
92         /*
93          * @see IPainter#paint(int)
94          */
95         public void paint(int reason) {
96                 if (!fIsActive) {
97                         fIsActive= true;
98                         fTextWidget.addPaintListener(this);
99                         if (fCachedWidgetX == -1)
100                                 computeWidgetX();
101                         fTextWidget.redraw();
102                 }
103         }
104
105         /*
106          * @see IPainter#setPositionManager(IPositionManager)
107          */
108         public void setPositionManager(IPositionManager manager) {
109         }
110         
111         /*
112          * @see PaintListener#paintControl(PaintEvent)
113          */
114         public void paintControl(PaintEvent e) {
115                 if (fTextWidget != null) {
116                         int x= fCachedWidgetX - fTextWidget.getHorizontalPixel();
117                         if (x >= 0) {
118                                 Rectangle area= fTextWidget.getClientArea();
119                                 e.gc.setForeground(fColor);
120                                 e.gc.setLineStyle(fLineStyle);
121                                 e.gc.setLineWidth(fLineWidth);
122                                 e.gc.drawLine(x, 0, x, area.height);
123                         }
124                 }
125         }
126 }