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 5.1KB

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