Source Code Cross Referenced for Segment.java in  » 6.0-JDK-Core » swing » javax » swing » text » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » swing » javax.swing.text 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025        package javax.swing.text;
026
027        import java.text.CharacterIterator;
028
029        /**
030         * A segment of a character array representing a fragment
031         * of text.  It should be treated as immutable even though
032         * the array is directly accessible.  This gives fast access
033         * to fragments of text without the overhead of copying
034         * around characters.  This is effectively an unprotected
035         * String.
036         * <p>
037         * The Segment implements the java.text.CharacterIterator
038         * interface to support use with the i18n support without
039         * copying text into a string.
040         *
041         * @author  Timothy Prinzing
042         * @version 1.31 05/05/07
043         */
044        public class Segment implements  Cloneable, CharacterIterator,
045                CharSequence {
046
047            /**
048             * This is the array containing the text of
049             * interest.  This array should never be modified;
050             * it is available only for efficiency.
051             */
052            public char[] array;
053
054            /**
055             * This is the offset into the array that
056             * the desired text begins.
057             */
058            public int offset;
059
060            /**
061             * This is the number of array elements that
062             * make up the text of interest.
063             */
064            public int count;
065
066            private boolean partialReturn;
067
068            /**
069             * Creates a new segment.
070             */
071            public Segment() {
072                this (null, 0, 0);
073            }
074
075            /**
076             * Creates a new segment referring to an existing array.
077             *
078             * @param array the array to refer to
079             * @param offset the offset into the array
080             * @param count the number of characters
081             */
082            public Segment(char[] array, int offset, int count) {
083                this .array = array;
084                this .offset = offset;
085                this .count = count;
086                partialReturn = false;
087            }
088
089            /** 
090             * Flag to indicate that partial returns are valid.  If the flag is true, 
091             * an implementation of the interface method Document.getText(position,length,Segment) 
092             * should return as much text as possible without making a copy.  The default 
093             * state of the flag is false which will cause Document.getText(position,length,Segment) 
094             * to provide the same return behavior it always had, which may or may not 
095             * make a copy of the text depending upon the request.   
096             * 
097             * @param p whether or not partial returns are valid.
098             * @since 1.4
099             */
100            public void setPartialReturn(boolean p) {
101                partialReturn = p;
102            }
103
104            /**
105             * Flag to indicate that partial returns are valid. 
106             *
107             * @return whether or not partial returns are valid.
108             * @since 1.4
109             */
110            public boolean isPartialReturn() {
111                return partialReturn;
112            }
113
114            /**
115             * Converts a segment into a String.
116             *
117             * @return the string
118             */
119            public String toString() {
120                if (array != null) {
121                    return new String(array, offset, count);
122                }
123                return new String();
124            }
125
126            // --- CharacterIterator methods -------------------------------------
127
128            /**
129             * Sets the position to getBeginIndex() and returns the character at that
130             * position.
131             * @return the first character in the text, or DONE if the text is empty
132             * @see #getBeginIndex
133             * @since 1.3
134             */
135            public char first() {
136                pos = offset;
137                if (count != 0) {
138                    return array[pos];
139                }
140                return DONE;
141            }
142
143            /**
144             * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
145             * and returns the character at that position.
146             * @return the last character in the text, or DONE if the text is empty
147             * @see #getEndIndex
148             * @since 1.3
149             */
150            public char last() {
151                pos = offset + count;
152                if (count != 0) {
153                    pos -= 1;
154                    return array[pos];
155                }
156                return DONE;
157            }
158
159            /**
160             * Gets the character at the current position (as returned by getIndex()).
161             * @return the character at the current position or DONE if the current
162             * position is off the end of the text.
163             * @see #getIndex
164             * @since 1.3
165             */
166            public char current() {
167                if (count != 0 && pos < offset + count) {
168                    return array[pos];
169                }
170                return DONE;
171            }
172
173            /**
174             * Increments the iterator's index by one and returns the character
175             * at the new index.  If the resulting index is greater or equal
176             * to getEndIndex(), the current index is reset to getEndIndex() and
177             * a value of DONE is returned.
178             * @return the character at the new position or DONE if the new
179             * position is off the end of the text range.
180             * @since 1.3
181             */
182            public char next() {
183                pos += 1;
184                int end = offset + count;
185                if (pos >= end) {
186                    pos = end;
187                    return DONE;
188                }
189                return current();
190            }
191
192            /**
193             * Decrements the iterator's index by one and returns the character
194             * at the new index. If the current index is getBeginIndex(), the index
195             * remains at getBeginIndex() and a value of DONE is returned.
196             * @return the character at the new position or DONE if the current
197             * position is equal to getBeginIndex().
198             * @since 1.3
199             */
200            public char previous() {
201                if (pos == offset) {
202                    return DONE;
203                }
204                pos -= 1;
205                return current();
206            }
207
208            /**
209             * Sets the position to the specified position in the text and returns that
210             * character.
211             * @param position the position within the text.  Valid values range from
212             * getBeginIndex() to getEndIndex().  An IllegalArgumentException is thrown
213             * if an invalid value is supplied.
214             * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
215             * @since 1.3
216             */
217            public char setIndex(int position) {
218                int end = offset + count;
219                if ((position < offset) || (position > end)) {
220                    throw new IllegalArgumentException("bad position: "
221                            + position);
222                }
223                pos = position;
224                if ((pos != end) && (count != 0)) {
225                    return array[pos];
226                }
227                return DONE;
228            }
229
230            /**
231             * Returns the start index of the text.
232             * @return the index at which the text begins.
233             * @since 1.3
234             */
235            public int getBeginIndex() {
236                return offset;
237            }
238
239            /**
240             * Returns the end index of the text.  This index is the index of the first
241             * character following the end of the text.
242             * @return the index after the last character in the text
243             * @since 1.3
244             */
245            public int getEndIndex() {
246                return offset + count;
247            }
248
249            /**
250             * Returns the current index.
251             * @return the current index.
252             * @since 1.3
253             */
254            public int getIndex() {
255                return pos;
256            }
257
258            // --- CharSequence methods -------------------------------------
259
260            /** 
261             * {@inheritDoc} 
262             * @since 1.6
263             */
264            public char charAt(int index) {
265                if (index < 0 || index >= count) {
266                    throw new StringIndexOutOfBoundsException(index);
267                }
268                return array[offset + index];
269            }
270
271            /** 
272             * {@inheritDoc} 
273             * @since 1.6
274             */
275            public int length() {
276                return count;
277            }
278
279            /** 
280             * {@inheritDoc} 
281             * @since 1.6
282             */
283            public CharSequence subSequence(int start, int end) {
284                if (start < 0) {
285                    throw new StringIndexOutOfBoundsException(start);
286                }
287                if (end > count) {
288                    throw new StringIndexOutOfBoundsException(end);
289                }
290                if (start > end) {
291                    throw new StringIndexOutOfBoundsException(end - start);
292                }
293                Segment segment = new Segment();
294                segment.array = this .array;
295                segment.offset = this .offset + start;
296                segment.count = end - start;
297                return segment;
298            }
299
300            /**
301             * Creates a shallow copy.
302             *
303             * @return the copy
304             */
305            public Object clone() {
306                Object o;
307                try {
308                    o = super .clone();
309                } catch (CloneNotSupportedException cnse) {
310                    o = null;
311                }
312                return o;
313            }
314
315            private int pos;
316
317        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.