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 } 045 } 046 047 public void validate(FacesContext facesContext) { 048 if (isRequired()) { 049 if (getSubmittedValue() instanceof FileItem) { 050 FileItem file = (FileItem) getSubmittedValue(); 051 if (file == null || file.getName().length() == 0) { 052 addErrorMessage(facesContext); 053 setValid(false); 054 } 055 } else { 056 addErrorMessage(facesContext); 057 setValid(false); 058 } 059 } 060 super.validate(facesContext); 061 } 062 063 private void addErrorMessage(FacesContext facesContext) { 064 FacesMessage facesMessage = MessageFactory.createFacesMessage( 065 facesContext, REQUIRED_MESSAGE_ID, FacesMessage.SEVERITY_ERROR); 066 facesContext.addMessage(getClientId(facesContext), facesMessage); 067 } 068 069 public void restoreState(FacesContext context, Object state) { 070 Object[] values = (Object[]) state; 071 super.restoreState(context, values[0]); 072 tabIndex = (Integer) values[1]; 073 } 074 075 public Object saveState(FacesContext context) { 076 Object[] values = new Object[2]; 077 values[0] = super.saveState(context); 078 values[1] = tabIndex; 079 return values; 080 } 081 082 public Integer getTabIndex() { 083 if (tabIndex != null) { 084 return tabIndex; 085 } 086 ValueBinding vb = getValueBinding(ATTR_TAB_INDEX); 087 if (vb != null) { 088 Number number = (Number) vb.getValue(getFacesContext()); 089 if (number != null) { 090 return Integer.valueOf(number.intValue()); 091 } 092 } 093 return null; 094 } 095 096 public void setTabIndex(Integer tabIndex) { 097 this.tabIndex = tabIndex; 098 } 099 }