KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > editors > xml > NonRuleBasedDamagerRepairer


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso.editors.xml;
5
6 import org.eclipse.jface.text.BadLocationException;
7 import org.eclipse.jface.text.DocumentEvent;
8 import org.eclipse.jface.text.IDocument;
9 import org.eclipse.jface.text.IRegion;
10 import org.eclipse.jface.text.ITypedRegion;
11 import org.eclipse.jface.text.Region;
12 import org.eclipse.jface.text.TextAttribute;
13 import org.eclipse.jface.text.TextPresentation;
14 import org.eclipse.jface.text.presentation.IPresentationDamager;
15 import org.eclipse.jface.text.presentation.IPresentationRepairer;
16 import org.eclipse.jface.util.Assert;
17 import org.eclipse.swt.custom.StyleRange;
18
19 public class NonRuleBasedDamagerRepairer
20     implements IPresentationDamager, IPresentationRepairer {
21
22     /** The document this object works on */
23     protected IDocument fDocument;
24     /** The default text attribute if non is returned as data by the current token */
25     protected TextAttribute fDefaultTextAttribute;
26     
27     /**
28      * Constructor for NonRuleBasedDamagerRepairer.
29      */

30     public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
31         Assert.isNotNull(defaultTextAttribute);
32
33         fDefaultTextAttribute = defaultTextAttribute;
34     }
35
36     /**
37      * @see IPresentationRepairer#setDocument(IDocument)
38      */

39     public void setDocument(IDocument document) {
40         fDocument = document;
41     }
42
43     /**
44      * Returns the end offset of the line that contains the specified offset or
45      * if the offset is inside a line delimiter, the end offset of the next line.
46      *
47      * @param offset the offset whose line end offset must be computed
48      * @return the line end offset for the given offset
49      * @exception BadLocationException if offset is invalid in the current document
50      */

51     protected int endOfLineOf(int offset) throws BadLocationException {
52
53         IRegion info = fDocument.getLineInformationOfOffset(offset);
54         if (offset <= info.getOffset() + info.getLength())
55             return info.getOffset() + info.getLength();
56
57         int line = fDocument.getLineOfOffset(offset);
58         try {
59             info = fDocument.getLineInformation(line + 1);
60             return info.getOffset() + info.getLength();
61         } catch (BadLocationException x) {
62             return fDocument.getLength();
63         }
64     }
65
66     /**
67      * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
68      */

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

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

126     protected void addRange(
127         TextPresentation presentation,
128         int offset,
129         int length,
130         TextAttribute attr) {
131         if (attr != null)
132             presentation.addStyleRange(
133                 new StyleRange(
134                     offset,
135                     length,
136                     attr.getForeground(),
137                     attr.getBackground(),
138                     attr.getStyle()));
139     }
140 }
Popular Tags