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.

ActionModel.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2006-2011 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.actions;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.Precondition;
  25. import com.dmdirc.ServerManager;
  26. import com.dmdirc.WritableFrameContainer;
  27. import com.dmdirc.actions.interfaces.ActionType;
  28. import com.dmdirc.commandparser.parsers.CommandParser;
  29. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  30. import com.dmdirc.ui.WindowManager;
  31. import com.dmdirc.ui.interfaces.Window;
  32. import java.util.ArrayList;
  33. import java.util.Arrays;
  34. import java.util.List;
  35. /**
  36. * Represents the basic model of an action, and its triggering mechanism.
  37. * Saving and loading are handled by the Action class.
  38. *
  39. * @author chris
  40. */
  41. public class ActionModel {
  42. /** The group this action belongs to. */
  43. protected String group;
  44. /** The name of this action. */
  45. protected String name;
  46. /** The ActionTypes that trigger this action. */
  47. protected ActionType[] triggers;
  48. /** The commands to execute if this action is triggered. */
  49. protected String[] response;
  50. /** The change that should be made to the format string, if any. */
  51. protected String newFormat;
  52. /** The conditions for this action. */
  53. protected List<ActionCondition> conditions = new ArrayList<ActionCondition>();
  54. /** The condition tree used for evaluating conditions. */
  55. protected ConditionTree conditionTree;
  56. /** Whether this action has been modified or not. */
  57. protected boolean modified;
  58. /** Whether this action wishes the event to be stopped. */
  59. protected boolean stop;
  60. /** The concurrency group this action belongs to, if any. */
  61. protected String concurrencyGroup;
  62. /**
  63. * Creates a new instance of ActionModel with the specified properties.
  64. *
  65. * @param group The group the action belongs to
  66. * @param name The name of the action
  67. */
  68. public ActionModel(final String group, final String name) {
  69. this.group = group;
  70. this.name = name;
  71. }
  72. /**
  73. * Creates a new instance of ActionModel with the specified properties.
  74. *
  75. * @param group The group the action belongs to
  76. * @param name The name of the action
  77. * @param triggers The triggers to use
  78. * @param response The response to use
  79. * @param conditions The conditions to use
  80. * @param conditionTree The condition tree to use
  81. * @param newFormat The new formatter to use
  82. */
  83. public ActionModel(final String group, final String name,
  84. final ActionType[] triggers, final String[] response,
  85. final List<ActionCondition> conditions,
  86. final ConditionTree conditionTree, final String newFormat) {
  87. this.group = group;
  88. this.name = name;
  89. this.triggers = triggers.clone();
  90. this.response = response.clone();
  91. this.conditions = conditions;
  92. this.conditionTree = conditionTree;
  93. this.newFormat = newFormat;
  94. this.modified = true;
  95. }
  96. /**
  97. * Triggers this action.
  98. *
  99. * @param format The format of the message that's going to be displayed.
  100. * @param arguments The arguments from the action that caused this trigger.
  101. * @return True if the execution of the event should be stopped, or false
  102. * if the event may continue
  103. */
  104. @Precondition({
  105. "This action has at least one trigger",
  106. "This action's primary trigger is non-null"
  107. })
  108. public boolean trigger(final StringBuffer format,
  109. final Object... arguments) {
  110. assert(triggers.length > 0);
  111. assert(triggers[0] != null);
  112. final ActionSubstitutor sub = new ActionSubstitutor(triggers[0]);
  113. if (!test(sub, arguments)) {
  114. return true;
  115. }
  116. final FrameContainer<?> active = WindowManager.getActiveWindow();
  117. Window window = null;
  118. WritableFrameContainer<?> cw = null;
  119. CommandParser cp = null;
  120. if (arguments.length > 0
  121. && arguments[0] instanceof WritableFrameContainer<?>) {
  122. cw = (WritableFrameContainer<?>) arguments[0];
  123. } else if (active instanceof WritableFrameContainer<?>) {
  124. cw = (WritableFrameContainer<?>) active;
  125. } else if (ServerManager.getServerManager().numServers() > 0) {
  126. cw = ServerManager.getServerManager().getServers().get(0);
  127. }
  128. if (arguments.length > 0 && arguments[0] instanceof Window) {
  129. window = (Window) arguments[0];
  130. } else if (cw != null) {
  131. window = cw.getFrame();
  132. }
  133. if (cw == null) {
  134. cp = GlobalCommandParser.getGlobalCommandParser();
  135. } else {
  136. cp = cw.getCommandParser();
  137. }
  138. for (String command : response) {
  139. cp.parseCommand(cw, window, sub.doSubstitution(command, arguments));
  140. }
  141. if (newFormat != null && format != null) {
  142. format.setLength(0);
  143. format.append(newFormat);
  144. }
  145. return stop;
  146. }
  147. /**
  148. * Tests to see if this action should be triggered or not.
  149. *
  150. * @param sub The ActionsSubstitutor to use to substitute args
  151. * @param arguments The arguments for the action event
  152. * @return True if the action should be executed, false otherwise
  153. */
  154. public boolean test(final ActionSubstitutor sub,
  155. final Object ... arguments) {
  156. final boolean[] results = new boolean[conditions.size()];
  157. int i = 0;
  158. for (ActionCondition condition : conditions) {
  159. results[i++] = condition.test(sub, arguments);
  160. }
  161. return getRealConditionTree().evaluate(results);
  162. }
  163. /**
  164. * Retrieves a list of this action's conditions.
  165. *
  166. * @return A list of this action's conditions
  167. */
  168. public List<ActionCondition> getConditions() {
  169. return conditions;
  170. }
  171. /**
  172. * Sets this action's conditions.
  173. *
  174. * @param conditions A list of conditions to use
  175. */
  176. public void setConditions(final List<ActionCondition> conditions) {
  177. this.conditions = conditions;
  178. this.modified = true;
  179. }
  180. /**
  181. * Retrieves this action's triggers.
  182. *
  183. * @return The triggers used by this action
  184. */
  185. public ActionType[] getTriggers() {
  186. return triggers == null ? triggers : triggers.clone();
  187. }
  188. /**
  189. * Sets this action's triggers.
  190. *
  191. * @param triggers The new triggers to use
  192. */
  193. public void setTriggers(final ActionType[] triggers) {
  194. this.triggers = triggers.clone();
  195. this.modified = true;
  196. }
  197. /**
  198. * Retrieves this action's new format setting.
  199. *
  200. * @return The format that this action will use, or null if no change
  201. */
  202. public String getNewFormat() {
  203. return newFormat;
  204. }
  205. /**
  206. * Sets this action's new format setting.
  207. *
  208. * @param newFormat The new 'new format' setting
  209. */
  210. public void setNewFormat(final String newFormat) {
  211. this.newFormat = newFormat;
  212. this.modified = true;
  213. }
  214. /**
  215. * Retrieves this action's response.
  216. *
  217. * @return The commands that will be executed if this action is triggered
  218. */
  219. public String[] getResponse() {
  220. return response == null ? response : response.clone();
  221. }
  222. /**
  223. * Sets this action's response.
  224. *
  225. * @param response The new response to use
  226. */
  227. public void setResponse(final String[] response) {
  228. this.response = response.clone();
  229. this.modified = true;
  230. }
  231. /**
  232. * Retrieves this action's group name.
  233. *
  234. * @return This action's group name
  235. */
  236. public String getGroup() {
  237. return group;
  238. }
  239. /**
  240. * Sets the group of this action.
  241. *
  242. * @param newGroup The new group for this action
  243. */
  244. public void setGroup(final String newGroup) {
  245. this.group = newGroup;
  246. this.modified = true;
  247. }
  248. /**
  249. * Retrieves this action's name.
  250. *
  251. * @return This action's name
  252. */
  253. public String getName() {
  254. return name;
  255. }
  256. /**
  257. * Sets the name of this action.
  258. *
  259. * @param newName The new name for this action
  260. */
  261. public void setName(final String newName) {
  262. this.name = newName;
  263. this.modified = true;
  264. }
  265. /**
  266. * Retrieves the condition tree used for this action. Condition trees may
  267. * be null, in which case the arguments are conjoined together.
  268. *
  269. * @return This action's condition tree
  270. */
  271. public ConditionTree getConditionTree() {
  272. return conditionTree;
  273. }
  274. /**
  275. * Retrieves a concrete condition tree used for this action. If there is
  276. * no condition tree defined for this action, returns a conjunction tree
  277. * for the arguments.
  278. *
  279. * @since 0.6
  280. * @return A {@link ConditionTree} object for this action
  281. */
  282. public ConditionTree getRealConditionTree() {
  283. return conditionTree == null ? ConditionTree.createConjunction(
  284. conditions.size()) : conditionTree;
  285. }
  286. /**
  287. * Sets the condition tree used for this action.
  288. *
  289. * @param conditionTree The new condition tree to be used
  290. */
  291. public void setConditionTree(final ConditionTree conditionTree) {
  292. this.conditionTree = conditionTree;
  293. this.modified = true;
  294. }
  295. /**
  296. * Retrieves the concurrency group of this action.
  297. *
  298. * @return This action's concurrency group
  299. * @since 0.6.3
  300. */
  301. public String getConcurrencyGroup() {
  302. return concurrencyGroup;
  303. }
  304. /**
  305. * Sets the concurrency group of this action.
  306. *
  307. * @param concurrencyGroup This action's new concurrency group
  308. * @since 0.6.3
  309. */
  310. public void setConcurrencyGroup(final String concurrencyGroup) {
  311. this.concurrencyGroup = (concurrencyGroup == null
  312. || concurrencyGroup.isEmpty()) ? null : concurrencyGroup;
  313. }
  314. /**
  315. * Determines whether or not this action will stop the event execution
  316. * if it is triggered.
  317. *
  318. * @return The stopping preference of this action
  319. * @see #setStopping(boolean)
  320. * @since 0.6.4
  321. */
  322. public boolean isStopping() {
  323. return stop;
  324. }
  325. /**
  326. * Sets the stopping preference of this action. If the stopping preference
  327. * is <code>true</code> then when this action is successfully triggered,
  328. * it will request that execution of the event is stopped. This will
  329. * prevent the default behaviour of the callee being executed.
  330. *
  331. * @param stop The new stopping preference of this action
  332. * @see #isStopping()
  333. * @since 0.6.4
  334. */
  335. public void setStopping(final boolean stop) {
  336. this.stop = stop;
  337. }
  338. /**
  339. * Determine if this model has been modified since it was constructed or
  340. * its modified status was reset.
  341. *
  342. * @return True if this model has been modified, false otherwise
  343. */
  344. public boolean isModified() {
  345. return modified;
  346. }
  347. /**
  348. * Resets the modified status of this model. After a call to
  349. * resetModified(), this model will report that it has not been modified,
  350. * until one of the set* methods is used.
  351. */
  352. public void resetModified() {
  353. this.modified = false;
  354. }
  355. /** {@inheritDoc} */
  356. @Override
  357. public String toString() {
  358. return "[name=" + group + "/" + name + ", triggers="
  359. + Arrays.toString(triggers) + ", response="
  360. + Arrays.toString(response) + ", "
  361. + conditions + ", format='" + newFormat + "']";
  362. }
  363. }