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

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