1 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 33 public class MarginPainter implements IPainter, PaintListener { 34 35 36 private StyledText fTextWidget; 37 38 39 private int fMarginWidth= 80; 40 41 private Color fColor; 42 43 private int fLineStyle= SWT.LINE_SOLID; 44 45 private int fLineWidth= 0; 47 private int fCachedWidgetX= -1; 48 49 private boolean fIsActive= false; 50 51 56 public MarginPainter(ITextViewer textViewer) { 57 fTextWidget= textViewer.getTextWidget(); 58 } 59 60 65 public void setMarginRulerColumn(int width) { 66 fMarginWidth= width; 67 initialize(); 68 } 69 70 75 public void setMarginRulerStyle(int lineStyle) { 76 fLineStyle= lineStyle; 77 } 78 79 84 public void setMarginRulerWidth(int lineWidth) { 85 if (lineWidth == 1) 86 lineWidth= 0; fLineWidth= lineWidth; 88 } 89 90 95 public void setMarginRulerColor(Color color) { 96 fColor= color; 97 } 98 99 103 public void initialize() { 104 computeWidgetX(); 105 fTextWidget.redraw(); 106 } 107 108 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 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 136 public void dispose() { 137 fTextWidget= null; 138 } 139 140 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 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 173 public void setPositionManager(IPaintPositionManager manager) { 174 } 175 } 176 | Popular Tags |