com.realtime.crossfire.jxclient.gui.log
Class Buffer

java.lang.Object
  extended by com.realtime.crossfire.jxclient.gui.log.Buffer

public class Buffer
extends java.lang.Object

Manages the contents of the contents of a log window. It consists of a list of Lines.


Field Summary
private  java.awt.font.FontRenderContext context
          The FontRenderContext to use.
private  Fonts fonts
          The Fonts instance for looking up fonts.
private  java.awt.Color lastColor
          The color of the previously added line of text.
private  int lastCount
          The number of repetitions of the previously added line of text.
private  java.lang.String lastText
          The contents of the previously added line of text.
private  java.util.List<Line> lines
          The lines in display order.
private  java.util.Collection<BufferListener> listeners
          The listeners to notify about changes.
static int MAX_LINES
          The maximum number of lines the buffer can hold.
static int MIN_LINE_HEIGHT
          The minimal height of a line in pixels.
private  int renderWidth
          The width to render.
private  java.lang.Object sync
          Object to synchronized access to lines and totalHeight.
private  int totalHeight
          The total height of all lines.
 
Constructor Summary
Buffer(Fonts fonts, java.awt.font.FontRenderContext context, int renderWidth)
          Create a new instance.
 
Method Summary
 void addBufferListener(BufferListener listener)
          Add a listener to notify of changes.
 void addLine(Line line)
          Append a Line to the end of the buffer.
private  int calculateHeight(Line line)
          Determine the height of a Line in pixels.
 void clear()
          Clear all lines from the buffer.
 int getLastCount()
          Returns the number of merged lines.
 Line getLine(int line)
          Return one Line by line index.
 java.lang.Object getSyncObject()
          Returns the object to synchronize on when calling iterator() or listIterator(int).
 int getTotalHeight()
          Return the total height of all lines.
 java.util.Iterator<Line> iterator()
          Return an Iterator for the lines in this buffer.
 java.util.ListIterator<Line> listIterator(int line)
          Return a ListIterator for the lines in this buffer.
 boolean mergeLines(java.lang.String text, java.awt.Color color)
          Checks whether a new text line should be merged with a preceding line.
 void prune()
          Prune excess lines.
 void removeBufferListener(BufferListener listener)
          Removes a listener to be notified of changes.
 void replaceLine(Line line)
          Replace the last Line of this buffer.
 void setRenderWidth(int renderWidth)
          Updates the width to render.
 int size()
          Return the number of lines.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

context

@NotNull
private final java.awt.font.FontRenderContext context
The FontRenderContext to use.


fonts

@NotNull
private final Fonts fonts
The Fonts instance for looking up fonts.


lastColor

@Nullable
private java.awt.Color lastColor
The color of the previously added line of text.


lastCount

private int lastCount
The number of repetitions of the previously added line of text.


lastText

@NotNull
private java.lang.String lastText
The contents of the previously added line of text.


lines

@NotNull
private final java.util.List<Line> lines
The lines in display order.


listeners

@NotNull
private final java.util.Collection<BufferListener> listeners
The listeners to notify about changes.


MAX_LINES

public static final int MAX_LINES
The maximum number of lines the buffer can hold.

See Also:
Constant Field Values

MIN_LINE_HEIGHT

public static final int MIN_LINE_HEIGHT
The minimal height of a line in pixels. Normally applies to empty lines.

See Also:
Constant Field Values

renderWidth

private int renderWidth
The width to render.


sync

@NotNull
private final java.lang.Object sync
Object to synchronized access to lines and totalHeight.


totalHeight

private int totalHeight
The total height of all lines.

Constructor Detail

Buffer

public Buffer(@NotNull
              Fonts fonts,
              @NotNull
              java.awt.font.FontRenderContext context,
              int renderWidth)
Create a new instance.

Parameters:
fonts - The Fonts instance for looking up fonts.
context - The FontRenderContext to use.
renderWidth - The width to render.
Method Detail

addBufferListener

public void addBufferListener(@NotNull
                              BufferListener listener)
Add a listener to notify of changes.

Parameters:
listener - The listener.

addLine

public void addLine(@NotNull
                    Line line)
Append a Line to the end of the buffer.

Parameters:
line - The line to append.

calculateHeight

private int calculateHeight(@NotNull
                            Line line)
Determine the height of a Line in pixels.

Parameters:
line - The line to process.
Returns:
The height in pixels.

clear

public void clear()
Clear all lines from the buffer.


getLastCount

public int getLastCount()
Returns the number of merged lines. Should not be called unless directly after mergeLines(String, Color) did freturn true.

Returns:
the number of merged lines

getLine

@NotNull
public Line getLine(int line)
Return one Line by line index. The first line has the index 0.

Parameters:
line - The line index.
Returns:
The line.

getSyncObject

@NotNull
public java.lang.Object getSyncObject()
Returns the object to synchronize on when calling iterator() or listIterator(int).

Returns:
the object

getTotalHeight

public int getTotalHeight()
Return the total height of all lines.

Returns:
The total height.

iterator

@NotNull
public java.util.Iterator<Line> iterator()
Return an Iterator for the lines in this buffer. The caller must hold sync's lock.

Returns:
the iterator

listIterator

@NotNull
public java.util.ListIterator<Line> listIterator(int line)
Return a ListIterator for the lines in this buffer.

Parameters:
line - The initial line index of the list iterator.
Returns:
The list iterator.

mergeLines

public boolean mergeLines(@NotNull
                          java.lang.String text,
                          @Nullable
                          java.awt.Color color)
Checks whether a new text line should be merged with a preceding line.

Parameters:
text - the text line contents
color - the text line color
Returns:
whether the line should be merged

prune

public void prune()
Prune excess lines.


removeBufferListener

public void removeBufferListener(@NotNull
                                 BufferListener listener)
Removes a listener to be notified of changes.

Parameters:
listener - the listener

replaceLine

public void replaceLine(@NotNull
                        Line line)
Replace the last Line of this buffer.

Parameters:
line - The replacing line.

setRenderWidth

public void setRenderWidth(int renderWidth)
Updates the width to render.

Parameters:
renderWidth - the width to render

size

public int size()
Return the number of lines.

Returns:
The number of lines.