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.

PrefsComponentFactory.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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;
  23. import com.dmdirc.config.prefs.PreferencesSetting;
  24. import com.dmdirc.config.prefs.validator.NumericalValidator;
  25. import com.dmdirc.addons.ui_swing.components.colours.ColourChooser;
  26. import com.dmdirc.addons.ui_swing.components.FontPicker;
  27. import com.dmdirc.addons.ui_swing.components.OptionalJSpinner;
  28. import com.dmdirc.addons.ui_swing.components.colours.OptionalColourChooser;
  29. import com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay;
  30. import com.dmdirc.addons.ui_swing.components.durationeditor.DurationListener;
  31. import com.dmdirc.addons.ui_swing.components.renderers.MapEntryRenderer;
  32. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  33. import com.dmdirc.config.prefs.validator.OptionalValidator;
  34. import com.dmdirc.config.prefs.validator.Validator;
  35. import java.awt.Dimension;
  36. import java.awt.Font;
  37. import java.awt.event.ActionEvent;
  38. import java.awt.event.ActionListener;
  39. import java.awt.event.KeyAdapter;
  40. import java.awt.event.KeyEvent;
  41. import java.util.Map;
  42. import javax.swing.JCheckBox;
  43. import javax.swing.JComboBox;
  44. import javax.swing.JComponent;
  45. import javax.swing.JSpinner;
  46. import javax.swing.JTextField;
  47. import javax.swing.SpinnerNumberModel;
  48. import javax.swing.event.ChangeEvent;
  49. import javax.swing.event.ChangeListener;
  50. import org.jdesktop.jxlayer.JXLayer;
  51. /**
  52. * Provides methods for constructing a JComponent from a PreferencesSetting.
  53. */
  54. public final class PrefsComponentFactory {
  55. /**
  56. * Creates a new instance of PrefsComponentFactory.
  57. */
  58. private PrefsComponentFactory() {
  59. // Shouldn't be initialised
  60. }
  61. /**
  62. * Retrieves the component for the specified setting. Components are
  63. * initialised with the current value(s) of the setting, and have listeners
  64. * added to update the setting whenever the components are changed.
  65. *
  66. * @param setting The setting whose component is being requested
  67. * @return An appropriate JComponent descendant
  68. */
  69. public static JComponent getComponent(final PreferencesSetting setting) {
  70. JComponent option;
  71. switch (setting.getType()) {
  72. case TEXT:
  73. option = getTextOption(setting);
  74. break;
  75. case BOOLEAN:
  76. option = getBooleanOption(setting);
  77. break;
  78. case MULTICHOICE:
  79. option = getComboOption(setting);
  80. break;
  81. case INTEGER:
  82. option = getIntegerOption(setting);
  83. break;
  84. case OPTIONALINTEGER:
  85. option = getOptionalIntegerOption(setting);
  86. break;
  87. case DURATION:
  88. option = getDurationOption(setting);
  89. break;
  90. case COLOUR:
  91. option = getColourOption(setting);
  92. break;
  93. case OPTIONALCOLOUR:
  94. option = getOptionalColourOption(setting);
  95. break;
  96. case FONT:
  97. option = getFontOption(setting);
  98. break;
  99. default:
  100. throw new IllegalArgumentException(setting.getType()
  101. + " is not a valid option type");
  102. }
  103. option.setPreferredSize(new Dimension(Short.MAX_VALUE, option.getFont().
  104. getSize()));
  105. return new JXLayer<JComponent>(option);
  106. }
  107. /**
  108. * Initialises and returns a ValidatingJTextField for the specified setting.
  109. *
  110. * @param setting The setting to create the component for
  111. * @return A JComponent descendent for the specified setting
  112. */
  113. private static JComponent getTextOption(final PreferencesSetting setting) {
  114. final ValidatingJTextField option = new ValidatingJTextField(setting.getValidator());
  115. option.setText(setting.getValue());
  116. option.addKeyListener(new KeyAdapter() {
  117. @Override
  118. public void keyReleased(final KeyEvent e) {
  119. setting.setValue(((JTextField) e.getSource()).getText());
  120. }
  121. });
  122. return option;
  123. }
  124. /**
  125. * Initialises and returns a JCheckBox for the specified setting.
  126. *
  127. * @param setting The setting to create the component for
  128. * @return A JComponent descendent for the specified setting
  129. */
  130. private static JComponent getBooleanOption(final PreferencesSetting setting) {
  131. final JCheckBox option = new JCheckBox();
  132. option.setSelected(Boolean.parseBoolean(setting.getValue()));
  133. option.addChangeListener(new ChangeListener() {
  134. /** {@inheritDoc} */
  135. @Override
  136. public void stateChanged(final ChangeEvent e) {
  137. setting.setValue(String.valueOf(((JCheckBox) e.getSource()).isSelected()));
  138. }
  139. });
  140. return option;
  141. }
  142. /**
  143. * Initialises and returns a JComboBox for the specified setting.
  144. *
  145. * @param setting The setting to create the component for
  146. * @return A JComponent descendent for the specified setting
  147. */
  148. private static JComponent getComboOption(final PreferencesSetting setting) {
  149. final JComboBox option = new JComboBox(setting.getComboOptions().entrySet().toArray());
  150. option.setRenderer(new MapEntryRenderer());
  151. option.setEditable(false);
  152. for (Map.Entry<String, String> entry : setting.getComboOptions().entrySet()) {
  153. if (entry.getKey().equals(setting.getValue())) {
  154. option.setSelectedItem(entry);
  155. break;
  156. }
  157. }
  158. option.addActionListener(new ActionListener() {
  159. /** {@inheritDoc} */
  160. @Override
  161. public void actionPerformed(final ActionEvent e) {
  162. final Object selected = ((JComboBox) e.getSource()).getSelectedItem();
  163. if (selected != null) {
  164. setting.setValue((String) ((Map.Entry) selected).getKey());
  165. }
  166. }
  167. });
  168. return option;
  169. }
  170. /**
  171. * Initialises and returns a JSpinner for the specified setting.
  172. *
  173. * @param setting The setting to create the component for
  174. * @return A JComponent descendent for the specified setting
  175. */
  176. private static JComponent getIntegerOption(final PreferencesSetting setting) {
  177. JSpinner option;
  178. try {
  179. if (setting.getValidator() instanceof NumericalValidator) {
  180. option = new JSpinner(
  181. new SpinnerNumberModel(Integer.parseInt(setting.getValue()),
  182. ((NumericalValidator) setting.getValidator()).getMin(),
  183. ((NumericalValidator) setting.getValidator()).getMax(),
  184. 1));
  185. } else {
  186. option = new JSpinner(new SpinnerNumberModel());
  187. option.setValue(Integer.parseInt(setting.getValue()));
  188. }
  189. } catch (NumberFormatException ex) {
  190. option = new JSpinner(new SpinnerNumberModel());
  191. }
  192. option.addChangeListener(new ChangeListener() {
  193. /** {@inheritDoc} */
  194. @Override
  195. public void stateChanged(final ChangeEvent e) {
  196. setting.setValue(((JSpinner) e.getSource()).getValue().
  197. toString());
  198. }
  199. });
  200. return option;
  201. }
  202. /**
  203. * Initialises and returns a JSpinner for the specified setting.
  204. *
  205. * @param setting The setting to create the component for
  206. * @return A JComponent descendent for the specified setting
  207. */
  208. private static JComponent getOptionalIntegerOption(final PreferencesSetting setting) {
  209. final boolean state = setting.getValue() != null
  210. && !setting.getValue().startsWith("false:");
  211. final String integer = setting.getValue() == null ? "0" : setting.getValue().
  212. substring(1 + setting.getValue().indexOf(':'));
  213. OptionalJSpinner option;
  214. Validator optionalValidator = setting.getValidator();
  215. Validator numericalValidator = null;
  216. if (optionalValidator instanceof OptionalValidator) {
  217. numericalValidator = ((OptionalValidator) setting.getValidator()).
  218. getValidator();
  219. if (!(numericalValidator instanceof NumericalValidator)) {
  220. numericalValidator = null;
  221. }
  222. }
  223. try {
  224. if (numericalValidator != null) {
  225. option = new OptionalJSpinner(
  226. new SpinnerNumberModel(Integer.parseInt(integer),
  227. ((NumericalValidator) numericalValidator).getMin(),
  228. ((NumericalValidator) numericalValidator).getMax(),
  229. 1), state);
  230. } else {
  231. option = new OptionalJSpinner(new SpinnerNumberModel());
  232. option.setValue(Integer.parseInt(integer));
  233. option.setSelected(state);
  234. }
  235. } catch (NumberFormatException ex) {
  236. option = new OptionalJSpinner(new SpinnerNumberModel(), state);
  237. }
  238. option.addChangeListener(new ChangeListener() {
  239. /** {@inheritDoc} */
  240. @Override
  241. public void stateChanged(final ChangeEvent e) {
  242. setting.setValue(((OptionalJSpinner) e.getSource()).isSelected() + ":" +
  243. ((OptionalJSpinner) e.getSource()).getValue().
  244. toString());
  245. }
  246. });
  247. return option;
  248. }
  249. /**
  250. * Initialises and returns a DurationDisplay for the specified setting.
  251. *
  252. * @param setting The setting to create the component for
  253. * @return A JComponent descendent for the specified setting
  254. */
  255. private static JComponent getDurationOption(final PreferencesSetting setting) {
  256. DurationDisplay option;
  257. try {
  258. option = new DurationDisplay(Integer.parseInt(setting.getValue()));
  259. } catch (NumberFormatException ex) {
  260. option = new DurationDisplay();
  261. }
  262. option.addDurationListener(new DurationListener() {
  263. /** {@inheritDoc} */
  264. @Override
  265. public void durationUpdated(final int newDuration) {
  266. setting.setValue(String.valueOf(newDuration));
  267. }
  268. });
  269. return option;
  270. }
  271. /**
  272. * Initialises and returns a ColourChooser for the specified setting.
  273. *
  274. * @param setting The setting to create the component for
  275. * @return A JComponent descendent for the specified setting
  276. */
  277. private static JComponent getColourOption(final PreferencesSetting setting) {
  278. final ColourChooser option = new ColourChooser(setting.getValue(), true, true);
  279. option.addActionListener(new ActionListener() {
  280. /** {@inheritDoc} */
  281. @Override
  282. public void actionPerformed(final ActionEvent e) {
  283. setting.setValue(((ColourChooser) e.getSource()).getColour());
  284. }
  285. });
  286. return option;
  287. }
  288. /**
  289. * Initialises and returns an OptionalColourChooser for the specified setting.
  290. *
  291. * @param setting The setting to create the component for
  292. * @return A JComponent descendent for the specified setting
  293. */
  294. private static JComponent getOptionalColourOption(final PreferencesSetting setting) {
  295. final boolean state = setting.getValue() != null
  296. && !setting.getValue().startsWith("false:");
  297. final String colour = setting.getValue() == null ? "0" : setting.getValue().
  298. substring(1 + setting.getValue().indexOf(':'));
  299. final OptionalColourChooser option = new OptionalColourChooser(colour, state, true, true);
  300. option.addActionListener(new ActionListener() {
  301. /** {@inheritDoc} */
  302. @Override
  303. public void actionPerformed(final ActionEvent e) {
  304. setting.setValue(
  305. ((OptionalColourChooser) e.getSource()).isEnabled() + ":"
  306. + ((OptionalColourChooser) e.getSource()).getColour());
  307. }
  308. });
  309. return option;
  310. }
  311. /**
  312. * Initialises and returns an Font Chooser for the specified setting.
  313. *
  314. * @param setting The setting to create the component for
  315. * @return A JComponent descendent for the specified setting
  316. */
  317. private static JComponent getFontOption(final PreferencesSetting setting) {
  318. final String value = setting.getValue();
  319. final FontPicker option = new FontPicker(value);
  320. option.addActionListener(new ActionListener() {
  321. /** {@inheritDoc} */
  322. @Override
  323. public void actionPerformed(final ActionEvent e) {
  324. Object value = ((FontPicker) e.getSource()).getSelectedItem();
  325. if (value instanceof Font) {
  326. setting.setValue(((Font) value).getFamily());
  327. } else {
  328. setting.setValue(null);
  329. }
  330. }
  331. });
  332. return option;
  333. }
  334. }