123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.ui.messages;
-
- import com.dmdirc.DMDircMBassador;
- import com.dmdirc.events.AppErrorEvent;
- import com.dmdirc.events.DisplayPropertyMap;
- import com.dmdirc.interfaces.config.AggregateConfigProvider;
- import com.dmdirc.interfaces.config.ConfigChangeListener;
- import com.dmdirc.logger.ErrorLevel;
- import com.dmdirc.util.collections.ListenerList;
- import com.dmdirc.util.collections.RollingList;
-
- import java.awt.Font;
- import java.io.Serializable;
- import java.text.AttributedCharacterIterator;
- import java.text.AttributedString;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- import javax.swing.UIManager;
- import javax.swing.text.BadLocationException;
-
- /**
- * Data contained in a TextPane.
- */
- public class IRCDocument implements Serializable, ConfigChangeListener {
-
- /** A version number for this class. */
- private static final long serialVersionUID = 4;
- /** List of lines of text. */
- private final List<Line> lines;
- /** Listener list. */
- private final ListenerList listeners;
- /** Cached lines. */
- private final RollingList<Line> cachedLines;
- /** Cached attributed strings. */
- private final RollingList<AttributedString> cachedStrings;
- /** Config Manager for getting settings. */
- private final AggregateConfigProvider configManager;
- /** This document's styliser. */
- private final Styliser styliser;
- /** The event bus to post errors to. */
- private final DMDircMBassador eventBus;
- /** Font size. */
- private int fontSize;
- /** Font name. */
- private String fontName;
- /** Frame buffer size. */
- private Integer frameBufferSize;
-
- public IRCDocument(final AggregateConfigProvider configManager,
- final Styliser styliser, final DMDircMBassador eventBus) {
- this.configManager = configManager;
- this.styliser = styliser;
- this.eventBus = eventBus;
-
- lines = new ArrayList<>();
- listeners = new ListenerList();
-
- cachedLines = new RollingList<>(50);
- cachedStrings = new RollingList<>(50);
- frameBufferSize = configManager.getOptionInt("ui", "frameBufferSize", false);
-
- configManager.addChangeListener("ui", "textPaneFontSize", this);
- configManager.addChangeListener("ui", "textPaneFontName", this);
- configManager.addChangeListener("ui", "frameBufferSize", this);
-
- setCachedSettings();
- }
-
- /**
- * Returns the number of lines in this document.
- *
- * @return Number of lines
- */
- public int getNumLines() {
- synchronized (lines) {
- return lines.size();
- }
- }
-
- /**
- * Returns the Line at the specified number.
- *
- * @param lineNumber Line number to retrieve
- *
- * @return Line at the specified number or null
- */
- public Line getLine(final int lineNumber) {
- synchronized (lines) {
- return lines.get(lineNumber);
- }
- }
-
- /**
- * Adds the stylised string to the canvas.
- *
- * @param timestamp The timestamp to show along with the text.
- * @param displayPropertyMap The display properties to use
- * @param text stylised string to add to the document
- */
- public void addText(final long timestamp, final DisplayPropertyMap displayPropertyMap,
- final String text) {
- final int start;
- synchronized (lines) {
- start = lines.size();
- lines.add(new Line(styliser, formatTimestamp(timestamp), text, displayPropertyMap,
- fontSize, fontName));
- }
- fireLinesAdded(start, 1);
- }
-
- private String formatTimestamp(final long timestamp) {
- return Formatter.formatMessage(configManager, "timestamp", new Date(timestamp));
- }
-
- /**
- * Trims the document to the specified number of lines.
- *
- * @param numLines Number of lines to trim the document to
- */
- public void trim(final int numLines) {
- synchronized (lines) {
- if (frameBufferSize != null && frameBufferSize > 0) {
- final int i = lines.size() - numLines;
- if (i > 0) {
- lines.subList(0, i).clear();
- fireTrimmed(numLines, i);
- }
- }
- }
- }
-
- /** Clears all lines from the document. */
- public void clear() {
- synchronized (lines) {
- lines.clear();
- }
- fireCleared();
- }
-
- /**
- * Adds a IRCDocumentListener to the listener list.
- *
- * @param listener Listener to add
- */
- public void addIRCDocumentListener(final IRCDocumentListener listener) {
- if (listener == null) {
- return;
- }
-
- listeners.add(IRCDocumentListener.class, listener);
- }
-
- /**
- * Removes a IRCDocumentListener from the listener list.
- *
- * @param listener Listener to remove
- */
- public void removeIRCDocumentListener(final IRCDocumentListener listener) {
- listeners.remove(IRCDocumentListener.class, listener);
- }
-
- /**
- * Fires the lines added method on all listeners.
- *
- * @param index Index of the added line
- * @param size Number of lines added
- */
- protected void fireLinesAdded(final int index, final int size) {
- for (IRCDocumentListener listener
- : listeners.get(IRCDocumentListener.class)) {
- listener.linesAdded(index, size, lines.size());
- }
- trim(frameBufferSize);
- }
-
- /**
- * Fires the trimmed method on all listeners.
- *
- * @param newSize New document size
- * @param trimmedLines Number of trimmed lines
- */
- protected void fireTrimmed(final int newSize, final int trimmedLines) {
- for (IRCDocumentListener listener
- : listeners.get(IRCDocumentListener.class)) {
- listener.trimmed(newSize, trimmedLines);
- }
- }
-
- /**
- * fires the cleared method on all listeners.
- */
- protected void fireCleared() {
- listeners.get(IRCDocumentListener.class).forEach(IRCDocumentListener::cleared);
- }
-
- /**
- * fires the need repaint method on all listeners.
- */
- protected void fireRepaintNeeded() {
- listeners.get(IRCDocumentListener.class).forEach(IRCDocumentListener::repaintNeeded);
- }
-
- /**
- * Returns an attributed character iterator for a particular line, utilising the document cache
- * where possible.
- *
- * @param line Line to be styled
- *
- * @return Styled line
- */
- protected AttributedCharacterIterator getStyledLine(final Line line) {
- synchronized (lines) {
- AttributedString styledLine = null;
- if (cachedLines.contains(line)) {
- final int index = cachedLines.getList().indexOf(line);
- styledLine = cachedStrings.get(index);
- }
-
- if (styledLine == null) {
- try {
- styledLine = line.getStyled();
- } catch (BadLocationException ex) {
- eventBus.publish(new AppErrorEvent(ErrorLevel.MEDIUM, ex,
- "Unable to add line: " + ex.getMessage(), ""));
- }
- cachedLines.add(line);
- cachedStrings.add(styledLine);
- }
- // TODO: Consider what to do if this is null from the BLE above
- return styledLine.getIterator();
- }
- }
-
- /**
- * Returns an attributed string for a particular line, utilising the document cache where
- * possible.
- *
- * @param line Line number to be styled
- *
- * @return Styled line
- */
- public AttributedCharacterIterator getStyledLine(final int line) {
- return getStyledLine(getLine(line));
- }
-
- /**
- * Returns the line height of the specified line.
- *
- * @param line Line
- *
- * @return Line height
- */
- protected int getLineHeight(final Line line) {
- return line.getFontSize();
- }
-
- /**
- * Returns the line height of the specified.
- *
- * @param line Line
- *
- * @return Line height
- */
- public int getLineHeight(final int line) {
- return getLineHeight(getLine(line));
- }
-
- /**
- * Sets all the cached settings in this document.
- */
- private void setCachedSettings() {
- final Font defaultFont = UIManager.getFont("TextPane.font");
- if (configManager.hasOptionString("ui", "textPaneFontName")) {
- fontName = configManager.getOption("ui", "textPaneFontName");
- } else {
- fontName = defaultFont.getName();
- }
- if (configManager.hasOptionString("ui", "textPaneFontSize")) {
- fontSize = configManager.getOptionInt("ui", "textPaneFontSize");
- } else {
- fontSize = defaultFont.getSize();
- }
- frameBufferSize = configManager.getOptionInt("ui", "frameBufferSize", true);
- trim(frameBufferSize);
- }
-
- @Override
- public void configChanged(final String domain, final String key) {
- setCachedSettings();
- cachedLines.clear();
- cachedStrings.clear();
- synchronized (lines) {
- for (Line line : lines) {
- line.setFontName(fontName);
- line.setFontSize(fontSize);
- }
- }
- fireRepaintNeeded();
- }
- }
|