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.

FontPicker.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.components;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.renderers.FontListCellRenderer;
  20. import com.dmdirc.events.eventbus.EventBus;
  21. import com.google.common.collect.Lists;
  22. import java.awt.Font;
  23. import java.awt.GraphicsEnvironment;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import javax.swing.DefaultComboBoxModel;
  27. import javax.swing.JComboBox;
  28. import javax.swing.MutableComboBoxModel;
  29. import static com.google.common.base.Preconditions.checkNotNull;
  30. /**
  31. * System font picking component.
  32. */
  33. public class FontPicker extends JComboBox<Object> {
  34. /** A version number for this class. */
  35. private static final long serialVersionUID = -9054812588033935839L;
  36. /** Font family to choose from. */
  37. private final String fontFamily;
  38. /**
  39. * Creates a new Font picker for the specified font family.
  40. *
  41. * @param eventBus The event bus to post errors to
  42. * @param fontFamily Font family
  43. */
  44. public FontPicker(final EventBus eventBus, final String fontFamily) {
  45. super(new DefaultComboBoxModel<>());
  46. this.fontFamily = fontFamily;
  47. setRenderer(new FontListCellRenderer(getRenderer()));
  48. UIUtilities.invokeOffEDT(this::getFonts, this::loadFonts);
  49. }
  50. /**
  51. * Loads the fonts and adds them to the font picker.
  52. *
  53. * @param fonts Fonts to load
  54. */
  55. private void loadFonts(final List<String> fonts) {
  56. checkNotNull(fonts);
  57. final int size = getFont() == null ? 12 : getFont().getSize();
  58. for (final String font : fonts) {
  59. ((MutableComboBoxModel<Object>) getModel()).addElement(new Font(font, Font.PLAIN, size));
  60. }
  61. setSelectedItem(new Font(fontFamily, Font.PLAIN, size));
  62. }
  63. private List<String> getFonts() {
  64. final String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
  65. .getAvailableFontFamilyNames();
  66. if (fonts == null) {
  67. return new ArrayList<>();
  68. }
  69. return Lists.newArrayList(fonts);
  70. }
  71. }