⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectfield.java

📁 OBPM是一个开源
💻 JAVA
字号:
// Source file:
// C:\\Java\\workspace\\SmartWeb3\\src\\com\\cyberway\\dynaform\\form\\ejb\\SelectField.java

package cn.myapps.core.dynaform.form.ejb;

import java.util.Iterator;

import cn.myapps.base.action.ParamsTable;
import cn.myapps.core.dynaform.PermissionType;
import cn.myapps.core.dynaform.document.ejb.Document;
import cn.myapps.core.dynaform.document.ejb.Item;
import cn.myapps.core.macro.runner.JavaScriptRunner;
import cn.myapps.core.user.action.WebUser;
import cn.myapps.core.validate.repository.ejb.ValidateRepositoryProcess;
import cn.myapps.core.validate.repository.ejb.ValidateRepositoryVO;
import cn.myapps.util.HtmlEncoder;
import cn.myapps.util.ProcessFactory;
import cn.myapps.util.StringUtil;

public class SelectField extends FormField {

	protected static String cssClass = "select-cmd";

	/**
	 * 计算多值选项
	 */
	protected String optionsScript;

	protected String validateLibs = "";

	public String getValidateLibs() {
		return validateLibs;
	}

	public void setValidateLibs(String validateLibs) {
		this.validateLibs = validateLibs;
	}

	/**
	 * @roseuid 41ECB66D031D
	 */
	public SelectField() {

	}

	/**
	 * @return Returns the optionsScript.
	 */
	public String getOptionsScript() {
		return optionsScript;
	}

	/**
	 * @param optionsScript
	 *            The optionsScript to set.
	 */
	public void setOptionsScript(String optionsScript) {
		this.optionsScript = optionsScript;
	}

	/**
	 * @return boolean
	 * @roseuid 41ECB66D0331
	 */
	public ValidateMessage validate(JavaScriptRunner runner, Document doc)
			throws Exception {
		Object result = null;
		String rtn = "";
		ValidateMessage msg = new ValidateMessage();
		if (getValidateLibs() != null && getValidateLibs().trim().length() > 0) {
			String libs[] = getValidateLibs().split(";");
			ValidateRepositoryProcess vp = (ValidateRepositoryProcess) ProcessFactory
					.createProcess(ValidateRepositoryProcess.class);
			for (int i = 0; i < libs.length; i++) {
				ValidateRepositoryVO vo = (ValidateRepositoryVO) vp
						.doView(libs[i]);
				String cotent = "";
				String functionName = getFunctionNames(vo.getContent());
				if (functionName != null) {
					cotent += functionName + "('" + getName() + "');";
					cotent += vo.getContent();
					Object obj = runner.run(cotent);
					if (obj instanceof String) {
						rtn += (String) obj;
					}
				}
			}
		}

		if (getValidateRule() != null && getValidateRule().trim().length() > 0) {
			result = runner.run(StringUtil.dencodeHTML(getValidateRule()));
			System.out.print(getValidateLibs());
			// Item item = doc.findItem(this.getName());
			if (result instanceof String) {
				String rs = (String) result;
				if (rs != null && rs.trim().length() > 0) {
					rtn += rs;

				}
			}
		}
		if (rtn != null && rtn.trim().length() > 0) {
			msg.setFieldname(this.getName());
			msg.setErrmessage(rtn);
			return msg;
		} else
			return null;
	}

	public String getFunctionNames(String content) {

		if (content == null || content.trim().length() == 0)
			return null;
		int i = content.indexOf("function");
		int j = content.substring(i + 8, content.length()).indexOf("(");
		String functionname = content.substring(i + 8, i + 8 + j);
		if (functionname != null && functionname.trim().length() > 0)
			return functionname;
		else
			return null;
	}

	/**
	 * 重新计算
	 * 
	 * @roseuid 41DB89D700F9
	 */
	public void recalculate(JavaScriptRunner runner, Document doc)
			throws Exception {
		// Debug.println("SelectField.recalculate");
		runValueScript(runner, doc);
		runOptionsScript(runner, doc);
	}

	/*
	 * 运行计算Field值Script,返回值即为计算结果。
	 * 
	 * @return java.lang.String @roseuid 41DB8C1E03E7
	 */
	public Object runValueScript(JavaScriptRunner runner, Document doc)
			throws Exception {
		Object result = null;

		if (getValueScript() != null && getValueScript().trim().length() > 0) {
			// Debug.println("JavaScript->"
			// + StringUtil.dencodeHTML(getValueScript()));
			result = runner.run(StringUtil.dencodeHTML(getValueScript()));
			Item item = doc.findItem(this.getName());
			item.setValue(result);
		}
		return result;
	}

