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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.input.TabCompleter;
  31. import com.dmdirc.ui.input.TabCompletionType;
  32. import com.dmdirc.ui.interfaces.InputWindow;
  33. /**
  34. * A window which can be used to execute global commands.
  35. *
  36. * @author chris
  37. */
  38. public class GlobalWindow extends WritableFrameContainer {
  39. /** The window we're using. */
  40. private InputWindow window;
  41. /** The global window that's in use, if any. */
  42. private static GlobalWindow globalWindow;
  43. /** The tab completer we use. */
  44. private final TabCompleter tabCompleter;
  45. /** Creates a new instance of GlobalWindow. */
  46. public GlobalWindow() {
  47. super("icon", "Global", IdentityManager.getGlobalConfig());
  48. tabCompleter = new TabCompleter();
  49. tabCompleter.addEntries(TabCompletionType.COMMAND,
  50. CommandManager.getCommandNames(CommandType.TYPE_GLOBAL));
  51. tabCompleter.addEntries(TabCompletionType.COMMAND,
  52. AliasWrapper.getAliasWrapper().getAliases());
  53. window = Main.getUI().getInputWindow(this, GlobalCommandParser.getGlobalCommandParser());
  54. window.setTitle("(Global)");
  55. window.getInputHandler().setTabCompleter(tabCompleter);
  56. WindowManager.addWindow(window);
  57. window.open();
  58. }
  59. /** {@inheritDoc} */
  60. @Override
  61. public InputWindow getFrame() {
  62. return window;
  63. }
  64. /** {@inheritDoc} */
  65. @Override
  66. public void windowClosing() {
  67. // 1: Make the window non-visible
  68. window.setVisible(false);
  69. // 2: Remove any callbacks or listeners
  70. // 3: Trigger any actions neccessary
  71. // 4: Trigger action for the window closing
  72. // 5: Inform any parents that the window is closing
  73. // 6: Remove the window from the window manager
  74. WindowManager.removeWindow(window);
  75. // 7: Remove any references to the window and parents
  76. window = null;
  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. if (!line.isEmpty()) {
  88. GlobalCommandParser.getGlobalCommandParser().parseCommand(window,
  89. CommandManager.getCommandChar() + line);
  90. }
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public int getMaxLineLength() {
  95. return -1;
  96. }
  97. /**
  98. * Retrieves the tab completer used by this global window.
  99. *
  100. * @return This global window's tab completer.
  101. */
  102. public TabCompleter getTabCompleter() {
  103. return tabCompleter;
  104. }
  105. /**
  106. * Initialises the global window if it's enabled in the config.
  107. */
  108. public static void init() {
  109. IdentityManager.getGlobalConfig().addChangeListener("general", "showglobalwindow",
  110. new ConfigChangeListener() {
  111. @Override
  112. public void configChanged(final String domain, final String key) {
  113. updateWindowState();
  114. }
  115. });
  116. updateWindowState();
  117. }
  118. protected static void updateWindowState() {
  119. synchronized (GlobalWindow.class) {
  120. if (IdentityManager.getGlobalConfig().getOptionBool("general", "showglobalwindow")) {
  121. if (globalWindow == null) {
  122. globalWindow = new GlobalWindow();
  123. }
  124. } else {
  125. if (globalWindow != null) {
  126. globalWindow.close();
  127. }
  128. }
  129. }
  130. }
  131. /**
  132. * Retrieves the global window if it has been previously opened by
  133. * {@link #init()}.
  134. *
  135. * @return A Global Window instance or null
  136. */
  137. public static GlobalWindow getGlobalWindow() {
  138. return globalWindow;
  139. }
  140. }