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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2006-2008 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.config.prefs;
  23. import com.dmdirc.util.ListenerList;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. /**
  27. * Represents one category of preferences. Categories can contain 0 or more
  28. * subcategories, and either 0 or more PreferencesSettings or exactly 1
  29. * PreferencesInterface object.
  30. *
  31. * @author chris
  32. */
  33. public class PreferencesCategory {
  34. /** The title (name) of this category. */
  35. private final String title;
  36. /** A description of this category. */
  37. private final String description;
  38. /** Whether or not this category is inline. */
  39. private boolean isInline = false;
  40. /** Whether or not to show inline categories before settings. */
  41. private boolean inlineBefore = true;
  42. /** A list of settings in this category. */
  43. private final List<PreferencesSetting> settings = new ArrayList<PreferencesSetting>();
  44. /** A list of subcategories of this category. */
  45. private final List<PreferencesCategory> subcats = new ArrayList<PreferencesCategory>();
  46. /** The replacement object to use for this category. */
  47. private final PreferencesInterface object;
  48. /** A list of listeners who are interested in this category. */
  49. private final ListenerList listeners = new ListenerList();
  50. /**
  51. * Creates a new preferences category that contains settings.
  52. *
  53. * @param title The title of this preferences category
  54. * @param description The description of this category
  55. */
  56. public PreferencesCategory(final String title, final String description) {
  57. this(title, description, null);
  58. }
  59. /**
  60. * Creates a new preferences category that contains an object.
  61. *
  62. * @param title The title of this preferences category
  63. * @param description The description of this category
  64. * @param object The replacement object for this category
  65. */
  66. public PreferencesCategory(final String title, final String description,
  67. final PreferencesInterface object) {
  68. this.title = title;
  69. this.description = description;
  70. this.object = object;
  71. }
  72. /**
  73. * Sets this as an inline category.
  74. *
  75. * @return A reference to this category, for convenience
  76. */
  77. public PreferencesCategory setInline() {
  78. isInline = true;
  79. return this;
  80. }
  81. /**
  82. * Sets this category to show inline categories after settings, rather than
  83. * before.
  84. *
  85. * @return A reference to this category, for convenience
  86. */
  87. public PreferencesCategory setInlineAfter() {
  88. inlineBefore = false;
  89. return this;
  90. }
  91. /**
  92. * Determines if this category is meant to be displayed inline or not.
  93. *
  94. * @return True if this category should be shown inline, false otherwise
  95. */
  96. public boolean isInline() {
  97. return isInline;
  98. }
  99. /**
  100. * Determines whether this category wants inline subcats to be displayed
  101. * before the settings, or after.
  102. *
  103. * @return True if subcats should be displayed first, false otherwise.
  104. */
  105. public boolean getInlineBefore() {
  106. return inlineBefore;
  107. }
  108. /**
  109. * Adds the specified setting to this category.
  110. *
  111. * @param setting The setting to be added
  112. */
  113. public void addSetting(final PreferencesSetting setting) {
  114. if (hasObject()) {
  115. throw new IllegalArgumentException("Can't add settings to a " +
  116. "category that uses a replacement object");
  117. }
  118. settings.add(setting);
  119. }
  120. /**
  121. * Adds the specified subcategory to this category.
  122. *
  123. * @param subcategory The category to be asdded
  124. */
  125. public void addSubCategory(final PreferencesCategory subcategory) {
  126. if (isInline() && !subcategory.isInline()) {
  127. throw new IllegalArgumentException("Can't add non-inline " +
  128. "subcategories to inline ones");
  129. }
  130. subcats.add(subcategory);
  131. }
  132. /**
  133. * Retrieves the description of this category.
  134. *
  135. * @return This category's description
  136. */
  137. public String getDescription() {
  138. return description;
  139. }
  140. /**
  141. * Retrieves the settings in this category.
  142. *
  143. * @return This category's settings
  144. */
  145. public List<PreferencesSetting> getSettings() {
  146. return settings;
  147. }
  148. /**
  149. * Retrieves the subcategories of this category.
  150. *
  151. * @return This category's subcategories
  152. */
  153. public List<PreferencesCategory> getSubcats() {
  154. return subcats;
  155. }
  156. /**
  157. * Retrieves the title of this category.
  158. *
  159. * @return This category's title
  160. */
  161. public String getTitle() {
  162. return title;
  163. }
  164. /**
  165. * Determines if this category has a replacement object.
  166. *
  167. * @return True if the category has a replacement object, false otherwise
  168. * @see #getObject()
  169. */
  170. public boolean hasObject() {
  171. return object != null;
  172. }
  173. /**
  174. * Retrieves this category's replacement object.
  175. *
  176. * @return This category's replacement object.
  177. * @see #hasObject()
  178. */
  179. public PreferencesInterface getObject() {
  180. return object;
  181. }
  182. /**
  183. * Registers a change listener for this category.
  184. *
  185. * @param listener The listener to be added
  186. */
  187. public void addChangeListener(final CategoryChangeListener listener) {
  188. listeners.add(CategoryChangeListener.class, listener);
  189. }
  190. /**
  191. * Removes a change listener from this category.
  192. *
  193. * @param listener The listener to be added
  194. */
  195. public void removeChangeListener(final CategoryChangeListener listener) {
  196. listeners.remove(CategoryChangeListener.class, listener);
  197. }
  198. /**
  199. * Informs all registered listeners that this category has been selected.
  200. */
  201. public void fireCategorySelected() {
  202. for (CategoryChangeListener listener : listeners.get(CategoryChangeListener.class)) {
  203. listener.categorySelected(this);
  204. }
  205. }
  206. /**
  207. * Informs all registered listeners that this category has been deselected.
  208. */
  209. public void fireCategoryDeselected() {
  210. for (CategoryChangeListener listener : listeners.get(CategoryChangeListener.class)) {
  211. listener.categoryDeselected(this);
  212. }
  213. }
  214. }