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