KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > text > Segment


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: Segment.java,v $
11    Revision 1.1 2004/01/26 08:11:14 bobintetley
12    Many bugfixes and addition of SwingSet
13
14
15 */

16
17 package swingwtx.swing.text;
18
19 import java.text.CharacterIterator JavaDoc;
20
21 public class Segment implements Cloneable JavaDoc, CharacterIterator JavaDoc {
22     
23     public char[] array;
24     public int offset;
25     public int count;
26     
27     private boolean partialReturn;
28     
29     public Segment() {
30         this(null, 0, 0);
31     }
32     public Segment(char[] array, int offset, int count) {
33         this.array = array;
34         this.offset = offset;
35         this.count = count;
36         partialReturn = false;
37     }
38     public void setPartialReturn(boolean p) {
39         partialReturn = p;
40     }
41     public boolean isPartialReturn() {
42         return partialReturn;
43     }
44     public String JavaDoc toString() {
45         if (array != null) {
46             return new String JavaDoc(array, offset, count);
47         }
48         return new String JavaDoc();
49     }
50     
51     public char first() {
52         pos = offset;
53         if (count != 0) {
54             return array[pos];
55         }
56         return DONE;
57     }
58     public char last() {
59         pos = offset + count;
60         if (count != 0) {
61             pos -= 1;
62             return array[pos];
63         }
64         return DONE;
65     }
66     public char current() {
67         if (count != 0 && pos < offset + count) {
68             return array[pos];
69         }
70         return DONE;
71     }
72     public char next() {
73         pos += 1;
74         int end = offset + count;
75         if (pos >= end) {
76             pos = end;
77             return DONE;
78         }
79         return current();
80     }
81     public char previous() {
82         if (pos == offset) {
83             return DONE;
84         }
85         pos -= 1;
86         return current();
87     }
88     public char setIndex(int position) {
89         int end = offset + count;
90         if ((position < offset) || (position > end)) {
91             throw new IllegalArgumentException JavaDoc("bad position: " + position);
92         }
93         pos = position;
94         if ((pos != end) && (count != 0)) {
95             return array[pos];
96         }
97         return DONE;
98     }
99     public int getBeginIndex() {
100         return offset;
101     }
102     public int getEndIndex() {
103         return offset + count;
104     }
105     public int getIndex() {
106         return pos;
107     }
108     public Object JavaDoc clone() {
109         Object JavaDoc o;
110         try {
111             o = super.clone();
112         } catch (CloneNotSupportedException JavaDoc cnse) {
113             o = null;
114         }
115         return o;
116     }
117     
118     private int pos;
119 }
Popular Tags