Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PreferencesCategory.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.config.prefs;
  23. import com.dmdirc.util.collections.ListenerList;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import org.slf4j.LoggerFactory;
  27. /**
  28. * Represents one category of preferences. Categories can contain 0 or more subcategories, and
  29. * either 0 or more PreferencesSettings or exactly 1 PreferencesInterface object.
  30. */
  31. public class PreferencesCategory {
  32. private static final org.slf4j.Logger log = LoggerFactory.getLogger(PreferencesCategory.class);
  33. /** The title (name) of this category. */
  34. private final String title;
  35. /** A description of this category. */
  36. private final String description;
  37. /** The icon to use for this category. */
  38. private final String icon;
  39. /** The warning displayed for this category, if any. */
  40. private String warning;
  41. /** Whether or not this category is inline. */
  42. private boolean isInline;
  43. /** Whether or not to show inline categories before settings. */
  44. private boolean inlineBefore = true;
  45. /** Our parent category, if known. */
  46. private PreferencesCategory parent;
  47. /** A list of settings in this category. */
  48. private final List<PreferencesSetting> settings = new ArrayList<>();
  49. /** A list of subcategories of this category. */
  50. private final List<PreferencesCategory> subcats = new ArrayList<>();
  51. /** The replacement object to use for this category. */
  52. private final PreferencesInterface object;
  53. /** A list of listeners who are interested in this category. */
  54. private final ListenerList listeners = new ListenerList();
  55. /**
  56. * Creates a new preferences category that contains settings.
  57. *
  58. * @param title The title of this preferences category
  59. * @param description The description of this category
  60. */
  61. public PreferencesCategory(final String title, final String description) {
  62. this(title, description, null, null);
  63. }
  64. /**
  65. * Creates a new preferences category that contains settings.
  66. *
  67. * @since 0.6.3m1
  68. * @param title The title of this preferences category
  69. * @param description The description of this category
  70. * @param icon The icon to use for this category
  71. */
  72. public PreferencesCategory(final String title, final String description,
  73. final String icon) {
  74. this(title, description, icon, null);
  75. }
  76. /**
  77. * Creates a new preferences category that contains an object.
  78. *
  79. * @param title The title of this preferences category
  80. * @param description The description of this category
  81. * @param object The replacement object for this category
  82. */
  83. public PreferencesCategory(final String title, final String description,
  84. final PreferencesInterface object) {
  85. this(title, description, null, object);
  86. }
  87. /**
  88. * Creates a new preferences category that contains an object.
  89. *
  90. * @since 0.6.3m1
  91. * @param title The title of this preferences category
  92. * @param description The description of this category
  93. * @param icon The icon to use for this category
  94. * @param object The replacement object for this category
  95. */
  96. public PreferencesCategory(final String title, final String description,
  97. final String icon, final PreferencesInterface object) {
  98. this.title = title;
  99. this.description = description;
  100. this.icon = icon;
  101. this.object = object;
  102. }
  103. public String getTitle() {
  104. return title;
  105. }
  106. public String getDescription() {
  107. return description;
  108. }
  109. public String getIcon() {
  110. return icon;
  111. }
  112. public boolean isInline() {
  113. return isInline;
  114. }
  115. public boolean isInlineBefore() {
  116. return inlineBefore;
  117. }
  118. public List<PreferencesSetting> getSettings() {
  119. return settings;
  120. }
  121. public List<PreferencesCategory> getSubcats() {
  122. return subcats;
  123. }
  124. public PreferencesInterface getObject() {
  125. return object;
  126. }
  127. public String getWarning() {
  128. return warning;
  129. }
  130. public void setWarning(final String warning) {
  131. this.warning = warning;
  132. }
  133. public PreferencesCategory getParent() {
  134. return parent;
  135. }
  136. public void setParent(final PreferencesCategory parent) {
  137. this.parent = parent;
  138. }
  139. /**
  140. * Sets this as an inline category.
  141. *
  142. * @return A reference to this category, for convenience
  143. */
  144. public PreferencesCategory setInline() {
  145. isInline = true;
  146. return this;
  147. }
  148. /**
  149. * Sets this category to show inline categories after settings, rather than before.
  150. *
  151. * @return A reference to this category, for convenience
  152. */
  153. public PreferencesCategory setInlineAfter() {
  154. inlineBefore = false;
  155. return this;
  156. }
  157. /**
  158. * Adds the specified setting to this category.
  159. *
  160. * @param setting The setting to be added
  161. */
  162. public void addSetting(final PreferencesSetting setting) {
  163. if (hasObject()) {
  164. throw new IllegalArgumentException("Can't add settings to a "
  165. + "category that uses a replacement object");
  166. }
  167. settings.add(setting);
  168. }
  169. /**
  170. * Adds the specified subcategory to this category.
  171. *
  172. * @param subcategory The category to be added
  173. */
  174. public void addSubCategory(final PreferencesCategory subcategory) {
  175. if (isInline() && !subcategory.isInline()) {
  176. throw new IllegalArgumentException("Can't add non-inline "
  177. + "subcategories to inline ones");
  178. }
  179. subcategory.setParent(this);
  180. subcats.add(subcategory);
  181. }
  182. /**
  183. * Determines if this category has a replacement object.
  184. *
  185. * @return True if the category has a replacement object, false otherwise
  186. */
  187. public boolean hasObject() {
  188. return object != null;
  189. }
  190. /**
  191. * Retrieves the full path of this category. A category's path is the name of each of its parent
  192. * categories, starting with the furthest up the hierarchy, separated by '→' characters.
  193. *
  194. * @return This category's path
  195. *
  196. * @since 0.6.3m1
  197. */
  198. public String getPath() {
  199. return (parent == null ? "" : parent.getPath() + " → ") + getTitle();
  200. }
  201. /**
  202. * Saves all the settings in this category.
  203. *
  204. * @return Is a restart needed after saving?
  205. */
  206. public boolean save() {
  207. log.debug("{} save method called", getTitle());
  208. boolean restart = false;
  209. for (PreferencesSetting setting : settings) {
  210. log.trace("{}: saving setting '{}'", getTitle(), setting.getTitle());
  211. if (setting.save() && setting.isRestartNeeded()) {
  212. restart = true;
  213. }
  214. }
  215. for (PreferencesCategory child : getSubcats()) {
  216. restart |= child.save();
  217. }
  218. return restart;
  219. }
  220. /**
  221. * Dismisses all the settings in this category.
  222. */
  223. public void dismiss() {
  224. for (PreferencesSetting setting : settings) {
  225. setting.dismiss();
  226. }
  227. for (PreferencesCategory child : getSubcats()) {
  228. child.dismiss();
  229. }
  230. }
  231. /**
  232. * Registers a change listener for this category.
  233. *
  234. * @param listener The listener to be added
  235. */
  236. public void addChangeListener(final CategoryChangeListener listener) {
  237. listeners.add(CategoryChangeListener.class, listener);
  238. }
  239. /**
  240. * Removes a change listener from this category.
  241. *
  242. * @param listener The listener to be added
  243. */
  244. public void removeChangeListener(final CategoryChangeListener listener) {
  245. listeners.remove(CategoryChangeListener.class, listener);
  246. }
  247. /**
  248. * Informs all registered listeners that this category has been selected.
  249. */
  250. public void fireCategorySelected() {
  251. for (CategoryChangeListener listener : listeners.get(CategoryChangeListener.class)) {
  252. listener.categorySelected(this);
  253. }
  254. }
  255. /**
  256. * Informs all registered listeners that this category has been deselected.
  257. */
  258. public void fireCategoryDeselected() {
  259. for (CategoryChangeListener listener : listeners.get(CategoryChangeListener.class)) {
  260. listener.categoryDeselected(this);
  261. }
  262. }
  263. }