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.

SwingModule.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.injection;
  18. import com.dmdirc.ClientModule;
  19. import com.dmdirc.addons.ui_swing.Apple;
  20. import com.dmdirc.addons.ui_swing.MainFrame;
  21. import com.dmdirc.addons.ui_swing.NoopClipboard;
  22. import com.dmdirc.addons.ui_swing.QuitWorker;
  23. import com.dmdirc.addons.ui_swing.SimpleActiveFrameManager;
  24. import com.dmdirc.addons.ui_swing.SwingController;
  25. import com.dmdirc.addons.ui_swing.SwingManager;
  26. import com.dmdirc.addons.ui_swing.UIUtilities;
  27. import com.dmdirc.addons.ui_swing.commands.ChannelSettings;
  28. import com.dmdirc.addons.ui_swing.commands.Input;
  29. import com.dmdirc.addons.ui_swing.commands.PopInCommand;
  30. import com.dmdirc.addons.ui_swing.commands.PopOutCommand;
  31. import com.dmdirc.addons.ui_swing.commands.ServerSettings;
  32. import com.dmdirc.addons.ui_swing.components.IconManager;
  33. import com.dmdirc.addons.ui_swing.components.addonpanel.PluginPanel;
  34. import com.dmdirc.addons.ui_swing.components.addonpanel.ThemePanel;
  35. import com.dmdirc.addons.ui_swing.components.statusbar.ErrorPanel;
  36. import com.dmdirc.addons.ui_swing.components.statusbar.InviteLabel;
  37. import com.dmdirc.addons.ui_swing.components.statusbar.MessageLabel;
  38. import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
  39. import com.dmdirc.addons.ui_swing.components.statusbar.UpdaterLabel;
  40. import com.dmdirc.addons.ui_swing.dialogs.prefs.URLConfigPanel;
  41. import com.dmdirc.addons.ui_swing.dialogs.prefs.UpdateConfigPanel;
  42. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  43. import com.dmdirc.addons.ui_swing.framemanager.FrameManager;
  44. import com.dmdirc.addons.ui_swing.framemanager.FrameManagerProvider;
  45. import com.dmdirc.addons.ui_swing.framemanager.tree.TreeFrameManagerProvider;
  46. import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
  47. import com.dmdirc.config.GlobalConfig;
  48. import com.dmdirc.config.UserConfig;
  49. import com.dmdirc.config.prefs.PreferencesDialogModel;
  50. import com.dmdirc.interfaces.ConnectionManager;
  51. import com.dmdirc.events.eventbus.EventBus;
  52. import com.dmdirc.interfaces.LifecycleController;
  53. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  54. import com.dmdirc.interfaces.config.ConfigProvider;
  55. import com.dmdirc.plugins.PluginDomain;
  56. import com.dmdirc.plugins.PluginInfo;
  57. import com.dmdirc.plugins.ServiceLocator;
  58. import com.dmdirc.plugins.ServiceManager;
  59. import com.dmdirc.ui.core.util.URLHandler;
  60. import com.dmdirc.util.URLBuilder;
  61. import dagger.Module;
  62. import dagger.Provides;
  63. import java.awt.Toolkit;
  64. import java.awt.Window;
  65. import java.awt.datatransfer.Clipboard;
  66. import javax.inject.Provider;
  67. import javax.inject.Singleton;
  68. /**
  69. * Dagger module that provides Swing-specific dependencies.
  70. */
  71. @Module(
  72. addsTo = ClientModule.class,
  73. includes = DialogModule.class,
  74. injects = {
  75. SwingManager.class,
  76. PopInCommand.class,
  77. PopOutCommand.class,
  78. Input.class,
  79. ServerSettings.class,
  80. ChannelSettings.class
  81. })
  82. @SuppressWarnings("TypeMayBeWeakened")
  83. public class SwingModule {
  84. private final SwingController controller;
  85. private final PluginInfo pluginInfo;
  86. private final String domain;
  87. public SwingModule(final SwingController controller, final PluginInfo pluginInfo,
  88. final String domain) {
  89. this.controller = controller;
  90. this.pluginInfo = pluginInfo;
  91. this.domain = domain;
  92. }
  93. @Provides
  94. @PluginDomain(SwingController.class)
  95. public String getSettingsDomain() {
  96. return domain;
  97. }
  98. @Provides
  99. public SwingController getController() {
  100. return controller;
  101. }
  102. @Provides
  103. @Singleton
  104. public Clipboard getClipboard() {
  105. if (Toolkit.getDefaultToolkit().getSystemClipboard() == null) {
  106. return new NoopClipboard();
  107. } else {
  108. return Toolkit.getDefaultToolkit().getSystemClipboard();
  109. }
  110. }
  111. @Provides
  112. @Singleton
  113. public MainFrame getMainFrame(
  114. final Apple apple,
  115. final LifecycleController lifecycleController,
  116. @GlobalConfig final AggregateConfigProvider globalConfig,
  117. final Provider<QuitWorker> quitWorker,
  118. final URLBuilder urlBuilder,
  119. final Provider<FrameManager> frameManagerProvider,
  120. final EventBus eventBus,
  121. final SwingEventBus swingEventBus) {
  122. return UIUtilities.invokeAndWait(() -> new MainFrame(
  123. apple,
  124. lifecycleController,
  125. globalConfig,
  126. quitWorker,
  127. new IconManager(globalConfig, urlBuilder),
  128. frameManagerProvider,
  129. eventBus,
  130. swingEventBus));
  131. }
  132. @Provides
  133. @Singleton
  134. @MainWindow
  135. public Window getMainWindow(final MainFrame mainFrame) {
  136. return mainFrame;
  137. }
  138. @Provides
  139. @Singleton
  140. public ActiveFrameManager getActiveFrameManager(final SimpleActiveFrameManager frameManager,
  141. final SwingEventBus eventBus) {
  142. eventBus.subscribe(frameManager);
  143. return frameManager;
  144. }
  145. @Provides
  146. @Singleton
  147. public SwingStatusBar getSwingStatusBar(
  148. final InviteLabel inviteLabel,
  149. final ErrorPanel errorLabel,
  150. final UpdaterLabel updaterLabel,
  151. final MessageLabel messageLabel,
  152. final EventBus eventBus) {
  153. final SwingStatusBar sb = UIUtilities.invokeAndWait(
  154. () -> new SwingStatusBar(inviteLabel, updaterLabel, errorLabel, messageLabel));
  155. eventBus.subscribe(messageLabel);
  156. eventBus.subscribe(sb);
  157. return sb;
  158. }
  159. @Provides
  160. @Singleton
  161. public URLHandler getURLHandler(
  162. final EventBus eventBus,
  163. @GlobalConfig final AggregateConfigProvider globalConfig,
  164. final ConnectionManager connectionManager) {
  165. return new URLHandler(eventBus, globalConfig, connectionManager);
  166. }
  167. @Provides
  168. public PreferencesDialogModel getPrefsDialogModel(
  169. final PluginPanel pluginPanel,
  170. final ThemePanel themePanel,
  171. final UpdateConfigPanel updatePanel,
  172. final URLConfigPanel urlPanel,
  173. @GlobalConfig final AggregateConfigProvider configManager,
  174. @UserConfig final ConfigProvider identity,
  175. final ServiceManager serviceManager,
  176. final EventBus eventBus) {
  177. return new PreferencesDialogModel(pluginPanel, themePanel, updatePanel, urlPanel,
  178. configManager, identity, serviceManager, eventBus);
  179. }
  180. @Provides
  181. public FrameManager getFrameManager(
  182. @GlobalConfig final AggregateConfigProvider globalConfig,
  183. final TreeFrameManagerProvider fallbackProvider,
  184. final ServiceLocator locator) {
  185. final String manager = globalConfig.getOption("ui", "framemanager");
  186. FrameManagerProvider provider = locator.getService(FrameManagerProvider.class, manager);
  187. if (provider == null) {
  188. // Couldn't find the user's selected provider - let's just use the fallback.
  189. provider = fallbackProvider;
  190. }
  191. return provider.getFrameManager();
  192. }
  193. @Provides
  194. @PluginDomain(SwingController.class)
  195. public PluginInfo getPluginInfo() {
  196. return pluginInfo;
  197. }
  198. }