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.

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