123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- /*
- * Copyright (c) 2006-2015 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.commandparser.parsers.CommandParser;
- import com.dmdirc.events.ClientLineAddedEvent;
- import com.dmdirc.events.DisplayPropertyMap;
- import com.dmdirc.events.FrameClosingEvent;
- import com.dmdirc.events.FrameComponentAddedEvent;
- import com.dmdirc.events.FrameComponentRemovedEvent;
- import com.dmdirc.events.FrameIconChangedEvent;
- import com.dmdirc.events.FrameNameChangedEvent;
- import com.dmdirc.events.FrameTitleChangedEvent;
- import com.dmdirc.interfaces.Connection;
- import com.dmdirc.interfaces.config.AggregateConfigProvider;
- import com.dmdirc.interfaces.config.ConfigChangeListener;
- import com.dmdirc.parser.common.CompositionState;
- import com.dmdirc.ui.IconManager;
- import com.dmdirc.ui.input.TabCompleter;
- import com.dmdirc.ui.messages.BackBuffer;
- import com.dmdirc.ui.messages.BackBufferFactory;
- import com.dmdirc.ui.messages.Formatter;
- import com.dmdirc.ui.messages.IRCDocument;
- import com.dmdirc.ui.messages.Styliser;
- import com.dmdirc.ui.messages.UnreadStatusManager;
- import com.dmdirc.ui.messages.sink.MessageSinkManager;
- import com.dmdirc.util.ChildEventBusManager;
- import com.dmdirc.util.URLBuilder;
- import com.dmdirc.util.collections.ListenerList;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Optional;
- import java.util.Set;
- import java.util.concurrent.CopyOnWriteArrayList;
-
- import javax.annotation.Nullable;
-
- import static com.google.common.base.Preconditions.checkState;
-
- /**
- * 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 children of this frame. */
- private final Collection<FrameContainer> children = new CopyOnWriteArrayList<>();
- /** The parent of this frame. */
- private final Optional<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 ConfigChangeListener changer = (d, k) -> iconUpdated();
- /** The UI components that this frame requires. */
- private final Set<String> components;
- /** The manager to use to manage our event bus. */
- private final ChildEventBusManager eventBusManager;
- /** Event bus to dispatch events to. */
- private final DMDircMBassador eventBus;
- /** The icon manager to use for this container. */
- private final IconManager iconManager;
- /** The manager handling this frame's unread status. */
- private final UnreadStatusManager unreadStatusManager;
- /** Whether or not this container is writable. */
- private final boolean writable;
- /** The back buffer factory. */
- private final BackBufferFactory backBufferFactory;
- /** The back buffer for this container. */
- private BackBuffer backBuffer;
- /** Lock for access to {@link #backBuffer}. */
- private final Object backBufferLock = new Object();
-
- /**
- * The command parser used for commands in this container.
- * <p>
- * Only defined if this container is {@link #writable}.
- */
- private final Optional<CommandParser> commandParser;
- /**
- * The manager to use to dispatch messages to sinks.
- * <p>
- * Only defined if this container is {@link #writable}.
- */
- private final Optional<MessageSinkManager> messageSinkManager;
- /**
- * The tab completer to use.
- * <p>
- * Only defined if this container is {@link #writable}.
- */
- private final Optional<TabCompleter> tabCompleter;
-
- /**
- * Instantiate new frame container.
- */
- protected FrameContainer(
- @Nullable final FrameContainer parent,
- final String icon,
- final String name,
- final String title,
- final AggregateConfigProvider config,
- final BackBufferFactory backBufferFactory,
- final URLBuilder urlBuilder,
- final DMDircMBassador eventBus,
- final Collection<String> components) {
- this.parent = Optional.ofNullable(parent);
- this.configManager = config;
- this.name = name;
- this.title = title;
- this.components = new HashSet<>(components);
- this.iconManager = new IconManager(configManager, urlBuilder);
- this.writable = false;
- this.commandParser = Optional.empty();
- this.tabCompleter = Optional.empty();
- this.messageSinkManager = Optional.empty();
- this.backBufferFactory = backBufferFactory;
-
- eventBusManager = new ChildEventBusManager(eventBus);
- eventBusManager.connect();
- this.eventBus = eventBusManager.getChildBus();
- this.unreadStatusManager = new UnreadStatusManager(this);
- this.eventBus.subscribe(unreadStatusManager);
-
- setIcon(icon);
- }
-
- /**
- * Instantiate new frame container that accepts user input.
- */
- protected FrameContainer(
- @Nullable final FrameContainer parent,
- final String icon,
- final String name,
- final String title,
- final AggregateConfigProvider config,
- final BackBufferFactory backBufferFactory,
- final URLBuilder urlBuilder,
- final CommandParser commandParser,
- final TabCompleter tabCompleter,
- final MessageSinkManager messageSinkManager,
- final DMDircMBassador eventBus,
- final Collection<String> components) {
- this.parent = Optional.ofNullable(parent);
- this.configManager = config;
- this.name = name;
- this.title = title;
- this.components = new HashSet<>(components);
- this.iconManager = new IconManager(configManager, urlBuilder);
- this.writable = true;
- this.commandParser = Optional.of(commandParser);
- this.tabCompleter = Optional.of(tabCompleter);
- this.messageSinkManager = Optional.of(messageSinkManager);
- this.backBufferFactory = backBufferFactory;
- commandParser.setOwner(this);
-
- eventBusManager = new ChildEventBusManager(eventBus);
- eventBusManager.connect();
- this.eventBus = eventBusManager.getChildBus();
- this.unreadStatusManager = new UnreadStatusManager(this);
- this.eventBus.subscribe(unreadStatusManager);
-
- setIcon(icon);
- }
-
- public Optional<FrameContainer> getParent() {
- return parent;
- }
-
- public String getIcon() {
- return icon;
- }
-
- public String getName() {
- return name;
- }
-
- public String getTitle() {
- return title;
- }
-
- public AggregateConfigProvider getConfigManager() {
- return configManager;
- }
-
- public DMDircMBassador getEventBus() {
- return eventBus;
- }
-
- public boolean isWritable() {
- return writable;
- }
-
- /**
- * 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);
- }
-
- /**
- * 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);
- }
-
- /**
- * 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);
- }
-
- /**
- * Gets an icon manager for this container.
- *
- * @return An icon manager for this container.
- */
- public IconManager getIconManager() {
- return iconManager;
- }
-
- /**
- * Retrieves the {@link IRCDocument} used to store this frame's content.
- *
- * @return This frame's document
- *
- * @since 0.6.4
- * @deprecated Use {@link #getBackBuffer()}
- */
- @Deprecated
- public IRCDocument getDocument() {
- return getBackBuffer().getDocument();
- }
-
- /**
- * Changes the name of this container, and fires a {@link FrameNameChangedEvent}.
- *
- * @param name The new name for this frame.
- */
- protected void setName(final String name) {
- this.name = name;
-
- eventBus.publishAsync(new FrameNameChangedEvent(this, name));
- }
-
- /**
- * Changes the title of this container, and fires a {@link FrameTitleChangedEvent}.
- *
- * @param title The new title for this frame.
- */
- public void setTitle(final String title) {
- this.title = title;
-
- eventBus.publishAsync(new FrameTitleChangedEvent(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);
- eventBus.publishAsync(new FrameComponentAddedEvent(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);
-
- eventBus.publishAsync(new FrameComponentRemovedEvent(this, component));
- }
-
- /**
- * Closes this container (and its associated frame).
- */
- public void close() {
- eventBus.unsubscribe(unreadStatusManager);
- eventBus.publish(new FrameClosingEvent(this));
- eventBusManager.disconnect();
- getBackBuffer().stopAddingEvents();
- }
-
- /**
- * Returns the connection that this container is associated with.
- *
- * @return the associated connection.
- */
- public abstract Optional<Connection> getConnection();
-
- /**
- * Sets the icon to be used by this frame container and fires a {@link FrameIconChangedEvent}.
- *
- * @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() {
- eventBus.publish(new FrameIconChangedEvent(this, icon));
- }
-
- /**
- * Retrieves the styliser which should be used by this container.
- *
- * @return this container's styliser
- * @deprecated Use {@link #getBackBuffer()}
- */
- @Deprecated
- public Styliser getStyliser() {
- return getBackBuffer().getStyliser();
- }
-
- /**
- * Gets the back buffer for this container.
- *
- * @return This container's back buffer.
- */
- public BackBuffer getBackBuffer() {
- synchronized (backBufferLock) {
- if (backBuffer == null) {
- backBuffer = backBufferFactory.getBackBuffer(this);
- backBuffer.startAddingEvents();
- }
- }
-
- return backBuffer;
- }
-
- /**
- * 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) {
- for (final String myLine : line.split("\n")) {
- getBackBuffer().getDocument().addText(
- timestamp.getTime(), DisplayPropertyMap.EMPTY, myLine);
- eventBus.publishAsync(new ClientLineAddedEvent(this, myLine));
- }
- }
-
- /**
- * Sends a line of text to this container's source.
- *
- * @param line The line to be sent
- */
- public void sendLine(final String line) {
- throw new UnsupportedOperationException("Container doesn't override sendLine");
- }
-
- /**
- * Retrieves the command parser to be used for this container.
- *
- * @return This container's command parser
- */
- public CommandParser getCommandParser() {
- checkState(writable);
- return commandParser.get();
- }
-
- /**
- * Retrieves the tab completer which should be used for this container.
- *
- * @return This container's tab completer
- */
- public TabCompleter getTabCompleter() {
- checkState(writable);
- return tabCompleter.get();
- }
-
- /**
- * Returns the maximum length that a line passed to sendLine() should be, in order to prevent it
- * being truncated or causing protocol violations.
- *
- * @return The maximum line length for this container
- */
- public int getMaxLineLength() {
- throw new UnsupportedOperationException("Container doesn't override getMaxLineLength");
- }
-
- /**
- * Splits the specified line into chunks that contain a number of bytes less than or equal to
- * the value returned by {@link #getMaxLineLength()}.
- *
- * @param line The line to be split
- *
- * @return An ordered list of chunks of the desired length
- */
- protected List<String> splitLine(final String line) {
- final List<String> result = new ArrayList<>();
-
- if (line.indexOf('\n') > -1) {
- for (String part : line.split("\n")) {
- result.addAll(splitLine(part));
- }
- } else {
- final StringBuilder remaining = new StringBuilder(line);
-
- while (getMaxLineLength() > -1 && remaining.toString().getBytes().length
- > getMaxLineLength()) {
- int number = Math.min(remaining.length(), getMaxLineLength());
-
- while (remaining.substring(0, number).getBytes().length > getMaxLineLength()) {
- number--;
- }
-
- result.add(remaining.substring(0, number));
- remaining.delete(0, number);
- }
-
- result.add(remaining.toString());
- }
-
- return result;
- }
-
- /**
- * Returns the number of lines that the specified string would be sent as.
- *
- * @param line The string to be split and sent
- *
- * @return The number of lines required to send the specified string
- */
- public final int getNumLines(final String line) {
- final String[] splitLines = line.split("(\n|\r\n|\r)", Integer.MAX_VALUE);
- int lines = 0;
-
- for (String splitLine : splitLines) {
- if (getMaxLineLength() <= 0) {
- lines++;
- } else {
- lines += (int) Math.ceil(splitLine.getBytes().length
- / (double) getMaxLineLength());
- }
- }
-
- return lines;
- }
-
- /**
- * Processes and displays a notification.
- *
- * @param messageType The name of the formatter to be used for the message
- * @param args The arguments for the message
- *
- * @return True if any further behaviour should be executed, false otherwise
- */
- public boolean doNotification(final String messageType, final Object... args) {
- return doNotification(new Date(), messageType, args);
- }
-
- /**
- * Processes and displays a notification.
- *
- * @param date The date/time at which the event occurred
- * @param messageType The name of the formatter to be used for the message
- * @param args The arguments for the message
- *
- * @return True if any further behaviour should be executed, false otherwise
- */
- public boolean doNotification(final Date date, final String messageType, final Object... args) {
- final List<Object> messageArgs = new ArrayList<>();
- final List<Object> actionArgs = new ArrayList<>();
-
- actionArgs.add(this);
-
- for (Object arg : args) {
- actionArgs.add(arg);
-
- if (!processNotificationArg(arg, messageArgs)) {
- messageArgs.add(arg);
- }
- }
-
- modifyNotificationArgs(actionArgs, messageArgs);
-
- handleNotification(date, messageType, messageArgs.toArray());
-
- return true;
- }
-
- /**
- * Allows subclasses to modify the lists of arguments for notifications.
- *
- * @param actionArgs The list of arguments to be passed to the actions system
- * @param messageArgs The list of arguments to be passed to the formatter
- */
- protected void modifyNotificationArgs(final List<Object> actionArgs,
- final List<Object> messageArgs) {
- // Do nothing
- }
-
- /**
- * Allows subclasses to process specific types of notification arguments.
- *
- * @param arg The argument to be processed
- * @param args The list of arguments that any data should be appended to
- *
- * @return True if the arg has been processed, false otherwise
- */
- protected boolean processNotificationArg(final Object arg, final List<Object> args) {
- return false;
- }
-
- /**
- * Handles general server notifications (i.e., ones not tied to a specific window). The user can
- * select where the notifications should go in their config.
- *
- * @param messageType The type of message that is being sent
- * @param args The arguments for the message
- */
- public void handleNotification(final String messageType, final Object... args) {
- handleNotification(new Date(), messageType, args);
- }
-
- /**
- * Handles general server notifications (i.e., ones not tied to a specific window). The user can
- * select where the notifications should go in their config.
- *
- * @param date The date/time at which the event occurred
- * @param messageType The type of message that is being sent
- * @param args The arguments for the message
- */
- public void handleNotification(final Date date, final String messageType, final Object... args) {
- checkState(writable);
- messageSinkManager.get().dispatchMessage(this, date, messageType, args);
- }
-
- /**
- * Sets the composition state for the local user for this chat.
- *
- * @param state The new composition state
- */
- public void setCompositionState(final CompositionState state) {
- // Default implementation does nothing. Subclasses that support
- // composition should override this.
- }
-
- public UnreadStatusManager getUnreadStatusManager() {
- return unreadStatusManager;
- }
-
- }
|