KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > component > UICommand


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package javax.faces.component;
17
18 import javax.faces.context.FacesContext;
19 import javax.faces.el.EvaluationException;
20 import javax.faces.el.MethodBinding;
21 import javax.faces.el.ValueBinding;
22 import javax.faces.event.*;
23
24 /**
25  * see Javadoc of JSF Specification
26  *
27  * @author Manfred Geiler (latest modification by $Author: mwessendorf $)
28  * @version $Revision: 1.6 $ $Date: 2004/07/01 22:00:50 $
29  */

30 public class UICommand
31         extends UIComponentBase
32         implements ActionSource
33 {
34     private MethodBinding _action = null;
35     private MethodBinding _actionListener = null;
36
37     public void setAction(MethodBinding action)
38     {
39         _action = action;
40     }
41
42     public MethodBinding getAction()
43     {
44         return _action;
45     }
46
47     public void setActionListener(MethodBinding actionListener)
48     {
49         _actionListener = actionListener;
50     }
51
52     public MethodBinding getActionListener()
53     {
54         return _actionListener;
55     }
56
57     public void addActionListener(ActionListener listener)
58     {
59         addFacesListener(listener);
60     }
61
62     public ActionListener[] getActionListeners()
63     {
64         return (ActionListener[])getFacesListeners(ActionListener.class);
65     }
66
67     public void removeActionListener(ActionListener listener)
68     {
69         removeFacesListener(listener);
70     }
71
72     public void broadcast(FacesEvent event)
73             throws AbortProcessingException
74     {
75         super.broadcast(event);
76
77         if (event instanceof ActionEvent)
78         {
79             FacesContext context = getFacesContext();
80
81             MethodBinding actionListenerBinding = getActionListener();
82             if (actionListenerBinding != null)
83             {
84                 try
85                 {
86                     actionListenerBinding.invoke(context, new Object JavaDoc[] {event});
87                 }
88                 catch (EvaluationException e)
89                 {
90                     Throwable JavaDoc cause = e.getCause();
91                     if (cause != null && cause instanceof AbortProcessingException)
92                     {
93                         throw (AbortProcessingException)cause;
94                     }
95                     else
96                     {
97                         throw e;
98                     }
99                 }
100             }
101
102             ActionListener defaultActionListener
103                     = context.getApplication().getActionListener();
104             if (defaultActionListener != null)
105             {
106                 defaultActionListener.processAction((ActionEvent)event);
107             }
108         }
109     }
110
111     public void queueEvent(FacesEvent event)
112     {
113         if (event != null && event instanceof ActionEvent)
114         {
115             if (isImmediate())
116             {
117                 event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
118             }
119             else
120             {
121                 event.setPhaseId(PhaseId.INVOKE_APPLICATION);
122             }
123         }
124         super.queueEvent(event);
125     }
126
127
128     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
129

130     public static final String JavaDoc COMPONENT_TYPE = "javax.faces.Command";
131     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Command";
132     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "javax.faces.Button";
133     private static final boolean DEFAULT_IMMEDIATE = false;
134
135     private Boolean JavaDoc _immediate = null;
136     private Object JavaDoc _value = null;
137
138     public UICommand()
139     {
140         setRendererType(DEFAULT_RENDERER_TYPE);
141     }
142
143     public String JavaDoc getFamily()
144     {
145         return COMPONENT_FAMILY;
146     }
147
148     public void setImmediate(boolean immediate)
149     {
150         _immediate = Boolean.valueOf(immediate);
151     }
152
153     public boolean isImmediate()
154     {
155         if (_immediate != null) return _immediate.booleanValue();
156         ValueBinding vb = getValueBinding("immediate");
157         Boolean JavaDoc v = vb != null ? (Boolean JavaDoc)vb.getValue(getFacesContext()) : null;
158         return v != null ? v.booleanValue() : DEFAULT_IMMEDIATE;
159     }
160
161     public void setValue(Object JavaDoc value)
162     {
163         _value = value;
164     }
165
166     public Object JavaDoc getValue()
167     {
168         if (_value != null) return _value;
169         ValueBinding vb = getValueBinding("value");
170         return vb != null ? (Object JavaDoc)vb.getValue(getFacesContext()) : null;
171     }
172
173
174     public Object JavaDoc saveState(FacesContext context)
175     {
176         Object JavaDoc values[] = new Object JavaDoc[5];
177         values[0] = super.saveState(context);
178         values[1] = saveAttachedState(context, _action);
179         values[2] = saveAttachedState(context, _actionListener);
180         values[3] = _immediate;
181         values[4] = _value;
182         return ((Object JavaDoc) (values));
183     }
184
185     public void restoreState(FacesContext context, Object JavaDoc state)
186     {
187         Object JavaDoc values[] = (Object JavaDoc[])state;
188         super.restoreState(context, values[0]);
189         _action = (MethodBinding)restoreAttachedState(context, values[1]);
190         _actionListener = (MethodBinding)restoreAttachedState(context, values[2]);
191         _immediate = (Boolean JavaDoc)values[3];
192         _value = (Object JavaDoc)values[4];
193     }
194     //------------------ GENERATED CODE END ---------------------------------------
195
}
196
Popular Tags