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

com.talk2object.plum.interaction.viewbuilder.ActionsViewBuilder Maven / Gradle / Ivy

There is a newer version: 0.1.22
Show newest version
package com.talk2object.plum.interaction.viewbuilder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.talk2object.common.SortableItem;
import com.talk2object.plum.interaction.interaction.SortedActions;
import com.talk2object.plum.interaction.viewgeneneration.AbstractViewBuilder;
import com.talk2object.plum.interaction.viewgeneneration.ViewBuildContext;
import com.talk2object.plum.interaction.viewgeneneration.ViewClass;
import com.talk2object.plum.model.Action;
import com.talk2object.plum.view._abstract.components.menu.Menu;
import com.talk2object.plum.view._abstract.components.menu.MenuItem;
import com.talk2object.plum.view._abstract.window.PackingCase;
import com.talk2object.plum.view._abstract.window.UIComponent;

/**
 * create menu view for Actions.
 * 
 * @author jack
 *
 */
public class ActionsViewBuilder extends AbstractViewBuilder {

	@Override
	public UIComponent build(Object model, String viewClass,
			ViewBuildContext context, UIComponent parentView) {
		PackingCase packingCase = new PackingCase();

		SortedActions actions = (SortedActions) model;

		if (ViewClass.matches(ViewClass.MENU, viewClass)) {

			//key is menu name. sub menus
			Map>> subMenus = new HashMap>>();

			buildMenuItems(actions, subMenus);
			createMenu(actions, subMenus, packingCase);

			return packingCase;
		}

		return super.build(model, viewClass, context, parentView);
	}

	protected void buildMenuItems(SortedActions actionList,
			Map>> subMenus) {
		for (Action action : actionList.getActions()) {
			MenuItem menuItem = new MenuItem();
			menuItem.setModel(action);
			menuItem.updateView();

			addMenuItem(action.getCategory(), menuItem, action.getIndex(),
					subMenus);
		}

		for (List> list : subMenus.values()) {
			Collections.sort(list);
		}

	}

	protected void createMenu(SortedActions actions,
			Map>> subMenus,
			PackingCase packingCase) {
		// construct menu

		List menuNames = new ArrayList();
		for (String name : actions.getCategories())
			menuNames.add(name);

		// other menu names not in annotation append to tail of the name list.
		for (String name : subMenus.keySet()) {
			if (!menuNames.contains(name)) {
				menuNames.add(name);
			}
		}

		for (String menuName : menuNames) {
			UIComponent subMenu;
			if (!menuName.isEmpty()) {
				subMenu = new Menu();
				subMenu.setCaption(menuName);
				packingCase.addChild(subMenu);

			} else {
				subMenu = packingCase;
			}

			List> menuItems = subMenus.get(menuName);
			if (menuItems != null) {
				for (SortableItem si : menuItems) {
					subMenu.addChild(si.t);
				}
			}
		}

	}

	private void addMenuItem(String name, MenuItem menuItem, int index,
			Map>> subMenus) {
		List> menuItems = subMenus.get(name);
		if (menuItems == null) {
			menuItems = new ArrayList>();
			// menu.setCaption(parentMenu);

			subMenus.put(name, menuItems);
		}

		SortableItem sortableItem = new SortableItem(
				menuItem, index);
		menuItems.add(sortableItem);
	}

	@Override
	public Class getModelClass() {

		return SortedActions.class;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy