001: /* VariablesInfo.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Feb 28 19:19:49 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zk.ui.metainfo;
020:
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.HashMap;
024:
025: import org.zkoss.zk.ui.Page;
026: import org.zkoss.zk.ui.Component;
027: import org.zkoss.zk.ui.UiException;
028: import org.zkoss.zk.ui.util.Condition;
029: import org.zkoss.zk.ui.util.ConditionImpl;
030: import org.zkoss.zk.xel.ExValue;
031: import org.zkoss.zk.xel.Evaluator;
032: import org.zkoss.zk.xel.impl.EvaluatorRef;
033:
034: /**
035: * The information about the variables element in the ZUML page.
036: *
037: * @author tomyeh
038: */
039: public class VariablesInfo extends EvalRefStub implements Condition,
040: java.io.Serializable {
041: /** Map(String name, ExValue value). */
042: private final Map _vars;
043: private final ConditionImpl _cond;
044: private final boolean _local;
045:
046: /**
047: * @param evalr the evaluator reference. It cannot be null.
048: * Retrieve it from {@link LanguageDefinition#getEvaluatorRef}
049: * or {@link PageDefinition#getEvaluatorRef}, depending which it
050: * belongs.
051: * @param vars a map of (String name, String value).
052: * Note: once called, the caller cannot access it any more.
053: * In other words, it becomes part of this object.
054: */
055: public VariablesInfo(EvaluatorRef evalr, Map vars, boolean local,
056: ConditionImpl cond) {
057: if (evalr == null)
058: throw new IllegalArgumentException();
059: _evalr = evalr;
060: _vars = vars;
061: if (_vars != null) {
062: for (Iterator it = _vars.entrySet().iterator(); it
063: .hasNext();) {
064: final Map.Entry me = (Map.Entry) it.next();
065: me.setValue(new ExValue((String) me.getValue(),
066: Object.class));
067: }
068: }
069:
070: _local = local;
071: _cond = cond;
072: }
073:
074: /** Applies the variable element against the parent component.
075: *
076: * @param comp the parent component (it cannot be null)
077: */
078: public void apply(Component comp) {
079: if (_vars != null && isEffective(comp)) {
080: final Evaluator eval = _evalr.getEvaluator();
081: for (Iterator it = _vars.entrySet().iterator(); it
082: .hasNext();) {
083: final Map.Entry me = (Map.Entry) it.next();
084: final String name = (String) me.getKey();
085: final ExValue value = (ExValue) me.getValue();
086: comp.setVariable(name, value.getValue(eval, comp),
087: _local);
088: }
089: }
090: }
091:
092: /** Applies the variable element against the page.
093: * It is called if the element doesn't belong to any component.
094: */
095: public void apply(Page page) {
096: if (_vars != null && isEffective(page)) {
097: final Evaluator eval = _evalr.getEvaluator();
098: for (Iterator it = _vars.entrySet().iterator(); it
099: .hasNext();) {
100: final Map.Entry me = (Map.Entry) it.next();
101: final String name = (String) me.getKey();
102: final ExValue value = (ExValue) me.getValue();
103: page.setVariable(name, value.getValue(eval, page));
104: }
105: }
106: }
107:
108: //Condition//
109: public boolean isEffective(Component comp) {
110: return _cond == null || _cond.isEffective(_evalr, comp);
111: }
112:
113: public boolean isEffective(Page page) {
114: return _cond == null || _cond.isEffective(_evalr, page);
115: }
116:
117: //Object//
118: public String toString() {
119: final StringBuffer sb = new StringBuffer(40)
120: .append("[variables:");
121: if (_vars != null)
122: for (Iterator it = _vars.keySet().iterator(); it.hasNext();)
123: sb.append(' ').append(it.next());
124: return sb.append(']').toString();
125: }
126: }
|