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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing;
  18. import com.dmdirc.addons.ui_swing.components.FileBrowser;
  19. import com.dmdirc.addons.ui_swing.components.FontPicker;
  20. import com.dmdirc.addons.ui_swing.components.IconManager;
  21. import com.dmdirc.addons.ui_swing.components.OptionalJSpinner;
  22. import com.dmdirc.addons.ui_swing.components.TableTableModel;
  23. import com.dmdirc.addons.ui_swing.components.colours.OptionalColourChooser;
  24. import com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay;
  25. import com.dmdirc.addons.ui_swing.components.renderers.MapEntryRenderer;
  26. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  27. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  28. import com.dmdirc.config.GlobalConfig;
  29. import com.dmdirc.config.prefs.PreferencesSetting;
  30. import com.dmdirc.events.eventbus.EventBus;
  31. import com.dmdirc.ui.messages.ColourManager;
  32. import com.dmdirc.util.validators.NumericalValidator;
  33. import com.dmdirc.util.validators.OptionalValidator;
  34. import com.dmdirc.util.validators.Validator;
  35. import java.awt.Dimension;
  36. import java.awt.Font;
  37. import java.awt.event.KeyAdapter;
  38. import java.awt.event.KeyEvent;
  39. import java.util.Map;
  40. import javax.inject.Inject;
  41. import javax.inject.Singleton;
  42. import javax.swing.AbstractButton;
  43. import javax.swing.BorderFactory;
  44. import javax.swing.DefaultComboBoxModel;
  45. import javax.swing.JCheckBox;
  46. import javax.swing.JComboBox;
  47. import javax.swing.JComponent;
  48. import javax.swing.JFileChooser;
  49. import javax.swing.JLayer;
  50. import javax.swing.JPanel;
  51. import javax.swing.JScrollPane;
  52. import javax.swing.JSpinner;
  53. import javax.swing.JTable;
  54. import javax.swing.SpinnerNumberModel;
  55. import javax.swing.text.JTextComponent;
  56. import net.miginfocom.swing.MigLayout;
  57. /**
  58. * Provides methods for constructing a JComponent from a PreferencesSetting.
  59. */
  60. @Singleton
  61. public final class PrefsComponentFactory {
  62. /** The icon manager to use for dialog and error icons. */
  63. private final IconManager iconManager;
  64. /** The colour manager to use for colour preferences. */
  65. private final ColourManager colourManager;
  66. /** The global event bus. */
  67. private final EventBus eventBus;
  68. /**
  69. * Creates a new instance of PrefsComponentFactory.
  70. *
  71. * @param eventBus The global event bus.
  72. * @param iconManager The icon manager to use for dialog and error icons.
  73. * @param colourManager The colour manager to use for colour preferences.
  74. */
  75. @Inject
  76. public PrefsComponentFactory(
  77. final EventBus eventBus,
  78. final IconManager iconManager,
  79. @GlobalConfig final ColourManager colourManager) {
  80. this.iconManager = iconManager;
  81. this.colourManager = colourManager;
  82. this.eventBus = eventBus;
  83. }
  84. /**
  85. * Retrieves the component for the specified setting. Components are initialised with the
  86. * current value(s) of the setting, and have listeners added to update the setting whenever the
  87. * components are changed.
  88. *
  89. * @param setting The setting whose component is being requested
  90. *
  91. * @return An appropriate JComponent descendant
  92. */
  93. public JComponent getComponent(final PreferencesSetting setting) {
  94. final JComponent option;
  95. switch (setting.getType()) {
  96. case TEXT:
  97. option = getTextOption(setting);
  98. break;
  99. case BOOLEAN:
  100. option = getBooleanOption(setting);
  101. break;
  102. case MULTICHOICE:
  103. option = getComboOption(setting);
  104. break;
  105. case INTEGER:
  106. option = getIntegerOption(setting);
  107. break;
  108. case OPTIONALINTEGER:
  109. option = getOptionalIntegerOption(setting);
  110. break;
  111. case DURATION:
  112. option = getDurationOption(setting);
  113. break;
  114. case COLOUR:
  115. option = getColourOption(setting);
  116. break;
  117. case OPTIONALCOLOUR:
  118. option = getOptionalColourOption(setting);
  119. break;
  120. case FONT:
  121. option = getFontOption(setting);
  122. break;
  123. case FILE:
  124. option = getFileBrowseOption(setting, JFileChooser.FILES_ONLY);
  125. break;
  126. case DIRECTORY:
  127. option = getFileBrowseOption(setting,
  128. JFileChooser.DIRECTORIES_ONLY);
  129. break;
  130. case FILES_AND_DIRECTORIES:
  131. option = getFileBrowseOption(setting,
  132. JFileChooser.FILES_AND_DIRECTORIES);
  133. break;
  134. case LABEL:
  135. option = getLabelOption(setting);
  136. break;
  137. case TABLE:
  138. option = getTableOption(setting);
  139. break;
  140. default:
  141. throw new IllegalArgumentException(setting.getType()
  142. + " is not a valid option type");
  143. }
  144. option.setPreferredSize(new Dimension(Short.MAX_VALUE, option.getFont().
  145. getSize()));
  146. return new JLayer<>(option);
  147. }
  148. /**
  149. * Initialises and returns a ValidatingJTextField for the specified setting.
  150. *
  151. * @param setting The setting to create the component for
  152. *
  153. * @return A JComponent descendant for the specified setting
  154. */
  155. private JComponent getTextOption(final PreferencesSetting setting) {
  156. final ValidatingJTextField option = new ValidatingJTextField(
  157. iconManager, setting.getValidator());
  158. option.setText(setting.getValue());
  159. option.addKeyListener(new KeyAdapter() {
  160. @Override
  161. public void keyReleased(final KeyEvent e) {
  162. setting.setValue(((JTextComponent) e.getSource()).getText());
  163. }
  164. });
  165. return option;
  166. }
  167. /**
  168. * Initialises and returns a JCheckBox for the specified setting.
  169. *
  170. * @param setting The setting to create the component for
  171. *
  172. * @return A JComponent descendant for the specified setting
  173. */
  174. private JComponent getBooleanOption(
  175. final PreferencesSetting setting) {
  176. final JCheckBox option = new JCheckBox();
  177. option.setSelected(Boolean.parseBoolean(setting.getValue()));
  178. option.setOpaque(false);
  179. option.addChangeListener(e -> setting
  180. .setValue(String.valueOf(((AbstractButton) e.getSource()).isSelected())));
  181. return option;
  182. }
  183. /**
  184. * Initialises and returns a JComboBox for the specified setting.
  185. *
  186. * @param setting The setting to create the component for
  187. *
  188. * @return A JComponent descendant for the specified setting
  189. */
  190. private JComponent getComboOption(final PreferencesSetting setting) {
  191. final DefaultComboBoxModel<Map.Entry<String, String>> model = new DefaultComboBoxModel<>();
  192. setting.getComboOptions().entrySet().forEach(model::addElement);
  193. final JComboBox<Map.Entry<String, String>> option = new JComboBox<>(model);
  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(e -> {
  204. final Object selected = option.getSelectedItem();
  205. if (selected != null) {
  206. if (selected instanceof Map.Entry) {
  207. setting.setValue(castToMapEntry(selected).getKey());
  208. }
  209. }
  210. });
  211. return option;
  212. }
  213. @SuppressWarnings("unchecked")
  214. private Map.Entry<String, String> castToMapEntry(final Object value) {
  215. return (Map.Entry<String, String>) value;
  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 descendant 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(e -> setting.setValue(((JSpinner) e.getSource()).getValue().
  250. toString()));
  251. return option;
  252. }
  253. /**
  254. * Initialises and returns a JSpinner for the specified setting.
  255. *
  256. * @param setting The setting to create the component for
  257. *
  258. * @return A JComponent descendant for the specified setting
  259. */
  260. private JComponent getOptionalIntegerOption(
  261. final PreferencesSetting setting) {
  262. final boolean state = setting.getValue() != null
  263. && !setting.getValue().startsWith("false:");
  264. final String integer = setting.getValue() == null ? "0" : setting
  265. .getValue().substring(1 + setting.getValue().indexOf(':'));
  266. final Validator<?> optionalValidator = setting.getValidator();
  267. Validator<?> numericalValidator = null;
  268. if (optionalValidator instanceof OptionalValidator) {
  269. numericalValidator = ((OptionalValidator) setting.getValidator()).
  270. getValidator();
  271. if (!(numericalValidator instanceof NumericalValidator)) {
  272. numericalValidator = null;
  273. }
  274. }
  275. OptionalJSpinner option;
  276. try {
  277. if (numericalValidator == null) {
  278. option = new OptionalJSpinner(new SpinnerNumberModel());
  279. option.setValue(Integer.parseInt(integer));
  280. option.setSelected(state);
  281. } else {
  282. option = new OptionalJSpinner(
  283. new SpinnerNumberModel(Integer.parseInt(integer),
  284. ((NumericalValidator) numericalValidator).getMin(),
  285. ((NumericalValidator) numericalValidator).getMax(),
  286. 1), state);
  287. }
  288. } catch (final NumberFormatException ex) {
  289. option = new OptionalJSpinner(new SpinnerNumberModel(), state);
  290. }
  291. option.addChangeListener(e -> setting.setValue(((OptionalJSpinner) e.getSource()).isSelected()
  292. + ":" + ((OptionalJSpinner) e.getSource()).getValue()));
  293. return option;
  294. }
  295. /**
  296. * Initialises and returns a DurationDisplay for the specified setting.
  297. *
  298. * @param setting The setting to create the component for
  299. *
  300. * @return A JComponent descendant for the specified setting
  301. */
  302. private JComponent getDurationOption(
  303. final PreferencesSetting setting) {
  304. DurationDisplay option;
  305. try {
  306. option = new DurationDisplay(iconManager, Integer.parseInt(setting.getValue()));
  307. } catch (final NumberFormatException ex) {
  308. option = new DurationDisplay(iconManager);
  309. }
  310. option.addDurationListener(newDuration -> setting.setValue(String.valueOf(newDuration)));
  311. return option;
  312. }
  313. /**
  314. * Initialises and returns a ColourChooser for the specified setting.
  315. *
  316. * @param setting The setting to create the component for
  317. *
  318. * @return A JComponent descendant for the specified setting
  319. */
  320. private JComponent getColourOption(
  321. final PreferencesSetting setting) {
  322. final OptionalColourChooser option = new OptionalColourChooser(
  323. iconManager, colourManager, setting.getValue(), true, true, true);
  324. option.addActionListener(e -> {
  325. final OptionalColourChooser chooser = (OptionalColourChooser) e.getSource();
  326. setting.setValue(chooser.isEnabled() + ":" + chooser.getColour());
  327. });
  328. return option;
  329. }
  330. /**
  331. * Initialises and returns an OptionalColourChooser for the specified setting.
  332. *
  333. * @param setting The setting to create the component for
  334. *
  335. * @return A JComponent descendant for the specified setting
  336. */
  337. private JComponent getOptionalColourOption(
  338. final PreferencesSetting setting) {
  339. final boolean state = setting.getValue() != null
  340. && !setting.getValue().startsWith("false:");
  341. final String colour = setting.getValue() == null ? "0" : setting
  342. .getValue().substring(1 + setting.getValue().indexOf(':'));
  343. final OptionalColourChooser option = new OptionalColourChooser(
  344. iconManager, colourManager, colour, state, true, true);
  345. option.addActionListener(e -> setting.setValue(((OptionalColourChooser) e.getSource())
  346. .isEnabled() + ":" + ((OptionalColourChooser) e
  347. .getSource()).getColour()));
  348. return option;
  349. }
  350. /**
  351. * Initialises and returns an Font Chooser for the specified setting.
  352. *
  353. * @param setting The setting to create the component for
  354. *
  355. * @return A JComponent descendant for the specified setting
  356. */
  357. private JComponent getFontOption(final PreferencesSetting setting) {
  358. final String value = setting.getValue();
  359. final FontPicker option = new FontPicker(eventBus, value);
  360. option.addActionListener(e -> {
  361. final Object value1 = option.getSelectedItem();
  362. if (value1 instanceof Font) {
  363. setting.setValue(((Font) value1).getFamily());
  364. } else {
  365. setting.setValue(null);
  366. }
  367. });
  368. return option;
  369. }
  370. /**
  371. * Initialises and returns a FileBrowser for the specified setting.
  372. *
  373. * @param setting The setting to create the component for
  374. * @param type The type of file chooser we want (Files/Directories/Both)
  375. *
  376. * @return A JComponent descendant for the specified setting
  377. */
  378. private JComponent getFileBrowseOption(
  379. final PreferencesSetting setting, final int type) {
  380. final FileBrowser option = new FileBrowser(iconManager, setting, type);
  381. option.addActionListener(e -> setting.setValue(option.getPath()));
  382. option.addKeyListener(new KeyAdapter() {
  383. @Override
  384. public void keyReleased(final KeyEvent e) {
  385. setting.setValue(((JTextComponent) e.getSource()).getText());
  386. }
  387. });
  388. return option;
  389. }
  390. /**
  391. * Initialises and returns a Label for the specified setting.
  392. *
  393. * @param setting The setting to create the component for
  394. *
  395. * @return A JComponent descendant for the specified setting
  396. */
  397. private static JComponent getLabelOption(final PreferencesSetting setting) {
  398. final JPanel panel = new JPanel(new MigLayout("fill"));
  399. panel.add(new TextLabel(setting.getValue()));
  400. panel.setBorder(BorderFactory.createTitledBorder(panel.getBorder(), setting.getTitle()));
  401. return panel;
  402. }
  403. /**
  404. * Initialises and returns a Table for the specified setting.
  405. *
  406. * @param setting The setting to create the component for
  407. *
  408. * @return A JComponent descendant for the specified setting
  409. */
  410. private JComponent getTableOption(final PreferencesSetting setting) {
  411. final JTable table = new JTable(new TableTableModel(setting.getTableHeaders(),
  412. setting.getTableOptions(), (Integer i1, Integer i2) -> true));
  413. final JScrollPane sp = new JScrollPane();
  414. sp.setViewportView(table);
  415. table.setAutoCreateRowSorter(true);
  416. return sp;
  417. }
  418. }