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.

SwingController.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.addons.ui_swing.commands.ChannelSettings;
  24. import com.dmdirc.addons.ui_swing.commands.Input;
  25. import com.dmdirc.addons.ui_swing.commands.PopInCommand;
  26. import com.dmdirc.addons.ui_swing.commands.PopOutCommand;
  27. import com.dmdirc.addons.ui_swing.commands.ServerSettings;
  28. import com.dmdirc.addons.ui_swing.framemanager.FrameManagerProvider;
  29. import com.dmdirc.addons.ui_swing.injection.SwingModule;
  30. import com.dmdirc.interfaces.ui.UIController;
  31. import com.dmdirc.plugins.Exported;
  32. import com.dmdirc.plugins.PluginInfo;
  33. import com.dmdirc.plugins.implementations.BaseCommandPlugin;
  34. import com.dmdirc.updater.Version;
  35. import java.awt.GraphicsEnvironment;
  36. import javax.swing.UIManager;
  37. import dagger.ObjectGraph;
  38. /**
  39. * Controls the main swing UI.
  40. */
  41. public class SwingController extends BaseCommandPlugin implements UIController {
  42. /** This plugin's plugin info object. */
  43. private PluginInfo pluginInfo;
  44. /** The manager we're using for dependencies. */
  45. private SwingManager swingManager;
  46. @Override
  47. public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
  48. super.load(pluginInfo, graph);
  49. this.pluginInfo = pluginInfo;
  50. setObjectGraph(graph.plus(new SwingModule(this, pluginInfo, pluginInfo.getDomain())));
  51. getObjectGraph().validate();
  52. swingManager = getObjectGraph().get(SwingManager.class);
  53. registerCommand(ServerSettings.class, ServerSettings.INFO);
  54. registerCommand(ChannelSettings.class, ChannelSettings.INFO);
  55. registerCommand(Input.class, Input.INFO);
  56. registerCommand(PopOutCommand.class, PopOutCommand.INFO);
  57. registerCommand(PopInCommand.class, PopInCommand.INFO);
  58. }
  59. @Override
  60. public void onLoad() {
  61. if (GraphicsEnvironment.isHeadless()) {
  62. throw new IllegalStateException(
  63. "Swing UI can't be run in a headless environment");
  64. }
  65. swingManager.load();
  66. UIUtilities.invokeAndWait(() -> getMainFrame().setVisible(true));
  67. super.onLoad();
  68. }
  69. @Override
  70. public void onUnload() {
  71. if (swingManager != null) {
  72. swingManager.unload();
  73. }
  74. super.onUnload();
  75. }
  76. /**
  77. * Returns the version of this swing UI.
  78. *
  79. * @return Swing version
  80. */
  81. public Version getVersion() {
  82. return pluginInfo.getMetaData().getVersion();
  83. }
  84. /**
  85. * Returns the current look and feel.
  86. *
  87. * @return Current look and feel
  88. */
  89. public static String getLookAndFeel() {
  90. return UIManager.getLookAndFeel().getName();
  91. }
  92. /**
  93. * Returns an instance of SwingController. This method is exported for use in other plugins.
  94. *
  95. * @return A reference to this SwingController.
  96. */
  97. @Exported
  98. public UIController getController() {
  99. return this;
  100. }
  101. /**
  102. * Returns the exported tree manager provider.
  103. *
  104. * @return A tree manager provider.
  105. */
  106. @Exported
  107. public FrameManagerProvider getTreeManager() {
  108. return swingManager.getTreeProvider();
  109. }
  110. /**
  111. * Retrieves the main frame to use.
  112. *
  113. * @return The main frame to use.
  114. *
  115. * @deprecated Should be injected where needed.
  116. */
  117. @Deprecated
  118. public MainFrame getMainFrame() {
  119. return swingManager.getMainFrame();
  120. }
  121. }