Java tutorial
/** * PureInfo Dolphin * @(#)ShowCheckboxGroupFunctionHandler 1.0 2005-7-19 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.auth; import org.apache.commons.lang.ArrayUtils; import org.apache.log4j.Logger; import com.pureinfo.dolphin.script.execute.IContext; import com.pureinfo.dolphin.script.function.handler.FunctionHandlerUtil; import com.pureinfo.dolphin.script.function.handler.IFunctionHandler; import com.pureinfo.force.exception.PureException; /** * <P> * Created on 2005-9-8 8:45:33 <BR> * Last modified on 2005-9-8 * </P> * ShowCheckboxGroupFunctionHandler: handler for showCheckboxGroup() function. * <BR> * <BR> * <B>showCheckboxGroup(String _sName, Object _oValue, String _sMap) </B> <BR> * Sample: * <OL> * <li>showCheckboxGroup("type", "4", "4:$2:$1:")</li> * <li>showCheckboxGroup("type", #getProperty("type"), "4:$2:$1:") * </li> * </OL> * * @param _sName * the checkbox name in HTML * @param _oValue * current value of the checkbox group * @param _sMap * value-name mappings in format like * "value1:name1$value2:name2$...". * @return the checkbox group HTML <BR> * <BR> * @author elmar.chen * @version 1.0, 2005-7-19 * @since Dolphin 1.0 */ public class ShowCheckboxGroupFunctionHandler implements IFunctionHandler { private final static Logger logger = Logger.getLogger(ShowCheckboxGroupFunctionHandler.class.getName()); /** minimum number of arguments */ public final static int MIN_ARGS_NUM = 3; /** argument index of name */ public final static int ARG_NAME = 0; /** argument index of value */ public final static int ARG_VALUE = 1; /** argument index of map */ public final static int ARG_MAP = 2; private static final String defaultReturnValue = "<INPUT type='checkbox' value='1'>"; /** * @see com.pureinfo.dolphin.script.function.handler.IFunctionHandler#perform(java.lang.Object[], * com.pureinfo.dolphin.script.execute.IContext) */ public Object perform(Object[] _args, IContext _context) throws PureException { //to validate arguments FunctionHandlerUtil.validateArgsNum(_args, MIN_ARGS_NUM); //to read arguments String sName = (String) _args[ARG_NAME]; Object value = _args[ARG_VALUE]; String sMap = (String) _args[ARG_MAP]; //to render checkbox group String[] arrayMaps = sMap.split("\\$"); if (arrayMaps == null || arrayMaps.length < 1) { return defaultReturnValue; } if (value instanceof Integer) { return doPerform(sName, ((Integer) value).intValue(), arrayMaps); } if (value instanceof Boolean) { return doPerform(sName, ((Boolean) value).booleanValue(), arrayMaps); } //else return doPerform(sName, value == null ? null : value.toString(), arrayMaps); } private String doPerform(String _sName, String _sValue, String[] _arrayMaps) { String[] values = _sValue == null ? null : _sValue.split("\\$"); StringBuffer sb = new StringBuffer(); for (int i = 0; i < _arrayMaps.length; i++) { String map[] = _arrayMaps[i].split(":"); if (map == null || map.length < 1) { continue; } String sBoxValue = map[0]; String sBoxShow = map.length > 1 ? map[1] : " "; sb.append("<INPUT type=\"checkbox\" name=\"").append(_sName).append("\" value=\"").append(sBoxValue) .append("\""); if (ArrayUtils.contains(values, sBoxValue)) { sb.append(" checked"); } sb.append('>').append(sBoxShow); } String sResult = new String(sb); sb.setLength(0); return sResult; } private String doPerform(String _sName, boolean _bValue, String[] _arrayMaps) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < _arrayMaps.length; i++) { String map[] = _arrayMaps[i].split(":"); if (map == null || map.length < 1) { continue; } String sBoxValue = map[0]; String sBoxShow = map.length > 1 ? map[1] : " "; sb.append("<INPUT type=\"checkbox\" value=\"").append(sBoxValue).append('\"'); if ((sBoxValue.equals("1") || sBoxValue.equals("true")) && _bValue) { sb.append(" checked"); } sb.append(" onClick=\"document.getElementById('").append(_sName) .append("').value=this.checked?'1':'0';\""); //"onClick=document.getElementById("$_sName").value=this.checked?1:0" sb.append('>').append(sBoxShow); } String sResult = new String(sb); sb.setLength(0); return sResult; } private String doPerform(String _sName, int _nValue, String[] _arrayMaps) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < _arrayMaps.length; i++) { String map[] = _arrayMaps[i].split(":"); if (map == null || map.length < 1) { continue; } String sBoxValue = map[0]; boolean checked = false; String sBoxShow = map.length > 1 ? map[1] : " "; sb.append("<INPUT type=\"checkbox\" name=\"").append(_sName).append("\" ").append("value=\"") .append(sBoxValue).append('\"'); if (sBoxValue != null && sBoxValue.matches("[0-9]+")) { logger.debug("====" + sBoxValue + ":" + _nValue); if ((Integer.parseInt(sBoxValue) & _nValue) != 0) { sb.append(" checked"); } } if (checked) { sb.append(" checked"); } sb.append('>').append(sBoxShow); } String sResult = new String(sb); sb.setLength(0); return sResult; } }