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

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