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  
20  package org.apache.myfaces.cdi.behavior;
21  
22  import java.util.Set;
23  import javax.faces.FacesWrapper;
24  import javax.faces.component.PartialStateHolder;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.behavior.ClientBehavior;
27  import javax.faces.component.behavior.ClientBehaviorContext;
28  import javax.faces.component.behavior.ClientBehaviorHint;
29  import javax.faces.context.FacesContext;
30  import javax.faces.event.BehaviorEvent;
31  import org.apache.myfaces.cdi.util.CDIUtils;
32  
33  /**
34   *
35   */
36  public class FacesClientBehaviorCDIWrapper implements PartialStateHolder, ClientBehavior, FacesWrapper<ClientBehavior>
37  {
38      private transient ClientBehavior delegate;
39      
40      private String behaviorId;
41      private boolean _transient;
42  
43      public FacesClientBehaviorCDIWrapper()
44      {
45      }
46  
47      public FacesClientBehaviorCDIWrapper(Class<? extends ClientBehavior> behaviorClass, String behaviorId)
48      {
49          this.behaviorId = behaviorId;
50      }
51      
52      @Override
53      public void broadcast(BehaviorEvent event)
54      {
55          getWrapped().broadcast(event);
56      }
57      
58      @Override
59      public void decode(FacesContext context, UIComponent component)
60      {
61          getWrapped().decode(context, component);
62      }
63  
64      @Override
65      public Set<ClientBehaviorHint> getHints()
66      {
67          return getWrapped().getHints();
68      }
69  
70      @Override
71      public String getScript(ClientBehaviorContext behaviorContext)
72      {
73          return getWrapped().getScript(behaviorContext);
74      }
75  
76      @Override
77      public ClientBehavior getWrapped()
78      {
79          if (delegate == null)
80          {
81              delegate = (ClientBehavior) CDIUtils.getInstance(CDIUtils.getBeanManager(
82                  FacesContext.getCurrentInstance().getExternalContext()), 
83                      ClientBehavior.class, true, new FacesBehaviorAnnotationLiteral(behaviorId, true));
84          }
85          return delegate;
86      }
87      
88      @Override
89      public Object saveState(FacesContext context)
90      {
91          if (!initialStateMarked())
92          {
93              Object values[] = new Object[1];
94              values[0] = behaviorId;
95              return values;
96          }
97          return null;
98      }
99  
100     @Override
101     public void restoreState(FacesContext context, Object state)
102     {
103         if (state != null)
104         {
105             Object values[] = (Object[])state;
106             behaviorId = (String)values[0];
107         }
108     }
109 
110     @Override
111     public boolean isTransient()
112     {
113         return _transient;
114     }
115 
116     @Override
117     public void setTransient(boolean newTransientValue)
118     {
119         _transient = newTransientValue;
120     }
121 
122     private boolean _initialStateMarked = false;
123 
124     public void clearInitialState()
125     {
126         _initialStateMarked = false;
127     }
128 
129     public boolean initialStateMarked()
130     {
131         return _initialStateMarked;
132     }
133 
134     public void markInitialState()
135     {
136         _initialStateMarked = true;
137     }
138 
139 }