您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChatContainer.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2006-2015 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.DefaultInputModel;
  24. import com.dmdirc.addons.dcc.events.DccChatMessageEvent;
  25. import com.dmdirc.addons.dcc.events.DccChatSelfMessageEvent;
  26. import com.dmdirc.addons.dcc.events.DccChatSocketClosedEvent;
  27. import com.dmdirc.addons.dcc.events.DccChatSocketOpenedEvent;
  28. import com.dmdirc.addons.dcc.io.DCCChat;
  29. import com.dmdirc.events.CommandErrorEvent;
  30. import com.dmdirc.interfaces.CommandController;
  31. import com.dmdirc.interfaces.EventBus;
  32. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  33. import com.dmdirc.ui.core.components.WindowComponent;
  34. import com.dmdirc.ui.input.TabCompleterFactory;
  35. import com.dmdirc.ui.messages.BackBufferFactory;
  36. import java.util.Arrays;
  37. /**
  38. * This class links DCC Chat objects to a window.
  39. */
  40. public class ChatContainer extends DCCFrameContainer implements DCCChatHandler {
  41. /** The DCCChat object we are a window for. */
  42. private final DCCChat dccChat;
  43. /** My Nickname. */
  44. private final String nickname;
  45. /** Other Nickname. */
  46. private final String otherNickname;
  47. /** Event bus to post events on. */
  48. private final EventBus eventBus;
  49. /**
  50. * Creates a new instance of DCCChatWindow with a given DCCChat object.
  51. *
  52. * @param dcc The DCCChat object this window wraps around
  53. * @param configManager Config manager
  54. * @param commandController The controller to use in the command parser.
  55. * @param title The title of this window
  56. * @param nick My Current Nickname
  57. * @param targetNick Nickname of target
  58. * @param tabCompleterFactory The factory to use to create tab completers.
  59. * @param eventBus The bus to dispatch events on.
  60. */
  61. public ChatContainer(
  62. final DCCChat dcc,
  63. final AggregateConfigProvider configManager,
  64. final BackBufferFactory backBufferFactory,
  65. final CommandController commandController,
  66. final String title,
  67. final String nick,
  68. final String targetNick,
  69. final TabCompleterFactory tabCompleterFactory,
  70. final EventBus eventBus) {
  71. super(title, "dcc-chat-inactive", configManager, backBufferFactory,
  72. eventBus,
  73. Arrays.asList(
  74. WindowComponent.TEXTAREA.getIdentifier(),
  75. WindowComponent.INPUTFIELD.getIdentifier()));
  76. setInputModel(new DefaultInputModel(
  77. this::sendLine,
  78. new DCCCommandParser(configManager, commandController, eventBus),
  79. tabCompleterFactory.getTabCompleter(configManager),
  80. () -> 512));
  81. dccChat = dcc;
  82. dcc.setHandler(this);
  83. nickname = nick;
  84. otherNickname = targetNick;
  85. this.eventBus = eventBus;
  86. initBackBuffer();
  87. }
  88. /**
  89. * Get the DCCChat Object associated with this window.
  90. *
  91. * @return The DCCChat Object associated with this window
  92. */
  93. public DCCChat getDCC() {
  94. return dccChat;
  95. }
  96. private void sendLine(final String line) {
  97. if (dccChat.isWriteable()) {
  98. eventBus.publishAsync(new DccChatSelfMessageEvent(this, nickname, line));
  99. dccChat.sendLine(line);
  100. } else {
  101. eventBus.publishAsync(new CommandErrorEvent(this, "Socket is closed."));
  102. }
  103. }
  104. @Override
  105. public void handleChatMessage(final DCCChat dcc, final String message) {
  106. eventBus.publishAsync(new DccChatMessageEvent(this, otherNickname, message));
  107. }
  108. @Override
  109. public void socketClosed(final DCCChat dcc) {
  110. eventBus.publishAsync(new DccChatSocketClosedEvent(this));
  111. if (!isWindowClosing()) {
  112. setIcon("dcc-chat-inactive");
  113. }
  114. }
  115. @Override
  116. public void socketOpened(final DCCChat dcc) {
  117. eventBus.publishAsync(new DccChatSocketOpenedEvent(this));
  118. setIcon("dcc-chat-active");
  119. }
  120. @Override
  121. public void close() {
  122. super.close();
  123. dccChat.close();
  124. }
  125. }