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 static org.apache.myfaces.tobago.TobagoConstants.ATTR_TAB_INDEX; 023 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_READONLY; 024 import org.apache.myfaces.tobago.util.MessageFactory; 025 026 import javax.faces.application.FacesMessage; 027 import javax.faces.context.FacesContext; 028 import javax.faces.el.ValueBinding; 029 import java.io.IOException; 030 031 /* 032 * Date: May 31, 2005 033 * Time: 7:47:11 PM 034 */ 035 public class UISelectOne extends javax.faces.component.UISelectOne implements SupportsMarkup { 036 037 public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.SelectOne"; 038 public static final String MESSAGE_VALUE_REQUIRED = "tobago.SelectOne.MESSAGE_VALUE_REQUIRED"; 039 040 private String[] markup; 041 private Integer tabIndex; 042 private Boolean readonly; 043 044 public void restoreState(FacesContext context, Object state) { 045 Object[] values = (Object[]) state; 046 super.restoreState(context, values[0]); 047 markup = (String[]) values[1]; 048 tabIndex = (Integer) values[2]; 049 readonly = (Boolean) values[3]; 050 } 051 052 public Object saveState(FacesContext context) { 053 Object[] values = new Object[4]; 054 values[0] = super.saveState(context); 055 values[1] = markup; 056 values[2] = tabIndex; 057 values[3] = readonly; 058 return values; 059 } 060 061 public void encodeBegin(FacesContext facesContext) throws IOException { 062 // TODO change this should be renamed to DimensionUtils.prepare!!! 063 UILayout.getLayout(this).layoutBegin(facesContext, this); 064 super.encodeBegin(facesContext); 065 } 066 067 public void validate(FacesContext facesContext) { 068 if (isRequired() && !isReadonly()) { 069 070 Object submittedValue = getSubmittedValue(); 071 if (submittedValue == null || "".equals(submittedValue)) { 072 FacesMessage facesMessage = MessageFactory.createFacesMessage( 073 facesContext, MESSAGE_VALUE_REQUIRED, FacesMessage.SEVERITY_ERROR); 074 facesContext.addMessage(getClientId(facesContext), facesMessage); 075 setValid(false); 076 } 077 } 078 super.validate(facesContext); 079 } 080 081 public String[] getMarkup() { 082 if (markup != null) { 083 return markup; 084 } 085 return ComponentUtil.getMarkupBinding(getFacesContext(), this); 086 } 087 088 public void setMarkup(String[] markup) { 089 this.markup = markup; 090 } 091 092 public Integer getTabIndex() { 093 if (tabIndex != null) { 094 return tabIndex; 095 } 096 ValueBinding vb = getValueBinding(ATTR_TAB_INDEX); 097 if (vb != null) { 098 Number number = (Number) vb.getValue(getFacesContext()); 099 if (number != null) { 100 return Integer.valueOf(number.intValue()); 101 } 102 } 103 return null; 104 } 105 106 public void setTabIndex(Integer tabIndex) { 107 this.tabIndex = tabIndex; 108 } 109 110 public boolean isReadonly() { 111 if (readonly != null) { 112 return readonly; 113 } 114 javax.faces.el.ValueBinding vb = getValueBinding(ATTR_READONLY); 115 if (vb != null) { 116 Boolean bool = (Boolean) vb.getValue(getFacesContext()); 117 if (bool != null) { 118 return bool; 119 } 120 } 121 return false; 122 } 123 124 public void setReadonly(boolean readonly) { 125 this.readonly = readonly; 126 } 127 }