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.

ActionGroup.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2006-2013 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.Precondition;
  24. import com.dmdirc.config.prefs.PreferencesSetting;
  25. import com.dmdirc.logger.Logger;
  26. import com.dmdirc.updater.Version;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.Iterator;
  30. import java.util.List;
  31. import java.util.Map;
  32. /**
  33. * Represents a group of actions, along with their meta-data.
  34. */
  35. public class ActionGroup implements Iterable<Action> {
  36. /**
  37. * A version number for this class. It should be changed whenever the class
  38. * structure is changed (or anything else that would prevent serialized
  39. * objects being unserialized with the new class).
  40. */
  41. private static final long serialVersionUID = 1;
  42. /** The actions in this group. */
  43. private final List<Action> actions = new ArrayList<>();
  44. /** The name of this action group. */
  45. private final String name;
  46. /** The description of this action group. */
  47. private String description;
  48. /** The author of this action group. */
  49. private String author;
  50. /** The component number of this action group (for updating). */
  51. private int component = -1;
  52. /** The version of this action group. */
  53. private Version version;
  54. /** A list of settings used by this action group. */
  55. private final Map<String, PreferencesSetting> settings = new HashMap<>();
  56. /**
  57. * Creates a new instance of ActionGroup.
  58. *
  59. * @param name The name of this action group
  60. */
  61. public ActionGroup(final String name) {
  62. super();
  63. this.name = name;
  64. }
  65. /**
  66. * Retrieves the author of this ActionGroup.
  67. *
  68. * @return This action group's author, or null if the author isn't specified
  69. */
  70. public String getAuthor() {
  71. return author;
  72. }
  73. /**
  74. * Sets the author of this ActionGroup.
  75. *
  76. * @param author The new author for this action group
  77. */
  78. public void setAuthor(final String author) {
  79. this.author = author;
  80. }
  81. /**
  82. * Retrieves the description of this action group.
  83. *
  84. * @return This action group's description, or null if none is specified
  85. */
  86. public String getDescription() {
  87. return description;
  88. }
  89. /**
  90. * Sets the description for this action group.
  91. *
  92. * @param description The new description for this action group
  93. */
  94. public void setDescription(final String description) {
  95. this.description = description;
  96. }
  97. /**
  98. * Retrieves the name of this action group.
  99. *
  100. * @return This action group's name
  101. */
  102. public String getName() {
  103. return name;
  104. }
  105. /**
  106. * Retrieves a map settings used by this action group.
  107. *
  108. * @return A map of setting names to values
  109. */
  110. public Map<String, PreferencesSetting> getSettings() {
  111. return settings;
  112. }
  113. /**
  114. * Retrieves the version number of this action group.
  115. *
  116. * @return This action group's version number, or null if none is specified.
  117. * @since 0.6.4
  118. */
  119. public Version getVersion() {
  120. return version;
  121. }
  122. /**
  123. * Sets the version of this action group.
  124. *
  125. * @param version This action group's new version.
  126. * @since 0.6.4
  127. */
  128. public void setVersion(final Version version) {
  129. this.version = version;
  130. }
  131. /**
  132. * Retrieves the addon site component number for this action group.
  133. *
  134. * @return The component number for this action group, or -1 if none is
  135. * specified.
  136. */
  137. public int getComponent() {
  138. return component;
  139. }
  140. /**
  141. * Sets the addon site component number for this action group.
  142. *
  143. * @param component The component number for this action group
  144. */
  145. public void setComponent(final int component) {
  146. this.component = component;
  147. }
  148. /**
  149. * Removes the specified action from this group.
  150. *
  151. * @param action The action to be removed
  152. */
  153. public void remove(final Action action) {
  154. actions.remove(action);
  155. }
  156. /** {@inheritDoc} */
  157. @Override
  158. public Iterator<Action> iterator() {
  159. return actions.iterator();
  160. }
  161. /**
  162. * Removes all actions from this group, and removes all meta-data.
  163. */
  164. public void clear() {
  165. for (Action action : new ArrayList<>(actions)) {
  166. remove(action);
  167. }
  168. settings.clear();
  169. description = null;
  170. author = null;
  171. version = null;
  172. component = -1;
  173. }
  174. /**
  175. * Adds the specified action to this group.
  176. *
  177. * @param action The action to be added
  178. */
  179. public void add(final Action action) {
  180. actions.add(action);
  181. }
  182. /**
  183. * Retrieves a copy of the list of all actions in this group.
  184. *
  185. * @return A list of actions in this group
  186. */
  187. public List<Action> getActions() {
  188. return new ArrayList<>(actions);
  189. }
  190. /**
  191. * Deletes an action from this group.
  192. *
  193. * @param action The action to be deleted
  194. * @since 0.6.3
  195. */
  196. @Precondition({
  197. "The specified action is non-null",
  198. "The specified action exists in this group"
  199. })
  200. @SuppressWarnings("deprecation")
  201. public void deleteAction(final Action action) {
  202. Logger.assertTrue(action != null);
  203. Logger.assertTrue(actions.contains(action));
  204. ActionManager.getActionManager().removeAction(action);
  205. action.delete();
  206. }
  207. /**
  208. * Determines if this action group is delible or not.
  209. *
  210. * @return True if the group may be deleted, false if it may not.
  211. */
  212. public boolean isDelible() {
  213. return true;
  214. }
  215. }