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

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