123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- /*
- * 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;
-
- import com.dmdirc.actions.ActionManager;
- import com.dmdirc.actions.CoreActionType;
- import com.dmdirc.interfaces.Connection;
- import com.dmdirc.interfaces.FrameCloseListener;
- import com.dmdirc.interfaces.FrameComponentChangeListener;
- import com.dmdirc.interfaces.FrameInfoListener;
- import com.dmdirc.interfaces.NotificationListener;
- import com.dmdirc.interfaces.config.AggregateConfigProvider;
- import com.dmdirc.interfaces.config.ConfigChangeListener;
- import com.dmdirc.ui.Colour;
- import com.dmdirc.ui.IconManager;
- import com.dmdirc.ui.messages.Formatter;
- import com.dmdirc.ui.messages.IRCDocument;
- import com.dmdirc.ui.messages.Styliser;
- import com.dmdirc.util.URLBuilder;
- import com.dmdirc.util.collections.ListenerList;
-
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Set;
- import java.util.concurrent.CopyOnWriteArrayList;
-
- /**
- * The frame container implements basic methods that should be present in all objects that handle a
- * frame.
- */
- public abstract class FrameContainer {
-
- /** Listeners not yet using ListenerSupport. */
- protected final ListenerList listeners = new ListenerList();
- /** The colour of our frame's notifications. */
- private Colour notification = Colour.BLACK;
- /** The document used to store this container's content. */
- private IRCDocument document;
- /** The children of this frame. */
- private final Collection<FrameContainer> children = new CopyOnWriteArrayList<>();
- /** The parent of this frame. */
- private FrameContainer parent;
- /** The name of the icon being used for this container's frame. */
- private String icon;
- /** The name of this container. */
- private String name;
- /** The title of this container. */
- private String title;
- /** The config manager for this container. */
- private final AggregateConfigProvider configManager;
- /** The IconChanger for this container. */
- private final IconChanger changer = new IconChanger();
- /** The UI components that this frame requires. */
- private final Set<String> components;
- /** The styliser used by this container. */
- private Styliser styliser;
- /** Object used to synchronise styliser access. */
- private final Object styliserSync = new Object();
- /** Object used to synchronise styliser access. */
- private final Object documentSync = new Object();
- /** The icon manager to use for this container. */
- private final IconManager iconManager;
-
- /**
- * Instantiate new frame container.
- *
- * @param icon The icon to use for this container
- * @param name The name of this container
- * @param title The title of this container
- * @param config The config manager for this container
- * @param urlBuilder The URL builder to use when finding icons.
- * @param components The UI components that this frame requires
- *
- * @since 0.6.4
- */
- protected FrameContainer(
- final String icon,
- final String name,
- final String title,
- final AggregateConfigProvider config,
- final URLBuilder urlBuilder,
- final Collection<String> components) {
- this.configManager = config;
- this.name = name;
- this.title = title;
- this.components = new HashSet<>(components);
- this.iconManager = new IconManager(configManager, urlBuilder);
-
- setIcon(icon);
- }
-
- public Colour getNotification() {
- return notification;
- }
-
- public FrameContainer getParent() {
- return parent;
- }
-
- public String getIcon() {
- return icon;
- }
-
- public String getName() {
- return name;
- }
-
- public String getTitle() {
- return title;
- }
-
- public AggregateConfigProvider getConfigManager() {
- return configManager;
- }
-
- /**
- * Returns a collection of direct children of this frame.
- *
- * @return This frame's children
- *
- * @since 0.6.4
- */
- public Collection<FrameContainer> getChildren() {
- return Collections.unmodifiableCollection(children);
- }
-
- /**
- * Determines whether the specified target is a child of this container. Children may be
- * indirect (i.e., a child of another child).
- *
- * @param target The window to be tested
- *
- * @return True if the specified container is a child of this one
- */
- public boolean isChild(final FrameContainer target) {
- if (children.contains(target)) {
- return true;
- }
-
- for (FrameContainer child : children) {
- if (child.isChild(target)) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Adds a new child window to this frame.
- *
- * @param child The window to be added
- *
- * @since 0.6.4
- */
- public void addChild(final FrameContainer child) {
- children.add(child);
- child.setParent(this);
- }
-
- /**
- * Removes a child window from this frame.
- *
- * @param child The window to be removed
- *
- * @since 0.6.4
- */
- public void removeChild(final FrameContainer child) {
- children.remove(child);
- }
-
- /**
- * Sets the parent of this container to the one specified. If this container already had a
- * parent, it will deregister itself with the old parent.
- *
- * @param parent The new parent for this container
- *
- * @since 0.6.4
- */
- public synchronized void setParent(final FrameContainer parent) {
- if (this.parent != null && (parent == null || !parent.equals(this.parent))) {
- this.parent.removeChild(this);
- }
-
- this.parent = parent;
- }
-
- /**
- * Gets an icon manager for this container.
- *
- * @return An icon manager for this container.
- */
- public IconManager getIconManager() {
- return iconManager;
- }
-
- /**
- * Gets an icon manager for this container.
- *
- * @param urlBuilder The builder to use to construct icon URLs.
- *
- * @return An icon manager for this container.
- *
- * @deprecated Don't bother passing in a URL builder. That was a bad idea.
- */
- @Deprecated
- public IconManager getIconManager(final URLBuilder urlBuilder) {
- return iconManager;
- }
-
- /**
- * Retrieves the {@link IRCDocument} used to store this frame's content.
- *
- * @return This frame's document
- *
- * @since 0.6.4
- */
- public IRCDocument getDocument() {
- synchronized (documentSync) {
- if (document == null) {
- document = new IRCDocument(getConfigManager(), getStyliser());
- }
- return document;
- }
- }
-
- /**
- * Changes the name of this container, and notifies any {@link FrameInfoListener}s of the
- * change.
- *
- * @param name The new name for this frame.
- */
- protected void setName(final String name) {
- this.name = name;
-
- listeners.getCallable(FrameInfoListener.class).nameChanged(this, name);
- }
-
- /**
- * Changes the title of this container, and notifies any {@link FrameInfoListener}s of the
- * change.
- *
- * @param title The new title for this frame.
- */
- public void setTitle(final String title) {
- this.title = title;
-
- listeners.getCallable(FrameInfoListener.class).titleChanged(this, title);
- }
-
- /**
- * Returns the collection of UI component identifiers that this frame container requires for its
- * display.
- *
- * @since 0.6.6
- * @return Collection of UI component identifiers
- */
- public Set<String> getComponents() {
- return Collections.unmodifiableSet(components);
- }
-
- /**
- * Adds a new component to this container.
- *
- * @since 0.6.6
- * @param component The component to be added
- */
- public void addComponent(final String component) {
- components.add(component);
-
- for (FrameComponentChangeListener listener
- : listeners.get(FrameComponentChangeListener.class)) {
- listener.componentAdded(this, component);
- }
- }
-
- /**
- * Removes a component from this container.
- *
- * @since 0.6.6
- * @param component The component to be removed
- */
- public void removeComponent(final String component) {
- components.remove(component);
-
- for (FrameComponentChangeListener listener
- : listeners.get(FrameComponentChangeListener.class)) {
- listener.componentRemoved(this, component);
- }
- }
-
- /**
- * Closes this container (and it's associated frame).
- */
- public void close() {
- for (FrameCloseListener listener : listeners.get(FrameCloseListener.class)) {
- listener.windowClosing(this);
- }
- }
-
- /**
- * Returns the connection that this container is associated with.
- *
- * @return the associated connection, or {@code null}.
- */
- public abstract Connection getConnection();
-
- /**
- * Sets the icon to be used by this frame container.
- *
- * @param icon The new icon to be used
- */
- public final void setIcon(final String icon) {
- this.icon = icon;
-
- iconUpdated();
-
- configManager.removeListener(changer);
- configManager.addChangeListener("icon", icon, changer);
- }
-
- /**
- * Called when this container's icon is updated.
- */
- private void iconUpdated() {
- listeners.getCallable(FrameInfoListener.class).iconChanged(this, icon);
- }
-
- /**
- * Retrieves the styliser which should be used by this container.
- *
- * @return this container's styliser
- */
- public Styliser getStyliser() {
- synchronized (styliserSync) {
- if (styliser == null) {
- styliser = new Styliser(getConnection(), getConfigManager());
- }
- return styliser;
- }
- }
-
- /**
- * Clears any outstanding notifications this frame has set.
- */
- public void clearNotification() {
- // TODO: This should default ot something colour independent
- notification = Colour.BLACK;
-
- listeners.getCallable(NotificationListener.class).notificationCleared(this);
- }
-
- /**
- * Sends a notification to the frame manager if this fame isn't active.
- *
- * @param colour The colour to use for the notification
- */
- public void sendNotification(final Colour colour) {
- if (!colour.equals(notification)) {
- notification = colour;
-
- listeners.getCallable(NotificationListener.class).notificationSet(this, colour);
- }
- }
-
- /**
- * Adds a line to this container's window. If the window is null for some reason, the line is
- * silently discarded.
- *
- * @param type The message type to use
- * @param timestamp The timestamp to use for this line
- * @param args The message's arguments
- *
- * @since 0.6.4
- */
- public void addLine(final String type, final Date timestamp,
- final Object... args) {
- if (type != null && !type.isEmpty()) {
- addLine(Formatter.formatMessage(getConfigManager(), type, args),
- timestamp);
- }
- }
-
- /**
- * Adds a line to this container's window. If the window is null for some reason, the line is
- * silently discarded.
- *
- * @param type The message type to use
- * @param args The message's arguments
- */
- public void addLine(final String type, final Object... args) {
- addLine(type, new Date(), args);
- }
-
- /**
- * Adds a line to this container's window. If the window is null for some reason, the line is
- * silently discarded.
- *
- * @param type The message type to use
- * @param timestamp The timestamp to use for this line
- * @param args The message's arguments
- *
- * @since 0.6.4
- */
- public void addLine(final StringBuffer type, final Date timestamp,
- final Object... args) {
- if (type != null) {
- addLine(type.toString(), timestamp, args);
- }
- }
-
- /**
- * Adds a line to this container's window. If the window is null for some reason, the line is
- * silently discarded.
- *
- * @param type The message type to use
- * @param args The message's arguments
- */
- public void addLine(final StringBuffer type, final Object... args) {
- addLine(type, new Date(), args);
- }
-
- /**
- * Adds the specified raw line to the window, without using a formatter.
- *
- * @param line The line to be added
- * @param timestamp Whether or not to display the timestamp for this line
- */
- public void addLine(final String line, final boolean timestamp) {
- addLine(line, timestamp ? new Date() : null);
- }
-
- /**
- * Adds the specified raw line to the window, without using a formatter, and using the specified
- * timestamp. If the timestamp is
- * <code>null</code>, no timestamp is added.
- *
- * @param line The line to be added
- * @param timestamp The timestamp to use for the line
- *
- * @since 0.6.4
- */
- public void addLine(final String line, final Date timestamp) {
- final List<String[]> lines = new LinkedList<>();
- for (final String myLine : line.split("\n")) {
- if (timestamp != null) {
- lines.add(new String[]{
- Formatter.formatMessage(getConfigManager(), "timestamp",
- timestamp),
- myLine,});
- } else {
- lines.add(new String[]{
- myLine,});
- }
-
- ActionManager.getActionManager().triggerEvent(
- CoreActionType.CLIENT_LINE_ADDED, null, this, myLine);
- }
-
- getDocument().addText(lines);
- }
-
- /**
- * Adds a close listener for this frame container.
- *
- * @since 0.6.5
- * @param listener The listener to be added
- */
- public void addCloseListener(final FrameCloseListener listener) {
- listeners.add(FrameCloseListener.class, listener);
- }
-
- /**
- * Removes a close listener from this frame container.
- *
- * @since 0.6.5
- * @param listener The listener to be removed
- */
- public void removeCloseListener(final FrameCloseListener listener) {
- listeners.remove(FrameCloseListener.class, listener);
- }
-
- /**
- * Adds a component listener to this container.
- *
- * @since 0.6.6
- * @param listener The listener to be added
- */
- public void addComponentListener(final FrameComponentChangeListener listener) {
- listeners.add(FrameComponentChangeListener.class, listener);
- }
-
- /**
- * Removes a component listener from this container.
- *
- * @since 0.6.6
- * @param listener The listener to be removed
- */
- public void removeComponentListener(final FrameComponentChangeListener listener) {
- listeners.remove(FrameComponentChangeListener.class, listener);
- }
-
- /**
- * Adds a notification listener to this container.
- *
- * @param listener The listener to inform of notification events.
- */
- public void addNotificationListener(final NotificationListener listener) {
- listeners.add(NotificationListener.class, listener);
- }
-
- /**
- * Removes a notification listener from this container.
- *
- * @param listener The listener to be removed.
- */
- public void removeNotificationListener(final NotificationListener listener) {
- listeners.remove(NotificationListener.class, listener);
- }
-
- /**
- * Adds a frame info listener to this container.
- *
- * @param listener The listener to be informed of frame information changes.
- */
- public void addFrameInfoListener(final FrameInfoListener listener) {
- listeners.add(FrameInfoListener.class, listener);
- }
-
- /**
- * Removes a frame info listener from this container.
- *
- * @param listener The listener to be removed.
- */
- public void removeFrameInfoListener(final FrameInfoListener listener) {
- listeners.remove(FrameInfoListener.class, listener);
- }
-
- /**
- * Updates the icon of this frame if its config setting is changed.
- */
- private class IconChanger implements ConfigChangeListener {
-
- /** {@inheritDoc} */
- @Override
- public void configChanged(final String domain, final String key) {
- iconUpdated();
- }
-
- }
-
- }
|