KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > NonRuleBasedDamagerRepairer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 package org.eclipse.pde.internal.ui.editor.text;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.DocumentEvent;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITypedRegion;
18 import org.eclipse.jface.text.Region;
19 import org.eclipse.jface.text.TextAttribute;
20 import org.eclipse.jface.text.TextPresentation;
21 import org.eclipse.jface.text.presentation.IPresentationDamager;
22 import org.eclipse.jface.text.presentation.IPresentationRepairer;
23 import org.eclipse.jface.util.Assert;
24 import org.eclipse.swt.custom.StyleRange;
25
26 public class NonRuleBasedDamagerRepairer
27     implements IPresentationDamager, IPresentationRepairer {
28
29     /** The document this object works on */
30     protected IDocument fDocument;
31     /** The default text attribute if non is returned as data by the current token */
32     protected TextAttribute fDefaultTextAttribute;
33
34     /**
35      * Constructor for NonRuleBasedDamagerRepairer.
36      */

37     public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
38         Assert.isNotNull(defaultTextAttribute);
39
40         fDefaultTextAttribute = defaultTextAttribute;
41     }
42
43     /**
44      * @see IPresentationRepairer#setDocument(IDocument)
45      */

46     public void setDocument(IDocument document) {
47         fDocument = document;
48     }
49
50     /**
51      * Returns the end offset of the line that contains the specified offset or
52      * if the offset is inside a line delimiter, the end offset of the next line.
53      *
54      * @param offset the offset whose line end offset must be computed
55      * @return the line end offset for the given offset
56      * @exception BadLocationException if offset is invalid in the current document
57      */

58     protected int endOfLineOf(int offset) throws BadLocationException {
59
60         IRegion info = fDocument.getLineInformationOfOffset(offset);
61         if (offset <= info.getOffset() + info.getLength())
62             return info.getOffset() + info.getLength();
63
64         int line = fDocument.getLineOfOffset(offset);
65         try {
66             info = fDocument.getLineInformation(line + 1);
67             return info.getOffset() + info.getLength();
68         } catch (BadLocationException x) {
69             return fDocument.getLength();
70         }
71     }
72
73     /**
74      * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
75      */

76     public IRegion getDamageRegion(
77         ITypedRegion partition,
78         DocumentEvent event,
79         boolean documentPartitioningChanged) {
80         if (!documentPartitioningChanged) {
81             try {
82
83                 IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
84                 int start = Math.max(partition.getOffset(), info.getOffset());
85
86                 int end =
87                     event.getOffset()
88                         + (event.getText() == null ? event.getLength() : event.getText().length());
89
90                 if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
91                     // optimize the case of the same line
92
end = info.getOffset() + info.getLength();
93                 } else
94                     end = endOfLineOf(end);
95
96                 end = Math.min(partition.getOffset() + partition.getLength(), end);
97                 return new Region(start, end - start);
98
99             } catch (BadLocationException x) {
100             }
101         }
102
103         return partition;
104     }
105
106     /**
107      * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
108      */

109     public void createPresentation(
110         TextPresentation presentation,
111         ITypedRegion region) {
112         addRange(
113             presentation,
114             region.getOffset(),
115             region.getLength(),
116             fDefaultTextAttribute);
117     }
118
119     /**
120      * Adds style information to the given text presentation.
121      *
122      * @param presentation the text presentation to be extended
123      * @param offset the offset of the range to be styled
124      * @param length the length of the range to be styled
125      * @param attr the attribute describing the style of the range to be styled
126      */

127     protected void addRange(
128         TextPresentation presentation,
129         int offset,
130         int length,
131         TextAttribute attr) {
132         if (attr != null)
133             presentation.addStyleRange(
134                 new StyleRange(
135                     offset,
136                     length,
137                     attr.getForeground(),
138                     attr.getBackground(),
139                     attr.getStyle()));
140     }
141     
142     /**
143      * Configures the scanner's default return token. This is the text attribute
144      * which is returned when none is returned by the current token.
145      */

146     public void setDefaultTextAttribute(TextAttribute defaultTextAttribute) {
147         fDefaultTextAttribute= defaultTextAttribute;
148     }
149 }
150
Popular Tags