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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2006-2007 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.WritableFrameContainer;
  24. import com.dmdirc.config.ConfigManager;
  25. import com.dmdirc.config.IdentityManager;
  26. import com.dmdirc.ui.swing.components.InputTextFrame;
  27. import com.dmdirc.Main;
  28. import com.dmdirc.commandparser.CommandManager;
  29. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  30. import com.dmdirc.commandparser.parsers.CommandParser;
  31. import com.dmdirc.commandparser.commands.Command;
  32. import com.dmdirc.commandparser.commands.GlobalCommand;
  33. import com.dmdirc.IconManager;
  34. import com.dmdirc.Server;
  35. import com.dmdirc.config.ConfigManager;
  36. import com.dmdirc.config.IdentityManager;
  37. import com.dmdirc.ui.WindowManager;
  38. import com.dmdirc.ui.interfaces.InputWindow;
  39. /**
  40. * This class links DCC objects to a window.
  41. *
  42. * @author Shane 'Dataforce' McCormack
  43. * @version $Id: DCC.java 969 2007-04-30 18:38:20Z ShaneMcC $
  44. */
  45. public abstract class DCCFrame extends WritableFrameContainer {
  46. /**
  47. * Empty Frame.
  48. */
  49. private class EmptyFrame extends InputTextFrame {
  50. /** A version number for this class. */
  51. private static final long serialVersionUID = 200711271;
  52. /**
  53. * Creates a new instance of EmptyFrame.
  54. *
  55. * @param owner The frame container that owns this frame
  56. */
  57. public EmptyFrame(final WritableFrameContainer owner) {
  58. super(owner);
  59. setTextPane(null);
  60. pack();
  61. }
  62. /**
  63. * Retrieves the command Parser for this input window.
  64. *
  65. * @return This window's command parser
  66. */
  67. public final CommandParser getCommandParser() {
  68. return GlobalCommandParser.getGlobalCommandParser();
  69. }
  70. }
  71. /**
  72. * DCC CommandParser
  73. */
  74. private static class DCCCommandParser extends CommandParser {
  75. /** A version number for this class. */
  76. private static final long serialVersionUID = 200711271;
  77. /** Loads the relevant commands into the parser. */
  78. protected void loadCommands() { CommandManager.loadGlobalCommands(this); }
  79. /**
  80. * Executes the specified command with the given arguments.
  81. *
  82. * @param origin The window in which the command was typed
  83. * @param isSilent Whether the command is being silenced or not
  84. * @param command The command to be executed
  85. * @param args The arguments to the command
  86. */
  87. protected void executeCommand(final InputWindow origin, final boolean isSilent, final Command command, final String... args) {
  88. ((GlobalCommand) command).execute(origin, isSilent, args);
  89. }
  90. /**
  91. * Called when the input was a line of text that was not a command.
  92. * This normally means it is sent to the server/channel/user as-is, with
  93. * no further processing.
  94. *
  95. * @param origin The window in which the command was typed
  96. * @param line The line input by the user
  97. */
  98. protected void handleNonCommand(final InputWindow origin, final String line) {
  99. if (origin.getContainer() instanceof WritableFrameContainer) {
  100. ((WritableFrameContainer)origin.getContainer()).sendLine(line);
  101. }
  102. }
  103. }
  104. /** The singleton instance of the DCC command parser. */
  105. private static DCCCommandParser myDCCCommandParser;
  106. /** The window title. */
  107. protected final String title;
  108. /** The Window we're using. */
  109. protected InputWindow myWindow = null;
  110. /** The dcc plugin that owns this frame */
  111. protected final DCCPlugin plugin;
  112. /**
  113. * Creates a new instance of DCCFrame with an empty window.
  114. *
  115. * @param plugin The DCCPlugin that owns this frame
  116. * @param title The title of this window
  117. */
  118. public DCCFrame(final DCCPlugin plugin, final String title) {
  119. this(plugin, title, true);
  120. }
  121. /**
  122. * Creates a new instance of DCCFrame.
  123. *
  124. * @param plugin The DCCPlugin that owns this frame
  125. * @param title The title of this window
  126. * @param defaultWindow Create default (empty) window.
  127. */
  128. public DCCFrame(final DCCPlugin plugin, final String title, final boolean defaultWindow) {
  129. super();
  130. this.title = title;
  131. this.plugin = plugin;
  132. icon = IconManager.getIconManager().getIcon("raw");
  133. if (defaultWindow) {
  134. myWindow = new EmptyFrame(this);
  135. myWindow.setTitle(title);
  136. myWindow.setFrameIcon(icon);
  137. myWindow.setVisible(true);
  138. }
  139. }
  140. /**
  141. * Retrieves a singleton instance of the DCC command parser.
  142. *
  143. * @return A DCCCommandParser
  144. */
  145. public static synchronized DCCCommandParser getDCCCommandParser() {
  146. if (myDCCCommandParser == null) {
  147. myDCCCommandParser = new DCCCommandParser();
  148. }
  149. return myDCCCommandParser;
  150. }
  151. /**
  152. * Sends a line of text to this container's source.
  153. *
  154. * @param line The line to be sent
  155. */
  156. @Override
  157. public void sendLine(final String line) {
  158. return;
  159. }
  160. /**
  161. * Returns the maximum length that a line passed to sendLine() should be,
  162. * in order to prevent it being truncated or causing protocol violations.
  163. *
  164. * @return The maximum line length for this container
  165. */
  166. @Override
  167. public int getMaxLineLength() {
  168. return 512;
  169. }
  170. /**
  171. * Returns the internal frame associated with this object.
  172. *
  173. * @return The internal frame associated with this object
  174. */
  175. @Override
  176. public InputWindow getFrame() {
  177. return myWindow;
  178. }
  179. /**
  180. * Returns a string identifier for this object/its frame.
  181. *
  182. * @return String identifier
  183. */
  184. @Override
  185. public String toString() {
  186. return title;
  187. }
  188. /**
  189. * Retrieves the config manager for this command window.
  190. *
  191. * @return This window's config manager
  192. */
  193. @Override
  194. public ConfigManager getConfigManager() {
  195. return IdentityManager.getGlobalConfig();
  196. }
  197. /**
  198. * Returns the server instance associated with this container.
  199. *
  200. * @return the associated server connection
  201. */
  202. @Override
  203. public Server getServer() {
  204. return null;
  205. }
  206. /**
  207. * Closes this container (and it's associated frame).
  208. */
  209. @Override
  210. public void close() {
  211. plugin.delWindow(this);
  212. myWindow.setVisible(false);
  213. Main.getUI().getMainWindow().delChild(myWindow);
  214. WindowManager.removeWindow(myWindow);
  215. }
  216. }