1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.myfaces.trinidad.util; 20 21 import javax.faces.application.FacesMessage; 22 import javax.faces.context.FacesContext; 23 import javax.faces.el.ValueBinding; 24 25 /** 26 * Extension to FacesMessage which keeps track of the label on the component 27 * that generated the message. 28 * @version $Name: $ ($Revision: adfrt/faces/adf-faces-api/src/main/java/oracle/adf/view/faces/util/LabeledFacesMessage.java#0 $) $Date: 10-nov-2005.19:08:38 $ 29 */ 30 public class LabeledFacesMessage extends FacesMessage 31 { 32 public LabeledFacesMessage() 33 { 34 } 35 36 /** 37 * Creates a LabeledFacesMessage without a pre-set label. 38 * @param severity the severity of the message 39 * @param summary the message summary 40 * @param detail the message detail 41 */ 42 public LabeledFacesMessage( 43 FacesMessage.Severity severity, 44 String summary, 45 String detail) 46 { 47 super(severity, summary, detail); 48 } 49 50 51 /** 52 * Creates a LabeledFacesMessage with a label. 53 * @param severity the severity of the message 54 * @param summary the message summary 55 * @param detail the message detail 56 * @param label the message label - either a String or a ValueBinding 57 */ 58 public LabeledFacesMessage( 59 FacesMessage.Severity severity, 60 String summary, 61 String detail, 62 Object label) 63 { 64 super(severity, summary, detail); 65 _label = label; 66 } 67 68 /** 69 * Returns the label, which can be either a String or a ValueBinding. 70 */ 71 public Object getLabel() 72 { 73 return _label; 74 } 75 76 /** 77 * Sets the label, which can be either a String or a ValueBinding. 78 */ 79 public void setLabel(Object label) 80 { 81 _label = label; 82 } 83 84 /** 85 * Gets a string representation of the label. If the label 86 * is a ValueBinding, the expression is evaluated and the string 87 * value returned. 88 */ 89 public String getLabelAsString(FacesContext context) 90 { 91 Object label = getLabel(); 92 if (label instanceof ValueBinding) 93 { 94 label = ((ValueBinding) label).getValue(context); 95 } 96 97 if (label == null) 98 return null; 99 100 return label.toString(); 101 } 102 103 104 private Object _label; 105 private static final long serialVersionUID = 1L; 106 }