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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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.DMDircMBassador;
  25. import com.dmdirc.addons.ui_swing.components.FileBrowser;
  26. import com.dmdirc.addons.ui_swing.components.FontPicker;
  27. import com.dmdirc.addons.ui_swing.components.OptionalJSpinner;
  28. import com.dmdirc.addons.ui_swing.components.colours.OptionalColourChooser;
  29. import com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay;
  30. import com.dmdirc.addons.ui_swing.components.durationeditor.DurationListener;
  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.ui.IconManager;
  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.ActionEvent;
  43. import java.awt.event.ActionListener;
  44. import java.awt.event.KeyAdapter;
  45. import java.awt.event.KeyEvent;
  46. import java.util.Map;
  47. import javax.inject.Inject;
  48. import javax.inject.Singleton;
  49. import javax.swing.BorderFactory;
  50. import javax.swing.JCheckBox;
  51. import javax.swing.JComboBox;
  52. import javax.swing.JComponent;
  53. import javax.swing.JFileChooser;
  54. import javax.swing.JPanel;
  55. import javax.swing.JSpinner;
  56. import javax.swing.JTextField;
  57. import javax.swing.SpinnerNumberModel;
  58. import javax.swing.event.ChangeEvent;
  59. import javax.swing.event.ChangeListener;
  60. import net.miginfocom.swing.MigLayout;
  61. import org.jdesktop.jxlayer.JXLayer;
  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 DMDircMBassador 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 DMDircMBassador eventBus,
  83. @GlobalConfig 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. default:
  143. throw new IllegalArgumentException(setting.getType()
  144. + " is not a valid option type");
  145. }
  146. option.setPreferredSize(new Dimension(Short.MAX_VALUE, option.getFont().
  147. getSize()));
  148. return new JXLayer<>(option);
  149. }
  150. /**
  151. * Initialises and returns a ValidatingJTextField for the specified setting.
  152. *
  153. * @param setting The setting to create the component for
  154. *
  155. * @return A JComponent descendant for the specified setting
  156. */
  157. private JComponent getTextOption(final PreferencesSetting setting) {
  158. final ValidatingJTextField option = new ValidatingJTextField(
  159. iconManager, setting.getValidator());
  160. option.setText(setting.getValue());
  161. option.addKeyListener(new KeyAdapter() {
  162. @Override
  163. public void keyReleased(final KeyEvent e) {
  164. setting.setValue(((JTextField) e.getSource()).getText());
  165. }
  166. });
  167. return option;
  168. }
  169. /**
  170. * Initialises and returns a JCheckBox for the specified setting.
  171. *
  172. * @param setting The setting to create the component for
  173. *
  174. * @return A JComponent descendant for the specified setting
  175. */
  176. private JComponent getBooleanOption(
  177. final PreferencesSetting setting) {
  178. final JCheckBox option = new JCheckBox();
  179. option.setSelected(Boolean.parseBoolean(setting.getValue()));
  180. option.setOpaque(false);
  181. option.addChangeListener(new ChangeListener() {
  182. @Override
  183. public void stateChanged(final ChangeEvent e) {
  184. setting.setValue(String.valueOf(((JCheckBox) e.getSource())
  185. .isSelected()));
  186. }
  187. });
  188. return option;
  189. }
  190. /**
  191. * Initialises and returns a JComboBox for the specified setting.
  192. *
  193. * @param setting The setting to create the component for
  194. *
  195. * @return A JComponent descendant for the specified setting
  196. */
  197. private JComponent getComboOption(final PreferencesSetting setting) {
  198. final JComboBox<Object> option = new JComboBox<>(setting.getComboOptions().entrySet().
  199. toArray());
  200. option.setRenderer(new MapEntryRenderer(option.getRenderer()));
  201. option.setEditable(false);
  202. for (final Map.Entry<String, String> entry : setting.getComboOptions()
  203. .entrySet()) {
  204. if (entry.getKey().equals(setting.getValue())) {
  205. option.setSelectedItem(entry);
  206. break;
  207. }
  208. }
  209. option.addActionListener(new ActionListener() {
  210. @Override
  211. @SuppressWarnings("unchecked")
  212. public void actionPerformed(final ActionEvent e) {
  213. final Object selected = ((JComboBox<?>) e.getSource())
  214. .getSelectedItem();
  215. if (selected != null) {
  216. setting.setValue(((Map.Entry<String, String>) selected).getKey());
  217. }
  218. }
  219. });
  220. return option;
  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(new ChangeListener() {
  255. @Override
  256. public void stateChanged(final ChangeEvent e) {
  257. setting.setValue(((JSpinner) e.getSource()).getValue().
  258. toString());
  259. }
  260. });
  261. return option;
  262. }
  263. /**
  264. * Initialises and returns a JSpinner for the specified setting.
  265. *
  266. * @param setting The setting to create the component for
  267. *
  268. * @return A JComponent descendant for the specified setting
  269. */
  270. private JComponent getOptionalIntegerOption(
  271. final PreferencesSetting setting) {
  272. final boolean state = setting.getValue() != null
  273. && !setting.getValue().startsWith("false:");
  274. final String integer = setting.getValue() == null ? "0" : setting
  275. .getValue().substring(1 + setting.getValue().indexOf(':'));
  276. OptionalJSpinner option;
  277. final Validator<?> optionalValidator = setting.getValidator();
  278. Validator<?> numericalValidator = null;
  279. if (optionalValidator instanceof OptionalValidator) {
  280. numericalValidator = ((OptionalValidator) setting.getValidator()).
  281. getValidator();
  282. if (!(numericalValidator instanceof NumericalValidator)) {
  283. numericalValidator = null;
  284. }
  285. }
  286. try {
  287. if (numericalValidator == null) {
  288. option = new OptionalJSpinner(new SpinnerNumberModel());
  289. option.setValue(Integer.parseInt(integer));
  290. option.setSelected(state);
  291. } else {
  292. option = new OptionalJSpinner(
  293. new SpinnerNumberModel(Integer.parseInt(integer),
  294. ((NumericalValidator) numericalValidator).getMin(),
  295. ((NumericalValidator) numericalValidator).getMax(),
  296. 1), state);
  297. }
  298. } catch (final NumberFormatException ex) {
  299. option = new OptionalJSpinner(new SpinnerNumberModel(), state);
  300. }
  301. option.addChangeListener(new ChangeListener() {
  302. @Override
  303. public void stateChanged(final ChangeEvent e) {
  304. setting.setValue(((OptionalJSpinner) e.getSource()).isSelected()
  305. + ":" + ((OptionalJSpinner) e.getSource()).getValue().
  306. toString());
  307. }
  308. });
  309. return option;
  310. }
  311. /**
  312. * Initialises and returns a DurationDisplay for the specified setting.
  313. *
  314. * @param setting The setting to create the component for
  315. *
  316. * @return A JComponent descendant for the specified setting
  317. */
  318. private JComponent getDurationOption(
  319. final PreferencesSetting setting) {
  320. DurationDisplay option;
  321. try {
  322. option = new DurationDisplay(iconManager, Integer.parseInt(setting.getValue()));
  323. } catch (final NumberFormatException ex) {
  324. option = new DurationDisplay(iconManager);
  325. }
  326. option.addDurationListener(new DurationListener() {
  327. @Override
  328. public void durationUpdated(final int newDuration) {
  329. setting.setValue(String.valueOf(newDuration));
  330. }
  331. });
  332. return option;
  333. }
  334. /**
  335. * Initialises and returns a ColourChooser for the specified setting.
  336. *
  337. * @param setting The setting to create the component for
  338. *
  339. * @return A JComponent descendant for the specified setting
  340. */
  341. private JComponent getColourOption(
  342. final PreferencesSetting setting) {
  343. final OptionalColourChooser option = new OptionalColourChooser(
  344. iconManager, colourManager, setting.getValue(), true, true, true);
  345. option.addActionListener(new ActionListener() {
  346. @Override
  347. public void actionPerformed(final ActionEvent e) {
  348. final OptionalColourChooser chooser = (OptionalColourChooser) e.getSource();
  349. setting.setValue(chooser.isEnabled() + ":" + 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 descendant 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. @Override
  371. public void actionPerformed(final ActionEvent e) {
  372. setting.setValue(((OptionalColourChooser) e.getSource())
  373. .isEnabled() + ":" + ((OptionalColourChooser) e
  374. .getSource()).getColour());
  375. }
  376. });
  377. return option;
  378. }
  379. /**
  380. * Initialises and returns an Font Chooser for the specified setting.
  381. *
  382. * @param setting The setting to create the component for
  383. *
  384. * @return A JComponent descendant for the specified setting
  385. */
  386. private JComponent getFontOption(final PreferencesSetting setting) {
  387. final String value = setting.getValue();
  388. final FontPicker option = new FontPicker(eventBus, value);
  389. option.addActionListener(new ActionListener() {
  390. @Override
  391. public void actionPerformed(final ActionEvent e) {
  392. final Object value = ((FontPicker) e.getSource())
  393. .getSelectedItem();
  394. if (value instanceof Font) {
  395. setting.setValue(((Font) value).getFamily());
  396. } else {
  397. setting.setValue(null);
  398. }
  399. }
  400. });
  401. return option;
  402. }
  403. /**
  404. * Initialises and returns a FileBrowser for the specified setting.
  405. *
  406. * @param setting The setting to create the component for
  407. * @param type The type of file chooser we want (Files/Directories/Both)
  408. *
  409. * @return A JComponent descendant for the specified setting
  410. */
  411. private JComponent getFileBrowseOption(
  412. final PreferencesSetting setting, final int type) {
  413. final FileBrowser option = new FileBrowser(iconManager, setting, type);
  414. option.addActionListener(new ActionListener() {
  415. @Override
  416. public void actionPerformed(final ActionEvent e) {
  417. setting.setValue(option.getPath());
  418. }
  419. });
  420. option.addKeyListener(new KeyAdapter() {
  421. @Override
  422. public void keyReleased(final KeyEvent e) {
  423. setting.setValue(((JTextField) e.getSource()).getText());
  424. }
  425. });
  426. return option;
  427. }
  428. /**
  429. * Initialises and returns a Label for the specified setting.
  430. *
  431. * @param setting The setting to create the component for
  432. *
  433. * @return A JComponent descendant for the specified setting
  434. */
  435. private static JComponent getLabelOption(final PreferencesSetting setting) {
  436. final JPanel panel = new JPanel(new MigLayout("fill"));
  437. panel.add(new TextLabel(setting.getValue()));
  438. panel.setBorder(BorderFactory.createTitledBorder(panel.getBorder(),
  439. setting.getTitle()));
  440. return panel;
  441. }
  442. }