1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.event;
20
21 import javax.el.ValueExpression;
22
23 import javax.faces.component.StateHolder;
24 import javax.faces.context.FacesContext;
25 import javax.faces.event.ActionEvent;
26 import javax.faces.event.ActionListener;
27
28 import org.apache.myfaces.trinidad.bean.FacesBean;
29 import org.apache.myfaces.trinidad.bean.FacesBeanImpl;
30 import org.apache.myfaces.trinidad.bean.PropertyKey;
31 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
32
33
34
35
36
37 public class SetActionListener implements ActionListener, StateHolder
38 {
39
40
41
42 public SetActionListener()
43 {
44 _bean = new Bean();
45 }
46
47
48
49
50
51 public void processAction(ActionEvent event)
52 {
53 ValueExpression to = _bean.getValueExpression(Bean.TO_KEY);
54 if (to != null)
55 {
56 Object from = getFrom();
57 try
58 {
59 to.setValue(FacesContext.getCurrentInstance().getELContext(), from);
60 }
61 catch (RuntimeException e)
62 {
63 if (_LOG.isWarning())
64 {
65 ValueExpression fromExpression = _bean.getValueExpression(Bean.FROM_KEY);
66 String mes = "Error setting:'"+to.getExpressionString() +
67 "' to value:"+from;
68 if (fromExpression != null)
69 mes += " from:'"+fromExpression.getExpressionString()+"'";
70
71 _LOG.warning(mes, e);
72 }
73 throw e;
74 }
75 }
76 }
77
78 public ValueExpression getValueExpression(String name)
79 {
80 PropertyKey key = Bean.TYPE.findKey(name);
81 if (key == null)
82 return null;
83
84 return _bean.getValueExpression(key);
85 }
86
87 public void setValueExpression(String name, ValueExpression binding)
88 {
89 PropertyKey key = Bean.TYPE.findKey(name);
90 if (key == null)
91 throw new IllegalArgumentException();
92 _bean.setValueExpression(key, binding);
93 }
94
95 public Object getFrom()
96 {
97 return _bean.getProperty(Bean.FROM_KEY);
98 }
99
100 public void setFrom(Object from)
101 {
102 _bean.setProperty(Bean.FROM_KEY, from);
103 }
104
105 public Object saveState(FacesContext context)
106 {
107 return _bean.saveState(context);
108 }
109
110 public void restoreState(FacesContext context, Object state)
111 {
112 _bean.restoreState(context, state);
113 }
114
115 public boolean isTransient()
116 {
117 return false;
118 }
119
120 public void setTransient(boolean newTransientValue)
121 {
122 throw new UnsupportedOperationException();
123 }
124
125
126 static private class Bean extends FacesBeanImpl
127 {
128 static public final FacesBean.Type TYPE = new FacesBean.Type();
129 static public final PropertyKey FROM_KEY =
130 TYPE.registerKey("from");
131
132 static public final PropertyKey TO_KEY =
133 TYPE.registerKey("to");
134
135
136 @Override
137 public Type getType()
138 {
139 return TYPE;
140 }
141
142 static
143 {
144 TYPE.lock();
145 }
146 }
147
148 private Bean _bean;
149
150 private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(SetActionListener.class);
151 }