miSim

misim.editor
Class Token

java.lang.Object
  |
  +--misim.editor.Token
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
TokenAlias, TokenColon, TokenComma, TokenCommand, TokenComment, TokenInstruction, TokenListing, TokenNumber, TokenOperator, TokenString, TokenWhitespace

public class Token
extends java.lang.Object
implements java.lang.Cloneable

A Token represents a section of text in a file. The token class may be subclassed to define particular behaviour of the token - such as it's visual characteristics, or the way in which the token is parsed. Tokens are immutable - that is, the text value of the token cannot be altered once set. This is relied upon for undo and redo operations in the EditorPanel. Tokens support cloning which is used by the Parser to create new instances of parsed tokens.


Field Summary
protected  char[] display
           
protected  java.lang.String text
          The text value of this token
static int TYPE_ALIAS
           
static int TYPE_COLON
           
static int TYPE_COMMA
           
static int TYPE_COMMAND
           
static int TYPE_COMMENT
           
static int TYPE_INSTRUCTION
           
static int TYPE_LISTING
           
static int TYPE_NUMBER
           
static int TYPE_OPERATOR
           
static int TYPE_STRING
           
static int TYPE_TOKEN
          Constants for the token types
static int TYPE_WHITESPACE
           
 
Constructor Summary
Token()
           
Token(java.lang.String text, EditorPrefs prefs)
           
 
Method Summary
 Token clone(java.lang.String source, int index)
           
static Token consume(Token aToken, int start)
          Parsing method - to be implemented by subclasses.
 int draw(java.awt.Graphics g, int x, int y, int startPos)
           
 int drawChars(java.awt.Graphics g, char[] charArray, int x, int y, int startPos)
           
 java.awt.Color getColour()
          Return the text colour for this token
 int getColumn(int position, int startPos)
          Find the character column for the given position.
 int getLength()
          Return the length of the token
 int getPixelPos(int column, int startPos)
          Return the pixel position of the given character
 EditorPrefs getPrefs()
          Return the preferences object for this token
 int getType()
          Return the type of this token
 java.lang.String getValue()
          Return the value of the token
 void init(java.lang.String text, EditorPrefs prefs, int tokenType, boolean uppercase)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TYPE_TOKEN

public static final int TYPE_TOKEN
Constants for the token types

TYPE_WHITESPACE

public static final int TYPE_WHITESPACE

TYPE_COMMA

public static final int TYPE_COMMA

TYPE_COMMENT

public static final int TYPE_COMMENT

TYPE_COMMAND

public static final int TYPE_COMMAND

TYPE_INSTRUCTION

public static final int TYPE_INSTRUCTION

TYPE_ALIAS

public static final int TYPE_ALIAS

TYPE_NUMBER

public static final int TYPE_NUMBER

TYPE_OPERATOR

public static final int TYPE_OPERATOR

TYPE_STRING

public static final int TYPE_STRING

TYPE_LISTING

public static final int TYPE_LISTING

TYPE_COLON

public static final int TYPE_COLON

text

protected java.lang.String text
The text value of this token

display

protected char[] display
Constructor Detail

Token

public Token()

Token

public Token(java.lang.String text,
             EditorPrefs prefs)
Method Detail

init

public void init(java.lang.String text,
                 EditorPrefs prefs,
                 int tokenType,
                 boolean uppercase)

consume

public static Token consume(Token aToken,
                            int start)
Parsing method - to be implemented by subclasses. Build a new token based on as much of the given token as possible, starting at the given index. The method may return null if the text is not appropriate for this token type.
Parameters:
aToken - The token to consume
start - The start index
Returns:
A new token, or null

getPrefs

public EditorPrefs getPrefs()
Return the preferences object for this token
Returns:
The EditorPrefs object for this token

getValue

public java.lang.String getValue()
Return the value of the token
Returns:
The text that this token represents

getColour

public java.awt.Color getColour()
Return the text colour for this token
Returns:
The forground colour for drawing this token

getLength

public int getLength()
Return the length of the token
Returns:
The length (number of characters) in this token

getType

public int getType()
Return the type of this token

draw

public int draw(java.awt.Graphics g,
                int x,
                int y,
                int startPos)

drawChars

public int drawChars(java.awt.Graphics g,
                     char[] charArray,
                     int x,
                     int y,
                     int startPos)

getPixelPos

public int getPixelPos(int column,
                       int startPos)
Return the pixel position of the given character
Parameters:
column - The character index
Returns:
The pixel offset for that column

getColumn

public int getColumn(int position,
                     int startPos)
Find the character column for the given position. If a column can be found, it is returned as a positive number. If not, the startPosition for the following token (ie. the end of this token) is returned as a negative number.
Parameters:
position - The pixel position to find
startPos - The pixel position of the start of the token
Returns:
The column for this character (positive) or the next startPos (negative)

clone

public Token clone(java.lang.String source,
                   int index)
            throws java.lang.CloneNotSupportedException

miSim