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