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.

SwingUIInitialiser.java 5.9KB

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