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