KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > CursorLinePainter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jface.text;
13
14
15 import org.eclipse.swt.custom.LineBackgroundEvent;
16 import org.eclipse.swt.custom.LineBackgroundListener;
17 import org.eclipse.swt.custom.StyledText;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Point;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITextViewerExtension5;
25 import org.eclipse.jface.text.Position;
26
27
28 /**
29  * A painter the draws the background of the caret line in a configured color.
30  * <p>
31  * Clients usually instantiate and configure object of this class.</p>
32  * <p>
33  * This class is not intended to be subclassed.</p>
34  *
35  * @since 2.1
36  */

37 public class CursorLinePainter implements IPainter, LineBackgroundListener {
38
39     /** The viewer the painter works on */
40     private final ITextViewer fViewer;
41     /** The cursor line back ground color */
42     private Color fHighlightColor;
43     /** The paint position manager for managing the line coordinates */
44     private IPaintPositionManager fPositionManager;
45
46     /** Keeps track of the line to be painted */
47     private Position fCurrentLine= new Position(0, 0);
48     /** Keeps track of the line to be cleared */
49     private Position fLastLine= new Position(0, 0);
50     /** Keeps track of the line number of the last painted line */
51     private int fLastLineNumber= -1;
52     /** Indicates whether this painter is active */
53     private boolean fIsActive;
54
55     /**
56      * Creates a new painter for the given source viewer.
57      *
58      * @param textViewer the source viewer for which to create a painter
59      */

60     public CursorLinePainter(ITextViewer textViewer) {
61         fViewer= textViewer;
62     }
63
64     /**
65      * Sets the color in which to draw the background of the cursor line.
66      *
67      * @param highlightColor the color in which to draw the background of the cursor line
68      */

69     public void setHighlightColor(Color highlightColor) {
70         fHighlightColor= highlightColor;
71     }
72
73     /*
74      * @see LineBackgroundListener#lineGetBackground(LineBackgroundEvent)
75      */

76     public void lineGetBackground(LineBackgroundEvent event) {
77         // don't use cached line information because of asynchronous painting
78

79         StyledText textWidget= fViewer.getTextWidget();
80         if (textWidget != null) {
81
82             int caret= textWidget.getCaretOffset();
83             int length= event.lineText.length();
84
85             if (event.lineOffset <= caret && caret <= event.lineOffset + length)
86                 event.lineBackground= fHighlightColor;
87             else
88                 event.lineBackground= textWidget.getBackground();
89         }
90     }
91
92     /**
93      * Updates all the cached information about the lines to be painted and to be cleared. Returns <code>true</code>
94      * if the line number of the cursor line has changed.
95      *
96      * @return <code>true</code> if cursor line changed
97      */

98     private boolean updateHighlightLine() {
99         try {
100
101             IDocument document= fViewer.getDocument();
102             int modelCaret= getModelCaret();
103             int lineNumber= document.getLineOfOffset(modelCaret);
104
105             // redraw if the current line number is different from the last line number we painted
106
// initially fLastLineNumber is -1
107
if (lineNumber != fLastLineNumber || !fCurrentLine.overlapsWith(modelCaret, 0)) {
108
109                 fLastLine.offset= fCurrentLine.offset;
110                 fLastLine.length= fCurrentLine.length;
111                 fLastLine.isDeleted= fCurrentLine.isDeleted;
112
113                 if (fCurrentLine.isDeleted) {
114                     fCurrentLine.isDeleted= false;
115                     fPositionManager.managePosition(fCurrentLine);
116                 }
117
118                 fCurrentLine.offset= document.getLineOffset(lineNumber);
119                 if (lineNumber == document.getNumberOfLines() - 1)
120                     fCurrentLine.length= document.getLength() - fCurrentLine.offset;
121                 else
122                     fCurrentLine.length= document.getLineOffset(lineNumber + 1) - fCurrentLine.offset;
123
124                 fLastLineNumber= lineNumber;
125                 return true;
126
127             }
128
129         } catch (BadLocationException e) {
130         }
131
132         return false;
133     }
134
135     /**
136      * Returns the location of the caret as offset in the source viewer's
137      * input document.
138      *
139      * @return the caret location
140      */

141     private int getModelCaret() {
142         int widgetCaret= fViewer.getTextWidget().getCaretOffset();
143         if (fViewer instanceof ITextViewerExtension5) {
144             ITextViewerExtension5 extension= (ITextViewerExtension5) fViewer;
145             return extension.widgetOffset2ModelOffset(widgetCaret);
146         }
147         IRegion visible= fViewer.getVisibleRegion();
148         return widgetCaret + visible.getOffset();
149     }
150
151     /**
152      * Assumes the given position to specify offset and length of a line to be painted.
153      *
154      * @param position the specification of the line to be painted
155      */

156     private void drawHighlightLine(Position position) {
157
158         // if the position that is about to be drawn was deleted then we can't
159
if (position.isDeleted())
160             return;
161
162         int widgetOffset= 0;
163         if (fViewer instanceof ITextViewerExtension5) {
164
165             ITextViewerExtension5 extension= (ITextViewerExtension5) fViewer;
166             widgetOffset= extension.modelOffset2WidgetOffset(position.getOffset());
167             if (widgetOffset == -1)
168                 return;
169
170         } else {
171
172             IRegion visible= fViewer.getVisibleRegion();
173             widgetOffset= position.getOffset() - visible.getOffset();
174             if (widgetOffset < 0 || visible.getLength() < widgetOffset )
175                 return;
176         }
177
178         StyledText textWidget= fViewer.getTextWidget();
179         // check for https://bugs.eclipse.org/bugs/show_bug.cgi?id=64898
180
// this is a guard against the symptoms but not the actual solution
181
if (0 <= widgetOffset && widgetOffset <= textWidget.getCharCount()) {
182             Point upperLeft= textWidget.getLocationAtOffset(widgetOffset);
183             int width= textWidget.getClientArea().width + textWidget.getHorizontalPixel();
184             int height= textWidget.getLineHeight(widgetOffset);
185             textWidget.redraw(0, upperLeft.y, width, height, false);
186         }
187     }
188
189     /*
190      * @see IPainter#deactivate(boolean)
191      */

192     public void deactivate(boolean redraw) {
193         if (fIsActive) {
194             fIsActive= false;
195
196             /* on turning off the feature one has to paint the currently
197              * highlighted line with the standard background color
198              */

199             if (redraw)
200                 drawHighlightLine(fCurrentLine);
201
202             fViewer.getTextWidget().removeLineBackgroundListener(this);
203
204             if (fPositionManager != null)
205                 fPositionManager.unmanagePosition(fCurrentLine);
206
207             fLastLineNumber= -1;
208             fCurrentLine.offset= 0;
209             fCurrentLine.length= 0;
210         }
211     }
212
213     /*
214      * @see IPainter#dispose()
215      */

216     public void dispose() {
217     }
218
219     /*
220      * @see IPainter#paint(int)
221      */

222     public void paint(int reason) {
223         if (fViewer.getDocument() == null) {
224             deactivate(false);
225             return;
226         }
227
228         StyledText textWidget= fViewer.getTextWidget();
229
230         // check selection
231
Point selection= textWidget.getSelection();
232         int startLine= textWidget.getLineAtOffset(selection.x);
233         int endLine= textWidget.getLineAtOffset(selection.y);
234         if (startLine != endLine) {
235             deactivate(true);
236             return;
237         }
238
239         // initialization
240
if (!fIsActive) {
241             textWidget.addLineBackgroundListener(this);
242             fPositionManager.managePosition(fCurrentLine);
243             fIsActive= true;
244         }
245
246         //redraw line highlight only if it hasn't been drawn yet on the respective line
247
if (updateHighlightLine()) {
248             // clear last line
249
drawHighlightLine(fLastLine);
250             // draw new line
251
drawHighlightLine(fCurrentLine);
252         }
253     }
254
255     /*
256      * @see IPainter#setPositionManager(IPaintPositionManager)
257      */

258     public void setPositionManager(IPaintPositionManager manager) {
259         fPositionManager = manager;
260     }
261 }
262
Popular Tags