您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SwingUIInitialiser.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.AddonConfig;
  24. import com.dmdirc.addons.ui_swing.dialogs.DialogKeyListener;
  25. import com.dmdirc.config.GlobalConfig;
  26. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  27. import com.dmdirc.interfaces.config.ConfigProvider;
  28. import com.dmdirc.util.LogUtils;
  29. import java.awt.Font;
  30. import java.awt.KeyboardFocusManager;
  31. import java.awt.Toolkit;
  32. import javax.inject.Inject;
  33. import javax.swing.UIManager;
  34. import javax.swing.UnsupportedLookAndFeelException;
  35. import net.miginfocom.layout.PlatformDefaults;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;
  38. /**
  39. * Initialises swing and system UI settings.
  40. */
  41. public class SwingUIInitialiser {
  42. private static final Logger LOG = LoggerFactory.getLogger(SwingUIInitialiser.class);
  43. private final Apple apple;
  44. private final AggregateConfigProvider globalConfig;
  45. private final ConfigProvider addonConfig;
  46. private final DialogKeyListener dialogKeyListener;
  47. private final DMDircEventQueue eventQueue;
  48. @Inject
  49. public SwingUIInitialiser(final Apple apple,
  50. @GlobalConfig final AggregateConfigProvider globalConfig,
  51. @AddonConfig final ConfigProvider addonConfig,
  52. final DialogKeyListener dialogKeyListener,
  53. final DMDircEventQueue eventQueue) {
  54. this.apple = apple;
  55. this.globalConfig = globalConfig;
  56. this.addonConfig = addonConfig;
  57. this.dialogKeyListener = dialogKeyListener;
  58. this.eventQueue = eventQueue;
  59. }
  60. public void load() {
  61. apple.load();
  62. setAntiAlias();
  63. initUISettings();
  64. installEventQueue();
  65. installKeyListener();
  66. }
  67. public void unload() {
  68. uninstallEventQueue();
  69. uninstallKeyListener();
  70. }
  71. /**
  72. * Make swing not use Anti Aliasing if the user doesn't want it.
  73. */
  74. private void setAntiAlias() {
  75. // For this to work it *HAS* to be before anything else UI related.
  76. final boolean aaSetting = globalConfig.getOptionBool("ui", "antialias");
  77. System.setProperty("awt.useSystemAAFontSettings",
  78. Boolean.toString(aaSetting));
  79. System.setProperty("swing.aatext", Boolean.toString(aaSetting));
  80. }
  81. /**
  82. * Initialises the global UI settings for the Swing UI.
  83. */
  84. private void initUISettings() {
  85. UIUtilities.invokeAndWait(() -> {
  86. // This will do nothing on non OS X Systems
  87. if (Apple.isApple()) {
  88. apple.setUISettings();
  89. apple.setListener();
  90. }
  91. final Font defaultFont = new Font(Font.DIALOG, Font.TRUETYPE_FONT, 12);
  92. if (UIManager.getFont("TextField.font") == null) {
  93. UIManager.put("TextField.font", defaultFont);
  94. }
  95. if (UIManager.getFont("TextPane.font") == null) {
  96. UIManager.put("TextPane.font", defaultFont);
  97. }
  98. addonConfig.setOption("ui", "textPaneFontName",
  99. UIManager.getFont("TextPane.font").getFamily());
  100. addonConfig.setOption("ui", "textPaneFontSize",
  101. UIManager.getFont("TextPane.font").getSize());
  102. try {
  103. UIUtilities.initUISettings();
  104. UIManager.setLookAndFeel(UIUtilities.getLookAndFeel(
  105. globalConfig.getOption("ui", "lookandfeel")));
  106. UIUtilities.setUIFont(new Font(globalConfig.getOption("ui", "textPaneFontName"),
  107. Font.PLAIN, 12));
  108. } catch (UnsupportedOperationException | UnsupportedLookAndFeelException |
  109. IllegalAccessException | InstantiationException | ClassNotFoundException ex) {
  110. LOG.info(LogUtils.USER_ERROR, "Unable to set UI settings", ex);
  111. }
  112. if ("Metal".equals(UIManager.getLookAndFeel().getName())
  113. || Apple.isAppleUI()) {
  114. PlatformDefaults.setPlatform(PlatformDefaults.WINDOWS_XP);
  115. }
  116. });
  117. }
  118. /**
  119. * Installs the dialog key listener.
  120. */
  121. private void installKeyListener() {
  122. UIUtilities.invokeAndWait(() -> KeyboardFocusManager.getCurrentKeyboardFocusManager()
  123. .addKeyEventDispatcher(dialogKeyListener));
  124. }
  125. /**
  126. * Removes the dialog key listener.
  127. */
  128. private void uninstallKeyListener() {
  129. KeyboardFocusManager.getCurrentKeyboardFocusManager()
  130. .removeKeyEventDispatcher(dialogKeyListener);
  131. }
  132. /**
  133. * Installs the DMDirc event queue.
  134. */
  135. private void installEventQueue() {
  136. UIUtilities.invokeAndWait(
  137. () -> Toolkit.getDefaultToolkit().getSystemEventQueue().push(eventQueue));
  138. }
  139. /**
  140. * Removes the DMDirc event queue.
  141. */
  142. private void uninstallEventQueue() {
  143. eventQueue.pop();
  144. }
  145. }