/* * Copyright (c) 2006-2017 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.addons.dcc; import com.dmdirc.DefaultInputModel; import com.dmdirc.addons.dcc.events.DccChatMessageEvent; import com.dmdirc.addons.dcc.events.DccChatSelfMessageEvent; import com.dmdirc.addons.dcc.events.DccChatSocketClosedEvent; import com.dmdirc.addons.dcc.events.DccChatSocketOpenedEvent; import com.dmdirc.addons.dcc.io.DCCChat; import com.dmdirc.events.CommandErrorEvent; import com.dmdirc.interfaces.CommandController; import com.dmdirc.events.eventbus.EventBus; import com.dmdirc.config.provider.AggregateConfigProvider; import com.dmdirc.ui.core.components.WindowComponent; import com.dmdirc.ui.input.TabCompleterFactory; import com.dmdirc.ui.messages.BackBufferFactory; import java.util.Arrays; /** * This class links DCC Chat objects to a window. */ public class ChatContainer extends DCCFrameContainer implements DCCChatHandler { /** The DCCChat object we are a window for. */ private final DCCChat dccChat; /** My Nickname. */ private final String nickname; /** Other Nickname. */ private final String otherNickname; /** Event bus to post events on. */ private final EventBus eventBus; /** * Creates a new instance of DCCChatWindow with a given DCCChat object. * * @param dcc The DCCChat object this window wraps around * @param configManager Config manager * @param commandController The controller to use in the command parser. * @param title The title of this window * @param nick My Current Nickname * @param targetNick Nickname of target * @param tabCompleterFactory The factory to use to create tab completers. * @param eventBus The bus to dispatch events on. */ public ChatContainer( final DCCChat dcc, final AggregateConfigProvider configManager, final BackBufferFactory backBufferFactory, final CommandController commandController, final String title, final String nick, final String targetNick, final TabCompleterFactory tabCompleterFactory, final EventBus eventBus) { super(title, "dcc-chat-inactive", configManager, backBufferFactory, eventBus, Arrays.asList( WindowComponent.TEXTAREA.getIdentifier(), WindowComponent.INPUTFIELD.getIdentifier())); setInputModel(new DefaultInputModel( this::sendLine, new DCCCommandParser(configManager, commandController, eventBus), tabCompleterFactory.getTabCompleter(configManager), () -> 512)); dccChat = dcc; dcc.setHandler(this); nickname = nick; otherNickname = targetNick; this.eventBus = eventBus; initBackBuffer(); } /** * Get the DCCChat Object associated with this window. * * @return The DCCChat Object associated with this window */ public DCCChat getDCC() { return dccChat; } private void sendLine(final String line) { if (dccChat.isWriteable()) { eventBus.publishAsync(new DccChatSelfMessageEvent(this, nickname, line)); dccChat.sendLine(line); } else { eventBus.publishAsync(new CommandErrorEvent(this, "Socket is closed.")); } } @Override public void handleChatMessage(final DCCChat dcc, final String message) { eventBus.publishAsync(new DccChatMessageEvent(this, otherNickname, message)); } @Override public void socketClosed(final DCCChat dcc) { eventBus.publishAsync(new DccChatSocketClosedEvent(this)); if (!isWindowClosing()) { setIcon("dcc-chat-inactive"); } } @Override public void socketOpened(final DCCChat dcc) { eventBus.publishAsync(new DccChatSocketOpenedEvent(this)); setIcon("dcc-chat-active"); } @Override public void close() { super.close(); dccChat.close(); } }