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

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