private ShadowMenu addChild(final ModuleInfo info, final int depth) { final MenuPath menuPath = info.getMenuPath(); final MenuEntry entry = menuPath.get(depth); final boolean leaf = isLeaf(depth, menuPath); // retrieve existing child final ShadowMenu existingChild = children.get(entry.getName()); final ShadowMenu child; if (existingChild == null) { // create new child and add to table final String menuName = entry.getName(); final ShadowMenu newChild = new ShadowMenu(getContext(), info, depth, this); children.put(menuName, newChild); child = newChild; } else { // fill in any missing menu properties of existing child final MenuEntry childMenuEntry = existingChild.getMenuEntry(); childMenuEntry.assignProperties(entry); child = existingChild; } // recursively add remaining child menus if (!leaf) child.addChild(info, depth + 1); else if (existingChild != null) { if (log != null) { final ModuleInfo childInfo = existingChild.getModuleInfo(); if (childInfo != null && info.getPriority() == childInfo.getPriority()) { log.warn( "ShadowMenu: menu item already exists:\n" + // "\texisting: " + details(childInfo) + "\n" + // "\t ignored: " + details(info)); } else { log.debug( "ShadowMenu: higher-priority menu item already exists:\n" + "\texisting: " + details(childInfo) + "\n" + // "\t ignored: " + details(info)); } } } return child; }
private ShadowMenu getMenu(final MenuPath menuPath, final int index) { final MenuEntry entry = menuPath.get(index); // search for a child with matching menu entry for (final ShadowMenu child : children.values()) { if (entry.getName().equals(child.getMenuEntry().getName())) { // found matching child if (isLeaf(index, menuPath)) { // return child directly return child; } // recurse downward return child.getMenu(menuPath, index + 1); } } return null; }
private ShadowMenu( final Context context, final ModuleInfo moduleInfo, final int menuDepth, final ShadowMenu parent) { setContext(context); if (moduleInfo == null) { this.moduleInfo = null; menuEntry = null; } else { final MenuPath menuPath = moduleInfo.getMenuPath(); // preserve moduleInfo reference only for leaf items final boolean leaf = menuDepth == menuPath.size() - 1; this.moduleInfo = leaf ? moduleInfo : null; menuEntry = menuPath.get(menuDepth); } this.menuDepth = menuDepth; this.parent = parent; children = new HashMap<>(); }