001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.form.visitor;
016:
017: import java.io.Serializable;
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021: import org.araneaframework.uilib.form.FormElement;
022: import org.araneaframework.uilib.form.FormWidget;
023: import org.araneaframework.uilib.form.GenericFormElement;
024: import org.araneaframework.uilib.form.formlist.BaseFormListWidget;
025: import org.araneaframework.uilib.util.NameUtil;
026:
027: /**
028: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
029: */
030: public abstract class FormElementVisitor implements Serializable {
031:
032: //*********************************************************************
033: //* CONTEXT STACK
034: //*********************************************************************
035:
036: protected List contextStack = new ArrayList();
037:
038: /**
039: * Pushes the context element to stack.
040: */
041: public void pushContext(String id, GenericFormElement element) {
042: contextStack.add(new IdElementPair(id, element));
043: }
044:
045: /**
046: * Pops the context element from the stack.
047: */
048: public void popContext() {
049: contextStack.remove(contextStack.size() - 1);
050: }
051:
052: /**
053: * Represents a context element paired with its id.
054: *
055: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
056: */
057: private static class IdElementPair implements Serializable {
058: private String id;
059: private GenericFormElement element;
060:
061: public IdElementPair(String id, GenericFormElement element) {
062: this .id = id;
063: this .element = element;
064: }
065:
066: public GenericFormElement getElement() {
067: return element;
068: }
069:
070: public String getId() {
071: return id;
072: }
073: }
074:
075: //*********************************************************************
076: //* CONTEXT METHODS
077: //*********************************************************************
078:
079: /**
080: * Returns the current context element that should be the parent of the element being visited.
081: */
082: public GenericFormElement getParent() {
083: return ((IdElementPair) contextStack
084: .get(contextStack.size() - 1)).getElement();
085: }
086:
087: /**
088: * Returns the identifier prefix to the element being visited in relation to the root
089: * visisted element.
090: */
091: public String getPrefix() {
092: String result = "";
093:
094: for (Iterator i = contextStack.iterator(); i.hasNext();) {
095: IdElementPair pair = (IdElementPair) i.next();
096: result = NameUtil.getFullName(result, pair.getId());
097: }
098:
099: return result;
100: }
101:
102: /**
103: * Returns the depth that visited element is on (number of parents).
104: */
105: public int getDepth() {
106: return contextStack.size();
107: }
108:
109: //*********************************************************************
110: //* VISITING METHODS
111: //*********************************************************************
112:
113: /**
114: * Visits all elements and subelements of the given composite element.
115: */
116: public void visitAll(GenericFormElement form) {
117: form.accept("", this );
118: }
119:
120: //*********************************************************************
121: //* ABSTRACT METHODS
122: //*********************************************************************
123:
124: /**
125: * Visits a simple element.
126: */
127: public abstract void visit(String id, FormElement element);
128:
129: /**
130: * Visits a composite element.
131: */
132: public abstract void visit(String id, FormWidget element);
133:
134: /**
135: * Visits a composite element.
136: */
137: public abstract void visit(String id, BaseFormListWidget element);
138: }
|