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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc;
  18. import com.dmdirc.commandparser.CommandType;
  19. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  20. import com.dmdirc.config.GlobalConfig;
  21. import com.dmdirc.events.CommandErrorEvent;
  22. import com.dmdirc.interfaces.Connection;
  23. import com.dmdirc.events.eventbus.EventBus;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.interfaces.config.ConfigChangeListener;
  26. import com.dmdirc.ui.WindowManager;
  27. import com.dmdirc.ui.core.components.WindowComponent;
  28. import com.dmdirc.ui.input.TabCompleterFactory;
  29. import com.dmdirc.ui.messages.BackBufferFactory;
  30. import java.util.Arrays;
  31. import java.util.Optional;
  32. import javax.inject.Inject;
  33. import javax.inject.Provider;
  34. import javax.inject.Singleton;
  35. /**
  36. * A window which can be used to execute global commands.
  37. */
  38. @Singleton
  39. public class GlobalWindow extends FrameContainer {
  40. /**
  41. * Creates a new instance of GlobalWindow.
  42. */
  43. @Inject
  44. public GlobalWindow(@GlobalConfig final AggregateConfigProvider config,
  45. final GlobalCommandParser parser, final TabCompleterFactory tabCompleterFactory,
  46. final EventBus eventBus, final BackBufferFactory backBufferFactory) {
  47. super("icon", "Global", "(Global)", config, backBufferFactory, eventBus,
  48. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  49. WindowComponent.INPUTFIELD.getIdentifier()));
  50. initBackBuffer();
  51. setInputModel(new DefaultInputModel(
  52. this::sendLine,
  53. parser,
  54. tabCompleterFactory.getTabCompleter(config, CommandType.TYPE_GLOBAL),
  55. this::getMaxLineLength
  56. ));
  57. }
  58. @Override
  59. public Optional<Connection> getConnection() {
  60. return Optional.empty();
  61. }
  62. private void sendLine(final String line) {
  63. getEventBus().publishAsync(
  64. new CommandErrorEvent(this, "You may only enter commands in the global window."));
  65. }
  66. private int getMaxLineLength() {
  67. return -1;
  68. }
  69. /** Handles the state of the global window. */
  70. @Singleton
  71. public static class GlobalWindowManager implements ConfigChangeListener {
  72. /** Provider to use to obtain global window instances. */
  73. private final Provider<GlobalWindow> globalWindowProvider;
  74. /** The global configuration to read settings from. */
  75. private final AggregateConfigProvider globalConfig;
  76. /** The provider to use to retrieve a window manager. */
  77. private final Provider<WindowManager> windowManagerProvider;
  78. /**
  79. * Creates a new instance of {@link GlobalWindowManager}.
  80. *
  81. * @param globalWindowProvider The provider to use to obtain global windows.
  82. * @param globalConfig Configuration provider to read settings from.
  83. * @param windowManagerProvider The provider to use to retrieve a window manager.
  84. */
  85. @Inject
  86. public GlobalWindowManager(final Provider<GlobalWindow> globalWindowProvider,
  87. @GlobalConfig final AggregateConfigProvider globalConfig,
  88. final Provider<WindowManager> windowManagerProvider) {
  89. this.globalWindowProvider = globalWindowProvider;
  90. this.globalConfig = globalConfig;
  91. this.windowManagerProvider = windowManagerProvider;
  92. }
  93. @Override
  94. public void configChanged(final String domain, final String key) {
  95. updateWindowState();
  96. }
  97. /**
  98. * Initialises the global window if it's enabled in the config.
  99. */
  100. public void init() {
  101. globalConfig.addChangeListener("general", "showglobalwindow", this);
  102. updateWindowState();
  103. }
  104. /**
  105. * Updates the state of the global window in line with the general.showglobalwindow config
  106. * setting.
  107. */
  108. protected void updateWindowState() {
  109. final WindowManager windowManager = windowManagerProvider.get();
  110. final GlobalWindow globalWindow = globalWindowProvider.get();
  111. final boolean globalWindowExists =
  112. windowManager.getRootWindows().contains(globalWindow);
  113. if (globalConfig.getOptionBool("general", "showglobalwindow")) {
  114. if (!globalWindowExists) {
  115. windowManager.addWindow(globalWindow);
  116. }
  117. } else {
  118. if (globalWindowExists) {
  119. globalWindow.close();
  120. }
  121. }
  122. }
  123. }
  124. }