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.component;
20  
21  import java.util.Iterator;
22  
23  import javax.faces.component.FacesComponent;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIComponentBase;
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.component.html.HtmlInputText;
28  import javax.faces.component.html.HtmlPanelGroup;
29  import javax.faces.context.FacesContext;
30  import javax.faces.event.AbortProcessingException;
31  import javax.faces.event.PreRenderViewEvent;
32  import javax.faces.event.SystemEvent;
33  import javax.faces.event.SystemEventListener;
34  
35  @FacesComponent(value = "com.myapp.UIInputComponent")
36  public class UIInputComponent extends UIComponentBase implements
37          SystemEventListener
38  {
39  
40      //
41      // Constructor
42      //
43  
44      public UIInputComponent()
45      {
46  
47          setRendererType("testcomponent");
48  
49          FacesContext context = FacesContext.getCurrentInstance();
50          UIViewRoot root = context.getViewRoot();
51  
52          root.subscribeToViewEvent(PreRenderViewEvent.class, this);
53      }
54  
55      //
56      // Public methods
57      //
58  
59      @Override
60      public String getFamily()
61      {
62  
63          return "com.myapp";
64      }
65  
66      public boolean isListenerForSource(Object source)
67      {
68  
69          return (source instanceof UIViewRoot);
70      }
71  
72      public void processEvent(SystemEvent event) throws AbortProcessingException
73      {
74  
75          FacesContext context = FacesContext.getCurrentInstance();
76  
77          if (!context.isPostback())
78          {
79  
80              UIComponent originalComponent = getChildren().remove(0);
81  
82              HtmlPanelGroup panelGroup = new HtmlPanelGroup();
83              panelGroup
84                      .setStyle("border: 1px dashed blue; padding: 5px; margin: 5px");
85              getChildren().add(panelGroup);
86  
87              // Move original HtmlInputText inside new HtmlPanelGroup
88  
89              panelGroup.getChildren().add(originalComponent);
90          }
91          else
92          {
93              // If the algorithm do a refresh, the inputText is added again, but since it is inside
94              // the HtmlPanelGroup, it is not affected by the c:if add/delete algorithm. We need to
95              // remove that duplicate.
96              for (Iterator<UIComponent> it = getChildren().iterator(); it
97                      .hasNext();)
98              {
99                  if (it.next() instanceof HtmlInputText)
100                 {
101                     it.remove();
102                 }
103             }
104         }
105     }
106 }