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

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