123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- /*
- * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * 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.addons.ui_swing.textpane;
-
- import com.dmdirc.config.ConfigManager;
- import com.dmdirc.interfaces.ConfigChangeListener;
- import com.dmdirc.util.RollingList;
- import java.awt.Font;
-
- import java.io.Serializable;
- import java.text.AttributedCharacterIterator;
- import java.text.AttributedString;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.UIManager;
-
- import javax.swing.event.EventListenerList;
-
- /**
- * Data contained in a TextPane.
- */
- public final class IRCDocument implements Serializable, ConfigChangeListener {
-
- /**
- * A version number for this class. It should be changed whenever the class
- * structure is changed (or anything else that would prevent serialized
- * objects being unserialized with the new class).
- */
- private static final long serialVersionUID = 4;
- /** List of lines of text. */
- private final List<Line> lines;
- /** Listener list. */
- private final EventListenerList listeners;
- /** Cached lines. */
- private RollingList<Line> cachedLines;
- /** Cached attributed strings. */
- private RollingList<AttributedString> cachedStrings;
- /** Configuration manager. */
- private ConfigManager config;
- /** Font size. */
- private int fontSize;
- /** Font name. */
- private String fontName;
-
- /**
- * Creates a new instance of IRCDocument.
- *
- * @param config Document's config manager
- */
- public IRCDocument(final ConfigManager config) {
- this.config = config;
- lines = new ArrayList<Line>();
- listeners = new EventListenerList();
-
- cachedLines = new RollingList<Line>(50);
- cachedStrings = new RollingList<AttributedString>(50);
-
- setCachedSettings();
-
- config.addChangeListener("ui", "textPaneFontSize", this);
- config.addChangeListener("ui", "textPaneFontName", this);
- }
-
- /**
- * 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
- */
- Line getLine(final int lineNumber) {
- synchronized (lines) {
- return lines.get(lineNumber);
- }
- }
-
- /**
- * Adds the stylised string to the canvas.
- *
- * @param text stylised string to add to the text
- */
- public void addText(final String[] text) {
- synchronized (lines) {
- lines.add(new Line(text, fontSize, fontName));
- fireLineAdded(lines.indexOf(text));
- }
- }
-
- /**
- * Adds the stylised string to the canvas.
- *
- * @param text stylised string to add to the text
- * @param lineHeight Line height for the new line of text
- */
- public void addText(final String[] text, final int lineHeight) {
- synchronized (lines) {
- lines.add(new Line(text, lineHeight, fontName));
- fireLineAdded(lines.indexOf(text));
- }
- }
-
- /**
- * Adds the stylised string to the canvas.
- *
- * @param text stylised string to add to the text
- */
- public void addText(final List<String[]> text) {
- synchronized (lines) {
- final int start = lines.size();
- for (String[] string : text) {
- lines.add(new Line(string, fontSize, fontName));
- }
- fireLinesAdded(start, text.size());
- }
- }
-
- /**
- * Adds the stylised string to the canvas.
- *
- * @param text stylised string to add to the text
- * @param lineHeights line heights for the new lines
- */
- public void addText(final List<String[]> text,
- final List<Integer> lineHeights) {
- synchronized (lines) {
- final int start = lines.size();
- for (int i = 0; i < text.size(); i++) {
- final String[] string = text.get(i);
- final int lineHeight = lineHeights.get(i);
- lines.add(new Line(string, lineHeight, fontName));
- }
- fireLinesAdded(start, text.size());
- }
- }
-
- /**
- * 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) {
- while (lines.size() > numLines) {
- lines.remove(0);
- }
- fireTrimmed();
- }
- }
-
- /** Clears all lines from the document. */
- protected 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) {
- synchronized (listeners) {
- 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 line added method on all listeners.
- *
- * @param index Index of the added line
- */
- protected void fireLineAdded(final int index) {
- final Object[] listenerList = listeners.getListenerList();
- for (int i = 0; i < listenerList.length; i += 2) {
- if (listenerList[i] == IRCDocumentListener.class) {
- ((IRCDocumentListener) listenerList[i + 1]).lineAdded(index,
- lines.size());
- }
- }
- }
-
- /**
- * 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) {
- final Object[] listenerList = listeners.getListenerList();
- for (int i = 0; i < listenerList.length; i += 2) {
- if (listenerList[i] == IRCDocumentListener.class) {
- ((IRCDocumentListener) listenerList[i + 1]).linesAdded(index,
- size,
- lines.size());
- }
- }
- }
-
- /**
- * Fires the trimmed method on all listeners.
- */
- protected void fireTrimmed() {
- final Object[] listenerList = listeners.getListenerList();
- for (int i = 0; i < listenerList.length; i += 2) {
- if (listenerList[i] == IRCDocumentListener.class) {
- ((IRCDocumentListener) listenerList[i + 1]).trimmed(lines.size());
- }
- }
- }
-
- /**
- * fires the cleared method on all listeners.
- */
- protected void fireCleared() {
- final Object[] listenerList = listeners.getListenerList();
- for (int i = 0; i < listenerList.length; i += 2) {
- if (listenerList[i] == IRCDocumentListener.class) {
- ((IRCDocumentListener) listenerList[i + 1]).cleared();
- }
- }
- }
-
- /**
- * fires the need repaint method on all listeners.
- */
- protected void fireRepaintNeeded() {
- final Object[] listenerList = listeners.getListenerList();
- for (int i = 0; i < listenerList.length; i += 2) {
- if (listenerList[i] == IRCDocumentListener.class) {
- ((IRCDocumentListener) listenerList[i + 1]).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
- */
- 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) {
- styledLine = line.getStyled();
- cachedLines.add(line);
- cachedStrings.add(styledLine);
- }
-
- 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
- */
- int getLineHeight(final Line line) {
- return line.getFontSize();
- }
-
- /**
- * Returns the line height of the specified line
- *
- * @param line Line
- *
- * @return Line height
- */
- public int getLineHeight(final int line) {
- return getLineHeight(getLine(line));
- }
-
- private void setCachedSettings() {
- final Font defaultFont = UIManager.getFont("TextPane.font");
- if (config.hasOptionString("ui", "textPaneFontName")) {
- fontName = config.getOption("ui", "textPaneFontName");
- } else {
- fontName = defaultFont.getName();
- }
- if (config.hasOptionString("ui", "textPaneFontSize")) {
- fontSize = config.getOptionInt("ui", "textPaneFontSize");
- } else {
- fontSize = defaultFont.getSize();
- }
- }
-
- /** {@inheritDoc} */
- @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();
- }
- }
|