Author: alexsmirnov
Date: 2007-05-24 20:55:20 -0400 (Thu, 24 May 2007)
New Revision: 220
Modified:
trunk/a4j-portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewState.java
Log:
CODING IN PROGRESS - issue RF-186: Portlet integration for JBoss Portal, portals jsr-168
http://jira.jboss.com/jira/browse/RF-186
Modified:
trunk/a4j-portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewState.java
===================================================================
---
trunk/a4j-portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewState.java 2007-05-25
00:16:48 UTC (rev 219)
+++
trunk/a4j-portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewState.java 2007-05-25
00:55:20 UTC (rev 220)
@@ -10,31 +10,43 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
/**
* @author asmirnov
- *
+ *
*/
public class PortletViewState implements Serializable {
-
+
/**
- *
- */
+ *
+ */
private static final long serialVersionUID = 5630637804542426709L;
- public static final String SAVED_VIEW_ATTRIBUTE = PortletViewState.class.getName();
-
+ public static final String SAVED_VIEW_ATTRIBUTE = PortletViewState.class
+ .getName();
+
private Map messages = new HashMap();
+
private Object treeStructure;
+
private Object componentsState;
+
private UIViewRoot viewRoot;
-
- public void save(FacesContext facesContext){
+
+ public void save(FacesContext facesContext) {
UIViewRoot viewRoot = facesContext.getViewRoot();
- setViewRoot(viewRoot);
+ // setViewRoot(viewRoot);
+ TreeStrutureNode rootNode = new TreeStrutureNode();
+ rootNode.apply(viewRoot);
+ setTreeStructure(rootNode);
+ setComponentsState(viewRoot.processSaveState(facesContext));
Iterator idsWithMessages = facesContext.getClientIdsWithMessages();
while (idsWithMessages.hasNext()) {
String id = (String) idsWithMessages.next();
@@ -44,96 +56,172 @@
addMessage(id, message);
}
}
-// facesContext.getExternalContext().getSessionMap().put(PortletViewState.SAVED_VIEW_ATTRIBUTE,
this);
+ //
facesContext.getExternalContext().getSessionMap().put(PortletViewState.SAVED_VIEW_ATTRIBUTE,
+ // this);
}
-
+
public void restore(FacesContext facesContext) {
- facesContext.setViewRoot(getViewRoot());
- Iterator idsWithMessages = getClientIdsWithMessages();
- while (idsWithMessages.hasNext()) {
- String id = (String) idsWithMessages.next();
- Iterator messages = getMessages(id);
- while (messages.hasNext()) {
- FacesMessage message = (FacesMessage) messages.next();
- facesContext.addMessage(id, message);
- }
+ facesContext.setViewRoot(getViewRoot());
+ Iterator idsWithMessages = getClientIdsWithMessages();
+ while (idsWithMessages.hasNext()) {
+ String id = (String) idsWithMessages.next();
+ Iterator messages = getMessages(id);
+ while (messages.hasNext()) {
+ FacesMessage message = (FacesMessage) messages.next();
+ facesContext.addMessage(id, message);
}
+ }
}
-
+
/**
- * @return the _messages
- */
+ * @return the _messages
+ */
public Map getMessages() {
- return messages;
+ return messages;
}
+
/**
- * @param _messages the _messages to set
- */
+ * @param _messages
+ * the _messages to set
+ */
public void setMessages(Map _messages) {
- this.messages = _messages;
+ this.messages = _messages;
}
+
/**
- * @return the _viewRoot
- */
+ * @return the _viewRoot
+ */
public UIViewRoot getViewRoot() {
- return viewRoot;
+ return viewRoot;
}
+
/**
- * @param root the _viewRoot to set
- */
+ * @param root
+ * the _viewRoot to set
+ */
public void setViewRoot(UIViewRoot root) {
- viewRoot = root;
+ viewRoot = root;
}
public void addMessage(String clientId, FacesMessage message) {
- List list = (List) messages.get(clientId);
- if (list == null) {
- list = new ArrayList();
- messages.put(clientId, list);
- }
- list.add(message);
+ List list = (List) messages.get(clientId);
+ if (list == null) {
+ list = new ArrayList();
+ messages.put(clientId, list);
+ }
+ list.add(message);
}
-
+
public Iterator getClientIdsWithMessages() {
- return (messages.keySet().iterator());
+ return (messages.keySet().iterator());
}
public Iterator getMessages(String clientId) {
- List list = (List) messages.get(clientId);
- if (list != null) {
- return (list.iterator());
- } else {
- return (Collections.EMPTY_LIST.iterator());
- }
+ List list = (List) messages.get(clientId);
+ if (list != null) {
+ return (list.iterator());
+ } else {
+ return (Collections.EMPTY_LIST.iterator());
+ }
}
/**
- * @return the componentsState
- */
+ * @return the componentsState
+ */
public Object getComponentsState() {
- return componentsState;
+ return componentsState;
}
/**
- * @param componentsState the componentsState to set
- */
+ * @param componentsState
+ * the componentsState to set
+ */
public void setComponentsState(Object componentsState) {
- this.componentsState = componentsState;
+ this.componentsState = componentsState;
}
/**
- * @return the treeStructure
- */
+ * @return the treeStructure
+ */
public Object getTreeStructure() {
- return treeStructure;
+ return treeStructure;
}
/**
- * @param treeStructure the treeStructure to set
- */
+ * @param treeStructure
+ * the treeStructure to set
+ */
public void setTreeStructure(Object treeStructure) {
- this.treeStructure = treeStructure;
+ this.treeStructure = treeStructure;
}
+ private static class TreeStrutureNode implements Serializable {
+ /**
+ *
+ */
+ private static final long serialVersionUID = -9038742487716977254L;
+
+ private Map facets = new HashMap();
+
+ private List children = new ArrayList();
+
+ private String type;
+
+ private String id;
+
+ public void apply(UIComponent component) {
+ type = component.getClass().getName();
+ id = component.getId();
+ Map componentFacets = component.getFacets();
+ for (Iterator i = componentFacets.entrySet().iterator(); i
+ .hasNext();) {
+ Entry element = (Entry) i.next();
+ UIComponent f = (UIComponent) element.getValue();
+ if (!f.isTransient()) {
+ TreeStrutureNode facet = new TreeStrutureNode();
+ facet.apply(f);
+ facets.put(element.getKey(), facet);
+
+ }
+ }
+ for (Iterator i = component.getChildren().iterator(); i.hasNext();) {
+ UIComponent child = (UIComponent) i.next();
+ if (!child.isTransient()) {
+ TreeStrutureNode t = new TreeStrutureNode();
+ t.apply(child);
+ children.add(t);
+
+ }
+ }
+ }
+
+ public UIComponent restore() {
+ UIComponent component;
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (loader == null) {
+ loader = this.getClass().getClassLoader();
+ }
+ try {
+ Class componentClass = loader.loadClass(type);
+ component = (UIComponent) componentClass.newInstance();
+ component.setId(id);
+ } catch (Exception e) {
+ throw new FacesException("Error to restore component instance",
+ e);
+ }
+ for (Iterator i = facets.entrySet().iterator(); i.hasNext();) {
+ Entry element = (Entry) i.next();
+ UIComponent facet = ((TreeStrutureNode) element.getValue())
+ .restore();
+ component.getFacets().put(element.getKey(), facet);
+ }
+ for (Iterator i = children.iterator(); i.hasNext();) {
+ TreeStrutureNode node = (TreeStrutureNode) i.next();
+ UIComponent child = node.restore();
+ component.getChildren().add(child);
+ }
+ return component;
+ }
+ }
}