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

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