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.

GlobalWindow.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.commandparser.CommandType;
  25. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  26. import com.dmdirc.interfaces.Connection;
  27. import com.dmdirc.interfaces.FrameCloseListener;
  28. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  29. import com.dmdirc.interfaces.config.ConfigChangeListener;
  30. import com.dmdirc.messages.MessageSinkManager;
  31. import com.dmdirc.ui.WindowManager;
  32. import com.dmdirc.ui.core.components.WindowComponent;
  33. import com.dmdirc.ui.input.TabCompleterFactory;
  34. import com.dmdirc.util.URLBuilder;
  35. import com.google.common.eventbus.EventBus;
  36. import java.util.Arrays;
  37. import javax.inject.Inject;
  38. import javax.inject.Provider;
  39. import javax.inject.Singleton;
  40. /**
  41. * A window which can be used to execute global commands.
  42. */
  43. @Singleton
  44. public class GlobalWindow extends FrameContainer {
  45. /**
  46. * Creates a new instance of GlobalWindow.
  47. *
  48. * @param config The ConfigManager to retrieve settings from.
  49. * @param parser The command parser to use to parse input.
  50. * @param tabCompleterFactory The factory to use to create tab completers.
  51. * @param messageSinkManager The sink manager to use to dispatch messages.
  52. * @param urlBuilder The URL builder to use when finding icons.
  53. * @param eventBus The bus to dispatch events on.
  54. */
  55. @Inject
  56. public GlobalWindow(
  57. @GlobalConfig final AggregateConfigProvider config,
  58. final GlobalCommandParser parser,
  59. final TabCompleterFactory tabCompleterFactory,
  60. final MessageSinkManager messageSinkManager,
  61. final URLBuilder urlBuilder,
  62. final EventBus eventBus) {
  63. super(null, "icon", "Global", "(Global)", config, urlBuilder, parser,
  64. tabCompleterFactory.getTabCompleter(config, CommandType.TYPE_GLOBAL),
  65. messageSinkManager, eventBus,
  66. Arrays.asList(
  67. WindowComponent.TEXTAREA.getIdentifier(),
  68. WindowComponent.INPUTFIELD.getIdentifier()));
  69. }
  70. @Override
  71. public Connection getConnection() {
  72. return null;
  73. }
  74. @Override
  75. public void sendLine(final String line) {
  76. addLine("commandError", "You may only enter commands in the global window.");
  77. }
  78. @Override
  79. public int getMaxLineLength() {
  80. return -1;
  81. }
  82. /** Handles the state of the global window. */
  83. @Singleton
  84. public static class GlobalWindowManager implements ConfigChangeListener {
  85. /** Provider to use to obtain global window instances. */
  86. private final Provider<GlobalWindow> globalWindowProvider;
  87. /** The global configuration to read settings from. */
  88. private final AggregateConfigProvider globalConfig;
  89. /** The provider to use to retrieve a window manager. */
  90. private final Provider<WindowManager> windowManagerProvider;
  91. /**
  92. * Creates a new instance of {@link GlobalWindowManager}.
  93. *
  94. * @param globalWindowProvider The provider to use to obtain global windows.
  95. * @param globalConfig Configuration provider to read settings from.
  96. * @param windowManagerProvider The provider to use to retrieve a window manager.
  97. */
  98. @Inject
  99. public GlobalWindowManager(
  100. final Provider<GlobalWindow> globalWindowProvider,
  101. @GlobalConfig final AggregateConfigProvider globalConfig,
  102. final Provider<WindowManager> windowManagerProvider) {
  103. this.globalWindowProvider = globalWindowProvider;
  104. this.globalConfig = globalConfig;
  105. this.windowManagerProvider = windowManagerProvider;
  106. }
  107. @Override
  108. public void configChanged(final String domain, final String key) {
  109. updateWindowState();
  110. }
  111. /**
  112. * Initialises the global window if it's enabled in the config.
  113. */
  114. public void init() {
  115. globalConfig.addChangeListener("general", "showglobalwindow", this);
  116. updateWindowState();
  117. }
  118. /**
  119. * Updates the state of the global window in line with the general.showglobalwindow config
  120. * setting.
  121. */
  122. protected void updateWindowState() {
  123. final WindowManager windowManager = windowManagerProvider.get();
  124. final GlobalWindow globalWindow = globalWindowProvider.get();
  125. final boolean globalWindowExists = windowManager.getRootWindows()
  126. .contains(globalWindow);
  127. if (globalConfig.getOptionBool("general", "showglobalwindow")) {
  128. if (!globalWindowExists) {
  129. addCloseListener(globalWindow);
  130. windowManager.addWindow(globalWindow);
  131. }
  132. } else {
  133. if (globalWindowExists) {
  134. globalWindow.close();
  135. }
  136. }
  137. }
  138. /**
  139. * Adds a {@link FrameCloseListener} to the specified window to update the global window
  140. * state if the user closes it from the UI.
  141. *
  142. * @param window The window to add a listener to.
  143. */
  144. private void addCloseListener(final GlobalWindow window) {
  145. window.addCloseListener(new FrameCloseListener() {
  146. @Override
  147. public void windowClosing(final FrameContainer container) {
  148. container.removeCloseListener(this);
  149. }
  150. });
  151. }
  152. }
  153. }