Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ActionModel.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.actions.interfaces.ActionType;
  24. import com.dmdirc.Main;
  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.interfaces.InputWindow;
  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. /**
  59. * Creates a new instance of ActionModel with the specified properties.
  60. *
  61. * @param group The group the action belongs to
  62. * @param name The name of the action
  63. */
  64. public ActionModel(final String group, final String name) {
  65. this.group = group;
  66. this.name = name;
  67. }
  68. /**
  69. * Creates a new instance of ActionModel with the specified properties.
  70. *
  71. * @param group The group the action belongs to
  72. * @param name The name of the action
  73. * @param triggers The triggers to use
  74. * @param response The response to use
  75. * @param conditions The conditions to use
  76. * @param conditionTree The condition tree to use
  77. * @param newFormat The new formatter to use
  78. */
  79. public ActionModel(final String group, final String name,
  80. final ActionType[] triggers, final String[] response,
  81. final List<ActionCondition> conditions,
  82. final ConditionTree conditionTree, final String newFormat) {
  83. this.group = group;
  84. this.name = name;
  85. this.triggers = triggers.clone();
  86. this.response = response.clone();
  87. this.conditions = conditions;
  88. this.conditionTree = conditionTree;
  89. this.newFormat = newFormat;
  90. this.modified = true;
  91. }
  92. /**
  93. * Triggers this action.
  94. *
  95. * @param format The format of the message that's going to be displayed.
  96. * @param arguments The arguments from the action that caused this trigger.
  97. */
  98. @Precondition({
  99. "This action has at least one trigger",
  100. "This action's primary trigger is non-null"
  101. })
  102. public void trigger(final StringBuffer format, final Object... arguments) {
  103. assert(triggers.length > 0);
  104. assert(triggers[0] != null);
  105. final ActionSubstitutor sub = new ActionSubstitutor(triggers[0]);
  106. if (!test(sub, arguments)) {
  107. return;
  108. }
  109. final Window active = Main.getUI().getActiveWindow();
  110. InputWindow cw = null;
  111. CommandParser cp = null;
  112. if (arguments.length > 0 && arguments[0] instanceof WritableFrameContainer) {
  113. cw = ((WritableFrameContainer) arguments[0]).getFrame();
  114. } else if (active instanceof InputWindow) {
  115. cw = (InputWindow) active;
  116. } else if (ServerManager.getServerManager().numServers() > 0) {
  117. cw = ServerManager.getServerManager().getServers().get(0).getFrame();
  118. }
  119. if (cw == null) {
  120. cp = GlobalCommandParser.getGlobalCommandParser();
  121. } else {
  122. cp = cw.getCommandParser();
  123. }
  124. for (String command : response) {
  125. cp.parseCommand(cw, sub.doSubstitution(command, arguments));
  126. }
  127. if (newFormat != null && format != null) {
  128. format.setLength(0);
  129. format.append(newFormat);
  130. }
  131. }
  132. /**
  133. * Tests to see if this action should be triggered or not.
  134. *
  135. * @param sub The ActionsSubstitutor to use to substitute args
  136. * @param arguments The arguments for the action event
  137. * @return True if the action should be executed, false otherwise
  138. */
  139. public boolean test(final ActionSubstitutor sub, final Object ... arguments) {
  140. final boolean[] results = new boolean[conditions.size()];
  141. int i = 0;
  142. for (ActionCondition condition : conditions) {
  143. results[i++] = condition.test(sub, arguments);
  144. }
  145. return getRealConditionTree().evaluate(results);
  146. }
  147. /**
  148. * Retrieves a list of this action's conditions.
  149. *
  150. * @return A list of this action's conditions
  151. */
  152. public List<ActionCondition> getConditions() {
  153. return conditions;
  154. }
  155. /**
  156. * Sets this action's conditions.
  157. *
  158. * @param conditions A list of conditions to use
  159. */
  160. public void setConditions(final List<ActionCondition> conditions) {
  161. this.conditions = conditions;
  162. this.modified = true;
  163. }
  164. /**
  165. * Retrieves this action's triggers.
  166. *
  167. * @return The triggers used by this action
  168. */
  169. public ActionType[] getTriggers() {
  170. return triggers == null ? triggers : triggers.clone();
  171. }
  172. /**
  173. * Sets this action's triggers.
  174. *
  175. * @param triggers The new triggers to use
  176. */
  177. public void setTriggers(final ActionType[] triggers) {
  178. this.triggers = triggers.clone();
  179. this.modified = true;
  180. }
  181. /**
  182. * Retrieves this action's new format setting.
  183. *
  184. * @return The format that this action will use, or null if no change
  185. */
  186. public String getNewFormat() {
  187. return newFormat;
  188. }
  189. /**
  190. * Sets this action's new format setting.
  191. *
  192. * @param newFormat The new 'new format' setting
  193. */
  194. public void setNewFormat(final String newFormat) {
  195. this.newFormat = newFormat;
  196. this.modified = true;
  197. }
  198. /**
  199. * Retrieves this action's response.
  200. *
  201. * @return The commands that will be executed if this action is triggered
  202. */
  203. public String[] getResponse() {
  204. return response == null ? response : response.clone();
  205. }
  206. /**
  207. * Sets this action's response.
  208. *
  209. * @param response The new response to use
  210. */
  211. public void setResponse(final String[] response) {
  212. this.response = response.clone();
  213. this.modified = true;
  214. }
  215. /**
  216. * Retrieves this action's group name.
  217. *
  218. * @return This action's group name
  219. */
  220. public String getGroup() {
  221. return group;
  222. }
  223. /**
  224. * Sets the group of this action.
  225. *
  226. * @param newGroup The new group for this action
  227. */
  228. public void setGroup(final String newGroup) {
  229. this.group = newGroup;
  230. this.modified = true;
  231. }
  232. /**
  233. * Retrieves this action's name.
  234. *
  235. * @return This action's name
  236. */
  237. public String getName() {
  238. return name;
  239. }
  240. /**
  241. * Sets the name of this action.
  242. *
  243. * @param newName The new name for this action
  244. */
  245. public void setName(final String newName) {
  246. this.name = newName;
  247. this.modified = true;
  248. }
  249. /**
  250. * Retrieves the condition tree used for this action. Condition trees may
  251. * be null, in which case the arguments are conjoined together.
  252. *
  253. * @return This action's condition tree
  254. */
  255. public ConditionTree getConditionTree() {
  256. return conditionTree;
  257. }
  258. /**
  259. * Retrieves a concrete condition tree used for this action. If there is
  260. * no condition tree defined for this action, returns a conjunction tree
  261. * for the arguments.
  262. *
  263. * @since 0.6
  264. * @return A {@link ConditionTree} object for this action
  265. */
  266. public ConditionTree getRealConditionTree() {
  267. return conditionTree == null
  268. ? ConditionTree.createConjunction(conditions.size()) : conditionTree;
  269. }
  270. /**
  271. * Sets the condition tree used for this action.
  272. *
  273. * @param conditionTree The new condition tree to be used
  274. */
  275. public void setConditionTree(final ConditionTree conditionTree) {
  276. this.conditionTree = conditionTree;
  277. this.modified = true;
  278. }
  279. /**
  280. * Determine if this model has been modified since it was constructed or
  281. * its modified status was reset.
  282. *
  283. * @return True if this model has been modified, false otherwise
  284. */
  285. public boolean isModified() {
  286. return modified;
  287. }
  288. /**
  289. * Resets the modified status of this model. After a call to
  290. * resetModified(), this model will report that it has not been modified,
  291. * until one of the set* methods is used.
  292. */
  293. public void resetModified() {
  294. this.modified = false;
  295. }
  296. /** {@inheritDoc} */
  297. @Override
  298. public String toString() {
  299. return "[name=" + group + "/" + name + ", triggers="
  300. + Arrays.toString(triggers) + ", response="
  301. + Arrays.toString(response) + ", "
  302. + conditions + ", format='" + newFormat + "']";
  303. }
  304. }