選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PrefsComponentFactory.java 15KB

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