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

com.jgeppert.struts2.jquery.components.Menu Maven / Gradle / Ivy

There is a newer version: 5.0.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.jgeppert.struts2.jquery.components;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.util.MakeIterator;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.apache.struts2.views.annotations.StrutsTagSkipInheritance;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * 

* Renders a menu. *

* *

* Examples *

* *

* Menu with Items *

*
 * 	<sj:menu id="menuWithItems" cssStyle="width:50%">
 * <sj:menuItem title="Struts2" href="http://struts.apache.org/2.x/index.html"/>
 * <sj:menuItem title="Struts2 jQuery News" href="http://www.jgeppert.com/category/java/struts2-jquery/"/>
 * <sj:menuItem title="Struts2 Plugins">
 * <sj:menu id="subMenuPlugins">
 * <sj:menuItem title="Struts2 Plugins" href="https://cwiki.apache.org/S2PLUGINS/home.html"/>
 * <sj:menuItem title="Struts2 jQuery Plugin" href="http://code.google.com/p/struts2-jquery/"/>
 * <sj:menuItem title="Struts2 Bootstrap Plugin" href="http://code.google.com/p/struts2-jquery/"/>
 * </sj:menu>
 * </sj:menuItem>
 *
 * <sj:menuItem title="Struts2 @ Social Media">
 * <sj:menu id="subMenuSocialMedia">
 * <sj:menuItem title="Struts2 @ Twitter" href="https://twitter.com/TheApacheStruts"/>
 * <sj:menuItem title="Struts2 @ Google+" href="https://www.google.com/+ApacheStruts"/>
 * <sj:menuItem title="Struts2 @ Facebook" href="http://www.facebook.com/struts2"/>
 * </sj:menu>
 * </sj:menuItem>
 * <sj:menuItem title="AJAX">
 * <sj:menu id="subMenuAjax">
 * <s:url var="ajax1" value="/ajax1.action"/>
 * <sj:menuItem title="Ajax 1" href="%{ajax1}" targets="result"/>
 * <s:url var="ajax2" value="/ajax2.action"/>
 * <sj:menuItem title="Ajax 2" href="%{ajax2}" targets="result" effect="highlight" effectDuration="2500"/>
 * <s:url var="ajax3" value="/ajax3.action"/>
 * <sj:menuItem title="Ajax 3" href="%{ajax3}" targets="result" onBeforeTopics="beforeLink" onCompleteTopics="completeLink"/>
 * <s:url var="ajax4" value="/ajax4.action"/>
 * <sj:menuItem title="Ajax 4" href="%{ajax4}" targets="result" effect="bounce" effectDuration="1000"/>
 * </sj:menu>
 * </sj:menuItem>
 * </sj:menu>
 *
 * <br/>
 * <strong>Result Div :</strong>
 * <div id="result" class="result ui-widget-content ui-corner-all">Click on the AJAX Links above.</div>
 *
 * 
