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

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