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.

SwingManager.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.Channel;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.addons.ui_swing.components.menubar.MenuBar;
  26. import com.dmdirc.addons.ui_swing.components.statusbar.FeedbackNag;
  27. import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
  28. import com.dmdirc.addons.ui_swing.dialogs.DialogKeyListener;
  29. import com.dmdirc.addons.ui_swing.dialogs.channelsetting.ChannelSettingsDialog;
  30. import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
  31. import com.dmdirc.addons.ui_swing.dialogs.url.URLDialogFactory;
  32. import com.dmdirc.addons.ui_swing.framemanager.ctrltab.CtrlTabWindowManager;
  33. import com.dmdirc.addons.ui_swing.injection.KeyedDialogProvider;
  34. import com.dmdirc.addons.ui_swing.wizard.firstrun.FirstRunWizardExecutor;
  35. import com.dmdirc.ui.WindowManager;
  36. import com.dmdirc.ui.core.components.StatusBarManager;
  37. import java.awt.KeyboardFocusManager;
  38. import java.awt.Toolkit;
  39. import javax.inject.Inject;
  40. import javax.inject.Provider;
  41. import javax.inject.Singleton;
  42. /**
  43. * Manages swing components and dependencies.
  44. */
  45. @Singleton
  46. public class SwingManager {
  47. /** The event queue to use. */
  48. private final DMDircEventQueue eventQueue;
  49. /** The window factory in use. */
  50. private final SwingWindowFactory windowFactory;
  51. /** The status bar manager to register our status bar with. */
  52. private final StatusBarManager statusBarManager;
  53. /** The status bar in use. */
  54. private final SwingStatusBar statusBar;
  55. /** The window manager to listen on for events. */
  56. private final WindowManager windowManager;
  57. /** The main frame of the Swing UI. */
  58. private final MainFrame mainFrame;
  59. /** The key listener that supports dialogs. */
  60. private final DialogKeyListener dialogKeyListener;
  61. /** Provider of first run executors. */
  62. private final Provider<FirstRunWizardExecutor> firstRunExecutor;
  63. /** Provider of server settings dialogs. */
  64. private final KeyedDialogProvider<Server, ServerSettingsDialog> serverSettingsDialogProvider;
  65. /** Provider of channel settings dialogs. */
  66. private final KeyedDialogProvider<Channel, ChannelSettingsDialog> channelSettingsDialogProvider;
  67. /** Provider of feedback nags. */
  68. private final Provider<FeedbackNag> feedbackNagProvider;
  69. /** Factory to use to create URL dialogs. */
  70. private final URLDialogFactory urlDialogFactory;
  71. /**
  72. * Creates a new instance of {@link SwingManager}.
  73. *
  74. * @param eventQueue The event queue to use.
  75. * @param windowFactory The window factory in use.
  76. * @param windowManager The window manager to listen on for events.
  77. * @param statusBarManager The core status bar manager to register our status bar
  78. * with.
  79. * @param mainFrame The main frame of the Swing UI.
  80. * @param menuBar The menu bar to use for the main frame.
  81. * @param statusBar The status bar to use in the main frame.
  82. * @param ctrlTabManager The window manager that handles ctrl+tab behaviour.
  83. * @param dialogKeyListener The key listener that supports dialogs.
  84. * @param firstRunExecutor A provider of first run executors.
  85. * @param serverSettingsDialogProvider Provider of server settings dialogs.
  86. * @param channelSettingsDialogProvider Provider of channel settings dialogs.
  87. * @param feedbackNagProvider Provider of feedback nags.
  88. * @param urlDialogFactory Factory to use to create URL dialogs.
  89. */
  90. @Inject
  91. public SwingManager(
  92. final DMDircEventQueue eventQueue,
  93. final SwingWindowFactory windowFactory,
  94. final WindowManager windowManager,
  95. final StatusBarManager statusBarManager,
  96. final MainFrame mainFrame,
  97. final MenuBar menuBar,
  98. final SwingStatusBar statusBar,
  99. final CtrlTabWindowManager ctrlTabManager,
  100. final DialogKeyListener dialogKeyListener,
  101. final Provider<FirstRunWizardExecutor> firstRunExecutor,
  102. final KeyedDialogProvider<Server, ServerSettingsDialog> serverSettingsDialogProvider,
  103. final KeyedDialogProvider<Channel, ChannelSettingsDialog> channelSettingsDialogProvider,
  104. final Provider<FeedbackNag> feedbackNagProvider,
  105. final URLDialogFactory urlDialogFactory) {
  106. this.eventQueue = eventQueue;
  107. this.windowFactory = windowFactory;
  108. this.windowManager = windowManager;
  109. this.statusBar = statusBar;
  110. this.statusBarManager = statusBarManager;
  111. this.dialogKeyListener = dialogKeyListener;
  112. this.firstRunExecutor = firstRunExecutor;
  113. this.serverSettingsDialogProvider = serverSettingsDialogProvider;
  114. this.channelSettingsDialogProvider = channelSettingsDialogProvider;
  115. this.feedbackNagProvider = feedbackNagProvider;
  116. this.urlDialogFactory = urlDialogFactory;
  117. this.mainFrame = mainFrame;
  118. this.mainFrame.setMenuBar(menuBar);
  119. this.mainFrame.setWindowManager(ctrlTabManager);
  120. this.mainFrame.setStatusBar(statusBar);
  121. this.mainFrame.initComponents();
  122. }
  123. /**
  124. * Handles loading of the UI.
  125. */
  126. public void load() {
  127. installEventQueue();
  128. installKeyListener();
  129. windowManager.addListenerAndSync(windowFactory);
  130. statusBarManager.registerStatusBar(statusBar);
  131. }
  132. /**
  133. * Handles unloading of the UI.
  134. */
  135. public void unload() {
  136. uninstallEventQueue();
  137. uninstallKeyListener();
  138. windowManager.removeListener(windowFactory);
  139. windowFactory.dispose();
  140. mainFrame.dispose();
  141. statusBarManager.unregisterStatusBar(statusBar);
  142. }
  143. /**
  144. * Gets a first run wizard executor to use.
  145. *
  146. * @return A first run wizard executor.
  147. */
  148. public FirstRunWizardExecutor getFirstRunExecutor() {
  149. return firstRunExecutor.get();
  150. }
  151. @Deprecated
  152. public KeyedDialogProvider<Server, ServerSettingsDialog> getServerSettingsDialogProvider() {
  153. return serverSettingsDialogProvider;
  154. }
  155. @Deprecated
  156. public KeyedDialogProvider<Channel, ChannelSettingsDialog> getChannelSettingsDialogProvider() {
  157. return channelSettingsDialogProvider;
  158. }
  159. @Deprecated
  160. public Provider<FeedbackNag> getFeedbackNagProvider() {
  161. return feedbackNagProvider;
  162. }
  163. @Deprecated
  164. public URLDialogFactory getUrlDialogFactory() {
  165. return urlDialogFactory;
  166. }
  167. /**
  168. * Retrieves the window factory to use.
  169. *
  170. * @return A swing window factory instance.
  171. *
  172. * @deprecated Should be injected.
  173. */
  174. @Deprecated
  175. public SwingWindowFactory getWindowFactory() {
  176. return windowFactory;
  177. }
  178. /**
  179. * Retrieves the main frame.
  180. *
  181. * @return A main frame instance.
  182. *
  183. * @deprecated Should be injected.
  184. */
  185. @Deprecated
  186. public MainFrame getMainFrame() {
  187. return mainFrame;
  188. }
  189. /**
  190. * Installs the DMDirc event queue.
  191. */
  192. private void installEventQueue() {
  193. UIUtilities.invokeAndWait(new Runnable() {
  194. /** {@inheritDoc} */
  195. @Override
  196. public void run() {
  197. Toolkit.getDefaultToolkit().getSystemEventQueue().push(eventQueue);
  198. }
  199. });
  200. }
  201. /**
  202. * Removes the DMDirc event queue.
  203. */
  204. private void uninstallEventQueue() {
  205. eventQueue.pop();
  206. }
  207. /**
  208. * Installs the dialog key listener.
  209. */
  210. private void installKeyListener() {
  211. UIUtilities.invokeAndWait(new Runnable() {
  212. /** {@inheritDoc} */
  213. @Override
  214. public void run() {
  215. KeyboardFocusManager.getCurrentKeyboardFocusManager()
  216. .addKeyEventDispatcher(dialogKeyListener);
  217. }
  218. });
  219. }
  220. /**
  221. * Removes the dialog key listener.
  222. */
  223. private void uninstallKeyListener() {
  224. KeyboardFocusManager.getCurrentKeyboardFocusManager()
  225. .removeKeyEventDispatcher(dialogKeyListener);
  226. }
  227. }