KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > rules > DefaultDamagerRepairer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.rules;
13
14
15 import org.eclipse.swt.custom.StyleRange;
16 import org.eclipse.swt.SWT;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.DocumentEvent;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITypedRegion;
25 import org.eclipse.jface.text.Region;
26 import org.eclipse.jface.text.TextAttribute;
27 import org.eclipse.jface.text.TextPresentation;
28 import org.eclipse.jface.text.presentation.IPresentationDamager;
29 import org.eclipse.jface.text.presentation.IPresentationRepairer;
30
31
32 /**
33  * A standard implementation of a syntax driven presentation damager
34  * and presentation repairer. It uses a token scanner to scan
35  * the document and to determine its damage and new text presentation.
36  * The tokens returned by the scanner are supposed to return text attributes
37  * as their data.
38  *
39  * @see ITokenScanner
40  * @since 2.0
41  */

42 public class DefaultDamagerRepairer implements IPresentationDamager, IPresentationRepairer {
43
44
45     /** The document this object works on */
46     protected IDocument fDocument;
47     /** The scanner it uses */
48     protected ITokenScanner fScanner;
49     /** The default text attribute if non is returned as data by the current token */
50     protected TextAttribute fDefaultTextAttribute;
51
52     /**
53      * Creates a damager/repairer that uses the given scanner and returns the given default
54      * text attribute if the current token does not carry a text attribute.
55      *
56      * @param scanner the token scanner to be used
57      * @param defaultTextAttribute the text attribute to be returned if non is specified by the current token,
58      * may not be <code>null</code>
59      *
60      * @deprecated use DefaultDamagerRepairer(ITokenScanner) instead
61      */

62     public DefaultDamagerRepairer(ITokenScanner scanner, TextAttribute defaultTextAttribute) {
63
64         Assert.isNotNull(defaultTextAttribute);
65
66         fScanner= scanner;
67         fDefaultTextAttribute= defaultTextAttribute;
68     }
69
70     /**
71      * Creates a damager/repairer that uses the given scanner. The scanner may not be <code>null</code>
72      * and is assumed to return only token that carry text attributes.
73      *
74      * @param scanner the token scanner to be used, may not be <code>null</code>
75      */

76     public DefaultDamagerRepairer(ITokenScanner scanner) {
77
78         Assert.isNotNull(scanner);
79
80         fScanner= scanner;
81         fDefaultTextAttribute= new TextAttribute(null);
82     }
83
84     /*
85      * @see IPresentationDamager#setDocument(IDocument)
86      * @see IPresentationRepairer#setDocument(IDocument)
87      */

88     public void setDocument(IDocument document) {
89         fDocument= document;
90     }
91
92
93     //---- IPresentationDamager
94

95     /**
96      * Returns the end offset of the line that contains the specified offset or
97      * if the offset is inside a line delimiter, the end offset of the next line.
98      *
99      * @param offset the offset whose line end offset must be computed
100      * @return the line end offset for the given offset
101      * @exception BadLocationException if offset is invalid in the current document
102      */

103     protected int endOfLineOf(int offset) throws BadLocationException {
104
105         IRegion info= fDocument.getLineInformationOfOffset(offset);
106         if (offset <= info.getOffset() + info.getLength())
107             return info.getOffset() + info.getLength();
108
109         int line= fDocument.getLineOfOffset(offset);
110         try {
111             info= fDocument.getLineInformation(line + 1);
112             return info.getOffset() + info.getLength();
113         } catch (BadLocationException x) {
114             return fDocument.getLength();
115         }
116     }
117
118     /*
119      * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
120      */

121     public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent e, boolean documentPartitioningChanged) {
122
123         if (!documentPartitioningChanged) {
124             try {
125
126                 IRegion info= fDocument.getLineInformationOfOffset(e.getOffset());
127                 int start= Math.max(partition.getOffset(), info.getOffset());
128
129                 int end= e.getOffset() + (e.getText() == null ? e.getLength() : e.getText().length());
130
131                 if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
132                     // optimize the case of the same line
133
end= info.getOffset() + info.getLength();
134                 } else
135                     end= endOfLineOf(end);
136
137                 end= Math.min(partition.getOffset() + partition.getLength(), end);
138                 return new Region(start, end - start);
139
140             } catch (BadLocationException x) {
141             }
142         }
143
144         return partition;
145     }
146
147     //---- IPresentationRepairer
148

149     /*
150      * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
151      */

152     public void createPresentation(TextPresentation presentation, ITypedRegion region) {
153
154         if (fScanner == null) {
155             // will be removed if deprecated constructor will be removed
156
addRange(presentation, region.getOffset(), region.getLength(), fDefaultTextAttribute);
157             return;
158         }
159
160         int lastStart= region.getOffset();
161         int length= 0;
162         boolean firstToken= true;
163         IToken lastToken= Token.UNDEFINED;
164         TextAttribute lastAttribute= getTokenTextAttribute(lastToken);
165
166         fScanner.setRange(fDocument, lastStart, region.getLength());
167
168         while (true) {
169             IToken token= fScanner.nextToken();
170             if (token.isEOF())
171                 break;
172
173             TextAttribute attribute= getTokenTextAttribute(token);
174             if (lastAttribute != null && lastAttribute.equals(attribute)) {
175                 length += fScanner.getTokenLength();
176                 firstToken= false;
177             } else {
178                 if (!firstToken)
179                     addRange(presentation, lastStart, length, lastAttribute);
180                 firstToken= false;
181                 lastToken= token;
182                 lastAttribute= attribute;
183                 lastStart= fScanner.getTokenOffset();
184                 length= fScanner.getTokenLength();
185             }
186         }
187
188         addRange(presentation, lastStart, length, lastAttribute);
189     }
190
191     /**
192      * Returns a text attribute encoded in the given token. If the token's
193      * data is not <code>null</code> and a text attribute it is assumed that
194      * it is the encoded text attribute. It returns the default text attribute
195      * if there is no encoded text attribute found.
196      *
197      * @param token the token whose text attribute is to be determined
198      * @return the token's text attribute
199      */

200     protected TextAttribute getTokenTextAttribute(IToken token) {
201         Object JavaDoc data= token.getData();
202         if (data instanceof TextAttribute)
203             return (TextAttribute) data;
204         return fDefaultTextAttribute;
205     }
206
207     /**
208      * Adds style information to the given text presentation.
209      *
210      * @param presentation the text presentation to be extended
211      * @param offset the offset of the range to be styled
212      * @param length the length of the range to be styled
213      * @param attr the attribute describing the style of the range to be styled
214      */

215     protected void addRange(TextPresentation presentation, int offset, int length, TextAttribute attr) {
216         if (attr != null) {
217             int style= attr.getStyle();
218             int fontStyle= style & (SWT.ITALIC | SWT.BOLD | SWT.NORMAL);
219             StyleRange styleRange= new StyleRange(offset, length, attr.getForeground(), attr.getBackground(), fontStyle);
220             styleRange.strikeout= (style & TextAttribute.STRIKETHROUGH) != 0;
221             styleRange.underline= (style & TextAttribute.UNDERLINE) != 0;
222             styleRange.font= attr.getFont();
223             presentation.addStyleRange(styleRange);
224         }
225     }
226 }
227
228
229
Popular Tags