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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.CommandParser;
  26. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  27. import com.dmdirc.interfaces.Connection;
  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.TabCompleter;
  34. import com.dmdirc.ui.input.TabCompleterFactory;
  35. import java.util.Arrays;
  36. import javax.inject.Inject;
  37. import javax.inject.Provider;
  38. import javax.inject.Singleton;
  39. /**
  40. * A window which can be used to execute global commands.
  41. */
  42. public class GlobalWindow extends WritableFrameContainer {
  43. /** The global window that's in use, if any. */
  44. private static GlobalWindow globalWindow;
  45. /** The tab completer we use. */
  46. private final TabCompleter tabCompleter;
  47. /**
  48. * Creates a new instance of GlobalWindow.
  49. *
  50. * @param config The ConfigManager to retrieve settings from.
  51. * @param parser The command parser to use to parse input.
  52. * @param tabCompleterFactory The factory to use to create tab completers.
  53. * @param messageSinkManager The sink manager to use to despatch messages.
  54. */
  55. public GlobalWindow(
  56. final AggregateConfigProvider config,
  57. final CommandParser parser,
  58. final TabCompleterFactory tabCompleterFactory,
  59. final MessageSinkManager messageSinkManager) {
  60. super("icon", "Global", "(Global)", config, parser, messageSinkManager,
  61. Arrays.asList(
  62. WindowComponent.TEXTAREA.getIdentifier(),
  63. WindowComponent.INPUTFIELD.getIdentifier()));
  64. tabCompleter = tabCompleterFactory.getTabCompleter(config, CommandType.TYPE_GLOBAL);
  65. }
  66. public TabCompleter getTabCompleter() {
  67. return tabCompleter;
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. public void windowClosing() {
  72. // 2: Remove any callbacks or listeners
  73. // 3: Trigger any actions neccessary
  74. // 4: Trigger action for the window closing
  75. // 5: Inform any parents that the window is closing
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public void windowClosed() {
  80. // 7: Remove any references to the window and parents
  81. globalWindow = null;
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public Connection getConnection() {
  86. return null;
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public void sendLine(final String line) {
  91. addLine("commandError", "You may only enter commands in the global window.");
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public int getMaxLineLength() {
  96. return -1;
  97. }
  98. /** Handles the state of the global window. */
  99. @Singleton
  100. public static class GlobalWindowManager implements ConfigChangeListener {
  101. /** The global configuration to read settings from. */
  102. private final AggregateConfigProvider globalConfig;
  103. /** The factory to use to create tab completers. */
  104. private final TabCompleterFactory tabCompleterFactory;
  105. /** The provider to use to retrieve a window manager. */
  106. private final Provider<WindowManager> windowManagerProvider;
  107. /** The provider to use to retrieve message sink managers. */
  108. private final Provider<MessageSinkManager> messageSinkManagerProvider;
  109. /** The provider to use to retrieve a global command parser. */
  110. private final Provider<GlobalCommandParser> globalCommandParserProvider;
  111. /**
  112. * Creates a new instance of {@link GlobalWindowManager}.
  113. *
  114. * @param globalConfig Configuration provider to read settings from.
  115. * @param tabCompleterFactory Factory to use to create tab completers.
  116. * @param windowManagerProvider The provider to use to retrieve a window manager.
  117. * @param messageSinkManagerProvider The provider to use to retrieve a sink manager.
  118. * @param globalCommandParserProvider The provider to use to retrieve a global command parser.
  119. */
  120. @Inject
  121. public GlobalWindowManager(
  122. @GlobalConfig final AggregateConfigProvider globalConfig,
  123. final TabCompleterFactory tabCompleterFactory,
  124. final Provider<WindowManager> windowManagerProvider,
  125. final Provider<MessageSinkManager> messageSinkManagerProvider,
  126. final Provider<GlobalCommandParser> globalCommandParserProvider) {
  127. this.globalConfig = globalConfig;
  128. this.tabCompleterFactory = tabCompleterFactory;
  129. this.windowManagerProvider = windowManagerProvider;
  130. this.messageSinkManagerProvider = messageSinkManagerProvider;
  131. this.globalCommandParserProvider = globalCommandParserProvider;
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public void configChanged(final String domain, final String key) {
  136. updateWindowState();
  137. }
  138. /**
  139. * Initialises the global window if it's enabled in the config.
  140. */
  141. public void init() {
  142. globalConfig.addChangeListener("general", "showglobalwindow", this);
  143. updateWindowState();
  144. }
  145. /**
  146. * Updates the state of the global window in line with the
  147. * general.showglobalwindow config setting.
  148. */
  149. protected void updateWindowState() {
  150. synchronized (GlobalWindow.class) {
  151. if (globalConfig.getOptionBool("general", "showglobalwindow")) {
  152. if (globalWindow == null) {
  153. globalWindow = new GlobalWindow(globalConfig,
  154. globalCommandParserProvider.get(),
  155. tabCompleterFactory,
  156. messageSinkManagerProvider.get());
  157. windowManagerProvider.get().addWindow(globalWindow);
  158. }
  159. } else {
  160. if (globalWindow != null) {
  161. globalWindow.close();
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }