001: package fr.aliacom.form.common;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.Iterator;
006:
007: import org.apache.log4j.Logger;
008:
009: /**
010: * This class is responsible of iterating an <code>ILoader</code> list and use
011: * them to push the variable from the <code>FormContext</code> to the form UI.
012: *
013: * Given a FormContext A and an IForm B, it is legal to have
014: * <code>B.getFormContext() != A.getForm()</code>.
015: *
016: * You can add multiple <code>ILoader</code> instances for a single form
017: * variable. It is usefull when several form components displaying the same
018: * variable.
019: *
020: * @author tom
021: *
022: * (C) 2001, 2003 Thomas Cataldo
023: */
024: public final class FormLoader {
025:
026: private HashMap loaderMap;
027: private IForm form;
028: private FormContext ctx;
029: private static final Logger log = Logger
030: .getLogger(FormLoader.class);
031:
032: /**
033: * Creates a form loader with the given form reference.
034: * @param f the form loaded by the current loader instance
035: */
036: public FormLoader(IForm f) {
037: this .form = f;
038: if (f != null) {
039: this .ctx = f.getFormContext();
040: }
041: loaderMap = new HashMap();
042: }
043:
044: /**
045: * Creates a form context with a null form reference.
046: */
047: public FormLoader() {
048: this ((IForm) null);
049: }
050:
051: /**
052: * Creates a form loader with a null form reference and the givent context.
053: * @param ctx
054: */
055: public FormLoader(FormContext ctx) {
056: this ((IForm) null);
057: this .ctx = ctx;
058: }
059:
060: public FormContext getCtx() {
061: return ctx;
062: }
063:
064: /**
065: * Add an ILoader used to load a specific FormVariable
066: *
067: * @param formVariable the name of the variable you want to add a loader to.
068: * @param loader the loader used to load the FormVariable
069: */
070: public void addLoader(String formVariable, ILoader loader) {
071: ArrayList al = (ArrayList) loaderMap.get(formVariable);
072: if (al == null) {
073: al = new ArrayList(1);
074: loaderMap.put(formVariable, al);
075: }
076:
077: al.add(loader);
078: }
079:
080: public void load() {
081: Iterator it = getCtx().getVariables();
082: while (it.hasNext()) {
083: FormVariable var = (FormVariable) it.next();
084: log.debug("Loading variable '" + var.getName() + "'");
085: load(var.getName());
086: }
087: }
088:
089: private void load(String formVariable) {
090: directLoad(formVariable, getCtx().get(formVariable).getValue());
091: }
092:
093: private void directLoad(String property, Object value) {
094: ArrayList v = (ArrayList) loaderMap.get(property);
095: if (v != null) {
096: int size = v.size();
097: for (int i = 0; i < size; i++) {
098: ((ILoader) v.get(i)).load(value);
099: }
100: }
101: }
102:
103: /**
104: * Changes the form this loader will load
105: *
106: * @param form the form that will be loaded
107: */
108: public void setForm(IForm form) {
109: this .form = form;
110: }
111:
112: /**
113: * Returns the form loaded by this loader.
114: * @return the form loaded by this loader.
115: */
116: public IForm getForm() {
117: return form;
118: }
119:
120: }
|