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