View Javadoc

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.el.ValueExpression;
22  
23  import javax.faces.application.FacesMessage;
24  import javax.faces.context.FacesContext;
25  import javax.faces.el.ValueBinding;
26  
27  /**
28   * Extension to FacesMessage which keeps track of the label on the component
29   * that generated the message.
30   * @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 $
31   */
32  public class LabeledFacesMessage extends FacesMessage
33  {
34    public LabeledFacesMessage()
35    {
36    }
37  
38    /**
39     * Creates a LabeledFacesMessage without a pre-set label.
40     * @param severity the severity of the message
41     * @param summary the message summary
42     * @param detail the message detail
43     */
44    public LabeledFacesMessage(
45      FacesMessage.Severity severity,
46      String summary,
47      String detail)
48    {
49      super(severity, summary, detail);
50    }
51  
52  
53    /**
54     * Creates a LabeledFacesMessage with a label.
55     * @param severity the severity of the message
56     * @param summary the message summary
57     * @param detail the message detail
58     * @param label the message label - either a String or a ValueBinding
59     */
60    public LabeledFacesMessage(
61      FacesMessage.Severity severity,
62      String summary,
63      String detail,
64      Object label)
65    {
66      super(severity, summary, detail);
67      _label = label;
68    }
69  
70    /**
71     * Returns the label, which can be either a String or a ValueBinding.
72     */
73    public Object getLabel()
74    {
75      return _label;
76    }
77  
78    /**
79     * Sets the label, which can be either a String or a ValueBinding.
80     */
81    public void setLabel(Object label)
82    {
83      _label = label;
84    }
85  
86    /**
87     * Gets a string representation of the label.  If the label
88     * is a ValueBinding, the expression is evaluated and the string
89     * value returned.
90     */
91    public String getLabelAsString(FacesContext context)
92    {
93      Object label = getLabel();
94  
95      if (label instanceof ValueExpression)
96      {
97        label = ((ValueExpression) label).getValue(context.getELContext());          
98      }
99      else if (label instanceof ValueBinding)
100     {
101       label = ((ValueBinding) label).getValue(context);
102     }
103 
104     if (label == null)
105       return null;
106 
107     return label.toString();
108   }
109 
110 
111   private Object _label;
112   private static final long serialVersionUID = 1L;
113 }