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

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