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.fileupload.FileItem;
023    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_ENCTYPE;
024    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_TAB_INDEX;
025    import org.apache.myfaces.tobago.util.MessageFactory;
026    
027    import javax.faces.application.FacesMessage;
028    import javax.faces.component.UIComponent;
029    import javax.faces.context.FacesContext;
030    import javax.faces.el.ValueBinding;
031    
032    /*
033     * Date: 10.02.2006
034     * Time: 19:02:13
035     */
036    public class UIFileInput extends javax.faces.component.UIInput {
037      public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.FileInput";
038    
039      private Integer tabIndex;
040    
041      public void setParent(UIComponent uiComponent) {
042        super.setParent(uiComponent);
043        UIPage form = ComponentUtil.findPage(getFacesContext(), uiComponent);
044        if (form != null) {
045          form.getAttributes().put(ATTR_ENCTYPE, "multipart/form-data");
046        } else {
047          FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(UIPage.ENCTYPE_KEY,
048              "multipart/form-data");
049        }
050      }
051    
052      public void validate(FacesContext facesContext) {
053        if (isRequired()) {
054          if (getSubmittedValue() instanceof FileItem) {
055            FileItem file = (FileItem) getSubmittedValue();
056            if (file == null || file.getName().length() == 0) {
057              addErrorMessage(facesContext);
058              setValid(false);
059            }
060          } else {
061            addErrorMessage(facesContext);
062            setValid(false);
063          }
064        }
065        super.validate(facesContext);
066      }
067    
068      private void addErrorMessage(FacesContext facesContext) {
069        FacesMessage facesMessage = MessageFactory.createFacesMessage(
070            facesContext, REQUIRED_MESSAGE_ID, FacesMessage.SEVERITY_ERROR);
071        facesContext.addMessage(getClientId(facesContext), facesMessage);
072      }
073    
074      public void restoreState(FacesContext context, Object state) {
075        Object[] values = (Object[]) state;
076        super.restoreState(context, values[0]);
077        tabIndex = (Integer) values[1];
078      }
079    
080      public Object saveState(FacesContext context) {
081        Object[] values = new Object[2];
082        values[0] = super.saveState(context);
083        values[1] = tabIndex;
084        return values;
085      }
086    
087      public Integer getTabIndex() {
088        if (tabIndex != null) {
089          return tabIndex;
090        }
091        ValueBinding vb = getValueBinding(ATTR_TAB_INDEX);
092        if (vb != null) {
093          Number number = (Number) vb.getValue(getFacesContext());
094          if (number != null) {
095            return Integer.valueOf(number.intValue());
096          }
097        }
098        return null;
099      }
100    
101      public void setTabIndex(Integer tabIndex) {
102        this.tabIndex = tabIndex;
103      }
104    }