Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ActionGroup.java 6.6KB

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