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.

PopupMenuItem.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2015 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.commandparser;
  23. import com.dmdirc.interfaces.CommandController;
  24. /**
  25. * Represents an abstract, UI-independent popup menu item.
  26. */
  27. public class PopupMenuItem {
  28. /** The command manager to use to retrieve command information. */
  29. private final CommandController commandManager;
  30. /** Whether this item is a divider. */
  31. private boolean divider;
  32. /** The submenu for this item, if any. */
  33. private PopupMenu submenu;
  34. /** The name of this item, if any. */
  35. private String name;
  36. /** The command for this item, if any. */
  37. private String command;
  38. /** The arity of the command. */
  39. private int arity;
  40. /**
  41. * Creates a new PopupMenuItem that is used as a divider.
  42. *
  43. * @param manager The command manager to use to retrieve command information
  44. */
  45. public PopupMenuItem(final CommandController manager) {
  46. divider = true;
  47. this.commandManager = manager;
  48. }
  49. /**
  50. * Creates a new PopupMenuItem that is used as a submenu.
  51. *
  52. * @param manager The command manager to use to retrieve command information
  53. * @param name The name of the menu item
  54. * @param submenu The submenu of this item
  55. */
  56. public PopupMenuItem(final CommandController manager, final String name, final PopupMenu submenu) {
  57. this.name = name;
  58. this.submenu = submenu;
  59. this.commandManager = manager;
  60. }
  61. /**
  62. * Creates a new PopupMenuItem that executes a command.
  63. *
  64. * @param manager The command manager to use to retrieve command information
  65. * @param name The name of the menu item
  66. * @param arity The arity of the command this item will execute
  67. * @param command The command to be executed
  68. */
  69. public PopupMenuItem(final CommandController manager, final String name, final int arity,
  70. final String command) {
  71. this.name = name;
  72. this.arity = arity;
  73. this.command = command;
  74. this.commandManager = manager;
  75. }
  76. /**
  77. * Determines if this menu item is a divider or not.
  78. *
  79. * @return True if this item is a divider, false otherwise.
  80. */
  81. public boolean isDivider() {
  82. return divider;
  83. }
  84. /**
  85. * Determines if this menu item contains a submenu or not.
  86. *
  87. * @return True if this item contains a submenu, false otherwise.
  88. */
  89. public boolean isSubMenu() {
  90. return submenu != null;
  91. }
  92. /**
  93. * Retrieves the submenu associated with this item.
  94. *
  95. * @return This menu item's submenu.
  96. */
  97. public PopupMenu getSubMenu() {
  98. return submenu;
  99. }
  100. /**
  101. * Retrieves the name of this menu item.
  102. *
  103. * @return This menu item's name.
  104. */
  105. public String getName() {
  106. return name;
  107. }
  108. /**
  109. * Retrieves the command for this menu item, with the specified arguments substituted in. Note
  110. * that the result may actually consist of multiple commands on separate lines.
  111. *
  112. * @param arguments A two dimensional array containing one array of arguments for each subject
  113. * of the command.
  114. *
  115. * @return The command to be passed to a command parser
  116. */
  117. public String getCommand(final Object[][] arguments) {
  118. final StringBuilder builder = new StringBuilder();
  119. final String actualCommand;
  120. final int expectedArgs;
  121. if (command.matches("^[0-9]+:.+")) {
  122. final int index = command.indexOf(':');
  123. expectedArgs = Integer.parseInt(command.substring(0, index));
  124. actualCommand = command.substring(index + 1);
  125. } else {
  126. expectedArgs = arity;
  127. actualCommand = command;
  128. }
  129. final Object[] args = new Object[expectedArgs];
  130. int offset = 0;
  131. for (Object[] singleArg : arguments) {
  132. System.arraycopy(singleArg, 0, args, offset, arity);
  133. offset += arity;
  134. if (offset >= expectedArgs) {
  135. if (builder.length() > 0) {
  136. builder.append('\n');
  137. }
  138. builder.append(commandManager.getCommandChar());
  139. builder.append(String.format(actualCommand, args));
  140. offset = 0;
  141. }
  142. }
  143. if (offset > 0) {
  144. for (int i = offset; i < expectedArgs; i++) {
  145. args[i] = "";
  146. }
  147. if (builder.length() > 0) {
  148. builder.append('\n');
  149. }
  150. builder.append(commandManager.getCommandChar());
  151. builder.append(String.format(actualCommand, args));
  152. }
  153. return builder.toString();
  154. }
  155. }