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.

AliasWrapper.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2006-2011 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.actions.wrappers;
  23. import com.dmdirc.GlobalWindow;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.ServerManager;
  26. import com.dmdirc.actions.Action;
  27. import com.dmdirc.actions.ActionCondition;
  28. import com.dmdirc.actions.ActionGroup;
  29. import com.dmdirc.actions.CoreActionType;
  30. import com.dmdirc.commandparser.CommandManager;
  31. import com.dmdirc.logger.ErrorLevel;
  32. import com.dmdirc.logger.Logger;
  33. import com.dmdirc.ui.input.TabCompletionType;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. /**
  37. * Encapsulates alias actions.
  38. */
  39. public final class AliasWrapper extends ActionGroup {
  40. /** Singleton instance of the alias wrapper. */
  41. private static AliasWrapper me;
  42. /** A list of registered alias names. */
  43. private final List<String> aliases = new ArrayList<String>();
  44. /**
  45. * Creates a new instance of AliasWrapper.
  46. */
  47. private AliasWrapper() {
  48. super("aliases");
  49. }
  50. /**
  51. * Retrieves a singleton instance of this alias wrapper.
  52. *
  53. * @return A singleton instance of AliasWrapper
  54. */
  55. public static synchronized AliasWrapper getAliasWrapper() {
  56. if (me == null) {
  57. me = new AliasWrapper();
  58. }
  59. return me;
  60. }
  61. /**
  62. * Retrieves a list of alias names registered with this wrapper.
  63. *
  64. * @return A list of alias names
  65. */
  66. public List<String> getAliases() {
  67. return new ArrayList<String>(aliases);
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. public void add(final Action action) {
  72. if (action.getTriggers()[0].equals(CoreActionType.UNKNOWN_COMMAND)) {
  73. final String commandName = getCommandName(action);
  74. if (commandName != null) {
  75. super.add(action);
  76. aliases.add(commandName);
  77. if (GlobalWindow.getGlobalWindow() != null) {
  78. GlobalWindow.getGlobalWindow().getTabCompleter()
  79. .addEntry(TabCompletionType.COMMAND, commandName);
  80. }
  81. for (Server server : ServerManager.getServerManager().getServers()) {
  82. server.getTabCompleter().addEntry(TabCompletionType.COMMAND, commandName);
  83. }
  84. } else {
  85. Logger.userError(ErrorLevel.MEDIUM, "Invalid alias action (no name): "
  86. + action.getName());
  87. }
  88. } else {
  89. Logger.userError(ErrorLevel.MEDIUM, "Invalid alias action (wrong trigger): "
  90. + action.getName());
  91. }
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public void remove(final Action action) {
  96. if (action.getTriggers()[0].equals(CoreActionType.UNKNOWN_COMMAND)) {
  97. super.remove(action);
  98. final String commandName = getCommandName(action);
  99. aliases.remove(commandName);
  100. for (Server server : ServerManager.getServerManager().getServers()) {
  101. server.getTabCompleter().removeEntry(TabCompletionType.COMMAND, commandName);
  102. }
  103. }
  104. }
  105. /**
  106. * Retrieves the command name of the specified alias action.
  107. *
  108. * @param action The action whose name is to be determined
  109. * @return The command name for the specified alias, or null if it has
  110. * no appropriate conditions.
  111. */
  112. public static String getCommandName(final Action action) {
  113. for (ActionCondition condition : action.getConditions()) {
  114. if (condition.getArg() == 1) {
  115. return CommandManager.getCommandManager().getCommandChar()
  116. + condition.getTarget();
  117. }
  118. }
  119. // How can we have an alias without a command name?
  120. return null;
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public boolean isDelible() {
  125. return false;
  126. }
  127. /** {@inheritDoc} */
  128. @Override
  129. public String getDescription() {
  130. return "Aliases allow you to create new commands that invoke one or "
  131. + "more other commands. You can manage aliases using the \""
  132. + "Alias Manager\", located in the Settings menu.";
  133. }
  134. }