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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2006-2014 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.actions.wrappers;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.actions.Action;
  25. import com.dmdirc.actions.ActionCondition;
  26. import com.dmdirc.actions.ActionGroup;
  27. import com.dmdirc.actions.CoreActionType;
  28. import com.dmdirc.interfaces.CommandController;
  29. import com.dmdirc.logger.ErrorLevel;
  30. import com.dmdirc.logger.Logger;
  31. import com.dmdirc.ui.WindowManager;
  32. import com.dmdirc.ui.input.TabCompletionType;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import javax.inject.Inject;
  36. import javax.inject.Singleton;
  37. /**
  38. * Encapsulates alias actions.
  39. */
  40. @Singleton
  41. public class AliasWrapper extends ActionGroup {
  42. /** The name of the actions group we wrap. */
  43. protected static final String GROUP_NAME = "aliases";
  44. /** A list of registered alias names. */
  45. private final List<String> aliases = new ArrayList<>();
  46. /** Command controller to get command info from. */
  47. private final CommandController commandController;
  48. /** Server Manager. */
  49. private final WindowManager windowManager;
  50. /**
  51. * Creates a new instance of AliasWrapper.
  52. *
  53. * @param commandController Command controller to get command info from.
  54. * @param windowManager The window manager to use to find root windows.
  55. */
  56. @Inject
  57. public AliasWrapper(
  58. final CommandController commandController,
  59. final WindowManager windowManager) {
  60. super(GROUP_NAME);
  61. this.commandController = commandController;
  62. this.windowManager = windowManager;
  63. }
  64. /**
  65. * Retrieves a list of alias names registered with this wrapper.
  66. *
  67. * @return A list of alias names
  68. */
  69. public List<String> getAliases() {
  70. return new ArrayList<>(aliases);
  71. }
  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. for (FrameContainer root : windowManager.getRootWindows()) {
  80. if (root.isWritable()) {
  81. root.getTabCompleter().addEntry(TabCompletionType.COMMAND, commandName);
  82. }
  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. @Override
  94. public void remove(final Action action) {
  95. if (action.getTriggers()[0].equals(CoreActionType.UNKNOWN_COMMAND)) {
  96. super.remove(action);
  97. final String commandName = getCommandName(action);
  98. aliases.remove(commandName);
  99. for (FrameContainer root : windowManager.getRootWindows()) {
  100. if (root.isWritable()) {
  101. root.getTabCompleter().removeEntry(TabCompletionType.COMMAND, commandName);
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * Retrieves the command name of the specified alias action.
  108. *
  109. * @param action The action whose name is to be determined
  110. *
  111. * @return The command name for the specified alias, or null if it has no appropriate
  112. * conditions.
  113. */
  114. public String getCommandName(final Action action) {
  115. for (ActionCondition condition : action.getConditions()) {
  116. if (condition.getArg() == 1) {
  117. return commandController.getCommandChar()
  118. + condition.getTarget();
  119. }
  120. }
  121. // How can we have an alias without a command name?
  122. return null;
  123. }
  124. @Override
  125. public boolean isDelible() {
  126. return false;
  127. }
  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. }