 |
Crossfire JXClient, Trunk
|
Go to the documentation of this file.
23 package com.realtime.crossfire.jxclient.gui.log;
26 import java.awt.Color;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Objects;
32 import java.util.concurrent.CopyOnWriteArrayList;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
63 private final List<Line>
lines =
new CopyOnWriteArrayList<>();
75 private final Object
sync =
new Object();
121 throw new IllegalArgumentException(
"maxLines="+
maxLines);
133 synchronized (
sync) {
148 synchronized (
sync) {
163 synchronized (
sync) {
177 final List<Line> removedLines;
178 synchronized (
sync) {
179 removedLines =
new ArrayList<>(
lines);
183 if (!removedLines.isEmpty()) {
185 listener.linesRemoved(removedLines);
200 synchronized (
sync) {
201 final int position =
lines.isEmpty() ? 0 :
lines.get(
lines.size()-1).getEndPosition();
207 listener.lineAdded();
216 synchronized (
sync) {
217 final int position =
lines.size() < 2 ? 0 :
lines.get(
lines.size()-2).getEndPosition();
219 final int lastIndex =
lines.size()-1;
221 lines.set(lastIndex, line);
225 listener.lineReplaced();
233 final List<Line> removedLines;
234 synchronized (
sync) {
240 int positionsRemoved = 0;
243 removedLines.add(line);
248 line.updatePosition(positionsRemoved);
251 if (!removedLines.isEmpty()) {
253 listener.linesRemoved(removedLines);
266 synchronized (
sync) {
267 return lines.get(line);
276 synchronized (
sync) {
287 public Iterable<Line>
lines(
final int line) {
288 assert Thread.holdsLock(
sync);
290 return Collections.unmodifiableList(
lines).subList(line,
lines.size());
291 }
catch (
final IndexOutOfBoundsException ignored) {
292 return Collections.emptyList();
301 synchronized (
sync) {
339 public boolean mergeLines(@NotNull
final String text,
final int type,
final int subtype, @Nullable
final Color color) {
368 synchronized (
sync) {
373 position = line.getEndPosition();
389 public void addTextSegment(@NotNull
final Line line, @NotNull
final String text,
final boolean bold,
final boolean italic,
final boolean underline, @NotNull
final FontID fontID, @NotNull
final Color color, @NotNull
final Color selectionColor) {
390 line.addTextSegment(
newTextSegment(text, bold, italic, underline, fontID, color, selectionColor));
405 protected abstract TextSegment newTextSegment(@NotNull
final String text,
final boolean bold,
final boolean italic,
final boolean underline, @NotNull
final FontID fontID, @NotNull
final Color color, @NotNull
final Color selectionColor);
416 for (
final TextSegment segment : line.segments()) {
417 if (y <= yLine+segment.getY()) {
418 if (y <= yLine+segment.getY()-segment.getHeight()) {
419 return segment.getPosition();
421 if (x <= segment.getX()+segment.getWidth()) {
422 final int length = segment.getText().length();
423 for (
int i = 0; i < length; i++) {
425 if (x < segment.getX()+width) {
426 return segment.getPosition()+i;
429 return segment.getPosition()+length;
434 yLine += line.getHeight();
436 return lines.isEmpty() ? 0 :
lines.get(
lines.size()-1).getEndPosition();
454 public String
getText(
final int beginSelection,
final int endSelection) {
455 if (beginSelection >= endSelection) {
460 synchronized (
sync) {
464 while (i <
lines.size()) {
471 final StringBuilder sb =
new StringBuilder();
472 while (i <
lines.size()) {
485 return sb.toString();
Line getLine(final int line)
Returns one Line by line index.
Interface for listeners for changes of Buffer contents.
int lastType
The message type of the previously added line of text.
void layoutLines()
Recalculates layout information in lines.
int lastSubtype
The message subtype of the previously added line of text.
String lastText
The contents of the previously added line of text.
int getHeight()
Returns the height of this line.
void addBufferListener(@NotNull final BufferListener listener)
Adds a listener to notify of changes.
Color lastColor
The color of the previously added line of text.
abstract int getSegmentWidth(@NotNull final TextSegment segment, final int characters)
Returns the width of a prefix of a TextSegment.
final Object sync
Object to synchronized access to lines and totalHeight.
String getText(final int beginSelection, final int endSelection)
Returns the selected text.
void setShowSentCommands(final boolean showSentCommands)
Sets whether commands sent to the server are shown.
int getEndPosition()
Returns the absolute buffer position of the first character after this line.
final List< Line > lines
The lines in display order.
boolean mergeLines(@NotNull final String text, final int type, final int subtype, @Nullable final Color color)
Checks whether a new text line should be merged with a preceding line.
A list of event listeners.
Object getSyncObject()
Returns the object to synchronize on when calling lines(int).
abstract TextSegment newTextSegment(@NotNull final String text, final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID fontID, @NotNull final Color color, @NotNull final Color selectionColor)
Creates a new segment.
One segment of a Line which should be displayed without changing attributes.
Manages the contents of one text line.
void addTextSegment(@NotNull final Line line, @NotNull final String text, final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID fontID, @NotNull final Color color, @NotNull final Color selectionColor)
Appends a TextSegment to the end of a line.
void addLine(@NotNull final Line line)
Appends a Line to the end of the buffer.
Iterable< Line > lines(final int line)
Returns an Iterator for the lines in this buffer.
Manages the contents of the contents of a log window.
void removeBufferListener(@NotNull final BufferListener listener)
Removes a listener to be notified of changes.
int renderWidth
The width to render.
int coordinateToPosition(final int x, final int y)
Returns the text position for a coordinate.
boolean isVisible(final boolean showSentCommands)
Returns whether this line is visible.
int lastCount
The number of repetitions of the previously added line of text.
void setRenderWidth(final int renderWidth)
Updates the width to render.
int getLastCount()
Returns the number of merged lines.
void setShowTimestamps(final boolean showTimestamps)
Sets whether timestamps are shown.
final int maxLines
The maximum number of lines the buffer can hold.
Buffer(final int maxLines, final int renderWidth)
Creates a new instance.
void prune()
Prunes excess lines.
int getTotalHeight()
Returns the total height of all lines.
final EventListenerList2< BufferListener > listeners
The listeners to notify about changes.
void clear()
Clears all lines from the buffer.
int getStartPosition()
Returns the absolute buffer position of the first character of this line.
int size()
Returns the number of lines.
void appendSelection(@NotNull final StringBuilder sb, final int beginSelection, final int endSelection)
Appends the selected text to a StringBuilder instance.
void replaceLine(@NotNull final Line line)
Replaces the last Line of this buffer.
boolean showSentCommands
Whether commands sent to the server are shown.
boolean showTimestamps
Whether timestamps are shown.
int totalHeight
The total height of all lines.