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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2006-2013 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.actions.ActionManager;
  24. import com.dmdirc.addons.dcc.actions.DCCActions;
  25. import com.dmdirc.addons.dcc.io.DCCChat;
  26. import com.dmdirc.config.ConfigManager;
  27. import com.dmdirc.ui.WindowManager;
  28. import com.dmdirc.ui.core.components.WindowComponent;
  29. import java.util.Arrays;
  30. /**
  31. * This class links DCC Chat objects to a window.
  32. */
  33. public class ChatContainer extends DCCFrameContainer implements DCCChatHandler {
  34. /** The DCCChat object we are a window for. */
  35. private final DCCChat dccChat;
  36. /** My Nickname. */
  37. private final String nickname;
  38. /** Other Nickname. */
  39. private final String otherNickname;
  40. /**
  41. * Creates a new instance of DCCChatWindow with a given DCCChat object.
  42. *
  43. * @param plugin the DCC Plugin responsible for this window
  44. * @param dcc The DCCChat object this window wraps around
  45. * @param configManager Config manager
  46. * @param title The title of this window
  47. * @param nick My Current Nickname
  48. * @param targetNick Nickname of target
  49. */
  50. public ChatContainer(final DCCPlugin plugin, final DCCChat dcc,
  51. final ConfigManager configManager, final String title,
  52. final String nick, final String targetNick) {
  53. super(title, "dcc-chat-inactive", configManager,
  54. DCCCommandParser.getDCCCommandParser(configManager),
  55. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  56. WindowComponent.INPUTFIELD.getIdentifier()));
  57. dccChat = dcc;
  58. dcc.setHandler(this);
  59. nickname = nick;
  60. otherNickname = targetNick;
  61. WindowManager.getWindowManager().addWindow(plugin.getContainer(), this);
  62. }
  63. /**
  64. * Get the DCCChat Object associated with this window.
  65. *
  66. * @return The DCCChat Object associated with this window
  67. */
  68. public DCCChat getDCC() {
  69. return dccChat;
  70. }
  71. /** {@inheritDoc} */
  72. @Override
  73. public void sendLine(final String line) {
  74. if (dccChat.isWriteable()) {
  75. final StringBuffer buff = new StringBuffer("DCCChatSelfMessage");
  76. ActionManager.getActionManager().triggerEvent(
  77. DCCActions.DCC_CHAT_SELFMESSAGE, buff, this, line);
  78. addLine(buff, nickname, line);
  79. dccChat.sendLine(line);
  80. } else {
  81. final StringBuffer buff = new StringBuffer("DCCChatError");
  82. addLine(buff, "Socket is closed.", line);
  83. }
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public void handleChatMessage(final DCCChat dcc, final String message) {
  88. final StringBuffer buff = new StringBuffer("DCCChatMessage");
  89. ActionManager.getActionManager().triggerEvent(
  90. DCCActions.DCC_CHAT_MESSAGE, buff, this, otherNickname, message);
  91. addLine(buff, otherNickname, message);
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public void socketClosed(final DCCChat dcc) {
  96. final StringBuffer buff = new StringBuffer("DCCChatInfo");
  97. ActionManager.getActionManager().triggerEvent(
  98. DCCActions.DCC_CHAT_SOCKETCLOSED, buff, this);
  99. addLine(buff, "Socket closed");
  100. if (!isWindowClosing()) {
  101. setIcon("dcc-chat-inactive");
  102. }
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public void socketOpened(final DCCChat dcc) {
  107. final StringBuffer buff = new StringBuffer("DCCChatInfo");
  108. ActionManager.getActionManager().triggerEvent(
  109. DCCActions.DCC_CHAT_SOCKETOPENED, buff, this);
  110. addLine(buff, "Socket opened");
  111. setIcon("dcc-chat-active");
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. public void windowClosing() {
  116. super.windowClosing();
  117. dccChat.close();
  118. }
  119. }