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.

DCCFrame.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.Server;
  24. import com.dmdirc.WritableFrameContainer;
  25. import com.dmdirc.commandparser.CommandManager;
  26. import com.dmdirc.commandparser.PopupType;
  27. import com.dmdirc.commandparser.commands.Command;
  28. import com.dmdirc.commandparser.commands.GlobalCommand;
  29. import com.dmdirc.commandparser.parsers.CommandParser;
  30. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  31. import com.dmdirc.config.IdentityManager;
  32. import com.dmdirc.ui.WindowManager;
  33. import com.dmdirc.ui.interfaces.InputWindow;
  34. import com.dmdirc.ui.swing.components.InputTextFrame;
  35. import com.dmdirc.ui.swing.components.TextFrame;
  36. import java.awt.Container;
  37. import javax.swing.JPopupMenu;
  38. /**
  39. * This class links DCC objects to a window.
  40. *
  41. * @author Shane 'Dataforce' McCormack
  42. */
  43. public abstract class DCCFrame extends WritableFrameContainer {
  44. /**
  45. * Empty Frame.
  46. */
  47. private class EmptyFrame extends InputTextFrame {
  48. /** A version number for this class. */
  49. private static final long serialVersionUID = 200711271;
  50. /**
  51. * Creates a new instance of EmptyFrame.
  52. *
  53. * @param owner The frame container that owns this frame
  54. */
  55. public EmptyFrame(final WritableFrameContainer owner) {
  56. super(owner);
  57. setTextPane(null);
  58. pack();
  59. }
  60. /**
  61. * Retrieves the command Parser for this input window.
  62. *
  63. * @return This window's command parser
  64. */
  65. public final CommandParser getCommandParser() {
  66. return GlobalCommandParser.getGlobalCommandParser();
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public PopupType getNicknamePopupType() {
  71. return null;
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public PopupType getChannelPopupType() {
  76. return null;
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public PopupType getHyperlinkPopupType() {
  81. return null;
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public PopupType getNormalPopupType() {
  86. return null;
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public void addCustomPopupItems(final JPopupMenu popupMenu) {
  91. //Add no custom popup items
  92. }
  93. }
  94. /**
  95. * DCC CommandParser
  96. */
  97. private static class DCCCommandParser extends CommandParser {
  98. /** A version number for this class. */
  99. private static final long serialVersionUID = 200711271;
  100. /** Loads the relevant commands into the parser. */
  101. @Override
  102. protected void loadCommands() { CommandManager.loadGlobalCommands(this); }
  103. /**
  104. * Executes the specified command with the given arguments.
  105. *
  106. * @param origin The window in which the command was typed
  107. * @param isSilent Whether the command is being silenced or not
  108. * @param command The command to be executed
  109. * @param args The arguments to the command
  110. */
  111. @Override
  112. protected void executeCommand(final InputWindow origin, final boolean isSilent, final Command command, final String... args) {
  113. ((GlobalCommand) command).execute(origin, isSilent, args);
  114. }
  115. /**
  116. * Called when the input was a line of text that was not a command.
  117. * This normally means it is sent to the server/channel/user as-is, with
  118. * no further processing.
  119. *
  120. * @param origin The window in which the command was typed
  121. * @param line The line input by the user
  122. */
  123. @Override
  124. protected void handleNonCommand(final InputWindow origin, final String line) {
  125. origin.getContainer().sendLine(line);
  126. }
  127. }
  128. /** The singleton instance of the DCC command parser. */
  129. private static DCCCommandParser myDCCCommandParser;
  130. /** The window title. */
  131. protected final String title;
  132. /** The Window we're using. */
  133. protected InputWindow myWindow = null;
  134. /** The dcc plugin that owns this frame */
  135. protected final DCCPlugin plugin;
  136. /** The Window we're using. */
  137. private boolean windowClosing = false;
  138. /**
  139. * Creates a new instance of DCCFrame with an empty window.
  140. *
  141. * @param plugin The DCCPlugin that owns this frame
  142. * @param title The title of this window
  143. * @param icon The icon to use
  144. */
  145. public DCCFrame(final DCCPlugin plugin, final String title, final String icon) {
  146. this(plugin, title, icon, true);
  147. }
  148. /**
  149. * Creates a new instance of DCCFrame.
  150. *
  151. * @param plugin The DCCPlugin that owns this frame
  152. * @param title The title of this window
  153. * @param defaultWindow Create default (empty) window. (non-default = chat frame)
  154. * @param icon The icon to use
  155. */
  156. public DCCFrame(final DCCPlugin plugin, final String title, final String icon ,final boolean defaultWindow) {
  157. super(icon, IdentityManager.getGlobalConfig());
  158. this.title = title;
  159. this.plugin = plugin;
  160. if (defaultWindow) {
  161. myWindow = new EmptyFrame(this);
  162. myWindow.setTitle(title);
  163. // myWindow.setVisible(true);
  164. }
  165. }
  166. /**
  167. * Retrieves a singleton instance of the DCC command parser.
  168. *
  169. * @return A DCCCommandParser
  170. */
  171. static synchronized DCCCommandParser getDCCCommandParser() {
  172. if (myDCCCommandParser == null) {
  173. myDCCCommandParser = new DCCCommandParser();
  174. }
  175. return myDCCCommandParser;
  176. }
  177. /**
  178. * Sends a line of text to this container's source.
  179. *
  180. * @param line The line to be sent
  181. */
  182. @Override
  183. public void sendLine(final String line) {
  184. }
  185. /**
  186. * Returns the maximum length that a line passed to sendLine() should be,
  187. * in order to prevent it being truncated or causing protocol violations.
  188. *
  189. * @return The maximum line length for this container
  190. */
  191. @Override
  192. public int getMaxLineLength() {
  193. return 512;
  194. }
  195. /**
  196. * Returns the internal frame associated with this object.
  197. *
  198. * @return The internal frame associated with this object
  199. */
  200. @Override
  201. public InputWindow getFrame() {
  202. return myWindow;
  203. }
  204. /**
  205. * Returns the content pane of the internal frame associated with this object.
  206. *
  207. * @return The content pane of the internal frame associated with this object
  208. */
  209. public Container getContentPane() {
  210. return ((TextFrame)getFrame()).getContentPane();
  211. }
  212. /**
  213. * Returns a string identifier for this object/its frame.
  214. *
  215. * @return String identifier
  216. */
  217. @Override
  218. public String toString() {
  219. return title;
  220. }
  221. /**
  222. * Returns the server instance associated with this container.
  223. *
  224. * @return the associated server connection
  225. */
  226. @Override
  227. public Server getServer() {
  228. return null;
  229. }
  230. /**
  231. * Is the window closing?
  232. *
  233. * @return True if windowClosing has been called.
  234. */
  235. public final boolean isWindowClosing() {
  236. return windowClosing;
  237. }
  238. /** {@inheritDoc} */
  239. @Override
  240. public void windowClosing() {
  241. windowClosing = true;
  242. // 1: Make the window non-visible
  243. myWindow.setVisible(false);
  244. // 2: Remove any callbacks or listeners
  245. // 3: Trigger any actions neccessary
  246. // 4: Trigger action for the window closing
  247. // 5: Inform any parents that the window is closing
  248. plugin.delWindow(this);
  249. // 6: Remove the window from the window manager
  250. WindowManager.removeWindow(myWindow);
  251. // 7: Remove any references to the window and parents
  252. myWindow = null; // NOPMD
  253. }
  254. }