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

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