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.

PreferencesCategory.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.config.prefs;
  18. import com.dmdirc.util.collections.ListenerList;
  19. import com.google.common.base.MoreObjects;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. /**
  25. * Represents one category of preferences. Categories can contain 0 or more subcategories, and
  26. * either 0 or more PreferencesSettings or exactly 1 PreferencesInterface object.
  27. */
  28. public class PreferencesCategory {
  29. private static final Logger LOG = LoggerFactory.getLogger(PreferencesCategory.class);
  30. /** The title (name) of this category. */
  31. private final String title;
  32. /** A description of this category. */
  33. private final String description;
  34. /** The icon to use for this category. */
  35. private final String icon;
  36. /** The warning displayed for this category, if any. */
  37. private String warning;
  38. /** Whether or not this category is inline. */
  39. private boolean isInline;
  40. /** Whether or not to show inline categories before settings. */
  41. private boolean inlineBefore = true;
  42. /** Our parent category, if known. */
  43. private PreferencesCategory parent;
  44. /** A list of settings in this category. */
  45. private final List<PreferencesSetting> settings = new ArrayList<>();
  46. /** A list of subcategories of this category. */
  47. private final List<PreferencesCategory> subcats = new ArrayList<>();
  48. /** The replacement object to use for this category. */
  49. private final PreferencesInterface object;
  50. /** A list of listeners who are interested in this category. */
  51. private final ListenerList listeners = new ListenerList();
  52. /**
  53. * Creates a new preferences category that contains settings.
  54. *
  55. * @param title The title of this preferences category
  56. * @param description The description of this category
  57. */
  58. public PreferencesCategory(final String title, final String description) {
  59. this(title, description, null, null);
  60. }
  61. /**
  62. * Creates a new preferences category that contains settings.
  63. *
  64. * @since 0.6.3m1
  65. * @param title The title of this preferences category
  66. * @param description The description of this category
  67. * @param icon The icon to use for this category
  68. */
  69. public PreferencesCategory(final String title, final String description,
  70. final String icon) {
  71. this(title, description, icon, null);
  72. }
  73. /**
  74. * Creates a new preferences category that contains an object.
  75. *
  76. * @param title The title of this preferences category
  77. * @param description The description of this category
  78. * @param object The replacement object for this category
  79. */
  80. public PreferencesCategory(final String title, final String description,
  81. final PreferencesInterface object) {
  82. this(title, description, null, object);
  83. }
  84. /**
  85. * Creates a new preferences category that contains an object.
  86. *
  87. * @since 0.6.3m1
  88. * @param title The title of this preferences category
  89. * @param description The description of this category
  90. * @param icon The icon to use for this category
  91. * @param object The replacement object for this category
  92. */
  93. public PreferencesCategory(final String title, final String description,
  94. final String icon, final PreferencesInterface object) {
  95. this.title = title;
  96. this.description = description;
  97. this.icon = icon;
  98. this.object = object;
  99. }
  100. public String getTitle() {
  101. return title;
  102. }
  103. public String getDescription() {
  104. return description;
  105. }
  106. public String getIcon() {
  107. return icon;
  108. }
  109. public boolean isInline() {
  110. return isInline;
  111. }
  112. public boolean isInlineBefore() {
  113. return inlineBefore;
  114. }
  115. public List<PreferencesSetting> getSettings() {
  116. return settings;
  117. }
  118. public List<PreferencesCategory> getSubcats() {
  119. return subcats;
  120. }
  121. public PreferencesInterface getObject() {
  122. return object;
  123. }
  124. public String getWarning() {
  125. return warning;
  126. }
  127. public void setWarning(final String warning) {
  128. this.warning = warning;
  129. }
  130. public PreferencesCategory getParent() {
  131. return parent;
  132. }
  133. public void setParent(final PreferencesCategory parent) {
  134. this.parent = parent;
  135. }
  136. /**
  137. * Sets this as an inline category.
  138. *
  139. * @return A reference to this category, for convenience
  140. */
  141. public PreferencesCategory setInline() {
  142. isInline = true;
  143. return this;
  144. }
  145. /**
  146. * Sets this category to show inline categories after settings, rather than before.
  147. *
  148. * @return A reference to this category, for convenience
  149. */
  150. public PreferencesCategory setInlineAfter() {
  151. inlineBefore = false;
  152. return this;
  153. }
  154. /**
  155. * Adds the specified setting to this category.
  156. *
  157. * @param setting The setting to be added
  158. */
  159. public void addSetting(final PreferencesSetting setting) {
  160. if (hasObject()) {
  161. throw new IllegalArgumentException("Can't add settings to a "
  162. + "category that uses a replacement object");
  163. }
  164. settings.add(setting);
  165. }
  166. /**
  167. * Adds the specified subcategory to this category.
  168. *
  169. * @param subcategory The category to be added
  170. */
  171. public void addSubCategory(final PreferencesCategory subcategory) {
  172. if (isInline() && !subcategory.isInline()) {
  173. throw new IllegalArgumentException("Can't add non-inline "
  174. + "subcategories to inline ones");
  175. }
  176. subcategory.setParent(this);
  177. subcats.add(subcategory);
  178. }
  179. /**
  180. * Determines if this category has a replacement object.
  181. *
  182. * @return True if the category has a replacement object, false otherwise
  183. */
  184. public boolean hasObject() {
  185. return object != null;
  186. }
  187. /**
  188. * Retrieves the full path of this category. A category's path is the name of each of its parent
  189. * categories, starting with the furthest up the hierarchy, separated by '→' characters.
  190. *
  191. * @return This category's path
  192. *
  193. * @since 0.6.3m1
  194. */
  195. public String getPath() {
  196. return (parent == null ? "" : parent.getPath() + " → ") + getTitle();
  197. }
  198. /**
  199. * Saves all the settings in this category.
  200. *
  201. * @return Is a restart needed after saving?
  202. */
  203. public boolean save() {
  204. LOG.debug("{} save method called", getTitle());
  205. boolean restart = false;
  206. for (PreferencesSetting setting : settings) {
  207. LOG.trace("{}: saving setting '{}'", getTitle(), setting.getTitle());
  208. if (setting.save() && setting.isRestartNeeded()) {
  209. restart = true;
  210. }
  211. }
  212. for (PreferencesCategory child : getSubcats()) {
  213. restart |= child.save();
  214. }
  215. return restart;
  216. }
  217. /**
  218. * Dismisses all the settings in this category.
  219. */
  220. public void dismiss() {
  221. settings.forEach(PreferencesSetting::dismiss);
  222. getSubcats().forEach(PreferencesCategory::dismiss);
  223. }
  224. /**
  225. * Registers a change listener for this category.
  226. *
  227. * @param listener The listener to be added
  228. */
  229. public void addChangeListener(final CategoryChangeListener listener) {
  230. listeners.add(CategoryChangeListener.class, listener);
  231. }
  232. /**
  233. * Removes a change listener from this category.
  234. *
  235. * @param listener The listener to be added
  236. */
  237. public void removeChangeListener(final CategoryChangeListener listener) {
  238. listeners.remove(CategoryChangeListener.class, listener);
  239. }
  240. /**
  241. * Informs all registered listeners that this category has been selected.
  242. */
  243. public void fireCategorySelected() {
  244. for (CategoryChangeListener listener : listeners.get(CategoryChangeListener.class)) {
  245. listener.categorySelected(this);
  246. }
  247. }
  248. /**
  249. * Informs all registered listeners that this category has been deselected.
  250. */
  251. public void fireCategoryDeselected() {
  252. for (CategoryChangeListener listener : listeners.get(CategoryChangeListener.class)) {
  253. listener.categoryDeselected(this);
  254. }
  255. }
  256. @Override
  257. public String toString() {
  258. return MoreObjects.toStringHelper(this).add("title", getTitle()).toString();
  259. }
  260. }