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