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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.events.ClientPopupGeneratedEvent;
  19. import com.dmdirc.interfaces.CommandController;
  20. import com.dmdirc.events.eventbus.EventBus;
  21. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  22. import javax.inject.Inject;
  23. /**
  24. * The popup manager manages which commands should be present in popup menus.
  25. */
  26. public class PopupManager {
  27. /** The command controller to use for items. */
  28. private final CommandController commandController;
  29. /** The bus to dispatch events on. */
  30. private final EventBus eventBus;
  31. /**
  32. * Creates a new instance of PopupManager.
  33. *
  34. * @param commandController The controller to use for commands.
  35. * @param eventBus The bus to dispatch events on.
  36. */
  37. @Inject
  38. public PopupManager(final CommandController commandController, final EventBus eventBus) {
  39. this.commandController = commandController;
  40. this.eventBus = eventBus;
  41. }
  42. /**
  43. * Returns the popup menu that should be used for the specified type. Configuration data is read
  44. * from the specified config manager.
  45. *
  46. * @param menuType The type of the menu that is needed
  47. * @param configManager The config manager to be used for the menu
  48. *
  49. * @return The PopupMenu that should be displayed
  50. */
  51. public PopupMenu getMenu(final PopupType menuType, final AggregateConfigProvider configManager) {
  52. final PopupMenu menu = getMenu(menuType.toString(), menuType, configManager);
  53. eventBus.publishAsync(new ClientPopupGeneratedEvent(menuType, menu, configManager));
  54. return menu;
  55. }
  56. /**
  57. * Retrieves the menu with the specified name.
  58. *
  59. * @param menuName The name of the menu to read
  60. * @param type The type of the menu that is needed
  61. * @param configManager The config manager to be used for the menu
  62. *
  63. * @return The PopupMenu with the specified name
  64. */
  65. private PopupMenu getMenu(final String menuName,
  66. final PopupType type, final AggregateConfigProvider configManager) {
  67. final PopupMenu res = new PopupMenu();
  68. for (String item : configManager.getOptionList("popups", menuName)) {
  69. if (!item.isEmpty() && item.charAt(0) == '<') {
  70. res.addAll(getMenu(item.substring(1), type, configManager).getItems());
  71. } else {
  72. res.add(getItem(item, type, configManager));
  73. }
  74. }
  75. return res;
  76. }
  77. /**
  78. * Creates a PopupMenuItem for the specified item.
  79. *
  80. * @param item The item to be turned into a PopupMenuItem
  81. * @param type The type of popup item to create
  82. * @param configManager The config manager to be used for the menu
  83. *
  84. * @return The corresponding PopupMenuItem
  85. */
  86. private PopupMenuItem getItem(final String item,
  87. final PopupType type, final AggregateConfigProvider configManager) {
  88. final PopupMenuItem res;
  89. if ("-".equals(item)) {
  90. res = new PopupMenuItem(commandController);
  91. } else {
  92. final int colon = item.indexOf(':');
  93. if (colon == -1) {
  94. throw new IllegalArgumentException("Invalid popup menu item: "
  95. + item);
  96. }
  97. final String name = item.substring(0, colon);
  98. final String command = item.substring(colon + 1);
  99. if (!command.isEmpty() && command.charAt(0) == '<') {
  100. res = new PopupMenuItem(commandController,
  101. name, getMenu(command.substring(1),
  102. type, configManager));
  103. } else {
  104. res = new PopupMenuItem(commandController,
  105. name, type.getArity(), command);
  106. }
  107. }
  108. return res;
  109. }
  110. }