Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SettingsPanel.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2006-2010 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.addons.ui_swing.components.expandingsettings;
  23. import com.dmdirc.config.Identity;
  24. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  25. import com.dmdirc.addons.ui_swing.UIUtilities;
  26. import java.util.LinkedHashMap;
  27. import java.util.Map;
  28. import java.util.Map.Entry;
  29. import javax.swing.BorderFactory;
  30. import javax.swing.JPanel;
  31. import javax.swing.JScrollPane;
  32. import javax.swing.UIManager;
  33. import net.miginfocom.swing.MigLayout;
  34. /**
  35. * Settings panel.
  36. */
  37. public final class SettingsPanel extends JPanel {
  38. /**
  39. * A version number for this class. It should be changed whenever the class
  40. * structure is changed (or anything else that would prevent serialized
  41. * objects being unserialized with the new class).
  42. */
  43. private static final long serialVersionUID = 2;
  44. /** Config manager. */
  45. private final transient Identity config;
  46. /** Valid option types. */
  47. public enum OptionType {
  48. /** Text field. */
  49. TEXTFIELD,
  50. /** Check box. */
  51. CHECKBOX,
  52. /** Colour chooser. */
  53. COLOUR,
  54. /** Number spinner. */
  55. SPINNER,
  56. /** Font picker. */
  57. FONT,
  58. /** Combo box. */
  59. COMBOBOX,
  60. }
  61. /** config option -> name. */
  62. private Map<String, String> names;
  63. /** config option -> type. */
  64. private Map<String, OptionType> types;
  65. /** Info label. */
  66. private TextLabel infoLabel;
  67. /** Current options panel. */
  68. private CurrentOptionsPanel currentOptionsPanel;
  69. /** Add option panel. */
  70. private AddOptionPanel addOptionPanel;
  71. /** Current options scroll pane. */
  72. private JScrollPane scrollPane;
  73. /**
  74. * Creates a new instance of SettingsPanel.
  75. *
  76. * @param config Config to use
  77. * @param infoText Info blurb.
  78. */
  79. public SettingsPanel(final Identity config, final String infoText) {
  80. super();
  81. this.setOpaque(UIUtilities.getTabbedPaneOpaque());
  82. this.config = config;
  83. initComponents(infoText);
  84. layoutComponents();
  85. }
  86. /**
  87. * Initialises the components.
  88. *
  89. * @param infoText Info blurb.
  90. */
  91. private void initComponents(final String infoText) {
  92. names = new LinkedHashMap<String, String>();
  93. types = new LinkedHashMap<String, OptionType>();
  94. infoLabel = new TextLabel(infoText);
  95. addOptionPanel =
  96. new AddOptionPanel(this);
  97. currentOptionsPanel =
  98. new CurrentOptionsPanel(this);
  99. scrollPane = new JScrollPane(currentOptionsPanel);
  100. scrollPane.setBorder(BorderFactory.createTitledBorder(UIManager.
  101. getBorder("TitledBorder.border"), "Current settings"));
  102. addOptionPanel.setBorder(BorderFactory.createTitledBorder(UIManager.
  103. getBorder("TitledBorder.border"), "Add new setting"));
  104. scrollPane.setOpaque(UIUtilities.getTabbedPaneOpaque());
  105. scrollPane.getViewport().setOpaque(UIUtilities.getTabbedPaneOpaque());
  106. }
  107. /**
  108. * Lays out the components.
  109. */
  110. private void layoutComponents() {
  111. setLayout(new MigLayout("fill, wrap 1"));
  112. add(infoLabel, "growx, pushx");
  113. add(scrollPane, "grow, push");
  114. add(addOptionPanel, "growx, pushx");
  115. }
  116. /**
  117. * Adds an option to the settings panel.
  118. *
  119. * @param optionName Option name
  120. * @param displayName Display name
  121. * @param type Option type
  122. */
  123. public void addOption(final String optionName,
  124. final String displayName,
  125. final OptionType type) {
  126. if (config == null) {
  127. return;
  128. }
  129. if (optionName.indexOf('.') == -1) {
  130. return;
  131. }
  132. final String[] splitOption = optionName.split("\\.");
  133. names.put(optionName, displayName);
  134. types.put(optionName, type);
  135. if (config.hasOptionString(splitOption[0], splitOption[1])) {
  136. addCurrentOption(optionName, type,
  137. config.getOption(splitOption[0], splitOption[1]));
  138. } else {
  139. addAddableOption(optionName);
  140. }
  141. }
  142. /** Updates the options. */
  143. public void update() {
  144. addOptionPanel.clearOptions();
  145. currentOptionsPanel.clearOptions();
  146. for (Entry<String, OptionType> entry : types.entrySet()) {
  147. final String[] splitOption = entry.getKey().split("\\.");
  148. if (config.hasOptionString(splitOption[0], splitOption[1])) {
  149. addCurrentOption(entry.getKey(), entry.getValue(),
  150. config.getOption(splitOption[0], splitOption[1]));
  151. } else {
  152. addAddableOption(entry.getKey());
  153. }
  154. }
  155. }
  156. /** Saves the options to the config. */
  157. public void save() {
  158. for (Entry<String, OptionType> entry : types.entrySet()) {
  159. final String value =
  160. currentOptionsPanel.getOption(entry.getKey(),
  161. entry.getValue());
  162. final String[] splitOption = entry.getKey().split("\\.");
  163. if (value == null) {
  164. config.unsetOption(splitOption[0], splitOption[1]);
  165. } else {
  166. config.setOption(splitOption[0], splitOption[1], value);
  167. }
  168. }
  169. }
  170. /**
  171. * Adds a current option.
  172. *
  173. * @param optionName option to add
  174. * @param type Option type
  175. * @param value Option value
  176. */
  177. protected void addCurrentOption(final String optionName,
  178. final OptionType type, final String value) {
  179. currentOptionsPanel.addOption(optionName, type, value);
  180. }
  181. /**
  182. * Deletes a current option.
  183. *
  184. * @param optionName Option to delete
  185. * @param type Option type
  186. */
  187. protected void removeCurrentOption(final String optionName,
  188. final OptionType type) {
  189. currentOptionsPanel.delOption(optionName, type);
  190. }
  191. /**
  192. * Adds an addable option.
  193. *
  194. * @param optionName Option name
  195. */
  196. protected void addAddableOption(final String optionName) {
  197. addOptionPanel.addOption(optionName);
  198. }
  199. /**
  200. * Returns the display name for a config option.
  201. *
  202. * @param optionName Option name to return the name for
  203. *
  204. * @return Display name for a specified option
  205. */
  206. public String getOptionName(final String optionName) {
  207. return names.get(optionName);
  208. }
  209. /**
  210. * Returns the option type for a config option.
  211. *
  212. * @param optionName Option name to return the type of
  213. *
  214. * @return Option type for a specified option
  215. */
  216. public OptionType getOptionType(final String optionName) {
  217. return types.get(optionName);
  218. }
  219. }