1 package org.eparapher.rcp.editors.xml;
2
3 import org.eclipse.swt.custom.StyleRange;
4
5 import org.eclipse.jface.util.Assert;
6
7 import org.eclipse.jface.text.BadLocationException;
8 import org.eclipse.jface.text.DocumentEvent;
9 import org.eclipse.jface.text.IDocument;
10 import org.eclipse.jface.text.IRegion;
11 import org.eclipse.jface.text.ITypedRegion;
12 import org.eclipse.jface.text.Region;
13 import org.eclipse.jface.text.TextAttribute;
14 import org.eclipse.jface.text.TextPresentation;
15 import org.eclipse.jface.text.presentation.IPresentationDamager;
16 import org.eclipse.jface.text.presentation.IPresentationRepairer;
17
18 public class NonRuleBasedDamagerRepairer
19 implements IPresentationDamager, IPresentationRepairer {
20
21
22 protected IDocument fDocument;
23
24 protected TextAttribute fDefaultTextAttribute;
25
26
27
28
29
30 public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
31 Assert.isNotNull(defaultTextAttribute);
32
33 fDefaultTextAttribute = defaultTextAttribute;
34 }
35
36
37
38
39 public void setDocument(IDocument document) {
40 fDocument = document;
41 }
42
43
44
45
46
47
48
49
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
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
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
103 return partition;
104 }
105
106
107
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
121
122
123
124
125
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 }