KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > inc > OriginalText


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer.inc;
21
22 import javax.swing.BoundedRangeModel JavaDoc;
23 import org.netbeans.lib.editor.util.AbstractCharSequence;
24 import org.netbeans.lib.editor.util.CharSubSequence;
25
26 /**
27  * Character sequence emulating state of a mutable input source
28  * before the last modification.
29  *
30  * @author Miloslav Metelka
31  * @version 1.00
32  */

33
34 public final class OriginalText extends AbstractCharSequence.StringLike {
35
36     private final CharSequence JavaDoc currentText;
37
38     private final int offset;
39
40     private final int insertedTextLength;
41
42     private final CharSequence JavaDoc removedText;
43
44     private final int origLength;
45
46     public OriginalText(CharSequence JavaDoc currentText, int offset, CharSequence JavaDoc removedText, int insertedTextLength) {
47         this.currentText = currentText;
48         this.offset = offset;
49         this.removedText = (removedText != null) ? removedText : ""; // always non-null
50
this.insertedTextLength = insertedTextLength;
51
52         this.origLength = currentText.length() - insertedTextLength + this.removedText.length();
53     }
54
55     public int length() {
56         return origLength;
57     }
58
59     public char charAt(int index) {
60         if (index < offset) {
61             return currentText.charAt(index);
62         }
63         index -= offset;
64         if (index < removedText.length()) {
65             return removedText.charAt(index);
66         }
67         return currentText.charAt(offset + index - removedText.length() + insertedTextLength);
68     }
69
70     public char[] toCharArray(int start, int end) {
71         char[] chars = new char[end - start];
72         int charsIndex = 0;
73         if (start < offset) {
74             int bound = (end < offset) ? end : offset;
75             while (start < bound) {
76                 chars[charsIndex++] = currentText.charAt(start++);
77             }
78             if (end == bound) {
79                 return chars;
80             }
81         }
82         start -= offset;
83         end -= offset;
84         int bound = removedText.length();
85         if (start < bound) {
86             if (end < bound) {
87                 bound = end;
88             }
89             while (start < bound) {
90                 chars[charsIndex++] = removedText.charAt(start++);
91             }
92             if (end == bound) {
93                 return chars;
94             }
95         }
96         bound = offset - removedText.length() + insertedTextLength;
97         start += bound;
98         bound += end;
99         while (start < bound) {
100             chars[charsIndex++] = currentText.charAt(start++);
101         }
102         return chars;
103     }
104
105 }
106
Popular Tags