View Javadoc

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  	/** The document this object works on */
22  	protected IDocument fDocument;
23  	/** The default text attribute if non is returned as data by the current token */
24  	protected TextAttribute fDefaultTextAttribute;
25  	
26  	/**
27  	 * Constructor for NonRuleBasedDamagerRepairer.
28  	 * @param defaultTextAttribute
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 
103 		return partition;
104 	}
105 
106 	/**
107 	 * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
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 	 * Adds style information to the given text presentation.
121 	 *
122 	 * @param presentation the text presentation to be extended
123 	 * @param offset the offset of the range to be styled
124 	 * @param length the length of the range to be styled
125 	 * @param attr the attribute describing the style of the range to be styled
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 }