javax.swing.text

Class Segment

Implemented Interfaces:
CharacterIterator, Cloneable

public class Segment
extends Object
implements Cloneable, CharacterIterator

A segment of a character array representing a fragment of text. It should be treated as immutable even though the array is directly accessible. This gives fast access to fragments of text without the overhead of copying around characters. This is effectively an unprotected String.

The Segment implements the java.text.CharacterIterator interface to support use with the i18n support without copying text into a string.

Field Summary

char[]
array
This is the array containing the text of interest.
int
count
This is the number of array elements that make up the text of interest.
int
offset
This is the offset into the array that the desired text begins.

Fields inherited from interface java.text.CharacterIterator

DONE

Constructor Summary

Segment()
Creates a new segment.
Segment(char[] array, int offset, int count)
Creates a new segment referring to an existing array.

Method Summary

Object
clone()
Creates a shallow copy.
char
current()
Gets the character at the current position (as returned by getIndex()).
char
first()
Sets the position to getBeginIndex() and returns the character at that position.
int
getBeginIndex()
Returns the start index of the text.
int
getEndIndex()
Returns the end index of the text.
int
getIndex()
Returns the current index.
boolean
isPartialReturn()
Flag to indicate that partial returns are valid.
char
last()
Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) and returns the character at that position.
char
next()
Increments the iterator's index by one and returns the character at the new index.
char
previous()
Decrements the iterator's index by one and returns the character at the new index.
char
setIndex(int position)
Sets the position to the specified position in the text and returns that character.
void
setPartialReturn(boolean p)
Flag to indicate that partial returns are valid.
String
toString()
Converts a segment into a String.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

array

public char[] array
This is the array containing the text of interest. This array should never be modified; it is available only for efficiency.
Usages and Demos :

