1 package net.sourceforge.phpeclipse.phpeditor;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
10 import org.eclipse.jface.text.IRegion;
11 import org.eclipse.jface.text.ITextViewerExtension5;
12 import org.eclipse.jface.text.Position;
13 import org.eclipse.jface.text.Region;
14 import org.eclipse.jface.text.source.ISourceViewer;
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.Point;
23 public final class BracketPainter implements IPainter, PaintListener {
25 private PHPPairMatcher fMatcher= new PHPPairMatcher(new char[] { '{', '}', '(', ')', '[', ']' });
26 private Position fBracketPosition= new Position(0, 0);
29 private boolean fIsActive= false;
30 private ISourceViewer fSourceViewer;
31 private StyledText fTextWidget;
34 private IPositionManager fPositionManager;
37 public BracketPainter(ISourceViewer sourceViewer) {
38 fSourceViewer= sourceViewer;
39 fTextWidget= sourceViewer.getTextWidget();
42 public void setHighlightColor(Color color) {
46 public void dispose() {
47 if (fMatcher != null) {
56 public void deactivate(boolean redraw) {
59 fTextWidget.removePaintListener(this);
60 if (fPositionManager != null)
61 fPositionManager.removeManagedPosition(fBracketPosition);
63 handleDrawRequest(null);
67 public void paintControl(PaintEvent event) {
68 if (fTextWidget != null)
69 handleDrawRequest(event.gc);
72 private void handleDrawRequest(GC gc) {
74 if (fBracketPosition.isDeleted)
77 int offset= fBracketPosition.getOffset();
78 int length= fBracketPosition.getLength();
82 if (fSourceViewer instanceof ITextViewerExtension5) {
83 ITextViewerExtension5 extension= (ITextViewerExtension5) fSourceViewer;
84 IRegion widgetRange= extension.modelRange2WidgetRange(new Region(offset, length));
85 if (widgetRange == null)
88 offset= widgetRange.getOffset();
89 length= widgetRange.getLength();
92 IRegion region= fSourceViewer.getVisibleRegion();
93 if (region.getOffset() > offset || region.getOffset() + region.getLength() < offset + length)
95 offset -= region.getOffset();
98 if (PHPPairMatcher.RIGHT == fAnchor)
101 draw(gc, offset + length -1, 1);
104 private void draw(GC gc, int offset, int length) {
106 Point left= fTextWidget.getLocationAtOffset(offset);
107 Point right= fTextWidget.getLocationAtOffset(offset + length);
109 gc.setForeground(fColor);
110 gc.drawRectangle(left.x, left.y, right.x - left.x - 1, gc.getFontMetrics().getHeight() - 1);
113 fTextWidget.redrawRange(offset, length, true);
118 * @see IPainter#paint(int)
120 public void paint(int reason) {
121 Point selection= fSourceViewer.getSelectedRange();
122 if (selection.y > 0) {
127 IRegion pair= fMatcher.match(fSourceViewer.getDocument(), selection.x);
135 if (pair.getOffset() != fBracketPosition.getOffset() ||
136 pair.getLength() != fBracketPosition.getLength() ||
137 fMatcher.getAnchor() != fAnchor) {
139 // remove old highlighting
140 handleDrawRequest(null);
142 fBracketPosition.isDeleted= false;
143 fBracketPosition.offset= pair.getOffset();
144 fBracketPosition.length= pair.getLength();
145 fAnchor= fMatcher.getAnchor();
146 // apply new highlighting
147 handleDrawRequest(null);
154 fBracketPosition.isDeleted= false;
155 fBracketPosition.offset= pair.getOffset();
156 fBracketPosition.length= pair.getLength();
157 fAnchor= fMatcher.getAnchor();
159 fTextWidget.addPaintListener(this);
160 fPositionManager.addManagedPosition(fBracketPosition);
161 handleDrawRequest(null);
166 * @see IPainter#setPositionManager(IPositionManager)
168 public void setPositionManager(IPositionManager manager) {
169 fPositionManager= manager;