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

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