You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatContainer.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.dcc;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.addons.dcc.events.DccChatMessageEvent;
  26. import com.dmdirc.addons.dcc.events.DccChatSelfmessageEvent;
  27. import com.dmdirc.addons.dcc.events.DccChatSocketclosedEvent;
  28. import com.dmdirc.addons.dcc.events.DccChatSocketopenedEvent;
  29. import com.dmdirc.addons.dcc.io.DCCChat;
  30. import com.dmdirc.util.EventUtils;
  31. import com.dmdirc.interfaces.CommandController;
  32. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  33. import com.dmdirc.messages.MessageSinkManager;
  34. import com.dmdirc.ui.core.components.WindowComponent;
  35. import com.dmdirc.ui.input.TabCompleterFactory;
  36. import com.dmdirc.util.URLBuilder;
  37. import java.util.Arrays;
  38. import javax.annotation.Nullable;
  39. /**
  40. * This class links DCC Chat objects to a window.
  41. */
  42. public class ChatContainer extends DCCFrameContainer implements DCCChatHandler {
  43. /** The DCCChat object we are a window for. */
  44. private final DCCChat dccChat;
  45. /** My Nickname. */
  46. private final String nickname;
  47. /** Other Nickname. */
  48. private final String otherNickname;
  49. /** Event bus to post events on. */
  50. private final DMDircMBassador eventBus;
  51. /**
  52. * Creates a new instance of DCCChatWindow with a given DCCChat object.
  53. *
  54. * @param parent The parent of this frame container, if any.
  55. * @param dcc The DCCChat object this window wraps around
  56. * @param configManager Config manager
  57. * @param commandController The controller to use in the command parser.
  58. * @param title The title of this window
  59. * @param nick My Current Nickname
  60. * @param targetNick Nickname of target
  61. * @param tabCompleterFactory The factory to use to create tab completers.
  62. * @param messageSinkManager The sink manager to use to dispatch messages.
  63. * @param urlBuilder The URL builder to use when finding icons.
  64. * @param eventBus The bus to dispatch events on.
  65. */
  66. public ChatContainer(
  67. @Nullable final FrameContainer parent,
  68. final DCCChat dcc,
  69. final AggregateConfigProvider configManager,
  70. final CommandController commandController,
  71. final String title,
  72. final String nick,
  73. final String targetNick,
  74. final TabCompleterFactory tabCompleterFactory,
  75. final MessageSinkManager messageSinkManager,
  76. final URLBuilder urlBuilder,
  77. final DMDircMBassador eventBus) {
  78. super(parent, title, "dcc-chat-inactive", configManager,
  79. new DCCCommandParser(configManager, commandController, eventBus),
  80. messageSinkManager,
  81. tabCompleterFactory,
  82. urlBuilder,
  83. eventBus,
  84. Arrays.asList(
  85. WindowComponent.TEXTAREA.getIdentifier(),
  86. WindowComponent.INPUTFIELD.getIdentifier()));
  87. dccChat = dcc;
  88. dcc.setHandler(this);
  89. nickname = nick;
  90. otherNickname = targetNick;
  91. this.eventBus = eventBus;
  92. }
  93. /**
  94. * Get the DCCChat Object associated with this window.
  95. *
  96. * @return The DCCChat Object associated with this window
  97. */
  98. public DCCChat getDCC() {
  99. return dccChat;
  100. }
  101. @Override
  102. public void sendLine(final String line) {
  103. if (dccChat.isWriteable()) {
  104. final DccChatSelfmessageEvent event = new DccChatSelfmessageEvent(this, line);
  105. final String format = EventUtils.postDisplayable(eventBus, event, "DCCChatSelfMessage");
  106. addLine(format, nickname, line);
  107. dccChat.sendLine(line);
  108. } else {
  109. addLine("DCCChatError", "Socket is closed.", line);
  110. }
  111. }
  112. @Override
  113. public void handleChatMessage(final DCCChat dcc, final String message) {
  114. final DccChatMessageEvent event = new DccChatMessageEvent(this, otherNickname, message);
  115. final String format = EventUtils.postDisplayable(eventBus, event, "DCCChatMessage");
  116. addLine(format, otherNickname, message);
  117. }
  118. @Override
  119. public void socketClosed(final DCCChat dcc) {
  120. final DccChatSocketclosedEvent event = new DccChatSocketclosedEvent(this);
  121. final String format = EventUtils.postDisplayable(eventBus, event, "DCCChatInfo");
  122. addLine(format, "Socket closed");
  123. if (!isWindowClosing()) {
  124. setIcon("dcc-chat-inactive");
  125. }
  126. }
  127. @Override
  128. public void socketOpened(final DCCChat dcc) {
  129. final DccChatSocketopenedEvent event = new DccChatSocketopenedEvent(this);
  130. final String format = EventUtils.postDisplayable(eventBus, event, "DCCChatInfo");
  131. addLine(format, "Socket opened");
  132. setIcon("dcc-chat-active");
  133. }
  134. @Override
  135. public void close() {
  136. super.close();
  137. dccChat.close();
  138. }
  139. }