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.orchestra.dynaForm.jsf.guiBuilder.impl.jsf;
20  
21  import javax.faces.component.StateHolder;
22  import javax.faces.component.UIComponentBase;
23  import javax.faces.context.FacesContext;
24  import javax.faces.el.EvaluationException;
25  import javax.faces.el.MethodBinding;
26  import javax.faces.el.PropertyNotFoundException;
27  import javax.faces.el.ValueBinding;
28  import javax.faces.model.SelectItem;
29  import java.util.Collection;
30  
31  /**
32   * wraps a datasource and presents it as selectItem list
33   */
34  public class ValueBindingDataSourceAdapter extends ValueBinding implements StateHolder
35  {
36      private MethodBinding mbValues;
37      private MethodBinding mbLabels;
38  
39      private boolean _transient = false;
40  
41      public ValueBindingDataSourceAdapter()
42      {
43      }
44  
45      public ValueBindingDataSourceAdapter(MethodBinding mbValues, MethodBinding mbLabels)
46      {
47          this.mbValues = mbValues;
48          this.mbLabels = mbLabels;
49      }
50  
51  
52      @Override
53      public Class<?> getType(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException
54      {
55          return SelectItem[].class;
56      }
57  
58      @Override
59      public Object getValue(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException
60      {
61          Object valueList = mbValues.invoke(facesContext, new Object[]{null});
62          if (valueList == null)
63          {
64              return null;
65          }
66  
67          if (valueList instanceof Collection)
68          {
69              Collection<?> valueCollection = (Collection<?>) valueList;
70  
71              SelectItem[] selectItems = new SelectItem[valueCollection.size()];
72  
73              int i = -1;
74              for (Object value : valueCollection)
75              {
76                  i++;
77  
78                  String label = (String) mbLabels.invoke(facesContext, new Object[]
79                      {
80                          value
81                      });
82  
83                  SelectItem si = new SelectItem(value, label);
84                  selectItems[i] = si;
85              }
86  
87              return selectItems;
88          }
89  
90          throw new IllegalArgumentException("don't know how to access " + valueList.getClass().getName());
91      }
92  
93      @Override
94      public boolean isReadOnly(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException
95      {
96          return true;
97      }
98  
99      @Override
100     public void setValue(FacesContext facesContext, Object object) throws EvaluationException, PropertyNotFoundException
101     {
102         throw new UnsupportedOperationException();
103     }
104 
105     public Object saveState(FacesContext facesContext)
106     {
107         return new Object[]
108             {
109                 UIComponentBase.saveAttachedState(facesContext, mbValues),
110                 UIComponentBase.saveAttachedState(facesContext, mbLabels),
111             };
112     }
113 
114     public void restoreState(FacesContext facesContext, Object object)
115     {
116         Object[] states = (Object[]) object;
117         mbValues = (MethodBinding) UIComponentBase.restoreAttachedState(facesContext, states[0]);
118         mbLabels = (MethodBinding) UIComponentBase.restoreAttachedState(facesContext, states[1]);
119     }
120 
121     public boolean isTransient()
122     {
123         return _transient;
124     }
125 
126     public void setTransient(boolean flag)
127     {
128         _transient = flag;
129     }
130 }