1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.change;
20
21 import java.io.Serializable;
22
23 import java.util.Map;
24
25 import javax.el.ValueExpression;
26
27 import javax.faces.component.UIComponent;
28 import javax.faces.el.ValueBinding;
29
30 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
31
32
33 /**
34 * Change specialization for change in attributes.
35 * While applying this Change, the specified attribute state is restored.
36 * @version $Name: $ ($Revision: adfrt/faces/adf-faces-api/src/main/java/oracle/adf/view/faces/change/AttributeComponentChange.java#0 $) $Date: 10-nov-2005.19:09:56 $
37 */
38 public class AttributeComponentChange extends ComponentChange
39 {
40 /**
41 * Constructs an AttributeChange with the given attributeName and
42 * attributeValue.
43 * @param attributeName The name of the attribute for which the value needs
44 * to be restored.
45 * @param attributeValue The value of the attribute that needs to be restored.
46 * This value should be of type java.io.Serializable in order to be
47 * persisted. If the value is of type ValueExpression or ValueBinding,
48 * the component's ValueBinding or ValueExpression for the attribute
49 * will be updated and the current attribute value, if any, removed so
50 * that the new ValueExpression or ValueBinding can take precedence.
51 * @throws IllegalArgumentException if specified attributeName were to be null or
52 * the specified attributeValue isn't serializable
53 */
54 public AttributeComponentChange(
55 String attributeName,
56 Object attributeValue
57 )
58 {
59 if ((attributeName == null) || (attributeName.length() == 0))
60 throw new IllegalArgumentException(_LOG.getMessage(
61 "CANNOT_CONSTRUCT_ATTRIBUTECHANGE_WITH_NULL_NAME"));
62
63 if (attributeValue != null && !(attributeValue instanceof Serializable))
64 throw new IllegalArgumentException(_LOG.getMessage(
65 "UNSERIALIZABLE_VALUE", attributeValue));
66
67 _attributeName = attributeName;
68 _attributeValue = attributeValue;
69 }
70
71 /**
72 * Returns the name of the attribute that represents this Change.
73 */
74 public String getAttributeName()
75 {
76 return _attributeName;
77 }
78
79 /**
80 * Returns the value of the attribute corresponding to this AttributeChange.
81 */
82 public Object getAttributeValue()
83 {
84 return _attributeValue;
85 }
86
87 /**
88 * {@inheritDoc}
89 */
90 @Override
91 @SuppressWarnings("deprecation")
92 public void changeComponent(UIComponent uiComponent)
93 {
94 Map<String, Object> attributeMap = uiComponent.getAttributes();
95
96
97
98
99
100 if (_attributeValue instanceof ValueExpression)
101 {
102 uiComponent.setValueExpression(_attributeName, (ValueExpression)_attributeValue);
103 attributeMap.remove(_attributeName);
104 }
105 else if (_attributeValue instanceof ValueBinding)
106 {
107 uiComponent.setValueBinding(_attributeName, (ValueBinding)_attributeValue);
108 attributeMap.remove(_attributeName);
109 }
110 else
111 {
112 attributeMap.put(_attributeName, _attributeValue);
113 }
114 }
115
116 private final String _attributeName;
117
118
119
120
121
122
123
124
125
126
127 private final Object _attributeValue;
128 private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
129 AttributeComponentChange.class);
130 private static final long serialVersionUID = 1L;
131 }