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 org.apache.myfaces.tobago.ajax.api.AjaxComponent;
023    import org.apache.myfaces.tobago.ajax.api.AjaxPhaseListener;
024    import org.apache.myfaces.tobago.ajax.api.AjaxUtils;
025    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_READONLY;
026    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_DISABLED;
027    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_PASSWORD;
028    
029    import javax.faces.context.FacesContext;
030    import javax.faces.el.MethodBinding;
031    import javax.faces.el.ValueBinding;
032    import java.io.IOException;
033    
034    public class UIInput extends javax.faces.component.UIInput implements AjaxComponent, SupportsMarkup {
035    
036      private static final Log LOG = LogFactory.getLog(UIInput.class);
037      public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Input";
038    
039      private Boolean readonly;
040      private Boolean disabled;
041      private Boolean password;
042      private String[] markup;
043      private javax.faces.el.MethodBinding suggestMethod;
044    
045      public void restoreState(FacesContext context, Object state) {
046        Object[] values = (Object[]) state;
047        super.restoreState(context, values[0]);
048        suggestMethod = (MethodBinding) restoreAttachedState(context, values[1]);
049        readonly = (Boolean) values[2];
050        password = (Boolean) values[3];
051        markup = (String[]) values[4];
052        disabled = (Boolean) values[5];
053      }
054    
055      public Object saveState(FacesContext context) {
056        Object[] values = new Object[6];
057        values[0] = super.saveState(context);
058        values[1] = saveAttachedState(context, suggestMethod);
059        values[2] = readonly;
060        values[3] = password;
061        values[4] = markup;
062        values[5] = disabled;
063        return values;
064      }
065    
066      public String[] getMarkup() {
067        if (markup != null) {
068          return markup;
069        }
070        return ComponentUtil.getMarkupBinding(getFacesContext(), this);
071      }
072    
073      public void setMarkup(String[] markup) {
074        this.markup = markup;
075      }
076    
077      public boolean isReadonly() {
078        if (readonly != null) {
079          return readonly;
080        }
081        ValueBinding vb = getValueBinding(ATTR_READONLY);
082        if (vb != null) {
083          return (Boolean.TRUE.equals(vb.getValue(getFacesContext())));
084        } else {
085          return false;
086        }
087      }
088    
089      public void setReadonly(boolean readonly) {
090        this.readonly = readonly;
091      }
092    
093      public boolean isDisabled() {
094          if (disabled != null) {
095            return disabled;
096          }
097          ValueBinding vb = getValueBinding(ATTR_DISABLED);
098          if (vb != null) {
099            return (Boolean.TRUE.equals(vb.getValue(getFacesContext())));
100          } else {
101            return false;
102          }
103        }
104    
105        public void setDisabled(boolean disabled) {
106          this.disabled = disabled;
107        }
108    
109    
110      public boolean isPassword() {
111        if (password != null) {
112          return password;
113        }
114        ValueBinding vb = getValueBinding(ATTR_PASSWORD);
115        if (vb != null) {
116          return (Boolean.TRUE.equals(vb.getValue(getFacesContext())));
117        } else {
118          return false;
119        }
120      }
121    
122    
123      public void setPassword(boolean password) {
124        this.password = password;
125      }
126    
127    
128      public MethodBinding getSuggestMethod() {
129        return suggestMethod;
130      }
131    
132      public void setSuggestMethod(MethodBinding suggestMethod) {
133        this.suggestMethod = suggestMethod;
134      }
135    
136    
137      // TODO can this removed?
138      public void updateModel(FacesContext facesContext) {
139        if (ComponentUtil.mayUpdateModel(this)) {
140          super.updateModel(facesContext);
141        }
142      }
143    
144      public void encodeBegin(FacesContext facesContext) throws IOException {
145        // TODO change this should be renamed to DimensionUtils.prepare!!!
146        UILayout.getLayout(this).layoutBegin(facesContext, this);
147        super.encodeBegin(facesContext);
148      }
149    
150      public void encodeAjax(FacesContext facesContext) throws IOException {
151        AjaxUtils.encodeAjaxComponent(facesContext, this);
152      }
153    
154      public void processAjax(FacesContext facesContext) throws IOException {
155        final String ajaxId = (String) facesContext.getExternalContext().
156            getRequestParameterMap().get(AjaxPhaseListener.AJAX_COMPONENT_ID);
157        if (ajaxId.equals(getClientId(facesContext))) {
158          encodeAjax(facesContext);
159        } else {
160          AjaxUtils.processAjaxOnChildren(facesContext, this);
161        }
162      }
163    }