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.custom.valueChangeNotifier;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.faces.component.StateHolder;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIData;
27  import javax.faces.context.FacesContext;
28  import javax.faces.event.AbortProcessingException;
29  import javax.faces.event.ValueChangeEvent;
30  import javax.faces.event.ValueChangeListener;
31  
32  /**
33   * receives valueChange events and pass them to the manager
34   * 
35   * @author Mario Ivankovits <imario - at - apache.org> (latest modification by $Author$)
36   * @version $Revision$ $Date$
37   */
38  public class ValueChangeCollector implements ValueChangeListener,
39          StateHolder
40  {
41      private String method = null;
42      private boolean isTransient = false;
43  
44      public ValueChangeCollector()
45      {
46      }
47  
48      protected ValueChangeCollector(String method)
49      {
50          this.method = method;
51      }
52  
53      /**
54       * This it the valueChange sink<br />
55       * The received event will be cloned and collected by the manager.  
56       */
57      public void processValueChange(ValueChangeEvent event)
58              throws AbortProcessingException
59      {
60          UIComponent valueChangeComponent = event.getComponent();
61          List restoreStateCommands = new ArrayList(); 
62          collectStates(restoreStateCommands, valueChangeComponent); 
63          
64          ValueChangeEvent clonedEvent = new ValueChangeEvent(
65              event.getComponent(),
66              event.getOldValue(),
67              event.getNewValue());
68  
69          ValueChangeManager manager = ValueChangeManager.getManager(FacesContext
70                  .getCurrentInstance());
71          manager.addEvent(method, clonedEvent, restoreStateCommands);
72      }
73  
74      protected void collectStates(List restoreStateCommands, UIComponent component)
75      {
76          while (component != null)
77          {
78              if (component instanceof UIData)
79              {
80                  final UIData data = (UIData) component;
81                  final int rowIndex = data.getRowIndex();
82                  
83                  restoreStateCommands.add(new RestoreStateCommand()
84                  {
85                      int currentRowIndex;
86                      
87                      public void saveCurrentState()
88                      {
89                          currentRowIndex = data.getRowIndex();
90                      }
91                      
92                      public void restoreCurrentState()
93                      {
94                          data.setRowIndex(currentRowIndex);
95                      }
96  
97                      public void restoreEventState()
98                      {
99                          data.setRowIndex(rowIndex);
100                     }
101                 });
102             }
103             
104             component = component.getParent(); 
105         }
106     }
107 
108     public Object saveState(FacesContext context)
109     {
110         return new Object[]
111         { this.method};
112     }
113 
114     public void restoreState(FacesContext context, Object state)
115     {
116         Object[] o = (Object[]) state;
117         
118         this.method = (String) o[0];
119     }
120 
121     public boolean isTransient()
122     {
123         return isTransient;
124     }
125 
126     public void setTransient(boolean isTransient)
127     {
128         this.isTransient = isTransient;
129     }
130 }