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.myfaces;
20  
21  import org.apache.myfaces.orchestra.dynaForm.jsf.guiBuilder.impl.jsf.JsfGuiBuilder;
22  import org.apache.myfaces.orchestra.dynaForm.metadata.MetaField;
23  import org.apache.myfaces.orchestra.dynaForm.lib.ObjectSerializationConverter;
24  import org.apache.myfaces.commons.converter.TypedNumberConverter;
25  import org.apache.myfaces.custom.calendar.HtmlInputCalendar;
26  import org.apache.myfaces.custom.suggestajax.inputsuggestajax.InputSuggestAjax;
27  
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.html.HtmlPanelGroup;
30  import javax.faces.component.html.HtmlInputText;
31  import javax.faces.convert.Converter;
32  import javax.faces.context.FacesContext;
33  import javax.faces.el.MethodBinding;
34  import javax.persistence.TemporalType;
35  import java.math.BigDecimal;
36  
37  /**
38   * use myfaces goodies
39   */
40  public class MyFacesGuiBuilder extends JsfGuiBuilder
41  {
42      public MyFacesGuiBuilder()
43      {
44          super();
45      }
46  
47      /* TODO: SANDBOX COMPONENT - promote to tomahawk */
48      @Override
49      public Converter doCreateConverter(MetaField field)
50      {
51          Class type = field.getType();
52  
53          if (Float.class.isAssignableFrom(type)
54              || Double.class.isAssignableFrom(type)
55              || float.class.isAssignableFrom(type)
56              || double.class.isAssignableFrom(type)
57              || BigDecimal.class.isAssignableFrom(type))
58          {
59              // use the super duper intelligent number converter
60              TypedNumberConverter converter = (TypedNumberConverter) getContext().getApplication()
61                  .createConverter(TypedNumberConverter.CONVERTER_ID);
62              converter.setDestType(type);
63              return converter;
64          }
65  
66          return super.doCreateConverter(field);
67      }
68  
69      @Override
70      public HtmlInputText doCreateInputDateComponent(MetaField field)
71      {
72          if (!TemporalType.DATE.equals(field.getTemporalType()))
73          {
74              // we can select dates only - yet!
75              return super.doCreateInputDateComponent(field);
76          }
77  
78          HtmlInputCalendar cmp = (HtmlInputCalendar) getContext()
79                  .getApplication().createComponent(
80                          HtmlInputCalendar.COMPONENT_TYPE);
81          cmp.setRenderAsPopup(true);
82  
83          if (Boolean.FALSE.equals(field.getCanWrite()))
84          {
85              cmp.setReadonly(true);
86          }
87          if (Boolean.TRUE.equals(field.getDisabled()))
88          {
89              cmp.setDisabled(true);
90          }
91  
92          return cmp;
93      }
94  
95      @SuppressWarnings("unchecked")
96      @Override
97      public void createInputDate(MetaField field)
98      {
99          if (isFieldDisplayOnly(field))
100         {
101             super.createInputDate(field);
102             return;
103         }
104 
105         UIComponent cmp = doCreateInputDate(field);
106 
107         // workaround to avoid wrap of date-component and its button ==>
108         HtmlPanelGroup panel = doCreatePanelGroupComponent();
109         panel.setId("pnl_" + cmp.getId());
110         panel.getChildren().add(cmp);
111         panel.setStyle("white-space:nowrap;");
112         // <==
113 
114         fireNewComponent(field, panel);
115     }
116 
117     /* TODO: SANDBOX COMPONENT - promote to tomahawk */
118     @Override
119     public UIComponent doCreateSearchFor(MetaField field)
120     {
121         InputSuggestAjax cmp = (InputSuggestAjax) getContext()
122             .getApplication().createComponent(InputSuggestAjax.COMPONENT_TYPE);
123         initInputDefaults(cmp, field);
124 
125         HtmlPanelGroup panel = doCreatePanelGroupComponent();
126         panel.setId("pnl_" + cmp.getId());
127 
128         String dataSource = field.getDataSource();
129         String dataSourceDescription = field.getDataSourceDescription();
130 
131         MethodBinding mbSearch = FacesContext.getCurrentInstance().getApplication().createMethodBinding(
132             dataSource,
133             new Class[]
134                 {
135                     String.class
136                 });
137         MethodBinding mbLabel = FacesContext.getCurrentInstance().getApplication().createMethodBinding(
138             dataSourceDescription,
139             new Class[]
140                 {
141                     field.getType()
142                 });
143 
144         cmp.setSuggestedItemsMethod(mbSearch);
145         cmp.setItemLabelMethod(mbLabel);
146         // set dojo-autocomplete to false to allow "in string search"
147         cmp.setAutoComplete(Boolean.FALSE);
148         // set html-autocomplete to false to avoid having the browser rendering its
149         // "autocomplete/lookup old values" stuff
150         cmp.setAutocomplete("off"); // NON-NLS
151 
152         if (cmp.getConverter() == null)
153         {
154             cmp.setConverter(new ObjectSerializationConverter());
155         }
156 
157         panel.getChildren().add(cmp);
158         return panel;
159     }
160 }