* * * @author Johannes Geppert */ @StrutsTag(name = "menu", tldTagClass = "com.jgeppert.struts2.jquery.views.jsp.ui.MenuTag", description = "Render an Menu.") public class Menu extends AbstractTopicsBean { final private static transient Random RANDOM = new Random(); public static final String JQUERYACTION = "menu"; public static final String TEMPLATE = "menu"; public static final String TEMPLATE_CLOSE = "menu-close"; public static final String COMPONENT_NAME = Menu.class.getName(); protected boolean throwExceptionOnNullValueAttribute = false; protected String disabled; protected String targets; protected String href; protected String paramName; protected Object list; protected String listKey; protected String listValue; public Menu(ValueStack stack, HttpServletRequest request, HttpServletResponse response) { super(stack, request, response); } @Override public String getDefaultOpenTemplate() { return TEMPLATE; } protected String getDefaultTemplate() { return TEMPLATE_CLOSE; } @SuppressWarnings("rawtypes") public void evaluateExtraParams() { super.evaluateExtraParams(); addParameter("jqueryaction", JQUERYACTION); if (disabled != null) addParameter("disabled", findValue(this.disabled, Boolean.class)); if (targets != null) addParameter("targets", findString(targets)); if (href != null) addParameter("href", findString(href)); if (paramName != null) addParameter("paramName", findString(paramName)); Object value = null; if (list == null) { list = parameters.get("list"); } if (list instanceof String) { value = findValue((String) list); } else if (list instanceof Collection) { value = list; } else if (MakeIterator.isIterable(list)) { value = MakeIterator.convert(list); } if (value == null) { if (throwExceptionOnNullValueAttribute) { // will throw an exception if not found value = findValue((list == null) ? (String) list : list.toString(), "list", "The requested list key '" + list + "' could not be resolved as a collection/array/map/enumeration/iterator type. " + "Example: people or people.{name}"); } else { // ww-1010, allows value with null value to be compatible with // ww // 2.1.7 behaviour value = findValue((list == null) ? (String) list : list.toString()); } } if (value instanceof Collection) { addParameter("list", value); } else { addParameter("list", MakeIterator.convert(value)); } if (value instanceof Collection) { addParameter("listSize", Integer.valueOf(((Collection) value).size())); } else if (value instanceof Map) { addParameter("listSize", Integer.valueOf(((Map) value).size())); } else if (value != null && value.getClass().isArray()) { addParameter("listSize", Integer.valueOf(Array.getLength(value))); } if (listKey != null) { listKey = stripExpressionIfAltSyntax(listKey); addParameter("listKey", listKey); } else if (value instanceof Map) { addParameter("listKey", "key"); } if (listValue != null) { listValue = stripExpressionIfAltSyntax(listValue); addParameter("listValue", listValue); } else if (value instanceof Map) { addParameter("listValue", "value"); } if ((this.id == null || this.id.length() == 0)) { // resolves Math.abs(Integer.MIN_VALUE) issue reported by FindBugs // http://findbugs.sourceforge.net/bugDescriptions.html#RV_ABSOLUTE_VALUE_OF_RANDOM_INT int nextInt = RANDOM.nextInt(); nextInt = nextInt == Integer.MIN_VALUE ? Integer.MAX_VALUE : Math.abs(nextInt); this.id = "menu_" + String.valueOf(nextInt); addParameter("id", this.id); } Menu parentMenu = (Menu) findAncestor(Menu.class); if (parentMenu != null) { addParameter("subMenu", true); addParameter("parentMenu", findString(parentMenu.getId())); } } @Override @StrutsTagSkipInheritance public void setTheme(String theme) { super.setTheme(theme); } @Override public String getTheme() { return "jquery"; } @StrutsTagAttribute(description = "Disables the menu if set to true.", defaultValue = "false", type = "Boolean") public void setDisabled(String disabled) { this.disabled = disabled; } @StrutsTagAttribute(name = "targets", description = "A comma separated list of ids of container elements to load with the contents from the result of this request", type = "String", defaultValue = "") public void setTargets(String targets) { this.targets = targets; } @StrutsTagAttribute(description = "Iterable source to populate from. If the list is a Map (key, value), the Map key will become the option 'value' parameter and the Map value will become the option body.") public void setList(Object list) { this.list = list; } @StrutsTagAttribute(description = "Property of list objects to get field value from") public void setListKey(String listKey) { this.listKey = listKey; } @StrutsTagAttribute(description = "Property of list objects to get field content from") public void setListValue(String listValue) { this.listValue = listValue; } @StrutsTagAttribute(description = "The URL to call to obtain the content. Note: If used with ajax context, the value must be set as an url tag value.") public void setHref(String href) { this.href = href; } @StrutsTagAttribute(description = "Parameter name for the href url used when rendered from a collection. e.g. id,name", defaultValue = "id") public void setParamName(String paramName) { this.paramName = paramName; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy