private void initialize() { final Map<ComponentCategory, DescriptorMenu> descriptorMenus = new HashMap<ComponentCategory, DescriptorMenu>(); // build sub menus { for (E descriptor : _descriptors) { Set<ComponentCategory> componentCategories = descriptor.getComponentCategories(); for (ComponentCategory componentCategory : componentCategories) { DescriptorMenu menu = descriptorMenus.get(componentCategory); if (menu == null) { menu = new DescriptorMenu(componentCategory); descriptorMenus.put(componentCategory, menu); } menu.addComponentClass(descriptor.getComponentClass()); } } } { // place sub menus List<DescriptorMenu> sortedMenus = CollectionUtils2.sorted(descriptorMenus.values()); for (DescriptorMenu descriptorMenu : sortedMenus) { int count = descriptorMenu.getComponentClassCount(); if (count <= 1) { // disregard categories with only a single component in // them! ComponentCategory category = descriptorMenu.getComponentCategory(); logger.info( "Disregarding menu for category '{}' because of too few components ({})", category, count); descriptorMenus.remove(category); } else { // add menu add(descriptorMenu); } } } // place items that are not in any submenus { for (final E descriptor : _descriptors) { boolean placedInSubmenu = false; Class<?> componentClass = descriptor.getComponentClass(); JMenuItem menuItem = createMenuItem(descriptor); if (menuItem != null) { for (DescriptorMenu descriptorMenu : descriptorMenus.values()) { if (descriptorMenu.containsComponentClass(componentClass)) { descriptorMenu.add(menuItem); placedInSubmenu = true; // create a new menu item (or else it will be moved // instead of added. menuItem = createMenuItem(descriptor); } } if (!placedInSubmenu) { // add menu item add(menuItem); } } } } // disregard WriteDataCategory DescriptorMenu writeDataMenu = descriptorMenus.get(new WriteDataCategory()); if (writeDataMenu != null) { remove(writeDataMenu); } }
public DescriptorPopupMenu(Collection<? extends E> descriptors) { _descriptors = CollectionUtils2.sorted(descriptors, new DisplayNameComparator()); initialize(); }