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.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_TAB_INDEX;
025
026 import javax.faces.context.FacesContext;
027 import javax.faces.el.ValueBinding;
028 import java.io.IOException;
029 import java.util.List;
030
031 /*
032 * User: weber
033 * Date: May 31, 2005
034 * Time: 7:47:11 PM
035 */
036 public class UISelectMany extends javax.faces.component.UISelectMany implements SupportsMarkup {
037
038 @SuppressWarnings("UnusedDeclaration")
039 private static final Log LOG = LogFactory.getLog(UISelectMany.class);
040
041 public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.SelectMany";
042
043 private String[] markup;
044 private Integer tabIndex;
045
046 public Object[] getSelectedValues() {
047 Object value = getValue();
048 if (value instanceof List) {
049 List list = (List) value;
050 return list.toArray();
051 } else {
052 return (Object[]) value;
053 }
054 }
055
056 public void restoreState(FacesContext context, Object state) {
057 Object[] values = (Object[]) state;
058 super.restoreState(context, values[0]);
059 markup = (String[]) values[1];
060 tabIndex = (Integer) values[2];
061 }
062
063 public Object saveState(FacesContext context) {
064 Object[] values = new Object[3];
065 values[0] = super.saveState(context);
066 values[1] = markup;
067 values[2] = tabIndex;
068 return values;
069 }
070
071 public String[] getMarkup() {
072 if (markup != null) {
073 return markup;
074 }
075 return ComponentUtil.getMarkupBinding(getFacesContext(), this);
076 }
077
078 public void setMarkup(String[] markup) {
079 this.markup = markup;
080 }
081
082 public void encodeBegin(FacesContext facesContext) throws IOException {
083 // TODO change this should be renamed to DimensionUtils.prepare!!!
084 UILayout.getLayout(this).layoutBegin(facesContext, this);
085 super.encodeBegin(facesContext);
086 }
087
088 public Integer getTabIndex() {
089 if (tabIndex != null) {
090 return tabIndex;
091 }
092 ValueBinding vb = getValueBinding(ATTR_TAB_INDEX);
093 if (vb != null) {
094 Number number = (Number) vb.getValue(getFacesContext());
095 if (number != null) {
096 return Integer.valueOf(number.intValue());
097 }
098 }
099 return null;
100 }
101
102 public void setTabIndex(Integer tabIndex) {
103 this.tabIndex = tabIndex;
104 }
105
106 }