001: /*
002: * Copyright 2004 The Apache Software Foundation.
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: */
016: package org.apache.myfaces.application;
017:
018: import org.apache.myfaces.shared_impl.util.ClassUtils;
019:
020: import javax.faces.component.UIComponent;
021: import javax.faces.component.UIViewRoot;
022: import java.io.Serializable;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: /**
029: * @author Manfred Geiler (latest modification by $Author: dennisbyrne $)
030: * @version $Revision: 511715 $ $Date: 2007-02-26 05:05:36 +0100 (Mo, 26 Feb 2007) $
031: */
032: public class TreeStructureManager {
033: //private static final Log log = LogFactory.getLog(TreeStructureManager.class);
034:
035: //private FacesContext _facesContext;
036:
037: public TreeStructureManager() {
038: //_facesContext = facesContext;
039: }
040:
041: public Object buildTreeStructureToSave(UIViewRoot viewRoot) {
042: return internalBuildTreeStructureToSave(viewRoot);
043: }
044:
045: private TreeStructComponent internalBuildTreeStructureToSave(
046: UIComponent component) {
047: TreeStructComponent structComp = new TreeStructComponent(
048: component.getClass().getName(), component.getId());
049:
050: //children
051: if (component.getChildCount() > 0) {
052: List childList = component.getChildren();
053: List<TreeStructComponent> structChildList = new ArrayList<TreeStructComponent>();
054: for (int i = 0, len = childList.size(); i < len; i++) {
055: UIComponent child = (UIComponent) childList.get(i);
056: if (!child.isTransient()) {
057: TreeStructComponent structChild = internalBuildTreeStructureToSave(child);
058: structChildList.add(structChild);
059: }
060: }
061: TreeStructComponent[] childArray = structChildList
062: .toArray(new TreeStructComponent[structChildList
063: .size()]);
064: structComp.setChildren(childArray);
065: }
066:
067: //facets
068: Map<String, UIComponent> facetMap = component.getFacets();
069: if (!facetMap.isEmpty()) {
070: List<Object[]> structFacetList = new ArrayList<Object[]>();
071: for (Iterator it = facetMap.entrySet().iterator(); it
072: .hasNext();) {
073: Map.Entry entry = (Map.Entry) it.next();
074: UIComponent child = (UIComponent) entry.getValue();
075: if (!child.isTransient()) {
076: String facetName = (String) entry.getKey();
077: TreeStructComponent structChild = internalBuildTreeStructureToSave(child);
078: structFacetList.add(new Object[] { facetName,
079: structChild });
080: }
081: }
082: Object[] facetArray = structFacetList
083: .toArray(new Object[structFacetList.size()]);
084: structComp.setFacets(facetArray);
085: }
086:
087: return structComp;
088: }
089:
090: public UIViewRoot restoreTreeStructure(Object treeStructRoot) {
091: if (treeStructRoot instanceof TreeStructComponent) {
092: return (UIViewRoot) internalRestoreTreeStructure((TreeStructComponent) treeStructRoot);
093: }
094:
095: throw new IllegalArgumentException("TreeStructure of type "
096: + treeStructRoot.getClass().getName()
097: + " is not supported.");
098:
099: }
100:
101: private UIComponent internalRestoreTreeStructure(
102: TreeStructComponent treeStructComp) {
103: String compClass = treeStructComp.getComponentClass();
104: String compId = treeStructComp.getComponentId();
105: UIComponent component = (UIComponent) ClassUtils
106: .newInstance(compClass);
107: component.setId(compId);
108:
109: //children
110: TreeStructComponent[] childArray = treeStructComp.getChildren();
111: if (childArray != null) {
112: List<UIComponent> childList = component.getChildren();
113: for (int i = 0, len = childArray.length; i < len; i++) {
114: UIComponent child = internalRestoreTreeStructure(childArray[i]);
115: childList.add(child);
116: }
117: }
118:
119: //facets
120: Object[] facetArray = treeStructComp.getFacets();
121: if (facetArray != null) {
122: Map<String, UIComponent> facetMap = component.getFacets();
123: for (int i = 0, len = facetArray.length; i < len; i++) {
124: Object[] tuple = (Object[]) facetArray[i];
125: String facetName = (String) tuple[0];
126: TreeStructComponent structChild = (TreeStructComponent) tuple[1];
127: UIComponent child = internalRestoreTreeStructure(structChild);
128: facetMap.put(facetName, child);
129: }
130: }
131:
132: return component;
133: }
134:
135: public static class TreeStructComponent implements Serializable {
136: private static final long serialVersionUID = 5069109074684737231L;
137: private String _componentClass;
138: private String _componentId;
139: private TreeStructComponent[] _children = null; // Array of children
140: private Object[] _facets = null; // Array of Array-tuples with Facetname and TreeStructComponent
141:
142: TreeStructComponent(String componentClass, String componentId) {
143: _componentClass = componentClass;
144: _componentId = componentId;
145: }
146:
147: public String getComponentClass() {
148: return _componentClass;
149: }
150:
151: public String getComponentId() {
152: return _componentId;
153: }
154:
155: void setChildren(TreeStructComponent[] children) {
156: _children = children;
157: }
158:
159: TreeStructComponent[] getChildren() {
160: return _children;
161: }
162:
163: Object[] getFacets() {
164: return _facets;
165: }
166:
167: void setFacets(Object[] facets) {
168: _facets = facets;
169: }
170: }
171:
172: }
|