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.

ActionManager.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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.Main;
  24. import com.dmdirc.Precondition;
  25. import com.dmdirc.actions.interfaces.ActionComparison;
  26. import com.dmdirc.actions.interfaces.ActionComponent;
  27. import com.dmdirc.actions.interfaces.ActionType;
  28. import com.dmdirc.actions.internal.WhoisNumericFormatter;
  29. import com.dmdirc.actions.wrappers.AliasWrapper;
  30. import com.dmdirc.actions.wrappers.PerformWrapper;
  31. import com.dmdirc.config.IdentityManager;
  32. import com.dmdirc.interfaces.ActionListener;
  33. import com.dmdirc.interfaces.ConfigChangeListener;
  34. import com.dmdirc.logger.ErrorLevel;
  35. import com.dmdirc.logger.Logger;
  36. import com.dmdirc.updater.components.ActionGroupComponent;
  37. import com.dmdirc.util.MapList;
  38. import com.dmdirc.util.resourcemanager.ZipResourceManager;
  39. import java.io.File;
  40. import java.io.IOException;
  41. import java.util.ArrayList;
  42. import java.util.HashMap;
  43. import java.util.List;
  44. import java.util.Map;
  45. /**
  46. * Manages all actions for the client.
  47. *
  48. * @author chris
  49. */
  50. public final class ActionManager {
  51. /** A list of registered action types. */
  52. private static final List<ActionType> ACTION_TYPES
  53. = new ArrayList<ActionType>();
  54. /** A list of registered action components. */
  55. private static final List<ActionComponent> ACTION_COMPONENTS
  56. = new ArrayList<ActionComponent>();
  57. /** A list of registered action comparisons. */
  58. private static final List<ActionComparison> ACTION_COMPARISON
  59. = new ArrayList<ActionComparison>();
  60. /** A map linking types and a list of actions that're registered for them. */
  61. private static final MapList<ActionType, Action> ACTIONS
  62. = new MapList<ActionType, Action>();
  63. /** A map linking groups and a list of actions that're in them. */
  64. private static final Map<String, ActionGroup> GROUPS
  65. = new HashMap<String, ActionGroup>();
  66. /** A map of objects to synchronise on for concurrency groups. */
  67. private static final Map<String, Object> LOCKS
  68. = new HashMap<String, Object>();
  69. /** A map of the action type groups to the action types within. */
  70. private static final MapList<String, ActionType> ACTIONTYPE_GROUPS
  71. = new MapList<String, ActionType>();
  72. /** The listeners that we have registered. */
  73. private static final MapList<ActionType, ActionListener> LISTENERS
  74. = new MapList<ActionType, ActionListener>();
  75. /** Indicates whether or not user actions should be killed (not processed). */
  76. private static boolean killSwitch
  77. = IdentityManager.getGlobalConfig().getOptionBool("actions", "killswitch");
  78. /** Creates a new instance of ActionManager. */
  79. private ActionManager() {
  80. // Shouldn't be instansiated
  81. }
  82. /**
  83. * Initialises the action manager.
  84. */
  85. public static void init() {
  86. registerActionTypes(CoreActionType.values());
  87. registerActionComparisons(CoreActionComparison.values());
  88. registerActionComponents(CoreActionComponent.values());
  89. registerGroup(AliasWrapper.getAliasWrapper());
  90. registerGroup(PerformWrapper.getPerformWrapper());
  91. new WhoisNumericFormatter(IdentityManager.getAddonIdentity()).register();
  92. // Register a listener for the closing event, so we can save actions
  93. addListener(new ActionListener() {
  94. /** {@inheritDoc} */
  95. @Override
  96. public void processEvent(final ActionType type, final StringBuffer format,
  97. final Object... arguments) {
  98. saveActions();
  99. }
  100. }, CoreActionType.CLIENT_CLOSED);
  101. // Make sure we listen for the killswitch
  102. IdentityManager.getGlobalConfig().addChangeListener("actions", "killswitch",
  103. new ConfigChangeListener() {
  104. /** {@inheritDoc} */
  105. @Override
  106. public void configChanged(final String domain, final String key) {
  107. killSwitch = IdentityManager.getGlobalConfig().getOptionBool(
  108. "actions", "killswitch");
  109. }
  110. });
  111. }
  112. /**
  113. * Saves all actions.
  114. */
  115. public static void saveActions() {
  116. for (ActionGroup group : GROUPS.values()) {
  117. for (Action action : group) {
  118. action.save();
  119. }
  120. }
  121. }
  122. /**
  123. * Registers the specified default setting for actions.
  124. *
  125. * @param name The name of the setting to be registered
  126. * @param value The default value for the setting
  127. */
  128. public static void registerDefault(final String name, final String value) {
  129. IdentityManager.getAddonIdentity().setOption("actions", name, value);
  130. }
  131. /**
  132. * Registers the specified group of actions with the manager.
  133. *
  134. * @param group The group of actions to be registered
  135. */
  136. public static void registerGroup(final ActionGroup group) {
  137. GROUPS.put(group.getName(), group);
  138. }
  139. /**
  140. * Registers a set of actiontypes with the manager.
  141. *
  142. * @param types An array of ActionTypes to be registered
  143. */
  144. @Precondition("None of the specified ActionTypes are null")
  145. public static void registerActionTypes(final ActionType[] types) {
  146. for (ActionType type : types) {
  147. Logger.assertTrue(type != null);
  148. if(!ACTION_TYPES.contains(type)) {
  149. ACTION_TYPES.add(type);
  150. ACTIONTYPE_GROUPS.add(type.getType().getGroup(), type);
  151. }
  152. }
  153. }
  154. /**
  155. * Registers a set of action components with the manager.
  156. *
  157. * @param comps An array of ActionComponents to be registered
  158. */
  159. @Precondition("None of the specified ActionComponents are null")
  160. public static void registerActionComponents(final ActionComponent[] comps) {
  161. for (ActionComponent comp : comps) {
  162. Logger.assertTrue(comp != null);
  163. ACTION_COMPONENTS.add(comp);
  164. }
  165. }
  166. /**
  167. * Registers a set of action comparisons with the manager.
  168. *
  169. * @param comps An array of ActionComparisons to be registered
  170. */
  171. @Precondition("None of the specified ActionComparisons are null")
  172. public static void registerActionComparisons(final ActionComparison[] comps) {
  173. for (ActionComparison comp : comps) {
  174. Logger.assertTrue(comp != null);
  175. ACTION_COMPARISON.add(comp);
  176. }
  177. }
  178. /**
  179. * Returns a map of groups to action lists.
  180. *
  181. * @return a map of groups to action lists
  182. */
  183. public static Map<String, ActionGroup> getGroups() {
  184. return GROUPS;
  185. }
  186. /**
  187. * Returns a map of type groups to types.
  188. *
  189. * @return A map of type groups to types
  190. */
  191. public static MapList<String, ActionType> getTypeGroups() {
  192. return ACTIONTYPE_GROUPS;
  193. }
  194. /**
  195. * Loads actions from the user's directory.
  196. */
  197. public static void loadActions() {
  198. ACTIONS.clear();
  199. for (ActionGroup group : GROUPS.values()) {
  200. group.clear();
  201. }
  202. final File dir = new File(getDirectory());
  203. if (!dir.exists()) {
  204. try {
  205. dir.mkdirs();
  206. dir.createNewFile();
  207. } catch (IOException ex) {
  208. Logger.userError(ErrorLevel.HIGH, "I/O error when creating actions directory: "
  209. + ex.getMessage());
  210. }
  211. }
  212. if (dir.listFiles() == null) {
  213. Logger.userError(ErrorLevel.MEDIUM, "Unable to load user action files");
  214. } else {
  215. for (File file : dir.listFiles()) {
  216. if (file.isDirectory()) {
  217. loadActions(file);
  218. }
  219. }
  220. }
  221. registerComponents();
  222. }
  223. /**
  224. * Creates new ActionGroupComponents for each action group.
  225. */
  226. private static void registerComponents() {
  227. for (ActionGroup group : GROUPS.values()) {
  228. new ActionGroupComponent(group);
  229. }
  230. }
  231. /**
  232. * Loads action files from a specified group directory.
  233. *
  234. * @param dir The directory to scan.
  235. */
  236. @Precondition("The specified File is not null and represents a directory")
  237. private static void loadActions(final File dir) {
  238. Logger.assertTrue(dir != null);
  239. Logger.assertTrue(dir.isDirectory());
  240. if (!GROUPS.containsKey(dir.getName())) {
  241. GROUPS.put(dir.getName(), new ActionGroup(dir.getName()));
  242. }
  243. for (File file : dir.listFiles()) {
  244. new Action(dir.getName(), file.getName());
  245. }
  246. }
  247. /**
  248. * Registers an action with the manager.
  249. *
  250. * @param action The action to be registered
  251. */
  252. @Precondition("The specified action is not null")
  253. public static void registerAction(final Action action) {
  254. Logger.assertTrue(action != null);
  255. for (ActionType trigger : action.getTriggers()) {
  256. ACTIONS.add(trigger, action);
  257. }
  258. getGroup(action.getGroup()).add(action);
  259. }
  260. /**
  261. * Retrieves the action group with the specified name. A new group is
  262. * created if it doesn't already exist.
  263. *
  264. * @param name The name of the group to retrieve
  265. * @return The corresponding ActionGroup
  266. */
  267. public static ActionGroup getGroup(final String name) {
  268. if (!GROUPS.containsKey(name)) {
  269. GROUPS.put(name, new ActionGroup(name));
  270. }
  271. return GROUPS.get(name);
  272. }
  273. /**
  274. * Unregisters an action with the manager.
  275. *
  276. * @param action The action to be unregistered
  277. */
  278. @Precondition("The specified action is not null")
  279. public static void unregisterAction(final Action action) {
  280. Logger.assertTrue(action != null);
  281. ACTIONS.removeFromAll(action);
  282. getGroup(action.getGroup()).remove(action);
  283. }
  284. /**
  285. * Reregisters the specified action. Should be used when the action's
  286. * triggers change.
  287. *
  288. * @param action The action to be reregistered
  289. */
  290. public static void reregisterAction(final Action action) {
  291. unregisterAction(action);
  292. registerAction(action);
  293. }
  294. /**
  295. * Deletes the specified action.
  296. *
  297. * @param action The action to be deleted
  298. * @deprecated Use {@link ActionGroup#deleteAction(com.dmdirc.actions.Action)} instead.
  299. */
  300. @Precondition("The specified Action is not null")
  301. @Deprecated
  302. public static void deleteAction(final Action action) {
  303. Logger.assertTrue(action != null);
  304. unregisterAction(action);
  305. action.delete();
  306. }
  307. /**
  308. * Processes an event of the specified type.
  309. *
  310. * @param type The type of the event to process
  311. * @param format The format of the message that's going to be displayed for
  312. * the event. Actions may change this format.
  313. * @param arguments The arguments for the event
  314. * @return True if the event should be processed, or false if an action
  315. * listener has requested the event be skipped.
  316. */
  317. @Precondition({
  318. "The specified ActionType is not null",
  319. "The specified ActionType has a valid ActionMetaType",
  320. "The length of the arguments array equals the arity of the ActionType's ActionMetaType"
  321. })
  322. public static boolean processEvent(final ActionType type,
  323. final StringBuffer format, final Object ... arguments) {
  324. Logger.assertTrue(type != null);
  325. Logger.assertTrue(type.getType() != null);
  326. Logger.assertTrue(type.getType().getArity() == arguments.length);
  327. boolean res = false;
  328. if (LISTENERS.containsKey(type)) {
  329. for (ActionListener listener
  330. : new ArrayList<ActionListener>(LISTENERS.get(type))) {
  331. try {
  332. listener.processEvent(type, format, arguments);
  333. } catch (Exception e) {
  334. Logger.appError(ErrorLevel.MEDIUM, "Error processing action: "
  335. + e.getMessage(), e);
  336. }
  337. }
  338. }
  339. if (!killSwitch) {
  340. res |= triggerActions(type, format, arguments);
  341. }
  342. return !res;
  343. }
  344. /**
  345. * Triggers actions that respond to the specified type.
  346. *
  347. * @param type The type of the event to process
  348. * @param format The format of the message that's going to be displayed for
  349. * the event. Actions may change this format.
  350. * @param arguments The arguments for the event
  351. * @return True if the event should be skipped, or false if it can continue
  352. */
  353. @Precondition("The specified ActionType is not null")
  354. private static boolean triggerActions(final ActionType type,
  355. final StringBuffer format, final Object ... arguments) {
  356. Logger.assertTrue(type != null);
  357. boolean res = false;
  358. if (ACTIONS.containsKey(type)) {
  359. for (Action action : new ArrayList<Action>(ACTIONS.get(type))) {
  360. try {
  361. if (action.getConcurrencyGroup() == null) {
  362. res |= action.trigger(format, arguments);
  363. } else {
  364. synchronized (LOCKS) {
  365. if (!LOCKS.containsKey(action.getConcurrencyGroup())) {
  366. LOCKS.put(action.getConcurrencyGroup(), new Object());
  367. }
  368. }
  369. synchronized (LOCKS.get(action.getConcurrencyGroup())) {
  370. res |= action.trigger(format, arguments);
  371. }
  372. }
  373. } catch (LinkageError e) {
  374. Logger.appError(ErrorLevel.MEDIUM, "Error processing action: "
  375. + e.getMessage(), e);
  376. } catch (Exception e) {
  377. Logger.appError(ErrorLevel.MEDIUM, "Error processing action: "
  378. + e.getMessage(), e);
  379. }
  380. }
  381. }
  382. return res;
  383. }
  384. /**
  385. * Returns the directory that should be used to store actions.
  386. *
  387. * @return The directory that should be used to store actions
  388. */
  389. public static String getDirectory() {
  390. return Main.getConfigDir() + "actions" + System.getProperty("file.separator");
  391. }
  392. /**
  393. * Creates a new group with the specified name.
  394. *
  395. * @param group The group to be created
  396. *
  397. * @return The newly created group
  398. */
  399. @Precondition({
  400. "The specified group is non-null and not empty",
  401. "The specified group is not an existing group"
  402. })
  403. public static ActionGroup makeGroup(final String group) {
  404. Logger.assertTrue(group != null);
  405. Logger.assertTrue(!group.isEmpty());
  406. Logger.assertTrue(!GROUPS.containsKey(group));
  407. final File file = new File(getDirectory() + group);
  408. if (file.isDirectory() || file.mkdir()) {
  409. final ActionGroup actionGroup = new ActionGroup(group);
  410. GROUPS.put(group, actionGroup);
  411. return actionGroup;
  412. } else {
  413. throw new IllegalArgumentException("Unable to create action group directory"
  414. + "\n\nDir: " + getDirectory() + group);
  415. }
  416. }
  417. /**
  418. * Removes the group with the specified name.
  419. *
  420. * @param group The group to be removed
  421. */
  422. @Precondition({
  423. "The specified group is non-null and not empty",
  424. "The specified group is an existing group"
  425. })
  426. public static void removeGroup(final String group) {
  427. Logger.assertTrue(group != null);
  428. Logger.assertTrue(!group.isEmpty());
  429. Logger.assertTrue(GROUPS.containsKey(group));
  430. for (Action action : GROUPS.get(group).getActions()) {
  431. unregisterAction(action);
  432. }
  433. final File dir = new File(getDirectory() + group);
  434. if (dir.isDirectory()) {
  435. for (File file : dir.listFiles()) {
  436. if (!file.delete()) {
  437. Logger.userError(ErrorLevel.MEDIUM, "Unable to remove file: "
  438. + file.getAbsolutePath());
  439. return;
  440. }
  441. }
  442. }
  443. if (!dir.delete()) {
  444. Logger.userError(ErrorLevel.MEDIUM, "Unable to remove directory: "
  445. + dir.getAbsolutePath());
  446. return;
  447. }
  448. GROUPS.remove(group);
  449. }
  450. /**
  451. * Renames the specified group.
  452. *
  453. * @param oldName The old name of the group
  454. * @param newName The new name of the group
  455. */
  456. @Precondition({
  457. "The old name is non-null and not empty",
  458. "The old name is an existing group",
  459. "The new name is non-null and not empty",
  460. "The new name is not an existing group",
  461. "The old name does not equal the new name"
  462. })
  463. public static void renameGroup(final String oldName, final String newName) {
  464. Logger.assertTrue(oldName != null);
  465. Logger.assertTrue(!oldName.isEmpty());
  466. Logger.assertTrue(newName != null);
  467. Logger.assertTrue(!newName.isEmpty());
  468. Logger.assertTrue(GROUPS.containsKey(oldName));
  469. Logger.assertTrue(!GROUPS.containsKey(newName));
  470. Logger.assertTrue(!newName.equals(oldName));
  471. makeGroup(newName);
  472. for (Action action : GROUPS.get(oldName).getActions()) {
  473. action.setGroup(newName);
  474. getGroup(oldName).remove(action);
  475. getGroup(newName).add(action);
  476. }
  477. removeGroup(oldName);
  478. }
  479. /**
  480. * Returns the action comparison specified by the given string, or null if it
  481. * doesn't match a valid registered action comparison.
  482. *
  483. * @param type The name of the action comparison to try and find
  484. * @return The actioncomparison with the specified name, or null on failure
  485. */
  486. public static ActionType getActionType(final String type) {
  487. if (type == null || type.isEmpty()) {
  488. return null;
  489. }
  490. for (ActionType target : ACTION_TYPES) {
  491. if (target.name().equals(type)) {
  492. return target;
  493. }
  494. }
  495. return null;
  496. }
  497. /**
  498. * Returns a list of action types that are compatible with the one
  499. * specified.
  500. *
  501. * @param type The type to be checked against
  502. * @return A list of compatible action types
  503. */
  504. @Precondition("The specified type is not null")
  505. public static List<ActionType> getCompatibleTypes(final ActionType type) {
  506. Logger.assertTrue(type != null);
  507. final List<ActionType> res = new ArrayList<ActionType>();
  508. for (ActionType target : ACTION_TYPES) {
  509. if (!target.equals(type) && target.getType().equals(type.getType())) {
  510. res.add(target);
  511. }
  512. }
  513. return res;
  514. }
  515. /**
  516. * Returns a list of action components that are compatible with the
  517. * specified class.
  518. *
  519. * @param target The class to be tested
  520. * @return A list of compatible action components
  521. */
  522. @Precondition("The specified target is not null")
  523. public static List<ActionComponent> getCompatibleComponents(final Class<?> target) {
  524. Logger.assertTrue(target != null);
  525. final List<ActionComponent> res = new ArrayList<ActionComponent>();
  526. for (ActionComponent subject : ACTION_COMPONENTS) {
  527. if (subject.appliesTo().equals(target)) {
  528. res.add(subject);
  529. }
  530. }
  531. return res;
  532. }
  533. /**
  534. * Returns a list of action comparisons that are compatible with the
  535. * specified class.
  536. *
  537. * @param target The class to be tested
  538. * @return A list of compatible action comparisons
  539. */
  540. @Precondition("The specified target is not null")
  541. public static List<ActionComparison> getCompatibleComparisons(final Class<?> target) {
  542. Logger.assertTrue(target != null);
  543. final List<ActionComparison> res = new ArrayList<ActionComparison>();
  544. for (ActionComparison subject : ACTION_COMPARISON) {
  545. if (subject.appliesTo().equals(target)) {
  546. res.add(subject);
  547. }
  548. }
  549. return res;
  550. }
  551. /**
  552. * Returns a list of all the action types registered by this manager.
  553. *
  554. * @return A list of registered action types
  555. */
  556. public static List<ActionType> getTypes() {
  557. return ACTION_TYPES;
  558. }
  559. /**
  560. * Returns a list of all the action types registered by this manager.
  561. *
  562. * @return A list of registered action comparisons
  563. */
  564. public static List<ActionComparison> getComparisons() {
  565. return ACTION_COMPARISON;
  566. }
  567. /**
  568. * Returns the action component specified by the given string, or null if it
  569. * doesn't match a valid registered action component.
  570. *
  571. * @param type The name of the action component to try and find
  572. * @return The actioncomponent with the specified name, or null on failure
  573. */
  574. @Precondition("The specified type is non-null and not empty")
  575. public static ActionComponent getActionComponent(final String type) {
  576. Logger.assertTrue(type != null);
  577. Logger.assertTrue(!type.isEmpty());
  578. for (ActionComponent target : ACTION_COMPONENTS) {
  579. if (target.name().equals(type)) {
  580. return target;
  581. }
  582. }
  583. return null;
  584. }
  585. /**
  586. * Returns the action type specified by the given string, or null if it
  587. * doesn't match a valid registered action type.
  588. *
  589. * @param type The name of the action type to try and find
  590. * @return The actiontype with the specified name, or null on failure
  591. */
  592. @Precondition("The specified type is non-null and not empty")
  593. public static ActionComparison getActionComparison(final String type) {
  594. Logger.assertTrue(type != null);
  595. Logger.assertTrue(!type.isEmpty());
  596. for (ActionComparison target : ACTION_COMPARISON) {
  597. if (target.name().equals(type)) {
  598. return target;
  599. }
  600. }
  601. return null;
  602. }
  603. /**
  604. * Installs an action pack located at the specified path.
  605. *
  606. * @param path The full path of the action pack .zip.
  607. * @throws IOException If the zip cannot be extracted
  608. */
  609. public static void installActionPack(final String path) throws IOException {
  610. final ZipResourceManager ziprm = ZipResourceManager.getInstance(path);
  611. ziprm.extractResources("", getDirectory());
  612. loadActions();
  613. new File(path).delete();
  614. }
  615. /**
  616. * Adds a new listener for the specified action type.
  617. *
  618. * @param types The action types that are to be listened for
  619. * @param listener The listener to be added
  620. */
  621. public static void addListener(final ActionListener listener, final ActionType ... types) {
  622. for (ActionType type : types) {
  623. LISTENERS.add(type, listener);
  624. }
  625. }
  626. /**
  627. * Removes a listener for the specified action type.
  628. *
  629. * @param types The action types that were being listened for
  630. * @param listener The listener to be removed
  631. */
  632. public static void removeListener(final ActionListener listener, final ActionType ... types) {
  633. for (ActionType type : types) {
  634. LISTENERS.remove(type, listener);
  635. }
  636. }
  637. /**
  638. * Removes a listener for all action types.
  639. *
  640. * @param listener The listener to be removed
  641. */
  642. public static void removeListener(final ActionListener listener) {
  643. LISTENERS.removeFromAll(listener);
  644. }
  645. }