miSim

misim.editor
Class Line

java.lang.Object
  |
  +--misim.editor.Line
All Implemented Interfaces:
TokenIterator, UndoIF
Direct Known Subclasses:
AsmLine

public class Line
extends java.lang.Object
implements UndoIF, TokenIterator

The Line class represents a single line of a file. It may contain zero or more tokens, and may also refer to an included file which itself may contain zero or more lines.

Note that the tokens represent the text part of the file.

The tokens are stored as a vector, which is immutable - all edit functions remove the existing vector and replace it with an appropriate new one. This appears heavy handed, but allows undo events to be stored simply by storing the token vector from the line being altered.

Line class objects are created by a class which implements the LineFactory interface.

Line class may be extended to support information generated during assembly or compilation. In this case, a new LineFactory must be implemented, and passed to the editor to create appropriate instances of the Line subclass as files are edited.


Field Summary
protected  int tokenIndex
          The index for the TokenIterator interface
protected  java.util.Vector tokens
          The vector of tokens that this line contains
 
Constructor Summary
Line()
          Constructor, creates a new line with no tokens
Line(java.util.Vector tokens)
          Constructor, creates a new line with the given vector of tokens
 
Method Summary
 void addError(EditorException editorException)
          Add an error to this line, that can be displayed by the editor
 EditorException addError(Token aToken, int errNo)
          Add an error to this line, that can be displayed by the editor
 void addWarning(Token aToken, int errNo)
          Add an warning to this line, that can be displayed by the editor
 void clearErrors()
          Remove all errors and warnings from this line.
 int countTokens()
           
 boolean delete(int start, int length, Line endLine, Parser parser, EditorPrefs prefs)
          Delete a range of characters starting on this line.
 void draw(java.awt.Graphics g, int x, int y, int lineHeight, int lineWidth, int leftMargin, boolean showErrors)
          Draw this line
 int errorCount()
          Return the number of errors for this line
 int getColumn(int pixelPos)
          Return the character column for the given pixel position.
 EditorException getError(int index)
          Return the given error for this line, or null
 Token getLastToken()
          Return the last token before this one, or null if at the start of the line.
 int getLength()
          Return the length of the line in characters
 Token getNextToken()
          Return this token, and advance the pointer to the next token.
 int getPixelPos(int column)
          Return the pixel position of the given text column.
 Token getToken(int index)
           
 java.util.Vector getTokenVector()
           
 java.lang.String getValue()
          Return the line as a text string
 EditorException getWarning(int index)
          Return the given warning for this line, or null
 int getWidth()
           
 Line insert(CursorPos cursor, char aChar, Parser parser, EditorPrefs prefs, LineFactory factory)
          Insert a character into the current line at a given column.
 java.util.Vector insert(CursorPos cursor, java.lang.String newText, Parser parser, EditorPrefs prefs, LineFactory factory)
          Insert a string into the current line at a given column.
 void previousToken()
          move to the previous token.
 void redo(Undoable undo, CursorPos cursor)
           
 void resetTokenIndex()
          Reset the token iterator index
 void undo(Undoable undo, CursorPos cursor)
           
 int warningCount()
          Return the number of warnings for this line
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tokens

protected java.util.Vector tokens
The vector of tokens that this line contains

tokenIndex

protected int tokenIndex
The index for the TokenIterator interface
Constructor Detail

Line

public Line()
Constructor, creates a new line with no tokens

Line

public Line(java.util.Vector tokens)
Constructor, creates a new line with the given vector of tokens
Parameters:
tokens - The vector of tokens for this line
Method Detail

getToken

public Token getToken(int index)

countTokens

public int countTokens()

getTokenVector

public java.util.Vector getTokenVector()

resetTokenIndex

public void resetTokenIndex()
Reset the token iterator index
Specified by:
resetTokenIndex in interface TokenIterator

getNextToken

public Token getNextToken()
Return this token, and advance the pointer to the next token. If the pointer is beyond the end of the list of tokens, return null.
Specified by:
getNextToken in interface TokenIterator
Returns:
The next token, or null if there are no more tokens

getLastToken

public Token getLastToken()
Return the last token before this one, or null if at the start of the line.
Specified by:
getLastToken in interface TokenIterator
Returns:
The last token returned by getNextToken()

previousToken

public void previousToken()
                   throws EditorException
