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

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