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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2006-2012 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.actions.wrappers.AliasWrapper;
  24. import com.dmdirc.commandparser.CommandManager;
  25. import com.dmdirc.commandparser.CommandType;
  26. import com.dmdirc.commandparser.parsers.CommandParser;
  27. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  28. import com.dmdirc.config.ConfigManager;
  29. import com.dmdirc.config.IdentityManager;
  30. import com.dmdirc.interfaces.ConfigChangeListener;
  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.TabCompletionType;
  35. import java.util.Arrays;
  36. import lombok.Getter;
  37. /**
  38. * A window which can be used to execute global commands.
  39. */
  40. public class GlobalWindow extends WritableFrameContainer {
  41. /** The global window that's in use, if any. */
  42. @Getter
  43. private static GlobalWindow globalWindow;
  44. /** The tab completer we use. */
  45. @Getter
  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. */
  53. public GlobalWindow(final ConfigManager config, final CommandParser parser) {
  54. super("icon", "Global", "(Global)",
  55. config, parser,
  56. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  57. WindowComponent.INPUTFIELD.getIdentifier()));
  58. tabCompleter = new TabCompleter();
  59. tabCompleter.addEntries(TabCompletionType.COMMAND,
  60. CommandManager.getCommandManager().getCommandNames(CommandType.TYPE_GLOBAL));
  61. tabCompleter.addEntries(TabCompletionType.COMMAND,
  62. AliasWrapper.getAliasWrapper().getAliases());
  63. WindowManager.getWindowManager().addWindow(this);
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public void windowClosing() {
  68. // 2: Remove any callbacks or listeners
  69. // 3: Trigger any actions neccessary
  70. // 4: Trigger action for the window closing
  71. // 5: Inform any parents that the window is closing
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public void windowClosed() {
  76. // 7: Remove any references to the window and parents
  77. globalWindow = null;
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public Server getServer() {
  82. return null;
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public void sendLine(final String line) {
  87. addLine("commandError", "You may only enter commands in the global window.");
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public int getMaxLineLength() {
  92. return -1;
  93. }
  94. /**
  95. * Initialises the global window if it's enabled in the config.
  96. */
  97. public static void init() {
  98. IdentityManager.getIdentityManager().getGlobalConfiguration()
  99. .addChangeListener("general", "showglobalwindow",
  100. new ConfigChangeListener() {
  101. @Override
  102. public void configChanged(final String domain, final String key) {
  103. updateWindowState();
  104. }
  105. });
  106. updateWindowState();
  107. }
  108. /**
  109. * Updates the state of the global window in line with the
  110. * general.showglobalwindow config setting.
  111. */
  112. protected static void updateWindowState() {
  113. final ConfigManager configManager = IdentityManager.getIdentityManager()
  114. .getGlobalConfiguration();
  115. synchronized (GlobalWindow.class) {
  116. if (configManager.getOptionBool("general", "showglobalwindow")) {
  117. if (globalWindow == null) {
  118. globalWindow = new GlobalWindow(configManager,
  119. new GlobalCommandParser(configManager,
  120. CommandManager.getCommandManager()));
  121. }
  122. } else {
  123. if (globalWindow != null) {
  124. globalWindow.close();
  125. }
  126. }
  127. }
  128. }
  129. }