move to the previous token. If the pointer is at the start of the file, throw an exception.
Specified by:
previousToken in interface TokenIterator

getValue

public java.lang.String getValue()
Return the line as a text string
Returns:
The text for this line

getLength

public int getLength()
Return the length of the line in characters
Returns:
The length of the line

getWidth

public int getWidth()

draw

public void draw(java.awt.Graphics g,
                 int x,
                 int y,
                 int lineHeight,
                 int lineWidth,
                 int leftMargin,
                 boolean showErrors)
Draw this line
Parameters:
g - The graphics context to draw to
x - The x co-ordinate of the line
y - The y co-ordinate of the line
lineHeight - The height of a displayed line
lineWidth - The width of a displayed line
leftMargin - The left margin for the line
showErrors - If true, indicate errors for this line

getPixelPos

public int getPixelPos(int column)
Return the pixel position of the given text column. This is largely used for cursor and selection display
Parameters:
column - The character position that we want the position of
Returns:
The pixel offset for that column

getColumn

public int getColumn(int pixelPos)
Return the character column for the given pixel position. This is the reverse of getPixelPos.
Parameters:
pixelPos - The pix position that we are concerned with
Returns:
The nearest character column for this position

addError

public EditorException addError(Token aToken,
                                int errNo)
Add an error to this line, that can be displayed by the editor
Parameters:
aToken - The token that caused the error, or null
errNo - The message error identifier
Returns:
The editor exception added to this line

addError

public void addError(EditorException editorException)
Add an error to this line, that can be displayed by the editor
Parameters:
editorException - The exception that caused the error
errNo - The message error identifier

clearErrors

public void clearErrors()
Remove all errors and warnings from this line.

errorCount

public int errorCount()
Return the number of errors for this line
Returns:
The number of errors

getError

public EditorException getError(int index)
Return the given error for this line, or null
Parameters:
index - The index for the error
Returns:
The EditorException at the given index, or null

addWarning

public void addWarning(Token aToken,
                       int errNo)
Add an warning to this line, that can be displayed by the editor
Parameters:
aToken - The token that caused the error, or null
errNo - The message error identifier

warningCount

public int warningCount()
Return the number of warnings for this line
Returns:
The number of warnings

getWarning

public EditorException getWarning(int index)
Return the given warning for this line, or null
Parameters:
index - The index for the warning
Returns:
The EditorException at the given index, or null

undo

public void undo(Undoable undo,
                 CursorPos cursor)
Specified by:
undo in interface UndoIF

redo

public void redo(Undoable undo,
                 CursorPos cursor)
Specified by:
redo in interface UndoIF

insert

public java.util.Vector insert(CursorPos cursor,
                               java.lang.String newText,
                               Parser parser,
                               EditorPrefs prefs,
                               LineFactory factory)
Insert a string into the current line at a given column. If the string contains newline characters, the line is split as appropriate, and the method returns a Vector containing the additional lines created. Otherwise, it returns null. This is a 'lazy' implementation - it builds a new string from the tokens in the line, and then re-parses the whole line into tokens.
Parameters:
col - The column to insert the text at
newText - The text to insert.
parser - The current parser
prefs - The Editor prefs object
factory - The LineFactory class to create additional lines
Returns:
A Vector of additional lines, or null.

insert

public Line insert(CursorPos cursor,
                   char aChar,
                   Parser parser,
                   EditorPrefs prefs,
                   LineFactory factory)
Insert a character into the current line at a given column. If the character is a newline character, the line is split as appropriate, and the method returns the additional Line created to contain the split. Otherwise, it returns null. This is a 'lazy' implementation - it builds a new string from the tokens in the line, and then re-parses the whole line into tokens.
Parameters:
col - The column to insert the text at
aChar - The character to insert.
parser - The current parser
prefs - The Editor prefs object
factory - The LineFactory class to create additional lines
Returns:
The second line if this one was split, or null.

delete

public boolean delete(int start,
                      int length,
                      Line endLine,
                      Parser parser,
                      EditorPrefs prefs)
Delete a range of characters starting on this line. If the range extends beyond this line, the last line that contains the range should be specified or otherwise left null.
Parameters:
start - The start column
length - The number of characters in the start and end lines to delete
endLine - The last line in the deletion range, or null
parser - The current parser
prefs - The Editor prefs object
Returns:
True if the endLine is joined to this one, and should be deleted..

miSim