|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 |
|
18 package com.symbian.smt.gui.editors.xmleditor; |
|
19 |
|
20 import org.eclipse.core.runtime.Assert; |
|
21 import org.eclipse.jface.text.BadLocationException; |
|
22 import org.eclipse.jface.text.DocumentEvent; |
|
23 import org.eclipse.jface.text.IDocument; |
|
24 import org.eclipse.jface.text.IRegion; |
|
25 import org.eclipse.jface.text.ITypedRegion; |
|
26 import org.eclipse.jface.text.Region; |
|
27 import org.eclipse.jface.text.TextAttribute; |
|
28 import org.eclipse.jface.text.TextPresentation; |
|
29 import org.eclipse.jface.text.presentation.IPresentationDamager; |
|
30 import org.eclipse.jface.text.presentation.IPresentationRepairer; |
|
31 import org.eclipse.swt.custom.StyleRange; |
|
32 |
|
33 /** |
|
34 * @author barbararosi-schwartz |
|
35 * |
|
36 */ |
|
37 public class NonRuleBasedDamagerRepairer implements IPresentationDamager, |
|
38 IPresentationRepairer { |
|
39 |
|
40 /** The document this object works on */ |
|
41 protected IDocument fDocument; |
|
42 /** |
|
43 * The default text attribute if non is returned as data by the current |
|
44 * token |
|
45 */ |
|
46 protected TextAttribute fDefaultTextAttribute; |
|
47 |
|
48 /** |
|
49 * Constructor for NonRuleBasedDamagerRepairer. |
|
50 */ |
|
51 public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) { |
|
52 Assert.isNotNull(defaultTextAttribute); |
|
53 |
|
54 fDefaultTextAttribute = defaultTextAttribute; |
|
55 } |
|
56 |
|
57 /** |
|
58 * Adds style information to the given text presentation. |
|
59 * |
|
60 * @param presentation |
|
61 * the text presentation to be extended |
|
62 * @param offset |
|
63 * the offset of the range to be styled |
|
64 * @param length |
|
65 * the length of the range to be styled |
|
66 * @param attr |
|
67 * the attribute describing the style of the range to be styled |
|
68 */ |
|
69 protected void addRange(TextPresentation presentation, int offset, |
|
70 int length, TextAttribute attr) { |
|
71 if (attr != null) |
|
72 presentation.addStyleRange(new StyleRange(offset, length, attr |
|
73 .getForeground(), attr.getBackground(), attr.getStyle())); |
|
74 } |
|
75 |
|
76 /** |
|
77 * @see IPresentationRepairer#createPresentation(TextPresentation, |
|
78 * ITypedRegion) |
|
79 */ |
|
80 public void createPresentation(TextPresentation presentation, |
|
81 ITypedRegion region) { |
|
82 addRange(presentation, region.getOffset(), region.getLength(), |
|
83 fDefaultTextAttribute); |
|
84 } |
|
85 |
|
86 /** |
|
87 * Returns the end offset of the line that contains the specified offset or |
|
88 * if the offset is inside a line delimiter, the end offset of the next |
|
89 * line. |
|
90 * |
|
91 * @param offset |
|
92 * the offset whose line end offset must be computed |
|
93 * @return the line end offset for the given offset |
|
94 * @exception BadLocationException |
|
95 * if offset is invalid in the current document |
|
96 */ |
|
97 protected int endOfLineOf(int offset) throws BadLocationException { |
|
98 |
|
99 IRegion info = fDocument.getLineInformationOfOffset(offset); |
|
100 if (offset <= info.getOffset() + info.getLength()) |
|
101 return info.getOffset() + info.getLength(); |
|
102 |
|
103 int line = fDocument.getLineOfOffset(offset); |
|
104 try { |
|
105 info = fDocument.getLineInformation(line + 1); |
|
106 return info.getOffset() + info.getLength(); |
|
107 } catch (BadLocationException x) { |
|
108 return fDocument.getLength(); |
|
109 } |
|
110 } |
|
111 |
|
112 /** |
|
113 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, |
|
114 * boolean) |
|
115 */ |
|
116 public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent event, |
|
117 boolean documentPartitioningChanged) { |
|
118 if (!documentPartitioningChanged) { |
|
119 try { |
|
120 |
|
121 IRegion info = fDocument.getLineInformationOfOffset(event |
|
122 .getOffset()); |
|
123 int start = Math.max(partition.getOffset(), info.getOffset()); |
|
124 |
|
125 int end = event.getOffset() |
|
126 + (event.getText() == null ? event.getLength() : event |
|
127 .getText().length()); |
|
128 |
|
129 if (info.getOffset() <= end |
|
130 && end <= info.getOffset() + info.getLength()) { |
|
131 // optimize the case of the same line |
|
132 end = info.getOffset() + info.getLength(); |
|
133 } else |
|
134 end = endOfLineOf(end); |
|
135 |
|
136 end = Math.min(partition.getOffset() + partition.getLength(), |
|
137 end); |
|
138 return new Region(start, end - start); |
|
139 |
|
140 } catch (BadLocationException x) { |
|
141 } |
|
142 } |
|
143 |
|
144 return partition; |
|
145 } |
|
146 |
|
147 /** |
|
148 * @see IPresentationRepairer#setDocument(IDocument) |
|
149 */ |
|
150 public void setDocument(IDocument document) { |
|
151 fDocument = document; |
|
152 } |
|
153 } |