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

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