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

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