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.

WritableFrameContainer.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2006-2014 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;
  23. import com.dmdirc.actions.ActionManager;
  24. import com.dmdirc.commandparser.parsers.CommandParser;
  25. import com.dmdirc.interfaces.actions.ActionType;
  26. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  27. import com.dmdirc.messages.MessageSinkManager;
  28. import com.dmdirc.parser.common.CompositionState;
  29. import com.dmdirc.ui.input.TabCompleter;
  30. import com.dmdirc.util.URLBuilder;
  31. import java.util.ArrayList;
  32. import java.util.Collection;
  33. import java.util.Date;
  34. import java.util.List;
  35. /**
  36. * The writable frame container adds additional methods to the frame container class that allow the
  37. * sending of lines back to whatever the container's data source is (e.g. an IRC channel or server).
  38. */
  39. public abstract class WritableFrameContainer extends FrameContainer {
  40. /** The name of the server notification target. */
  41. protected static final String NOTIFICATION_SERVER = "server";
  42. /** The name of the channel notification target. */
  43. protected static final String NOTIFICATION_CHANNEL = "channel";
  44. /** The command parser used for commands in this container. */
  45. protected final CommandParser commandParser;
  46. /** The manager to use to despatch messages to sinks. */
  47. private final MessageSinkManager messageSinkManager;
  48. /**
  49. * Creates a new WritableFrameContainer.
  50. *
  51. * @param icon The icon to use for this container
  52. * @param name The name of this container
  53. * @param title The title of this container
  54. * @param config The config manager for this container
  55. * @param parser The command parser for this container
  56. * @param messageSinkManager The sink manager to use to despatch messages
  57. * @param urlBuilder The URL builder to use when finding icons.
  58. * @param components The UI components that this frame requires
  59. *
  60. * @since 0.6.4
  61. */
  62. public WritableFrameContainer(
  63. final String icon,
  64. final String name,
  65. final String title,
  66. final AggregateConfigProvider config,
  67. final CommandParser parser,
  68. final MessageSinkManager messageSinkManager,
  69. final URLBuilder urlBuilder,
  70. final Collection<String> components) {
  71. super(icon, name, title, config, urlBuilder, components);
  72. this.commandParser = parser;
  73. this.messageSinkManager = messageSinkManager;
  74. parser.setOwner(this);
  75. }
  76. /**
  77. * Sends a line of text to this container's source.
  78. *
  79. * @param line The line to be sent
  80. */
  81. public abstract void sendLine(String line);
  82. /**
  83. * Retrieves the command parser to be used for this container.
  84. *
  85. * @return This container's command parser
  86. */
  87. public CommandParser getCommandParser() {
  88. return commandParser;
  89. }
  90. /**
  91. * Retrieves the tab completer which should be used for this cotnainer.
  92. *
  93. * @return This container's tab completer
  94. */
  95. public abstract TabCompleter getTabCompleter();
  96. /**
  97. * Returns the maximum length that a line passed to sendLine() should be, in order to prevent it
  98. * being truncated or causing protocol violations.
  99. *
  100. * @return The maximum line length for this container
  101. */
  102. public abstract int getMaxLineLength();
  103. /**
  104. * Splits the specified line into chunks that contain a number of bytes less than or equal to
  105. * the value returned by {@link #getMaxLineLength()}.
  106. *
  107. * @param line The line to be split
  108. *
  109. * @return An ordered list of chunks of the desired length
  110. */
  111. protected List<String> splitLine(final String line) {
  112. final List<String> result = new ArrayList<>();
  113. if (line.indexOf('\n') > -1) {
  114. for (String part : line.split("\n")) {
  115. result.addAll(splitLine(part));
  116. }
  117. } else {
  118. final StringBuilder remaining = new StringBuilder(line);
  119. while (getMaxLineLength() > -1 && remaining.toString().getBytes().length
  120. > getMaxLineLength()) {
  121. int number = Math.min(remaining.length(), getMaxLineLength());
  122. while (remaining.substring(0, number).getBytes().length > getMaxLineLength()) {
  123. number--;
  124. }
  125. result.add(remaining.substring(0, number));
  126. remaining.delete(0, number);
  127. }
  128. result.add(remaining.toString());
  129. }
  130. return result;
  131. }
  132. /**
  133. * Returns the number of lines that the specified string would be sent as.
  134. *
  135. * @param line The string to be split and sent
  136. *
  137. * @return The number of lines required to send the specified string
  138. */
  139. public final int getNumLines(final String line) {
  140. final String[] splitLines = line.split("(\n|\r\n|\r)", Integer.MAX_VALUE);
  141. int lines = 0;
  142. for (String splitLine : splitLines) {
  143. if (getMaxLineLength() <= 0) {
  144. lines++;
  145. } else {
  146. lines += (int) Math.ceil(splitLine.getBytes().length
  147. / (double) getMaxLineLength());
  148. }
  149. }
  150. return lines;
  151. }
  152. /**
  153. * Processes and displays a notification.
  154. *
  155. * @param messageType The name of the formatter to be used for the message
  156. * @param actionType The action type to be used
  157. * @param args The arguments for the message
  158. *
  159. * @return True if any further behaviour should be executed, false otherwise
  160. */
  161. public boolean doNotification(final String messageType,
  162. final ActionType actionType, final Object... args) {
  163. return doNotification(new Date(), messageType, actionType, args);
  164. }
  165. /**
  166. * Processes and displays a notification.
  167. *
  168. * @param date The date/time at which the event occured
  169. * @param messageType The name of the formatter to be used for the message
  170. * @param actionType The action type to be used
  171. * @param args The arguments for the message
  172. *
  173. * @return True if any further behaviour should be executed, false otherwise
  174. */
  175. public boolean doNotification(final Date date, final String messageType,
  176. final ActionType actionType, final Object... args) {
  177. final List<Object> messageArgs = new ArrayList<>();
  178. final List<Object> actionArgs = new ArrayList<>();
  179. final StringBuffer buffer = new StringBuffer(messageType);
  180. actionArgs.add(this);
  181. for (Object arg : args) {
  182. actionArgs.add(arg);
  183. if (!processNotificationArg(arg, messageArgs)) {
  184. messageArgs.add(arg);
  185. }
  186. }
  187. modifyNotificationArgs(actionArgs, messageArgs);
  188. final boolean res = ActionManager.getActionManager().triggerEvent(
  189. actionType, buffer, actionArgs.toArray());
  190. handleNotification(date, buffer.toString(), messageArgs.toArray());
  191. return res;
  192. }
  193. /**
  194. * Allows subclasses to modify the lists of arguments for notifications.
  195. *
  196. * @param actionArgs The list of arguments to be passed to the actions system
  197. * @param messageArgs The list of arguments to be passed to the formatter
  198. */
  199. protected void modifyNotificationArgs(final List<Object> actionArgs,
  200. final List<Object> messageArgs) {
  201. // Do nothing
  202. }
  203. /**
  204. * Allows subclasses to process specific types of notification arguments.
  205. *
  206. * @param arg The argument to be processed
  207. * @param args The list of arguments that any data should be appended to
  208. *
  209. * @return True if the arg has been processed, false otherwise
  210. */
  211. protected boolean processNotificationArg(final Object arg, final List<Object> args) {
  212. return false;
  213. }
  214. /**
  215. * Handles general server notifications (i.e., ones not tied to a specific window). The user can
  216. * select where the notifications should go in their config.
  217. *
  218. * @param messageType The type of message that is being sent
  219. * @param args The arguments for the message
  220. */
  221. public void handleNotification(final String messageType, final Object... args) {
  222. handleNotification(new Date(), messageType, args);
  223. }
  224. /**
  225. * Handles general server notifications (i.e., ones not tied to a specific window). The user can
  226. * select where the notifications should go in their config.
  227. *
  228. * @param date The date/time at which the event occured
  229. * @param messageType The type of message that is being sent
  230. * @param args The arguments for the message
  231. */
  232. public void handleNotification(final Date date, final String messageType, final Object... args) {
  233. messageSinkManager.despatchMessage(this, date, messageType, args);
  234. }
  235. /**
  236. * Sets the composition state for the local user for this chat.
  237. *
  238. * @param state The new composition state
  239. */
  240. public void setCompositionState(final CompositionState state) {
  241. // Default implementation does nothing. Subclasses that support
  242. // composition should override this.
  243. }
  244. }