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

com.cooldatasoft.horizontal.dropdown.chrome.ChromeDropDownMenu Maven / Gradle / Ivy

package com.cooldatasoft.horizontal.dropdown.chrome;

import java.util.List;

import org.apache.wicket.AttributeModifier;
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.model.Model;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cooldatasoft.common.MenuItem;

/**
 * 
 * @author Fatih Mehmet UCAR - [email protected]
 * 
 */
public class ChromeDropDownMenu extends Panel implements IHeaderContributor {

	private final static Logger LOGGER = LoggerFactory.getLogger(ChromeDropDownMenu.class);
	private static final long serialVersionUID = 1L;

	private final static ResourceReference DOWN_GIF = new PackageResourceReference(ChromeDropDownMenu.class, "images/down.gif");
	private static final ResourceReference MENU_JS = new JavaScriptResourceReference(ChromeDropDownMenu.class, "js/chrome.js");
	private final static CssResourceReference MENU_CSS_THEME1 = new CssResourceReference(ChromeDropDownMenu.class,
			"css/chrome1.css");
	private final static CssResourceReference MENU_CSS_THEME2 = new CssResourceReference(ChromeDropDownMenu.class,
			"css/chrome2.css");
	private final static CssResourceReference MENU_CSS_THEME3 = new CssResourceReference(ChromeDropDownMenu.class,
			"css/chrome3.css");
	private final static CssResourceReference MENU_CSS_THEME4 = new CssResourceReference(ChromeDropDownMenu.class,
			"css/chrome4.css");

	public enum CSS {
		THEME1, THEME2, THEME3, THEME4
	};

	private int numberOfMenu;
	private final ResourceReference menuCssResourceReference;

	@Override
	public void renderHead(IHeaderResponse response) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("var downGifRelativeLocation=''; ");

		response.getResponse().write(JavaScriptUtils.SCRIPT_OPEN_TAG);
		response.getResponse().write(buffer.toString());
		response.getResponse().write(JavaScriptUtils.SCRIPT_CLOSE_TAG);

		
		response.render(JavaScriptHeaderItem.forReference(MENU_JS));
		response.render(CssHeaderItem.forReference(menuCssResourceReference));
	}

	private void processResponse(MenuItem menuItem) {
		switch (menuItem.getDestinationType()) {
		case EXTERNAL_LINK:
			break;
		case WEB_PAGE_CLASS:
			setResponsePage(menuItem.getResponsePageClass());
			break;
		case WEB_PAGE_INSTANCE:
			setResponsePage(menuItem.getResponsePage());
			break;
		case AJAX_TARGET:
			// DO Nothing as ajax will execute
			break;
		case NONE:
			LOGGER.warn("MenuItem does not have a destination! ");
			break;
		default:
			throw new RuntimeException("Destination type not valid!");
		}
	}

	public ChromeDropDownMenu(String id, List menuItemList) {
		this(id, menuItemList, CSS.THEME1);
	}

	/**
	 * http://www.dynamicdrive.com/dynamicindex1/chrome/index.htm
	 * 
	 * First element of each list is assumed to be the top menu Use ChromeMenu.CSS.THEME1-4 for
	 * different css themes
	 * 
	 * @param id wicket id of field
	 * @param menuItemList list of menu items
	 * @param cssTheme css theme
	 */

	public ChromeDropDownMenu(String id, List menuItemList, CSS cssTheme) {
		super(id);
		if (cssTheme == CSS.THEME1) {
			menuCssResourceReference = MENU_CSS_THEME1;
		} else if (cssTheme == CSS.THEME2) {
			menuCssResourceReference = MENU_CSS_THEME2;
		} else if (cssTheme == CSS.THEME3) {
			menuCssResourceReference = MENU_CSS_THEME3;
		} else if (cssTheme == CSS.THEME4) {
			menuCssResourceReference = MENU_CSS_THEME4;
		} else {
			menuCssResourceReference = MENU_CSS_THEME1;
		}

		ListView chromePrimaryMenuListView = new ListView("primaryMenuList", menuItemList) {

			private static final long serialVersionUID = 1L;
			int itemCount = 0;

			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);
						}
					}
				};

				// Adding submenu to menu item
				link.add(new AttributeModifier("rel", new Model("dropmenu" + itemCount)));
				setNumberOfMenu(itemCount++);

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

		ListView submenuListView = new ListView("submenuList", menuItemList) {
			private static final long serialVersionUID = 1L;
			int itemCount = 0;

			@Override
			public void populateItem(final ListItem item) {
				MenuItem menuItem = (MenuItem) item.getModelObject();
				List subMenuList = menuItem.getSubMenuItemList();

				WebMarkupContainer submenuDiv = new WebMarkupContainer("submenuDiv");
				submenuDiv.add(new AttributeModifier("id", new Model("dropmenu" + itemCount)));

				ListView submenuItem = new ListView("submenuItem", subMenuList) {
					private static final long serialVersionUID = 1L;

					public void populateItem(final ListItem item) {

						final MenuItem subMenuItem = (MenuItem) item.getModelObject();

						Link link = new Link("menuLink") {
							private static final long serialVersionUID = 1L;

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

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

				submenuDiv.add(submenuItem);
				itemCount++;
				item.add(submenuDiv);
				item.setRenderBodyOnly(true);

			}
		};
		submenuListView.setReuseItems(true);
		add(submenuListView);
		setRenderBodyOnly(true);
	}

	public int getNumberOfMenu() {
		return numberOfMenu;
	}

	public void setNumberOfMenu(int numberOfMenu) {
		this.numberOfMenu = numberOfMenu;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy