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.logging.Log; 021 import org.apache.commons.logging.LogFactory; 022 import org.apache.myfaces.tobago.TobagoConstants; 023 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_TIP; 024 025 import javax.faces.component.UIComponent; 026 import javax.faces.component.UIOutput; 027 import javax.faces.context.FacesContext; 028 import javax.faces.el.ValueBinding; 029 import java.io.IOException; 030 031 public class UILabel extends UIOutput implements SupportsMarkup { 032 033 private static final Log LOG = LogFactory.getLog(UILabel.class); 034 035 public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Label"; 036 037 private String[] markup; 038 private String tip; 039 040 041 public String getTip() { 042 if (tip != null) { 043 return tip; 044 } 045 ValueBinding vb = getValueBinding(ATTR_TIP); 046 if (vb != null) { 047 return (String) vb.getValue(getFacesContext()); 048 } else { 049 return null; 050 } 051 } 052 053 public void setTip(String tip) { 054 this.tip = tip; 055 } 056 057 058 public void restoreState(FacesContext context, Object state) { 059 Object[] values = (Object[]) state; 060 super.restoreState(context, values[0]); 061 markup = (String[]) values[1]; 062 tip = (String) values[2]; 063 } 064 065 public Object saveState(FacesContext context) { 066 Object[] values = new Object[3]; 067 values[0] = super.saveState(context); 068 values[1] = markup; 069 values[2] = tip; 070 return values; 071 } 072 073 public String[] getMarkup() { 074 if (markup != null) { 075 return markup; 076 } 077 return ComponentUtil.getMarkupBinding(getFacesContext(), this); 078 } 079 080 public void setMarkup(String[] markup) { 081 this.markup = markup; 082 } 083 084 @Override 085 public void encodeBegin(FacesContext facesContext) throws IOException { 086 String forComponent = (String) getAttributes().get(TobagoConstants.ATTR_FOR); 087 if (LOG.isDebugEnabled()) { 088 LOG.debug("for = '" + forComponent + "'"); 089 } 090 if ("@auto".equals(forComponent)) { 091 for (Object object : getParent().getChildren()) { 092 UIComponent child = (UIComponent) object; 093 if (child instanceof javax.faces.component.UIInput) { 094 forComponent = child.getId(); 095 getAttributes().put(TobagoConstants.ATTR_FOR, forComponent); 096 break; 097 } 098 } 099 } 100 super.encodeBegin(facesContext); 101 } 102 }