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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser;
  18. import com.dmdirc.interfaces.CommandController;
  19. /**
  20. * Represents an abstract, UI-independent popup menu item.
  21. */
  22. public class PopupMenuItem {
  23. /** The command manager to use to retrieve command information. */
  24. private final CommandController commandManager;
  25. /** Whether this item is a divider. */
  26. private boolean divider;
  27. /** The submenu for this item, if any. */
  28. private PopupMenu submenu;
  29. /** The name of this item, if any. */
  30. private String name;
  31. /** The command for this item, if any. */
  32. private String command;
  33. /** The arity of the command. */
  34. private int arity;
  35. /**
  36. * Creates a new PopupMenuItem that is used as a divider.
  37. *
  38. * @param manager The command manager to use to retrieve command information
  39. */
  40. public PopupMenuItem(final CommandController manager) {
  41. divider = true;
  42. this.commandManager = manager;
  43. }
  44. /**
  45. * Creates a new PopupMenuItem that is used as a submenu.
  46. *
  47. * @param manager The command manager to use to retrieve command information
  48. * @param name The name of the menu item
  49. * @param submenu The submenu of this item
  50. */
  51. public PopupMenuItem(final CommandController manager, final String name, final PopupMenu submenu) {
  52. this.name = name;
  53. this.submenu = submenu;
  54. this.commandManager = manager;
  55. }
  56. /**
  57. * Creates a new PopupMenuItem that executes a command.
  58. *
  59. * @param manager The command manager to use to retrieve command information
  60. * @param name The name of the menu item
  61. * @param arity The arity of the command this item will execute
  62. * @param command The command to be executed
  63. */
  64. public PopupMenuItem(final CommandController manager, final String name, final int arity,
  65. final String command) {
  66. this.name = name;
  67. this.arity = arity;
  68. this.command = command;
  69. this.commandManager = manager;
  70. }
  71. /**
  72. * Determines if this menu item is a divider or not.
  73. *
  74. * @return True if this item is a divider, false otherwise.
  75. */
  76. public boolean isDivider() {
  77. return divider;
  78. }
  79. /**
  80. * Determines if this menu item contains a submenu or not.
  81. *
  82. * @return True if this item contains a submenu, false otherwise.
  83. */
  84. public boolean isSubMenu() {
  85. return submenu != null;
  86. }
  87. /**
  88. * Retrieves the submenu associated with this item.
  89. *
  90. * @return This menu item's submenu.
  91. */
  92. public PopupMenu getSubMenu() {
  93. return submenu;
  94. }
  95. /**
  96. * Retrieves the name of this menu item.
  97. *
  98. * @return This menu item's name.
  99. */
  100. public String getName() {
  101. return name;
  102. }
  103. /**
  104. * Retrieves the command for this menu item, with the specified arguments substituted in. Note
  105. * that the result may actually consist of multiple commands on separate lines.
  106. *
  107. * @param arguments A two dimensional array containing one array of arguments for each subject
  108. * of the command.
  109. *
  110. * @return The command to be passed to a command parser
  111. */
  112. public String getCommand(final Object[][] arguments) {
  113. final StringBuilder builder = new StringBuilder();
  114. final String actualCommand;
  115. final int expectedArgs;
  116. if (command.matches("^[0-9]+:.+")) {
  117. final int index = command.indexOf(':');
  118. expectedArgs = Integer.parseInt(command.substring(0, index));
  119. actualCommand = command.substring(index + 1);
  120. } else {
  121. expectedArgs = arity;
  122. actualCommand = command;
  123. }
  124. final Object[] args = new Object[expectedArgs];
  125. int offset = 0;
  126. for (Object[] singleArg : arguments) {
  127. System.arraycopy(singleArg, 0, args, offset, arity);
  128. offset += arity;
  129. if (offset >= expectedArgs) {
  130. if (builder.length() > 0) {
  131. builder.append('\n');
  132. }
  133. builder.append(commandManager.getCommandChar());
  134. builder.append(String.format(actualCommand, args));
  135. offset = 0;
  136. }
  137. }
  138. if (offset > 0) {
  139. for (int i = offset; i < expectedArgs; i++) {
  140. args[i] = "";
  141. }
  142. if (builder.length() > 0) {
  143. builder.append('\n');
  144. }
  145. builder.append(commandManager.getCommandChar());
  146. builder.append(String.format(actualCommand, args));
  147. }
  148. return builder.toString();
  149. }
  150. }