選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FontPicker.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.components;
  23. import com.dmdirc.addons.ui_swing.components.renderers.FontListCellRenderer;
  24. import com.dmdirc.logger.ErrorLevel;
  25. import com.dmdirc.logger.Logger;
  26. import java.awt.Font;
  27. import java.awt.GraphicsEnvironment;
  28. import java.util.concurrent.ExecutionException;
  29. import javax.swing.DefaultComboBoxModel;
  30. import javax.swing.JComboBox;
  31. import javax.swing.SwingUtilities;
  32. /**
  33. * System font picking component.
  34. */
  35. public class FontPicker extends JComboBox<Object> {
  36. /** A version number for this class. */
  37. private static final long serialVersionUID = -9054812588033935839L;
  38. /** Font family to choose from. */
  39. private final String fontFamily;
  40. /**
  41. * Creates a new Font picker for the specified font family.
  42. *
  43. * @param fontFamily Font family
  44. */
  45. public FontPicker(final String fontFamily) {
  46. super(new DefaultComboBoxModel<>());
  47. this.fontFamily = fontFamily;
  48. setRenderer(new FontListCellRenderer(getRenderer()));
  49. new LoggingSwingWorker<String[], String[]>() {
  50. /** {@inheritDoc} */
  51. @Override
  52. protected String[] doInBackground() {
  53. return GraphicsEnvironment.getLocalGraphicsEnvironment().
  54. getAvailableFontFamilyNames();
  55. }
  56. /** {@inheritDoc} */
  57. @Override
  58. protected void done() {
  59. try {
  60. loadFonts(get());
  61. } catch (InterruptedException ex) {
  62. //Ignore
  63. } catch (ExecutionException ex) {
  64. Logger.appError(ErrorLevel.MEDIUM, ex.getMessage(), ex);
  65. }
  66. }
  67. }.executeInExecutor();
  68. }
  69. /**
  70. * Loads the fonts and adds them to the font picker.
  71. *
  72. * @param fonts Fonts to load
  73. */
  74. private void loadFonts(final String[] fonts) {
  75. final int size = getFont().getSize();
  76. for (final String font : fonts) {
  77. SwingUtilities.invokeLater(new Runnable() {
  78. /** {@inheritDoc} */
  79. @Override
  80. public void run() {
  81. ((DefaultComboBoxModel<Object>) getModel()).addElement(new Font(
  82. font, Font.PLAIN, size));
  83. }
  84. });
  85. }
  86. SwingUtilities.invokeLater(new Runnable() {
  87. /** {@inheritDoc} */
  88. @Override
  89. public void run() {
  90. setSelectedItem(new Font(fontFamily, Font.PLAIN, size));
  91. }
  92. });
  93. }
  94. }