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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. 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.processEvent(CoreActionType.CLIENT_POPUP_GENERATED,
  49. null, menuType, menu, configManager);
  50. return menu;
  51. }
  52. /**
  53. * Retrieves the menu with the specified name.
  54. *
  55. * @param menuName The name of the menu to read
  56. * @param type The type of the menu that is needed
  57. * @param configManager The config manager to be used for the menu
  58. * @return The PopupMenu with the specified name
  59. */
  60. private static PopupMenu getMenu(final String menuName,
  61. final PopupType type, final ConfigManager configManager) {
  62. final PopupMenu res = new PopupMenu();
  63. for (String item : configManager.getOptionList("popups", menuName)) {
  64. if (item.length() > 0 && item.charAt(0) == '<') {
  65. res.addAll(getMenu(item.substring(1), type, configManager).getItems());
  66. } else {
  67. res.add(getItem(item, type, configManager));
  68. }
  69. }
  70. return res;
  71. }
  72. /**
  73. * Creates a PopupMenuItem for the specified item.
  74. *
  75. * @param item The item to be turned into a PopupMenuItem
  76. * @param type The type of popup item to create
  77. * @param configManager The config manager to beused for the menu
  78. * @return The corresponding PopupMenuItem
  79. */
  80. private static PopupMenuItem getItem(final String item,
  81. final PopupType type, final ConfigManager configManager) {
  82. PopupMenuItem res;
  83. if ("-".equals(item)) {
  84. res = new PopupMenuItem();
  85. } else {
  86. final int colon = item.indexOf(':');
  87. if (colon == -1) {
  88. throw new IllegalArgumentException("Invalid popup menu item: "
  89. + item);
  90. }
  91. final String name = item.substring(0, colon);
  92. final String command = item.substring(colon + 1);
  93. if (command.length() > 0 && command.charAt(0) == '<') {
  94. res = new PopupMenuItem(name, getMenu(command.substring(1),
  95. type, configManager));
  96. } else {
  97. res = new PopupMenuItem(name, type.getArity(), command);
  98. }
  99. }
  100. return res;
  101. }
  102. }