Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PreferencesSetting.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.config.prefs;
  23. import com.dmdirc.interfaces.config.ConfigProvider;
  24. import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
  25. import com.dmdirc.util.validators.PermissiveValidator;
  26. import com.dmdirc.util.validators.Validator;
  27. import com.google.common.collect.Table;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import java.util.Objects;
  33. /**
  34. * Represents a single setting.
  35. */
  36. public class PreferencesSetting {
  37. /** The type of this setting. */
  38. protected final PreferencesType type;
  39. /** The possible options for a multichoice setting. */
  40. protected final Map<String, String> comboOptions;
  41. /** The headers in the table. */
  42. private final Iterable<String> tableHeaders;
  43. /** The possible options for a table setting. */
  44. protected final Table<Integer, Integer, String> tableOptions;
  45. /** The validator to use to validate this setting. */
  46. protected final Validator<String> validator;
  47. /** The domain of the setting. */
  48. protected final String domain;
  49. /** The option name of the setting. */
  50. protected final String option;
  51. /** The title of this setting. */
  52. protected final String title;
  53. /** Text to inform the user what the setting is for. */
  54. protected final String helptext;
  55. /** The current value of the setting. */
  56. protected String value;
  57. /** Whether or not we need a restart. */
  58. protected boolean restartNeeded;
  59. /** The original value of this setting. */
  60. private String original;
  61. /** A list of change listeners. */
  62. private final Collection<SettingChangeListener> listeners = new ArrayList<>();
  63. /** Identity to save settings to. */
  64. private final ConfigProvider identity;
  65. /**
  66. * Creates a new preferences setting for any type except multi-choice.
  67. *
  68. * @param type The type of the setting to create
  69. * @param validator A validator to validate the setting's value
  70. * @param domain The domain of the setting
  71. * @param option The option name of the setting
  72. * @param title The title of this setting
  73. * @param helptext Text to display to help the user
  74. * @param configManager Config Manager
  75. * @param identity Identity to save setting to
  76. */
  77. public PreferencesSetting(final PreferencesType type,
  78. final Validator<String> validator, final String domain,
  79. final String option, final String title, final String helptext,
  80. final ReadOnlyConfigProvider configManager, final ConfigProvider identity) {
  81. if (PreferencesType.MULTICHOICE == type) {
  82. throw new IllegalArgumentException("Multi-choice preferences must "
  83. + "have their options specified.");
  84. }
  85. if (PreferencesType.TABLE == type) {
  86. throw new IllegalArgumentException("Table preferences must "
  87. + "have their table options specified.");
  88. }
  89. this.type = type;
  90. this.comboOptions = null;
  91. this.tableHeaders = null;
  92. this.tableOptions = null;
  93. this.validator = validator;
  94. this.domain = domain;
  95. this.option = option;
  96. this.title = title;
  97. this.helptext = helptext;
  98. this.identity = identity;
  99. value = configManager.getOption(domain, option);
  100. original = value;
  101. }
  102. /**
  103. * Creates a new preferences setting for any type except multi-choice, with a default permissive
  104. * validator.
  105. *
  106. * @param type The type of the setting to create
  107. * @param domain The domain of the setting
  108. * @param option The option name of the setting
  109. * @param title The title of this setting
  110. * @param helptext Text to display to help the user
  111. * @param configManager Config Manager
  112. * @param identity Identity to save setting to
  113. */
  114. public PreferencesSetting(final PreferencesType type, final String domain,
  115. final String option, final String title, final String helptext,
  116. final ReadOnlyConfigProvider configManager, final ConfigProvider identity) {
  117. if (PreferencesType.MULTICHOICE == type) {
  118. throw new IllegalArgumentException("Multi-choice preferences must "
  119. + "have their options specified.");
  120. }
  121. if (PreferencesType.TABLE == type) {
  122. throw new IllegalArgumentException("Table preferences must "
  123. + "have their table options specified.");
  124. }
  125. this.type = type;
  126. this.comboOptions = null;
  127. this.tableHeaders = null;
  128. this.tableOptions = null;
  129. this.validator = new PermissiveValidator<>();
  130. this.domain = domain;
  131. this.option = option;
  132. this.title = title;
  133. this.helptext = helptext;
  134. this.identity = identity;
  135. value = configManager.getOption(domain, option);
  136. original = value;
  137. }
  138. /**
  139. * Creates a new preferences setting for multi-choice preferences.
  140. *
  141. * @param domain The domain of the setting
  142. * @param option The option name of the setting
  143. * @param options A map of setting values to display names for this setting
  144. * @param title The title of this setting
  145. * @param helptext Text to display to help the user
  146. * @param configManager Config Manager
  147. * @param identity Identity to save setting to
  148. */
  149. public PreferencesSetting(final String domain, final String option,
  150. final String title, final String helptext,
  151. final Map<String, String> options,
  152. final ReadOnlyConfigProvider configManager, final ConfigProvider identity) {
  153. this.type = PreferencesType.MULTICHOICE;
  154. this.comboOptions = new HashMap<>(options);
  155. this.tableHeaders = null;
  156. this.tableOptions = null;
  157. this.validator = new PermissiveValidator<>();
  158. this.domain = domain;
  159. this.option = option;
  160. this.title = title;
  161. this.helptext = helptext;
  162. this.identity = identity;
  163. value = configManager.getOption(domain, option);
  164. original = value;
  165. if (!comboOptions.containsKey(value)) {
  166. comboOptions.put(value, "Current (" + value + ')');
  167. }
  168. }
  169. /**
  170. * Creates a new preferences setting for table preferences.
  171. *
  172. * @param domain The domain of the setting
  173. * @param option The option name of the setting
  174. * @param options A map of setting values to display names for this setting
  175. * @param title The title of this setting
  176. * @param helptext Text to display to help the user
  177. * @param configManager Config Manager
  178. * @param identity Identity to save setting to
  179. */
  180. public PreferencesSetting(final String domain, final String option,
  181. final String title, final String helptext,
  182. final Iterable<String> tableHeaders,
  183. final Table<Integer, Integer, String> options,
  184. final ReadOnlyConfigProvider configManager,
  185. final ConfigProvider identity) {
  186. this.type = PreferencesType.TABLE;
  187. this.comboOptions = null;
  188. this.tableHeaders = tableHeaders;
  189. this.tableOptions = options;
  190. this.validator = new PermissiveValidator<>();
  191. this.domain = domain;
  192. this.option = option;
  193. this.title = title;
  194. this.helptext = helptext;
  195. this.identity = identity;
  196. value = configManager.getOption(domain, option);
  197. original = value;
  198. }
  199. public PreferencesType getType() {
  200. return type;
  201. }
  202. public Map<String, String> getComboOptions() {
  203. return comboOptions;
  204. }
  205. public Iterable<String> getTableHeaders() {
  206. return tableHeaders;
  207. }
  208. public Table<Integer, Integer, String> getTableOptions() {
  209. return tableOptions;
  210. }
  211. public Validator<String> getValidator() {
  212. return validator;
  213. }
  214. public String getTitle() {
  215. return title;
  216. }
  217. public String getHelptext() {
  218. return helptext;
  219. }
  220. public String getValue() {
  221. return value;
  222. }
  223. /**
  224. * Sets the current value of this setting. Note that the setting is not saved to the
  225. * configuration file until the save method is called.
  226. *
  227. * @param newValue The new value of the setting
  228. */
  229. public void setValue(final String newValue) {
  230. value = newValue;
  231. for (SettingChangeListener listener : listeners) {
  232. listener.settingChanged(this);
  233. }
  234. }
  235. /**
  236. * Determines whether or not this setting needs a restart when it's changed.
  237. *
  238. * @return True if this setting needs a restart, false otherwise
  239. */
  240. public boolean isRestartNeeded() {
  241. return restartNeeded;
  242. }
  243. /**
  244. * Sets the "restart needed" flag for this setting, indicating a client restart is needed before
  245. * the setting takes effect.
  246. *
  247. * @return A reference to this setting, for convenience
  248. */
  249. public PreferencesSetting setRestartNeeded() {
  250. restartNeeded = true;
  251. return this;
  252. }
  253. /**
  254. * Registers the specified setting change listener.
  255. *
  256. * @param listener The listener to be registered
  257. *
  258. * @return A reference to this setting, for convenience
  259. */
  260. public PreferencesSetting registerChangeListener(final SettingChangeListener listener) {
  261. listeners.add(listener);
  262. return this;
  263. }
  264. /**
  265. * Saves the current value of this setting to the global configuration.
  266. *
  267. * @return True if the setting has changed, false otherwise
  268. */
  269. public boolean save() {
  270. if (!needsSaving()) {
  271. return false;
  272. }
  273. if (value == null) {
  274. identity.unsetOption(domain, option);
  275. } else {
  276. identity.setOption(domain, option, value);
  277. }
  278. original = value;
  279. return true;
  280. }
  281. /**
  282. * Dismisses changes to this setting.
  283. */
  284. public void dismiss() {
  285. if (Objects.equals(original, value)) {
  286. return;
  287. }
  288. value = original;
  289. for (SettingChangeListener listener : listeners) {
  290. listener.settingChanged(this);
  291. }
  292. }
  293. /**
  294. * Does the setting need saving?
  295. *
  296. * @return true iif the setting will be changed if saved
  297. */
  298. public boolean needsSaving() {
  299. return (!Objects.equals(value, original))
  300. && (value != null || original != null)
  301. && (validator == null || !validator.validate(value).isFailure());
  302. }
  303. /**
  304. * Does this setting's identity have this setting?
  305. *
  306. * @return true iif the setting is present
  307. */
  308. public boolean isSet() {
  309. return identity.hasOptionString(domain, option, validator);
  310. }
  311. }