jcurzez
Class AbstractWindow

java.lang.Object
  |
  +--jcurzez.AbstractWindow
Direct Known Subclasses:
Area, Screen

public abstract class AbstractWindow
extends java.lang.Object

AbstractWindow is the base class of Area and Screen.
An AbstractWindow is defined by:

Every AbstractWindow has a buffer that is used for all operations. One has to invoke refresh() to sync the content of the window with the buffer.

AbstractWindow is the biggest piece of Jcurzez. Bugs for sure are here.

Version:
0.0.3
Author:
Thomas Girard
See Also:
Rectangle, Cursor, Pen, Screen, Area

Field Summary
protected  Cell[][] buffer
          The buffer we use to perform all operations.
private  java.lang.Object bufferLock
          The lock on the buffer.
static org.apache.log4j.Category cat
          log4j category.
protected  Cursor cursor
          Cursor attached to this window (the one for drawing new characters).
protected  long id
          Number of this window.
protected  boolean linewrap
          Indicates if lines are wrapped.
private static long nextId
          Counter for next available window number.
protected  AbstractWindow parent
          Parent window.
protected  boolean scroll
          Would the window automatically scroll if needed ?
private static int tabSize
          Tabulation size: defaults to 8.
 
Constructor Summary
protected AbstractWindow(AbstractWindow parent, Cursor cursor, boolean linewrap, boolean scroll)
          Constructs a new window with specified parent, rectangle and cursor.
 
Method Summary
private  void _clear()
          Clears the current window.
private  void _clearToEndOfLine()
          Clears the content of the line, starting from current position, using the filling pen for new cells.
private  void _deleteChar()
          Deletes char at current position.
private  void _deleteLine()
          Deletes the current line.
private  void _insertChar(int c)
          Inserts a character at current cursor position, with the filling pen.
private  void _insertLine()
          Inserts a blank line at current position, with default pen.
private  void _printChar(int c)
          Prints specified character c at current position, using the drawing pen.
private  void backspace()
          What to do when a '\b' is printChar(int)'ed or insertChar(int)'ed.
abstract  void beep()
          Emits a beep.
private  void checkAllCoordinates(int x, int y, int width, int height)
          Checks coordinates for both copyCellsAt(int, int, int, int) and getCellsAt(int, int, int, int).
 void clear()
          Clears the current window.
 void clearToBottom()
          Clears the content of the window, from current position to the end of the screen, using the filling pen for filling new cells.
 void clearToEndOfLine()
          Clears the content of the line, starting from current position, using the filling pen for new cells.
protected  Cell[][] copyCellsAt(int x, int y, int width, int height)
          Retrieves the rectangle of cells starting at (x,y).
 void deleteChar()
          Deletes char at current position.
 void deleteLine()
          Deletes the current line.
 boolean equals(java.lang.Object o)
          Tests equality of this with o.
protected  Cell[][] getCellsAt(int x, int y, int width, int height)
          Retrieves the rectangle of cells starting at (x,y).
 Cursor getCursor()
          Returns the cursor attached to this.
 long getId()
          Returns the id of this window.
 AbstractWindow getParent()
          Returns the parent window of this.
 Rectangle getRectangle()
          Returns the rectangle contained within this.
static int getTabulationSize()
          Returns the size of the tabulation.
protected abstract  void gotoXY(int x, int y)
          Moves the cursor on the physical screen to position (x, y).
 int hashCode()
          Returns the hash code of this.
 boolean hasLinesWrapped()
          Returns true if lines are wrapped.
private  void increaseX()
          Moves the cursor one character right.
private  boolean increaseY()
          Moves the cursor one character down.
 void insertChar(int c)
          Inserts a character at current cursor position, with the filling pen.
 void insertLine()
          Inserts a blank line at current position, with default pen.
 void insertString(java.lang.String text)
          Inserts specified text text at current position, using the filling pen.
private  void internalPutChar(int c, ForegroundColor fg, BackgroundColor bg, Attribute attr)
          Puts c with given decoration at current position in buffer.
protected abstract  void printCell(int x, int y, Cell cell)
          Prints a cell on the physical screen at absolute position (x, y).
 void printChar(int c)
          Prints specified character c at current position, using the drawing pen.
 void printString(java.lang.String text)
          Prints specified String text at current position, using the drawing pen.
 void refresh()
          Redraws the window.
