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

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