View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.pss.acid.managed;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import javax.faces.bean.ManagedBean;
24  import javax.faces.bean.RequestScoped;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIOutput;
27  import javax.faces.component.UIPanel;
28  import javax.faces.component.html.HtmlPanelGroup;
29  import javax.faces.context.FacesContext;
30  import javax.faces.view.ViewDeclarationLanguage;
31  
32  /**
33   *
34   * @author Leonardo Uribe
35   */
36  @ManagedBean(name="componentBindingVDLBean5")
37  @RequestScoped
38  public class ComponentBindingVDLBean5
39  {
40      private UIPanel panel;
41      
42      public UIPanel getPanel()
43      {
44          if (panel == null)
45          {
46              panel = new HtmlPanelGroup();
47              FacesContext facesContext = FacesContext.getCurrentInstance();
48              if (facesContext.isPostback())
49              {
50                  // Just try to mess the binding. In theory this does
51                  // not have effect, because the binding with children
52                  // or facets should be restored fully.
53                  UIOutput out2 = new UIOutput();
54                  out2.setValue("hello2");
55                  panel.getChildren().add(out2);
56              }
57              UIOutput out = new UIOutput();
58              out.setValue("hello1");
59              panel.getChildren().add(out);
60              
61              ViewDeclarationLanguage vdl = facesContext.getApplication().
62                  getViewHandler().getViewDeclarationLanguage(
63                      facesContext, facesContext.getViewRoot().getViewId());
64              
65              Map<String, Object> attributes = new HashMap<String, Object>();
66              UIComponent cc = vdl.createComponent(facesContext, 
67                  "http://java.sun.com/jsf/composite/testComposite", 
68                  "dynComp_3", attributes);
69              cc.setId("ccpanel");
70              
71              Map<String, Object> attributes2 = new HashMap<String, Object>();
72              UIComponent text = (UIComponent) vdl.createComponent(facesContext, 
73                  "http://java.sun.com/jsf/composite/testComposite", 
74                  "dynComp_2", attributes2);
75              text.setId("component");
76  
77              cc.getChildren().add(text);
78              panel.getChildren().add(cc);
79  
80              if (!facesContext.isPostback())
81              {
82                  // Store something into the state
83                  panel.getAttributes().put("attr1", "value1");
84                  panel.getChildren().get(0).getAttributes().put("attr2", "value2");
85              }
86              else
87              {
88                  //Try to mess the state, in theory it should not have effect
89                  panel.getAttributes().remove("attr1");
90                  panel.getChildren().get(0).getAttributes().remove("attr2");
91              }
92          }
93          return panel;
94      }
95      
96      public void setPanel(UIPanel panel)
97      {
98          this.panel = panel;
99      }
100 }