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;
22 public final class BracketPainter implements IPainter, PaintListener {
24 private PHPPairMatcher fMatcher = new PHPPairMatcher(new char[] { '{', '}',
25 '(', ')', '[', ']' });
27 private Position fBracketPosition = new Position(0, 0);
31 private boolean fIsActive = false;
33 private ISourceViewer fSourceViewer;
35 private StyledText fTextWidget;
39 private IPositionManager fPositionManager;
41 public BracketPainter(ISourceViewer sourceViewer) {
42 fSourceViewer = sourceViewer;
43 fTextWidget = sourceViewer.getTextWidget();
46 public void setHighlightColor(Color color) {
50 public void dispose() {
51 if (fMatcher != null) {
60 public void deactivate(boolean redraw) {
63 fTextWidget.removePaintListener(this);
64 if (fPositionManager != null)
65 fPositionManager.removeManagedPosition(fBracketPosition);
67 handleDrawRequest(null);
71 public void paintControl(PaintEvent event) {
72 if (fTextWidget != null)
73 handleDrawRequest(event.gc);
76 private void handleDrawRequest(GC gc) {
78 if (fBracketPosition.isDeleted)
81 int offset = fBracketPosition.getOffset();
82 int length = fBracketPosition.getLength();
86 if (fSourceViewer instanceof ITextViewerExtension5) {
87 ITextViewerExtension5 extension = (ITextViewerExtension5) fSourceViewer;
88 IRegion widgetRange = extension.modelRange2WidgetRange(new Region(
90 if (widgetRange == null)
93 offset = widgetRange.getOffset();
94 length = widgetRange.getLength();
97 IRegion region = fSourceViewer.getVisibleRegion();
98 if (region.getOffset() > offset
99 || region.getOffset() + region.getLength() < offset
102 offset -= region.getOffset();
105 if (PHPPairMatcher.RIGHT == fAnchor)
108 draw(gc, offset + length - 1, 1);
111 private void draw(GC gc, int offset, int length) {
113 Point left = fTextWidget.getLocationAtOffset(offset);
114 Point right = fTextWidget.getLocationAtOffset(offset + length);
116 gc.setForeground(fColor);
117 gc.drawRectangle(left.x, left.y, right.x - left.x - 1, gc
118 .getFontMetrics().getHeight() - 1);
121 fTextWidget.redrawRange(offset, length, true);
126 * @see IPainter#paint(int)
128 public void paint(int reason) {
129 Point selection = fSourceViewer.getSelectedRange();
130 if (selection.y > 0) {
135 IRegion pair = fMatcher.match(fSourceViewer.getDocument(), selection.x);
143 if (pair.getOffset() != fBracketPosition.getOffset()
144 || pair.getLength() != fBracketPosition.getLength()
145 || fMatcher.getAnchor() != fAnchor) {
147 // remove old highlighting
148 handleDrawRequest(null);
150 fBracketPosition.isDeleted = false;
151 fBracketPosition.offset = pair.getOffset();
152 fBracketPosition.length = pair.getLength();
153 fAnchor = fMatcher.getAnchor();
154 // apply new highlighting
155 handleDrawRequest(null);
162 fBracketPosition.isDeleted = false;
163 fBracketPosition.offset = pair.getOffset();
164 fBracketPosition.length = pair.getLength();
165 fAnchor = fMatcher.getAnchor();
167 fTextWidget.addPaintListener(this);
168 fPositionManager.addManagedPosition(fBracketPosition);
169 handleDrawRequest(null);
174 * @see IPainter#setPositionManager(IPositionManager)
176 public void setPositionManager(IPositionManager manager) {
177 fPositionManager = manager;