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.

PopupManager.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2006-2012 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.actions.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.config.ConfigManager;
  26. /**
  27. * The popup manager manages which commands should be present in popup menus.
  28. */
  29. public class PopupManager {
  30. /**
  31. * Creates a new instance of PopupManager.
  32. */
  33. private PopupManager() {
  34. // Shouldn't be instansiated.
  35. }
  36. /**
  37. * Returns the popup menu that should be used for the specified type.
  38. * Configuration data is read from the specified config manager.
  39. *
  40. * @param menuType The type of the menu that is needed
  41. * @param configManager The config manager to be used for the menu
  42. * @return The PopupMenu that should be displayed
  43. */
  44. public static PopupMenu getMenu(final PopupType menuType, final ConfigManager configManager) {
  45. final PopupMenu menu = getMenu(menuType.toString(), menuType, configManager);
  46. ActionManager.getActionManager().triggerEvent(
  47. CoreActionType.CLIENT_POPUP_GENERATED, null, menuType, menu,
  48. configManager);
  49. return menu;
  50. }
  51. /**
  52. * Retrieves the menu with the specified name.
  53. *
  54. * @param menuName The name of the menu to read
  55. * @param type The type of the menu that is needed
  56. * @param configManager The config manager to be used for the menu
  57. * @return The PopupMenu with the specified name
  58. */
  59. private static PopupMenu getMenu(final String menuName,
  60. final PopupType type, final ConfigManager configManager) {
  61. final PopupMenu res = new PopupMenu();
  62. for (String item : configManager.getOptionList("popups", menuName)) {
  63. if (item.length() > 0 && item.charAt(0) == '<') {
  64. res.addAll(getMenu(item.substring(1), type, configManager).getItems());
  65. } else {
  66. res.add(getItem(item, type, configManager));
  67. }
  68. }
  69. return res;
  70. }
  71. /**
  72. * Creates a PopupMenuItem for the specified item.
  73. *
  74. * @param item The item to be turned into a PopupMenuItem
  75. * @param type The type of popup item to create
  76. * @param configManager The config manager to beused for the menu
  77. * @return The corresponding PopupMenuItem
  78. */
  79. private static PopupMenuItem getItem(final String item,
  80. final PopupType type, final ConfigManager configManager) {
  81. PopupMenuItem res;
  82. if ("-".equals(item)) {
  83. res = new PopupMenuItem(CommandManager.getCommandManager());
  84. } else {
  85. final int colon = item.indexOf(':');
  86. if (colon == -1) {
  87. throw new IllegalArgumentException("Invalid popup menu item: "
  88. + item);
  89. }
  90. final String name = item.substring(0, colon);
  91. final String command = item.substring(colon + 1);
  92. if (command.length() > 0 && command.charAt(0) == '<') {
  93. res = new PopupMenuItem(CommandManager.getCommandManager(),
  94. name, getMenu(command.substring(1),
  95. type, configManager));
  96. } else {
  97. res = new PopupMenuItem(CommandManager.getCommandManager(),
  98. name, type.getArity(), command);
  99. }
  100. }
  101. return res;
  102. }
  103. }