All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bndtools.utils.HierarchicalMenu Maven / Gradle / Ivy

The newest version!
package bndtools.utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;

/**
 * Helper to create menus with submenus from a list of
 * {@link HierarchicalLabel}s.
 */
public class HierarchicalMenu {

	private final List> labels = new ArrayList<>();

	public HierarchicalMenu() {

	}

	public HierarchicalMenu add(HierarchicalLabel label) {
		labels.add(label);
		return this;
	}

	public void build(IMenuManager root) {
		createMenu(labels, root);
	}


	public static void createMenu(List> labels, IMenuManager rootMenu) {

		Map menus = new LinkedHashMap<>();

		for (HierarchicalLabel hl : labels) {
			IMenuManager current = rootMenu;
			List hlLabels = hl.getLabels();
			for (int i = 0; i < hlLabels.size(); i++) {
				String currentLabel = hlLabels.get(i);
				if (i == hlLabels.size() - 1) {
					// currentMenu = getOrCreateSubMenu(menuMap, currentMenu,
					// currentLabel);
					current.add(hl.getLeafAction());
				} else {
					current = getOrCreateSubMenu(menus, current, currentLabel);
				}
			}
		}

	}

	private static IMenuManager getOrCreateSubMenu(Map menus, IMenuManager parent, String label) {
		String key = parent.toString() + " -> " + label;

		if (!menus.containsKey(key)) {
			IMenuManager newMenu = new MenuManager(label, null);
			parent.add(newMenu);
			menus.put(key, newMenu);
		}

		return menus.get(key);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy