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

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