Java tutorial
/* * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed 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 net.kamhon.ieagle.struts2.components; import java.util.Random; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.struts2.components.ClosingUIBean; import org.apache.struts2.components.Form; import org.apache.struts2.views.annotations.StrutsTagAttribute; import com.opensymphony.xwork2.util.ValueStack; public abstract class AbstractContainer extends ClosingUIBean implements StrutsContainer { final private static transient Random RANDOM = new Random(); public static final String THEME = "xhtml"; protected String parentTheme; protected String assignJsVar; protected String idConverted; /** * this used when id is not set, plugin will auto set it for you. * * @return */ public abstract String getIdPrefix(); public AbstractContainer(ValueStack stack, HttpServletRequest request, HttpServletResponse response) { super(stack, request, response); } @Override protected void evaluateExtraParams() { super.evaluateExtraParams(); Form form = (Form) findAncestor(Form.class); if (parentTheme != null) { addParameter("parentTheme", findString(parentTheme)); } else if (form != null) { if (form != null) addParameter("parentTheme", form.getTheme()); } else { addParameter("parentTheme", THEME); } if (theme == null || this.theme.length() == 0) { if (getParameters().containsKey("parentTheme")) { theme = (String) getParameters().get("parentTheme"); } } if ((this.id == null || this.id.length() == 0)) { this.id = generateRandomId(getIdPrefix()); addParameter("id", this.id); } convertedId(); addParameter("idConverted", this.idConverted); if (StringUtils.isNotBlank(assignJsVar)) { addParameter("assignJsVar", assignJsVar); } setDynamicAttributes(dynamicAttributes); } private void convertedId() { if ((this.id == null || this.id.length() == 0)) { this.id = generateRandomId(getIdPrefix()); } idConverted = id.replace('.', '_'); } protected String generateRandomId(String prefix) { // 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); return prefix + "_" + String.valueOf(nextInt); } // ---------------- GETTER & SETTER (START) ------------- @StrutsTagAttribute(description = "The parent theme. Default: value of parent form tag or simple if no parent form tag is available") public void setParentTheme(String parentTheme) { this.parentTheme = parentTheme; } @StrutsTagAttribute(description = "Assign it to a JavaScript Object") public void setAssignJsVar(String assignJsVar) { this.assignJsVar = assignJsVar; } public String getAssignJsVar() { return assignJsVar; } public String getIdConverted() { return idConverted; } // ---------------- GETTER & SETTER (END) ------------- }