001    package org.apache.myfaces.tobago.component;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_DISABLED;
023    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_PASSWORD;
024    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_READONLY;
025    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_TAB_INDEX;
026    import org.apache.myfaces.tobago.ajax.api.AjaxComponent;
027    import org.apache.myfaces.tobago.ajax.api.AjaxPhaseListener;
028    import org.apache.myfaces.tobago.ajax.api.AjaxUtils;
029    
030    import javax.faces.context.FacesContext;
031    import javax.faces.el.MethodBinding;
032    import javax.faces.el.ValueBinding;
033    import java.io.IOException;
034    
035    public class UIInput extends javax.faces.component.UIInput implements AjaxComponent, SupportsMarkup {
036    
037      private static final Log LOG = LogFactory.getLog(UIInput.class);
038      public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Input";
039    
040      private Boolean readonly;
041      private Boolean disabled;
042      private Boolean password;
043      private String[] markup;
044      private javax.faces.el.MethodBinding suggestMethod;
045      private Integer tabIndex;
046    
047      public void restoreState(FacesContext context, Object state) {
048        Object[] values = (Object[]) state;
049        super.restoreState(context, values[0]);
050        suggestMethod = (MethodBinding) restoreAttachedState(context, values[1]);
051        readonly = (Boolean) values[2];
052        password = (Boolean) values[3];
053        markup = (String[]) values[4];
054        disabled = (Boolean) values[5];
055        tabIndex = (Integer) values[6];
056      }
057    
058      public Object saveState(FacesContext context) {
059        Object[] values = new Object[7];
060        values[0] = super.saveState(context);
061        values[1] = saveAttachedState(context, suggestMethod);
062        values[2] = readonly;
063        values[3] = password;
064        values[4] = markup;
065        values[5] = disabled;
066        values[6] = tabIndex;
067        return values;
068      }
069    
070      public String[] getMarkup() {
071        if (markup != null) {
072          return markup;
073        }
074        return ComponentUtil.getMarkupBinding(getFacesContext(), this);
075      }
076    
077      public void setMarkup(String[] markup) {
078        this.markup = markup;
079      }
080    
081      public boolean isReadonly() {
082        if (readonly != null) {
083          return readonly;
084        }
085        ValueBinding vb = getValueBinding(ATTR_READONLY);
086        if (vb != null) {
087          return (Boolean.TRUE.equals(vb.getValue(getFacesContext())));
088        } else {
089          return false;
090        }
091      }
092    
093      public void setReadonly(boolean readonly) {
094        this.readonly = readonly;
095      }
096    
097      public boolean isDisabled() {
098          if (disabled != null) {
099            return disabled;
100          }
101          ValueBinding vb = getValueBinding(ATTR_DISABLED);
102          if (vb != null) {
103            return (Boolean.TRUE.equals(vb.getValue(getFacesContext())));
104          } else {
105            return false;
106          }
107        }
108    
109        public void setDisabled(boolean disabled) {
110          this.disabled = disabled;
111        }
112    
113    
114      public boolean isPassword() {
115        if (password != null) {
116          return password;
117        }
118        ValueBinding vb = getValueBinding(ATTR_PASSWORD);
119        if (vb != null) {
120          return (Boolean.TRUE.equals(vb.getValue(getFacesContext())));
121        } else {
122          return false;
123        }
124      }
125    
126    
127      public void setPassword(boolean password) {
128        this.password = password;
129      }
130    
131    
132      public MethodBinding getSuggestMethod() {
133        return suggestMethod;
134      }
135    
136      public void setSuggestMethod(MethodBinding suggestMethod) {
137        this.suggestMethod = suggestMethod;
138      }
139    
140      public Integer getTabIndex() {
141        if (tabIndex != null) {
142          return tabIndex;
143        }
144        ValueBinding vb = getValueBinding(ATTR_TAB_INDEX);
145        if (vb != null) {
146          Number number = (Number) vb.getValue(getFacesContext());
147          if (number != null) {
148            return Integer.valueOf(number.intValue());
149          }
150        }
151        return null;
152      }
153    
154      public void setTabIndex(Integer tabIndex) {
155        this.tabIndex = tabIndex;
156      }
157    
158      // TODO can this removed?
159      public void updateModel(FacesContext facesContext) {
160        if (ComponentUtil.mayUpdateModel(this)) {
161          super.updateModel(facesContext);
162        }
163      }
164    
165      public void encodeBegin(FacesContext facesContext) throws IOException {
166        // TODO change this should be renamed to DimensionUtils.prepare!!!
167        UILayout.getLayout(this).layoutBegin(facesContext, this);
168        super.encodeBegin(facesContext);
169      }
170    
171      public void encodeAjax(FacesContext facesContext) throws IOException {
172        AjaxUtils.encodeAjaxComponent(facesContext, this);
173      }
174    
175      public void processAjax(FacesContext facesContext) throws IOException {
176        final String ajaxId = (String) facesContext.getExternalContext().
177            getRequestParameterMap().get(AjaxPhaseListener.AJAX_COMPONENT_ID);
178        if (ajaxId.equals(getClientId(facesContext))) {
179          encodeAjax(facesContext);
180        } else {
181          AjaxUtils.processAjaxOnChildren(facesContext, this);
182        }
183      }
184    
185    }