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;
20  
21  import org.apache.myfaces.orchestra.dynaForm.jsf.component.DynaForm;
22  import org.apache.myfaces.orchestra.dynaForm.metadata.MetaData;
23  import org.apache.myfaces.orchestra.dynaForm.metadata.MetaField;
24  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
25  
26  import java.util.Iterator;
27  import java.util.Map;
28  
29  import javax.faces.context.FacesContext;
30  import javax.faces.el.ValueBinding;
31  
32  /**
33   * The slipstream will bring the configuration-metaData/gui together
34   */
35  public class Slipstream
36  {
37      private MetaData modelMetaData;
38      private GuiBuilder guiBuilder;
39      private Map<String, String> labelBundle;
40      private DynaForm dynaForm;
41  
42      public Slipstream()
43      {
44      }
45  
46      public MetaData getModelMetaData()
47      {
48          return modelMetaData;
49      }
50  
51      public void setModelMetaData(MetaData modelMetaData)
52      {
53          this.modelMetaData = modelMetaData;
54      }
55  
56      public GuiBuilder getGuiBuilder()
57      {
58          return guiBuilder;
59      }
60  
61      public void setGuiBuilder(GuiBuilder guiBuilder)
62      {
63          this.guiBuilder = guiBuilder;
64      }
65  
66      public Map<String, String> getLabelBundle()
67      {
68          return labelBundle;
69      }
70  
71      public void setLabelBundle(Map<String, String> labelBundle)
72      {
73          this.labelBundle = labelBundle;
74      }
75  
76      public DynaForm getDynaForm()
77      {
78          return dynaForm;
79      }
80  
81      public void setDynaForm(DynaForm dynaForm)
82      {
83          this.dynaForm = dynaForm;
84      }
85  
86      public void process()
87      {
88          configureGuiBuilder();
89  
90          MetaData metaData = modelMetaData;
91          Iterator<String> iterFieldNames = metaData.iterFieldNames();
92          while (iterFieldNames.hasNext())
93          {
94              String fieldName = iterFieldNames.next();
95              MetaField field = modelMetaData.getField(fieldName);
96              Object ch = field.getComponentHandler();
97              if (ch == null)
98              {
99                  // No component-handler; let the guiBuilder decide what JSF component to create
100                 // for this field.
101                 guiBuilder.buildField(field);
102             }
103             else
104             {
105                 getComponentHandler(ch).buildComponent(dynaForm, guiBuilder, field);
106             }
107         }
108     }
109 
110     protected DynaFormComponentHandler getComponentHandler(Object ch)
111     {
112         if (ch instanceof ValueBinding)
113         {
114             FacesContext facesContext = FacesContext.getCurrentInstance();
115             ValueBinding vb = (ValueBinding) ch;
116             ch = vb.getValue(facesContext);
117             
118             if (ch == null)
119             {
120                 throw new IllegalArgumentException(
121                         "ComponentHandler expression '" + vb.getExpressionString() + "' returned null.");
122             }
123         }
124 
125         if (ch instanceof DynaFormComponentHandler)
126         {
127             return (DynaFormComponentHandler) ch;
128         }
129 
130         if (ch instanceof String)
131         {
132             String chName = (String) ch;
133             Object bean = FrameworkAdapter.getCurrentInstance().getBean(chName);
134             if (bean == null)
135             {
136                 throw new IllegalArgumentException(
137                     "No component handler with bean name [" + chName + "] found.");
138             }
139             if (!(bean instanceof DynaFormComponentHandler))
140             {
141                 throw new IllegalArgumentException(
142                     "Managed bean with name [" + chName + 
143                     "] doesn't implement interface " + DynaFormComponentHandler.class.getName());
144             }
145             
146             return (DynaFormComponentHandler) bean;
147         }
148 
149         throw new IllegalArgumentException(
150                 "Invalid ComponentHandler of type '" +
151                 ch.getClass() +
152                 "' found; String or DynaFormComponentHandler expected.");
153     }
154 
155     protected void configureGuiBuilder()
156     {
157         guiBuilder.setFormDisplayOnly(dynaForm.isDisplayOnly());
158         guiBuilder.setLabelBundle(getLabelBundle());
159         guiBuilder.setIdAsDisplayOnly(dynaForm.isIdAsDisplayOnly());
160     }
161 }