	/**
	 * @param doc
	 * @return java.lang.String
	 * @throws Exception
	 * @roseuid 41ECB66D034F
	 */
	public String toHtmlTxt(ParamsTable params, WebUser user,
			JavaScriptRunner runner, Document doc) throws Exception {
		StringBuffer html = new StringBuffer();
		Item item = null;
		int displayType = getDisplayType(runner, doc);

		if (displayType == PermissionType.HIDDEN) {
			return "(hidden)";
		} else {
			if (doc != null) {
				item = doc.findItem(this.getName());

				html.append("<select");
				html.append(" id='" + getId() + "'");
				html.append(" name='" + getName() + "'");
				html.append(" " + toOtherpropsHtml());
				if (displayType == PermissionType.READONLY) {
					html.append(" disabled ");
				} else if (displayType == PermissionType.DISABLED) {
					html.append(" disabled ");
				}
				if (isRefreshOnChanged()) {
					html.append(" onchange='dy_refresh(this.name)'");
				}
				html.append(">");
				try {
					html.append(runOptionsScript(runner, doc));
					html.append("</select>");
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}
		return html.toString();
	}

	/**
	 * @param tmpltStr
	 * @return FormField
	 * @roseuid 41ECB66D0381
	 */
	public FormField init(String tmpltStr) {
		return null;
	}

	public String toTemplate() {
		StringBuffer template = new StringBuffer();
		template.append("<select'");
		template.append(" className='" + this.getClass().getName() + "'");
		template.append(" id='" + getId() + "'");
		template.append(" name='" + getName() + "'");
		template.append(" formid='" + getFormid() + "'");
		template.append(" discript='" + getDiscript() + "'");
		template.append(" hiddenScript='" + getHiddenScript() + "'");
		template.append(" hiddenPrintScript='" + getHiddenPrintScript() + "'");
		template.append(" refreshOnChanged='" + isRefreshOnChanged() + "'");
		template.append(" validateRule='" + getValidateRule() + "'");
		template.append(" valueScript='" + getValueScript() + "'");
		template.append(" optionScript='" + getOptionsScript() + "'");
		template.append("/>");
		return template.toString();

	}

	public String runOptionsScript(JavaScriptRunner runner, Document doc)
			throws Exception {
		Object result = null;

		if (getOptionsScript() != null
				&& getOptionsScript().trim().length() > 0) {
			// Debug.println("JavaScript->"
			// + StringUtil.dencodeHTML(getOptionsScript()));
			result = runner.run(StringUtil.dencodeHTML(getOptionsScript()));

			Options options = null;
			if (result != null && result instanceof String) {

				String[] strlst = ((String) result).split(";");
				options = new Options();
				for (int i = 0; i < strlst.length; i++) {
					options.add(strlst[i], strlst[i]);
				}
			} else if (result instanceof Options) {
				options = (Options) result;
			}

			if (options != null) {
				StringBuffer html = new StringBuffer();

				Object value = null;
				if (doc != null) {
					Item item = doc.findItem(this.getName());
					if (item != null)
						value = item.getValue();
				}

				Iterator iter = options.getOptions().iterator();
				while (iter.hasNext()) {
					Option element = (Option) iter.next();
					if (element.getValue() != null) {
						html.append("<option value=");
						html.append("\"");
						html.append(HtmlEncoder.encode(element.getValue()));
						html.append("\"");

						if (value != null && element.getValue() != null) {
							if (value.equals(element.getValue())) {
								html.append(" selected ");
							}
						} else {
							if (element.isDef()) {
								html.append(" selected ");
							}
						}
						html.append(" class='" + cssClass + "'");
						html.append(">");
						html.append(element.getOption());
						html.append("</option>");
					}
				}
				return html.toString();
			}
		}
		return "";
	}

	public String toPrintHtmlTxt(ParamsTable params, WebUser user,
			JavaScriptRunner runner, Document doc) throws Exception {
		Object result = null;

		if (doc != null
				&& !(getPrintDisplayType(runner, doc) == PermissionType.HIDDEN)) {

			if (getOptionsScript() != null
					&& getOptionsScript().trim().length() > 0) {
				result = runner.run(StringUtil.dencodeHTML(getOptionsScript()));

				Options options = null;
				if (result != null && result instanceof String) {

					String[] strlst = ((String) result).split(";");
					options = new Options();
					for (int i = 0; i < strlst.length; i++) {
						options.add(strlst[i], strlst[i]);
					}
				} else if (result instanceof Options) {
					options = (Options) result;
				}

				if (options != null) {
					StringBuffer html = new StringBuffer();

					Object value = null;
					if (doc != null) {
						Item item = doc.findItem(this.getName());
						if (item != null)
							value = item.getValue();
					}

					Iterator iter = options.getOptions().iterator();
					while (iter.hasNext()) {
						Option element = (Option) iter.next();
						if (value != null && element.getValue() != null) {
							if (value.equals(element.getValue())) {
								html.append("<SPAN style=\"FONT-SIZE: 9pt\">");
								html.append(element.getOption());
								html.append("</SPAN>");
							}
						}
					}
					return html.toString();
				}
			}
		}
		return "";
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -