1 package com.quantum.editors;
3 import org.eclipse.jface.text.BadLocationException;
4 import org.eclipse.jface.text.DocumentEvent;
5 import org.eclipse.jface.text.IDocument;
6 import org.eclipse.jface.text.IRegion;
7 import org.eclipse.jface.text.ITypedRegion;
8 import org.eclipse.jface.text.Region;
9 import org.eclipse.jface.text.TextAttribute;
10 import org.eclipse.jface.text.TextPresentation;
11 import org.eclipse.jface.text.presentation.IPresentationDamager;
12 import org.eclipse.jface.text.presentation.IPresentationRepairer;
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.swt.custom.StyleRange;
16 public class NonRuleBasedDamagerRepairer
17 implements IPresentationDamager, IPresentationRepairer {
19 /** The document this object works on */
20 protected IDocument fDocument;
21 /** The default text attribute if non is returned as data by the current token */
22 protected TextAttribute fDefaultTextAttribute;
25 * Constructor for NonRuleBasedDamagerRepairer.
27 public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
28 Assert.isNotNull(defaultTextAttribute);
30 fDefaultTextAttribute = defaultTextAttribute;
34 * @see IPresentationRepairer#setDocument(IDocument)
36 public void setDocument(IDocument document) {
40 public void setTextAttribute(TextAttribute attr) {
41 fDefaultTextAttribute = attr;
45 * Returns the end offset of the line that contains the specified offset or
46 * if the offset is inside a line delimiter, the end offset of the next line.
48 * @param offset the offset whose line end offset must be computed
49 * @return the line end offset for the given offset
50 * @exception BadLocationException if offset is invalid in the current document
52 protected int endOfLineOf(int offset) throws BadLocationException {
54 IRegion info = fDocument.getLineInformationOfOffset(offset);
55 if (offset <= info.getOffset() + info.getLength())
56 return info.getOffset() + info.getLength();
58 int line = fDocument.getLineOfOffset(offset);
60 info = fDocument.getLineInformation(line + 1);
61 return info.getOffset() + info.getLength();
62 } catch (BadLocationException x) {
63 return fDocument.getLength();
68 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
70 public IRegion getDamageRegion(
71 ITypedRegion partition,
73 boolean documentPartitioningChanged) {
74 if (!documentPartitioningChanged) {
77 IRegion info = fDocument.getLineInformationOfOffset(event.getOffset());
78 int start = Math.max(partition.getOffset(), info.getOffset());
82 + (event.getText() == null ? event.getLength() : event.getText().length());
84 if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
85 // optimize the case of the same line
86 end = info.getOffset() + info.getLength();
88 end = endOfLineOf(end);
90 end = Math.min(partition.getOffset() + partition.getLength(), end);
91 return new Region(start, end - start);
93 } catch (BadLocationException x) {
101 * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
103 public void createPresentation(
104 TextPresentation presentation,
105 ITypedRegion region) {
110 fDefaultTextAttribute);
114 * Adds style information to the given text presentation.
116 * @param presentation the text presentation to be extended
117 * @param offset the offset of the range to be styled
118 * @param length the length of the range to be styled
119 * @param attr the attribute describing the style of the range to be styled
121 protected void addRange(
122 TextPresentation presentation,
125 TextAttribute attr) {
127 presentation.addStyleRange(
131 attr.getForeground(),
132 attr.getBackground(),