Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

GlobalWindow.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.config.AggregateConfigProvider;
  28. import com.dmdirc.interfaces.config.ConfigChangeListener;
  29. import com.dmdirc.ui.WindowManager;
  30. import com.dmdirc.ui.messages.BackBufferFactory;
  31. import com.dmdirc.ui.core.components.WindowComponent;
  32. import com.dmdirc.ui.input.TabCompleterFactory;
  33. import com.dmdirc.ui.messages.sink.MessageSinkManager;
  34. import com.dmdirc.util.URLBuilder;
  35. import java.util.Arrays;
  36. import java.util.Optional;
  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. @Inject
  49. public GlobalWindow(@GlobalConfig final AggregateConfigProvider config,
  50. final GlobalCommandParser parser, final TabCompleterFactory tabCompleterFactory,
  51. final MessageSinkManager messageSinkManager, final URLBuilder urlBuilder,
  52. final DMDircMBassador eventBus, final BackBufferFactory backBufferFactory) {
  53. super(null, "icon", "Global", "(Global)", config, backBufferFactory, urlBuilder, parser,
  54. tabCompleterFactory.getTabCompleter(config, CommandType.TYPE_GLOBAL),
  55. messageSinkManager, eventBus,
  56. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  57. WindowComponent.INPUTFIELD.getIdentifier()));
  58. }
  59. @Override
  60. @Deprecated
  61. public Connection getConnection() {
  62. return null;
  63. }
  64. @Override
  65. public Optional<Connection> getOptionalConnection() {
  66. return Optional.empty();
  67. }
  68. @Override
  69. public void sendLine(final String line) {
  70. addLine("commandError", "You may only enter commands in the global window.");
  71. }
  72. @Override
  73. public int getMaxLineLength() {
  74. return -1;
  75. }
  76. /** Handles the state of the global window. */
  77. @Singleton
  78. public static class GlobalWindowManager implements ConfigChangeListener {
  79. /** Provider to use to obtain global window instances. */
  80. private final Provider<GlobalWindow> globalWindowProvider;
  81. /** The global configuration to read settings from. */
  82. private final AggregateConfigProvider globalConfig;
  83. /** The provider to use to retrieve a window manager. */
  84. private final Provider<WindowManager> windowManagerProvider;
  85. /**
  86. * Creates a new instance of {@link GlobalWindowManager}.
  87. *
  88. * @param globalWindowProvider The provider to use to obtain global windows.
  89. * @param globalConfig Configuration provider to read settings from.
  90. * @param windowManagerProvider The provider to use to retrieve a window manager.
  91. */
  92. @Inject
  93. public GlobalWindowManager(final Provider<GlobalWindow> globalWindowProvider,
  94. @GlobalConfig final AggregateConfigProvider globalConfig,
  95. final Provider<WindowManager> windowManagerProvider) {
  96. this.globalWindowProvider = globalWindowProvider;
  97. this.globalConfig = globalConfig;
  98. this.windowManagerProvider = windowManagerProvider;
  99. }
  100. @Override
  101. public void configChanged(final String domain, final String key) {
  102. updateWindowState();
  103. }
  104. /**
  105. * Initialises the global window if it's enabled in the config.
  106. */
  107. public void init() {
  108. globalConfig.addChangeListener("general", "showglobalwindow", this);
  109. updateWindowState();
  110. }
  111. /**
  112. * Updates the state of the global window in line with the general.showglobalwindow config
  113. * setting.
  114. */
  115. protected void updateWindowState() {
  116. final WindowManager windowManager = windowManagerProvider.get();
  117. final GlobalWindow globalWindow = globalWindowProvider.get();
  118. final boolean globalWindowExists =
  119. windowManager.getRootWindows().contains(globalWindow);
  120. if (globalConfig.getOptionBool("general", "showglobalwindow")) {
  121. if (!globalWindowExists) {
  122. windowManager.addWindow(globalWindow);
  123. }
  124. } else {
  125. if (globalWindowExists) {
  126. globalWindow.close();
  127. }
  128. }
  129. }
  130. }
  131. }