View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
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  * @author zhanghao
40  *
41  */
42  public class NonRuleBasedDamagerRepairer
43  	implements IPresentationDamager, IPresentationRepairer {
44  
45  	/** The document this object works on */
46  	protected IDocument fDocument;
47  	/** The default text attribute if non is returned as data by the current token */
48  	protected TextAttribute fDefaultTextAttribute;
49  	
50  	/**
51  	 * Constructor for NonRuleBasedDamagerRepairer.
52  	 */
53  	public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
54  		Assert.isNotNull(defaultTextAttribute);
55  
56  		fDefaultTextAttribute = defaultTextAttribute;
57  	}
58  
59  	/**
60  	 * @see IPresentationRepairer#setDocument(IDocument)
61  	 */
62  	public void setDocument(IDocument document) {
63  		fDocument = document;
64  	}
65  
66  	/**
67  	 * Returns the end offset of the line that contains the specified offset or
68  	 * if the offset is inside a line delimiter, the end offset of the next line.
69  	 *
70  	 * @param offset the offset whose line end offset must be computed
71  	 * @return the line end offset for the given offset
72  	 * @exception BadLocationException if offset is invalid in the current document
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  	 * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
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 					// optimize the case of the same line
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 	 * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
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 	 * Adds style information to the given text presentation.
144 	 *
145 	 * @param presentation the text presentation to be extended
146 	 * @param offset the offset of the range to be styled
147 	 * @param length the length of the range to be styled
148 	 * @param attr the attribute describing the style of the range to be styled
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 }