protected  java.lang.String removeExtraChars(java.lang.String text)
          Remove extra chars from text.
 java.lang.String toString()
          Returns a string representation of this.
protected  void touch()
          Marks all characters as changed so that the next call to refresh() will have the whole window redisplayed.
protected abstract  void update()
          Updates the physical screen.
 boolean wouldScroll()
          Returns true if the window would scroll if needed.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

nextId

private static long nextId
Counter for next available window number.


tabSize

private static final int tabSize
Tabulation size: defaults to 8.


id

protected final long id
Number of this window.


parent

protected final AbstractWindow parent
Parent window.


cursor

protected Cursor cursor
Cursor attached to this window (the one for drawing new characters).


linewrap

protected final boolean linewrap
Indicates if lines are wrapped. This can not be altered after creation.


scroll

protected final boolean scroll
Would the window automatically scroll if needed ?


buffer

protected final Cell[][] buffer
The buffer we use to perform all operations. Use refresh() to sync the content of the window with this buffer.


bufferLock

private java.lang.Object bufferLock
The lock on the buffer.


cat

public static org.apache.log4j.Category cat
log4j category.

Constructor Detail

AbstractWindow

protected AbstractWindow(AbstractWindow parent,
                         Cursor cursor,
                         boolean linewrap,
                         boolean scroll)
Constructs a new window with specified parent, rectangle and cursor.

Parameters:
parent - the parent window.
cursor - the cursor attached to this window.
linewrap - if lines will be wrapped.
scroll - if the window should scroll.
tabSize - size of the tabulation.
Throws:
InconsistentCoordinateException - if the window being created is not contained in its parent.
Method Detail

getParent

public AbstractWindow getParent()
Returns the parent window of this. null is the parent of the screen.

Returns:
the parent window of this.

getRectangle

public Rectangle getRectangle()
Returns the rectangle contained within this.

Returns:
the rectangle contained within this.

getCursor

public Cursor getCursor()
Returns the cursor attached to this.

Returns:
the cursor of this.

getTabulationSize

public static int getTabulationSize()
Returns the size of the tabulation.

Returns:
tabSize.

getId

public long getId()
Returns the id of this window.

Returns:
id.

hasLinesWrapped

public boolean hasLinesWrapped()
Returns true if lines are wrapped.

Returns:
true if lines are wrapped.

wouldScroll

public boolean wouldScroll()
Returns true if the window would scroll if needed.

Returns:
true if the window has the automatic scrolling feature.

insertChar

public void insertChar(int c)
Inserts a character at current cursor position, with the filling pen.

Parameters:
c - the character to insert.

_insertChar

private void _insertChar(int c)
Inserts a character at current cursor position, with the filling pen. This is the unsynchronized version.

Parameters:
c - the character to insert.

backspace

private void backspace()
What to do when a '\b' is printChar(int)'ed or insertChar(int)'ed.


deleteChar

public void deleteChar()
Deletes char at current position.


_deleteChar

private void _deleteChar()
Deletes char at current position. This is the unsynchronized version.


insertLine

public void insertLine()
Inserts a blank line at current position, with default pen.


_insertLine

private void _insertLine()
Inserts a blank line at current position, with default pen. This is the unsynchronized version.


deleteLine

public void deleteLine()
Deletes the current line.


_deleteLine

private void _deleteLine()
Deletes the current line. This is the unsynchronized version.


clear

public void clear()
Clears the current window.


_clear

private void _clear()
Clears the current window. This is the non synchronizd version.


beep

public abstract void beep()
Emits a beep. This method will be provided by an implementation of ScreenPeer.

See Also:
ScreenPeer.beep()

printCell

protected abstract void printCell(int x,
                                  int y,
                                  Cell cell)
Prints a cell on the physical screen at absolute position (x, y). This method will be provided by an implementation of ScreenPeer.

Parameters:
x - the x coordinate where to print the cell.
y - the y coordinate where to print the cell.
cell - the cell to print.
See Also:
ScreenPeer.printCell(int, int, jcurzez.Cell)

gotoXY

protected abstract void gotoXY(int x,
                               int y)
Moves the cursor on the physical screen to position (x, y). This method will be provided by an implementation of ScreenPeer.

Parameters:
x - the x coordinate where to move the cursor.
y - the y coordinate where to move the cursor.
See Also:
ScreenPeer.gotoXY(int, int)

