1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.fosstrak.llrp.commander.editors;
23
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.DocumentEvent;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.IRegion;
28 import org.eclipse.jface.text.ITypedRegion;
29 import org.eclipse.jface.text.Region;
30 import org.eclipse.jface.text.TextAttribute;
31 import org.eclipse.jface.text.TextPresentation;
32 import org.eclipse.jface.text.presentation.IPresentationDamager;
33 import org.eclipse.jface.text.presentation.IPresentationRepairer;
34 import org.eclipse.core.runtime.Assert;
35 import org.eclipse.swt.custom.StyleRange;
36
37
38
39
40
41
42 public class NonRuleBasedDamagerRepairer
43 implements IPresentationDamager, IPresentationRepairer {
44
45
46 protected IDocument fDocument;
47
48 protected TextAttribute fDefaultTextAttribute;
49
50
51
52
53 public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
54 Assert.isNotNull(defaultTextAttribute);
55
56 fDefaultTextAttribute = defaultTextAttribute;
57 }
58
59
60
61
62 public void setDocument(IDocument document) {
63 fDocument = document;
64 }
65
66
67
68
69
70
71
72
73
74 protected int endOfLineOf(int offset) throws BadLocationException {
75
76 IRegion info = fDocument.getLineInformationOfOffset(offset);
77 if (offset <= info.getOffset() + info.getLength())
78 return info.getOffset() + info.getLength();
79
80 int line = fDocument.getLineOfOffset(offset);
81 try {
82 info = fDocument.getLineInformation(line + 1);
83 return info.getOffset() + info.getLength();
84 } catch (BadLocationException x) {
85 return fDocument.getLength();
86 }
87 }
88
89
90
91
92 public IRegion getDamageRegion(
93 ITypedRegion partition,
94 DocumentEvent event,
95 boolean documentPartitioningChanged) {
96 if (!documentPartitioningChanged) {
97 try {
98
99 IRegion info =
100 fDocument.getLineInformationOfOffset(event.getOffset());
101 int start = Math.max(partition.getOffset(), info.getOffset());
102
103 int end =
104 event.getOffset()
105 + (event.getText() == null
106 ? event.getLength()
107 : event.getText().length());
108
109 if (info.getOffset() <= end
110 && end <= info.getOffset() + info.getLength()) {
111
112 end = info.getOffset() + info.getLength();
113 } else
114 end = endOfLineOf(end);
115
116 end =
117 Math.min(
118 partition.getOffset() + partition.getLength(),
119 end);
120 return new Region(start, end - start);
121
122 } catch (BadLocationException x) {
123 }
124 }
125
126 return partition;
127 }
128
129
130
131
132 public void createPresentation(
133 TextPresentation presentation,
134 ITypedRegion region) {
135 addRange(
136 presentation,
137 region.getOffset(),
138 region.getLength(),
139 fDefaultTextAttribute);
140 }
141
142
143
144
145
146
147
148
149
150 protected void addRange(
151 TextPresentation presentation,
152 int offset,
153 int length,
154 TextAttribute attr) {
155 if (attr != null)
156 presentation.addStyleRange(
157 new StyleRange(
158 offset,
159 length,
160 attr.getForeground(),
161 attr.getBackground(),
162 attr.getStyle()));
163 }
164 }