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