update

protected abstract void update()
Updates the physical screen. This method will be provided by an implementation of ScreenPeer.

See Also:
ScreenPeer.update()

clearToBottom

public void clearToBottom()
Clears the content of the window, from current position to the end of the screen, using the filling pen for filling new cells.


clearToEndOfLine

public void clearToEndOfLine()
Clears the content of the line, starting from current position, using the filling pen for new cells.


_clearToEndOfLine

private void _clearToEndOfLine()
Clears the content of the line, starting from current position, using the filling pen for new cells. This is the non synchronized version.


insertString

public void insertString(java.lang.String text)
Inserts specified text text at current position, using the filling pen.

Parameters:
text - the text to insert.

internalPutChar

private final void internalPutChar(int c,
                                   ForegroundColor fg,
                                   BackgroundColor bg,
                                   Attribute attr)
Puts c with given decoration at current position in buffer.

Parameters:
c - the nwe character for the cell.
fg - the new foreground color for the cell.
bg - the new background color for the cell.
attr - the new attrivute for the cell.

printChar

public void printChar(int c)
Prints specified character c at current position, using the drawing pen.

Parameters:
c - the character to print.

_printChar

private void _printChar(int c)
Prints specified character c at current position, using the drawing pen. This is the unsynchronized version.

Parameters:
c - the character to print.

printString

public void printString(java.lang.String text)
Prints specified String text at current position, using the drawing pen.

Parameters:
text - the text to print.

refresh

public void refresh()
Redraws the window.


removeExtraChars

protected java.lang.String removeExtraChars(java.lang.String text)
Remove extra chars from text. When linewrap is set to false, some chars won't appear, so they are removed.

Parameters:
text - the text to modify.
Returns:
modified text.

increaseY

private boolean increaseY()
Moves the cursor one character down. If needed, and scroll is true, then we insert a new line. Otherwise, the cursor will remain at the same position, and we return false.

Returns:
true if the y position could be changed.

increaseX

private void increaseX()
Moves the cursor one character right. Updates the y coordinate if x reach the right bound. If this is not possible, then the cursor is left where it is.


equals

public boolean equals(java.lang.Object o)
Tests equality of this with o. Two AbstractWindow are said to be equal if they have the same id.

Overrides:
equals in class java.lang.Object
Parameters:
o - the object we want to test for equality with this.
Returns:
true if this and o have the same id.

hashCode

public int hashCode()
Returns the hash code of this. It is computed using its id.

Overrides:
hashCode in class java.lang.Object
Returns:
the hash code to use for this.

toString

public java.lang.String toString()
Returns a string representation of this. It has the form [ x,y p1 p2 ] where p1 is the string representation of the drawing pen and p2 the string representation of the filling pen.

Overrides:
toString in class java.lang.Object
Returns:
the string representation of this.

touch

protected void touch()
Marks all characters as changed so that the next call to refresh() will have the whole window redisplayed.


checkAllCoordinates

private void checkAllCoordinates(int x,
                                 int y,
                                 int width,
                                 int height)
Checks coordinates for both copyCellsAt(int, int, int, int) and getCellsAt(int, int, int, int).


copyCellsAt

protected Cell[][] copyCellsAt(int x,
                               int y,
                               int width,
                               int height)
Retrieves the rectangle of cells starting at (x,y). The retrieved array has a width of width and a height of height. The cells are copied. The first subscript of the returned array is the y coordinate.

Parameters:
x - the x coordinate from where to retrieve cells.
y - the y coordinate from where to retrieve cells.
width - the width of the array to get.
height - the height of the array to get.
Throws:
InconsistentCoordinateException - if either x or y is not consistent.
java.lang.ArrayIndexOutOfBoundsException - if width or height is not consistent.

getCellsAt

protected Cell[][] getCellsAt(int x,
                              int y,
                              int width,
                              int height)
Retrieves the rectangle of cells starting at (x,y). The retrieved array has a width of width and a height of height. The cells are referenced. The first subscript of the returned array is the y coordinate.

Parameters:
x - the x coordinate from where to retrieve cells.
y - the y coordinate from where to retrieve cells.
width - the width of the array to get.
height - the height of the array to get.
Throws:
InconsistentCoordinateException - if either x or y is not consistent.
java.lang.ArrayIndexOutOfBoundsException - if width or height is not consistent.


Send me an email