View More Examples of array
   1: import java.io.Serializable;
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5: 
   6:     public SegmentCharSequence(Segment seg)
   7:     {
   8:         ...
   9: 
  10:     public SegmentCharSequence(Segment seg, boolean reverse)
  11:     {
  12:         ...
  13:             index = length - index - 1;
  14:         return seg.array[seg.offset + offset + index];

View Full Code Here
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5: 
   6:     public byte lookup(Segment segment, int offset, int length)
   7:     {
   8:         ...
   9:     {
  10:         char firstChar=Character.toUpperCase(segment.array[offset]);
  11:         ...
  12:         char lastChar=Character.toUpperCase(segment.array[offset+length-1]);

View Full Code Here
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5: 
   6:     public void getText(int start, int len, Segment seg)
   7:     {
   8:         ...
   9:         {
  10:             seg.array = text;
  11:             seg.offset = start + gapEnd - gapStart;
  12:         ...
  13:         {
  14:             seg.array = text;

View Full Code Here
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5: 
   6:   public byte markTokensImpl(byte token, Segment line, int lineIndex)
   7:   {
   8:         ...
   9:     {
  10:       switch(line.array[i])
  11:       {
  12:         ...
  13:       case '*':
  14:         if(token == JEditToken.COMMENT1 && length - i >= 1 && line.array[i+1] == '/')

View Full Code Here
   1: import javax.swing.table.TableModel;
   2: import javax.swing.text.Segment;
   3: import javax.swing.text.TabExpander;
   4:         ...
   5:     int numLines = 1;
   6:     Segment s = new Segment(text.toCharArray(), 0, 0);
   7:         ...
   8:     s.count = s.array.length;
   9:     TabExpander te = new MyTabExpander(fm);
  10:         ...
  11:     int breaks = getBreakLocation(s, fm, 0, width, te, 0);
  12:     while((breaks+s.offset) < s.array.length) {

View Full Code Here

count

public int count
This is the number of array elements that make up the text of interest.
Usages and Demos :

View More Examples of count
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5: 
   6:     public void getText(int start, int len, Segment seg)
   7:     {
   8:         ...
   9:             seg.offset = start + gapEnd - gapStart;
  10:             seg.count = len;
  11:         }
  12:         ...
  13:             seg.offset = start;
  14:             seg.count = len;

View Full Code Here
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5: 
   6:     public void paintPlainLine(Writer out, int lineNumber, Segment line, SyntaxToken tokens)
   7:     {
   8:         ...
   9:             int pos = this.position.getPos();
  10:             String expandedText = this.expander.expand(pos, line.array, line.offset, line.count);
  11:             this.position.incPos(expandedText.length());
  12:         ...
  13:     public void paintSyntaxLine(
  14:             Writer out, int lineNumber, Segment line, SyntaxToken tokens

View Full Code Here
   1: import javax.swing.table.TableModel;
   2: import javax.swing.text.Segment;
   3: import javax.swing.text.TabExpander;
   4:         ...
   5:     int numLines = 1;
   6:     Segment s = new Segment(text.toCharArray(), 0, 0);
   7:         ...
   8:     s.count = s.array.length;
   9:     TabExpander te = new MyTabExpander(fm);
  10:         ...
  11:       s.offset += breaks;
  12:       s.count = s.array.length - s.offset;

View Full Code Here
   1: import javax.swing.text.PlainView;
   2: import javax.swing.text.Segment;
   3: import javax.swing.text.TabExpander;
   4:         ...
   5: 
   6:     private Segment segment;
   7: 
   8:         ...
   9:             int segmentOffset=segment.offset;
  10:             segment.count=tokenLength;
  11:             Style style=styleSchema.getStyle(styleId);
  12:         ...
  13:                 {
  14:                     segment.count=selectionStart-start-offset;

View Full Code Here
   1: 
   2: import javax.swing.text.Segment;
   3: import java.io.*;
   4:         ...
   5:     {
   6:                 seg.count--;
   7:         }
   8:         ...
   9:                 buffer.setBooleanProperty(Buffer.TRAILING_EOL,true);
  10:                 seg.count--;
  11:                 endOffsets.setSize(endOffsets.getSize() - 1);
  12:         ...
  13: 
  14:         endOffsets.add(seg.count + 1);

View Full Code Here

offset

public int offset
This is the offset into the array that the desired text begins.
Usages and Demos :

View More Examples of offset
   1: import java.io.Serializable;
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5: 
   6:     public SegmentCharSequence(Segment seg)
   7:     {
   8:         ...
   9: 
  10:     public SegmentCharSequence(Segment seg, boolean reverse)
  11:     {
  12:         ...
  13:     {
  14:         this.offset = off;

View Full Code Here
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5: 
   6:     public void getText(int start, int len, Segment seg)
   7:     {
   8:         ...
   9:             seg.array = text;
  10:             seg.offset = start + gapEnd - gapStart;
  11:             seg.count = len;
  12:         ...
  13:             seg.array = text;
  14:             seg.offset = start;

View Full Code Here
   1: 
   2:     public void handleToken(Segment seg, byte id, int offset, int length,
   3:         TokenMarker.LineContext context)
   4:         ...
   5:                 if(Character.isWhitespace(seg.array[
   6:                     seg.offset + chunk.offset]))
   7:                 {
   8:         ...
   9:                         Chunk nextLine = new Chunk(endOfWhitespace,
  10:                             end.offset + end.length,
  11:                             getParserRuleSet(context));
  12:         ...
  13: 
  14:     protected void initChunk(Chunk chunk, Segment seg)

View Full Code Here
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5: 
   6:     public void paintPlainLine(Writer out, int lineNumber, Segment line, SyntaxToken tokens)
   7:     {
   8:         ...
   9:             int pos = this.position.getPos();
  10:             String expandedText = this.expander.expand(pos, line.array, line.offset, line.count);
  11:             this.position.incPos(expandedText.length());
  12:         ...
  13:     public void paintSyntaxLine(
  14:             Writer out, int lineNumber, Segment line, SyntaxToken tokens

View Full Code Here
   1: import javax.swing.table.TableModel;
   2: import javax.swing.text.Segment;
   3: import javax.swing.text.TabExpander;
   4:         ...
   5:     int numLines = 1;
   6:     Segment s = new Segment(text.toCharArray(), 0, 0);
   7:     s.count = s.array.length;
   8:         ...
   9:     int breaks = getBreakLocation(s, fm, 0, width, te, 0);
  10:     while((breaks+s.offset) < s.array.length) {
  11:         ...
  12:       s.offset += breaks;

View Full Code Here

Constructor Details

Segment

public Segment()
Creates a new segment.
Usages and Demos :

View More Examples of Segment()
   1:             if(end > start)
   2:             {    Segment segment = new Segment();
   3:                 requester.getDocument().getText( start, end - start, segment );
   4:                 return segment.toString().trim();
   5:             }

View Full Code Here
   1:     int length = de.getLength();
   2:     Segment seg = new Segment();
   3:     try {
   4:     } catch (BadLocationException ble) { }

View Full Code Here
   1:         int doclen = doc.getLength();
   2:         Segment segment = new Segment();
   3:         try {
   4:             doc.getText(mark, doclen - mark, segment);
   5:         } catch (BadLocationException ble) {

View Full Code Here
   1:     ntbl = 0;
   2:     seg = new Segment();
   3: }
   4: 

View Full Code Here
   1:     {
   2:         Segment seg = new Segment();
   3:         Content content = getContent();
   4:         try {
   5:             content.getChars(0,getLength(), seg);

View Full Code Here

Segment

public Segment(char[] array,
               int offset,
               int count)
Creates a new segment referring to an existing array.
Parameters:
array - the array to refer to
offset - the offset into the array
count - the number of characters
Usages and Demos :

View More Examples of Segment(char[] array,int offset,int count)
   1:         char[] chars = document.get().toCharArray();
   2:         text = new Segment(chars, 0, chars.length);
   3:     }
   4: 
   5:     protected int getNextWordStart(Segment text, int startPos) {

View Full Code Here

Method Details

clone

public Object clone()
Creates a shallow copy.
Specified by:
clone in interface CharacterIterator
Overrides:
clone in interface Object
Returns:
the copy

current

public char current()
Gets the character at the current position (as returned by getIndex()).
Specified by:
current in interface CharacterIterator
Returns:
the character at the current position or DONE if the current position is off the end of the text.
See Also:
getIndex()

first

public char first()
Sets the position to getBeginIndex() and returns the character at that position.
Specified by:
first in interface CharacterIterator
Returns:
the first character in the text, or DONE if the text is empty

getBeginIndex

public int getBeginIndex()
Returns the start index of the text.
Specified by:
getBeginIndex in interface CharacterIterator
Returns:
the index at which the text begins.

getEndIndex

public int getEndIndex()
Returns the end index of the text. This index is the index of the first character following the end of the text.
Specified by:
getEndIndex in interface CharacterIterator
Returns:
the index after the last character in the text
Usages and Demos :

View More Examples of getEndIndex()
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5:     protected int endDistance = 0;
   6:     protected Segment text;
   7:     protected BreakIterator sentenceIterator;
   8:         ...
   9:         char[] chars = document.get().toCharArray();
  10:         text = new Segment(chars, 0, chars.length);
  11:     }
  12:         ...
  13:             (selectionLength <= 0)
  14:                 ? text.getEndIndex()

View Full Code Here

getIndex

public int getIndex()
Returns the current index.
Specified by:
getIndex in interface CharacterIterator
Returns:
the current index.
Usages and Demos :

View More Examples of getIndex()
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5:     protected int endDistance = 0;
   6:     protected Segment text;
   7:     protected BreakIterator sentenceIterator;
   8:         ...
   9:         char[] chars = document.get().toCharArray();
  10:         text = new Segment(chars, 0, chars.length);
  11:     }
  12:         ...
  13:                     if (Character.isLetterOrDigit(ch) && isToBeChecked()) {
  14:                         return text.getIndex() - ignored;

View Full Code Here

isPartialReturn

public boolean isPartialReturn()
Flag to indicate that partial returns are valid.
Returns:
whether or not partial returns are valid.
Since:
1.4

last

public char last()
Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) and returns the character at that position.
Specified by:
last in interface CharacterIterator
Returns:
the last character in the text, or DONE if the text is empty
See Also:
getEndIndex()

next

public char next()
Increments the iterator's index by one and returns the character at the new index. If the resulting index is greater or equal to getEndIndex(), the current index is reset to getEndIndex() and a value of DONE is returned.
Specified by:
next in interface CharacterIterator
Returns:
the character at the new position or DONE if the new position is off the end of the text range.

previous

public char previous()
Decrements the iterator's index by one and returns the character at the new index. If the current index is getBeginIndex(), the index remains at getBeginIndex() and a value of DONE is returned.
Specified by:
previous in interface CharacterIterator
Returns:
the character at the new position or DONE if the current position is equal to getBeginIndex().
Usages and Demos :

View More Examples of previous()
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5:     protected int endDistance = 0;
   6:     protected Segment text;
   7:     protected BreakIterator sentenceIterator;
   8:         ...
   9:         char[] chars = document.get().toCharArray();
  10:         text = new Segment(chars, 0, chars.length);
  11:     }
  12:         ...
  13:                 if (ch2 == Segment.DONE || !Character.isLetterOrDigit(ch2)) {
  14:                     text.previous();

View Full Code Here

setIndex

public char setIndex(int position)
Sets the position to the specified position in the text and returns that character.
Specified by:
setIndex in interface CharacterIterator
Parameters:
position - the position within the text. Valid values range from getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown if an invalid value is supplied.
Returns:
the character at the specified position or DONE if the specified position is equal to getEndIndex()

setPartialReturn

public void setPartialReturn(boolean p)
Flag to indicate that partial returns are valid. If the flag is true, an implementation of the interface method Document.getText(position,length,Segment) should return as much text as possible without making a copy. The default state of the flag is false which will cause Document.getText(position,length,Segment) to provide the same return behavior it always had, which may or may not make a copy of the text depending upon the request.
Parameters:
p - whether or not partial returns are valid.
Since:
1.4
Usages and Demos :

View More Examples of setPartialReturn(boolean p)
   1: import javax.swing.text.Document;
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5:         int docLength = adoc.getLength();
   6:         Segment segment = new Segment();
   7:         ...
   8:         segment.setPartialReturn(true);
   9:         int start = locateWordStart(adoc, offset, docLength, segment);

View Full Code Here
   1:         javax.swing.text.Document doc = output.getDocument();
   2:         Segment seg = new Segment(new char[4096],0,4096);
   3:         ...
   4:         seg.setPartialReturn(true);
   5: 
   6:         int offset = 0;
   7:         while (offset < doc.getLength())

View Full Code Here

toString

public String toString()
Converts a segment into a String.
Overrides:
toString in interface Object
Returns:
the string
Usages and Demos :

View More Examples of toString()
   1: import javax.swing.text.html.HTML;
   2: import javax.swing.text.Segment;
   3: import javax.swing.JEditorPane;
   4:         ...
   5:             System.out.println
   6:             (    "\t"        + attributeName.toString()
   7:         ...
   8:                 + "\t-->\t"    + attributeValue.toString()
   9:                 + " ("        + attributeValue.getClass().getName() +")"
  10:         ...
  11:             if(end > start)
  12:             {    Segment segment = new Segment();

View Full Code Here
   1: 
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5:     protected int endDistance = 0;
   6:     protected Segment text;
   7:     protected BreakIterator sentenceIterator;
   8:         ...
   9:         char[] chars = document.get().toCharArray();
  10:         text = new Segment(chars, 0, chars.length);
  11:     }
  12:         ...
  13:     public String getContext() {
  14:         return text.toString();

View Full Code Here
   1: import javax.swing.text.Document;
   2: import javax.swing.text.Segment;
   3: 
   4:         ...
   5:     {
   6:         final String str = this.buffer.toString();
   7:         this.buffer.setLength(0);
   8:         ...
   9:         final int len = doc.getLength();
  10:         final Segment segment = new Segment();
  11: 
  12:         ...
  13:         {
  14:             this.history.add(segment.toString());

View Full Code Here
   1:             }
   2:             msg = buf.toString();
   3:         }
   4:         ...
   5:         int len = doc.getLength();
   6:         Segment segment = new Segment();
   7:         try {
   8:         ...
   9:         }
  10:         String text = segment.toString();
  11:         if (debugGui.dim.stringIsCompilableUnit(text)) {

View Full Code Here
   1:             }
   2:             msg = buf.toString();
   3:         }
   4:         ...
   5:         int len = doc.getLength();
   6:         Segment segment = new Segment();
   7:         try {
   8:         ...
   9:         }
  10:         String text = segment.toString();
  11:         if (db.stringIsCompilableUnit(text)) {
  12:         ...
  13:                 }
  14:                 url = sb.toString();

View Full Code Here