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

com.cooldatasoft.horizontal.dropdown.multiLevelCss.MultiLevelCssMenu Maven / Gradle / Ivy

There is a newer version: 7.0.0.4
Show newest version
package com.cooldatasoft.horizontal.dropdown.multiLevelCss;

import java.security.InvalidParameterException;
import java.util.List;

import org.apache.wicket.core.util.string.JavaScriptUtils;
import org.apache.wicket.markup.head.CssHeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.resource.CssResourceReference;
import org.apache.wicket.request.resource.JavaScriptResourceReference;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.resource.DynamicJQueryResourceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cooldatasoft.common.MenuItem;

/**
 * http://www.dynamicdrive.com/style/csslibrary/item/ jquery_multi_level_css_menu_2/
 * 
 * @author Fatih Mehmet UCAR - [email protected]
 * 
 */
public class MultiLevelCssMenu extends Panel implements IHeaderContributor {

	private final static Logger LOGGER = LoggerFactory.getLogger(MultiLevelCssMenu.class);

	private static final long serialVersionUID = 1L;

	private final static ResourceReference DOWN_GIF = new PackageResourceReference(MultiLevelCssMenu.class, "js/down.gif");
	private final static ResourceReference RIGHT_GIF = new PackageResourceReference(MultiLevelCssMenu.class, "js/right.gif");

	private final static ResourceReference MENU_CSS = new CssResourceReference(MultiLevelCssMenu.class,
			"css/MultiLevelCssMenu.css");
	private final static ResourceReference MENU_JAVASCRIPT = new JavaScriptResourceReference(MultiLevelCssMenu.class,
			"js/MultiLevelCssMenu.js");

	@Override
	public void renderHead(IHeaderResponse response) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("var downGifRelativeLocation='").append(RequestCycle.get().urlFor(DOWN_GIF, null)).append("';");
		buffer.append("var rightGifRelativeLocation='").append(RequestCycle.get().urlFor(RIGHT_GIF, null)).append("';");

		response.getResponse().write(JavaScriptUtils.SCRIPT_OPEN_TAG);
		response.getResponse().write(buffer.toString());
		response.getResponse().write(JavaScriptUtils.SCRIPT_CLOSE_TAG);
		
		response.render(JavaScriptHeaderItem.forReference(DynamicJQueryResourceReference.get()));
		response.render(JavaScriptHeaderItem.forReference(MENU_JAVASCRIPT));
		response.render(CssHeaderItem.forReference(MENU_CSS));
	}

	public MultiLevelCssMenu(String id, List menuItemList) {
		super(id);
		setRenderBodyOnly(true);
		MultiLevelMenu multiLevelMenu = new MultiLevelMenu("multiLevelMenu", menuItemList);
		multiLevelMenu.setRenderBodyOnly(true);
		add(multiLevelMenu);
	}

	private void processResponse(MenuItem menuItem) {
		switch (menuItem.getDestinationType()) {
		case EXTERNAL_LINK:
			// forward to external link
			break;
		case WEB_PAGE_CLASS:
			setResponsePage(menuItem.getResponsePageClass());
			break;
		case WEB_PAGE_INSTANCE:
			setResponsePage(menuItem.getResponsePage());
			break;
		case AJAX_TARGET:
			menuItem.getAjaxLink().onClick();
			break;
		case NONE:
			LOGGER.warn("MenuItem does not have a destination! ");
			break;
		default:
			throw new RuntimeException("Destination type not valid!");
		}
	}

	class MultiLevelMenu extends Panel {

		private static final long serialVersionUID = 1L;

		public MultiLevelMenu(String id, List menuItemList) {
			super(id);
			if (menuItemList == null || menuItemList.size() == 0) {
				return;
			}
			ListView menu = buildMultiLevelMenu("menuList", menuItemList);
			menu.setReuseItems(true);
			add(menu);
		}

		private ListView buildMultiLevelMenu(String id, List menuItemList) {
			return new ListView(id, menuItemList) {
				private static final long serialVersionUID = 1L;

				public void populateItem(final ListItem item) {
					final MenuItem menuItem = ((MenuItem) item.getModelObject());
					Link link = new Link("menuLink") {
						private static final long serialVersionUID = 1L;

						@Override
						public void onClick() {
							if (menuItem != null) {
								processResponse(menuItem);
							}
						}
					};

					Label linkText = new Label("menuLinkText", menuItem.getMenuText());
					linkText.setRenderBodyOnly(true);
					link.add(linkText);
					item.add(link);

					List submenuItemList = menuItem.getSubMenuItemList();
					// INFO If submenu exists, output it to html. If not, add
					// empty markup container and hide it.
					if (submenuItemList != null && submenuItemList.size() > 0) {
						MultiLevelMenu subLevelMenu = new MultiLevelMenu("submenuListContainer", submenuItemList);
						subLevelMenu.setRenderBodyOnly(true);
						item.add(subLevelMenu);
					} else {
						WebMarkupContainer submenuMarkupContainer = new WebMarkupContainer("submenuListContainer");
						submenuMarkupContainer.setRenderBodyOnly(true);
						item.add(submenuMarkupContainer);
					}
				}
			};
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy