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 javax.faces.component;
20  
21  import javax.el.ValueExpression;
22  import javax.faces.context.FacesContext;
23  
24  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
25  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
26  
27  /**
28   * Displays a graphical image.
29   * <p>
30   * See the javadoc for this class in the
31   * <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
32   * for further details.
33   */
34  @JSFComponent(defaultRendererType = "javax.faces.Image")
35  public class UIGraphic extends UIComponentBase
36  {
37      public static final String COMPONENT_TYPE = "javax.faces.Graphic";
38      public static final String COMPONENT_FAMILY = "javax.faces.Graphic";
39  
40      private static final String URL_PROPERTY = "url";
41      private static final String VALUE_PROPERTY = "value";
42  
43      private Object _value;
44  
45      /**
46       * Construct an instance of the UIGraphic.
47       */
48      public UIGraphic()
49      {
50          setRendererType("javax.faces.Image");
51      }
52  
53      @Override
54      public String getFamily()
55      {
56          return COMPONENT_FAMILY;
57      }
58  
59      /**
60       * An alias for the "value" attribute.
61       */
62      @JSFProperty
63      public String getUrl()
64      {
65          return (String) getValue();
66      }
67  
68      public void setUrl(String url)
69      {
70          setValue(url);
71      }
72  
73      @Override
74      public ValueExpression getValueExpression(String name)
75      {
76          if (URL_PROPERTY.equals(name))
77          {
78              return super.getValueExpression(VALUE_PROPERTY);
79          }
80          else
81          {
82              return super.getValueExpression(name);
83          }
84      }
85  
86      @Override
87      public void setValueExpression(String name, ValueExpression binding)
88      {
89          if (URL_PROPERTY.equals(name))
90          {
91              super.setValueExpression(VALUE_PROPERTY, binding);
92          }
93          else
94          {
95              super.setValueExpression(name, binding);
96          }
97      }
98  
99      /**
100      * The URL of the image.
101      * <p>
102      * If the URL starts with a '/', it is relative to the context path of the web application.
103      * </p>
104      */
105     @JSFProperty
106     public Object getValue()
107     {
108         if (_value != null)
109         {
110             return _value;
111         }
112         ValueExpression expression = getValueExpression("value");
113         if (expression != null)
114         {
115             return expression.getValue(getFacesContext().getELContext());
116         }
117         return null;
118     }
119 
120     public void setValue(Object value)
121     {
122         this._value = value;
123     }
124 
125     @Override
126     public Object saveState(FacesContext facesContext)
127     {
128         Object[] values = new Object[2];
129         values[0] = super.saveState(facesContext);
130         values[1] = _value;
131 
132         return values;
133     }
134 
135     @Override
136     public void restoreState(FacesContext facesContext, Object state)
137     {
138         Object[] values = (Object[]) state;
139         super.restoreState(facesContext, values[0]);
140         _value = values[1];
141     }
142 }