KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > Action


1 /*
2  * Copyright 1999-2005 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 org.apache.cocoon.forms.formmodel;
17
18 import org.apache.cocoon.environment.Request;
19 import org.apache.cocoon.forms.FormContext;
20 import org.apache.cocoon.forms.event.*;
21
22 /**
23  * An Action widget. An Action widget can cause an {@link ActionEvent} to be triggered
24  * on the server side, which will be handled by either the event handlers defined in the
25  * form definition, and/or by the {@link org.apache.cocoon.forms.event.FormHandler FormHandler}
26  * registered with the form, if any. An Action widget can e.g. be rendered as a button,
27  * or as a hidden field which gets its value set by javascript. The Action widget will generate its associated
28  * ActionEvent when a requestparameter is present with as name the id of this Action widget, and as
29  * value a non-empty value.
30  *
31  * @version $Id: Action.java 231335 2005-08-10 21:40:35Z vgritsenko $
32  */

33 public class Action extends AbstractWidget implements ActionListenerEnabled {
34
35     private final ActionDefinition definition;
36     /** Additional listeners to those defined as part of the widget definition (if any). */
37     private ActionListener listener;
38
39
40     public Action(ActionDefinition definition) {
41         super(definition);
42         this.definition = definition;
43     }
44
45     public WidgetDefinition getDefinition() {
46         return this.definition;
47     }
48
49     public void readFromRequest(final FormContext formContext) {
50         if (!getCombinedState().isAcceptingInputs()) {
51             return;
52         }
53
54         Form form = getForm();
55
56         // Set the submit widget if we can determine it from the request
57
String JavaDoc fullId = getRequestParameterName();
58         Request request = formContext.getRequest();
59
60         String JavaDoc value = request.getParameter(fullId);
61         if (value != null) {
62             form.setSubmitWidget(this);
63
64         } else {
65             // Special workaround an IE bug for <input type="image" name="foo"> :
66
// in that case, IE only sends "foo.x" and "foo.y" and not "foo" whereas
67
// standards-compliant browsers such as Mozilla do send the "foo" parameter.
68
//
69
// Note that since actions are terminal widgets, there's no chance of conflict
70
// with a child "x" or "y" widget.
71
value = request.getParameter(fullId + ".x");
72             if ((value != null) && value.length() > 0) {
73                 form.setSubmitWidget(this);
74             }
75         }
76
77         if (form.getSubmitWidget() == this) {
78             form.addWidgetEvent(new ActionEvent(this, definition.getActionCommand()));
79             handleActivate();
80         }
81     }
82
83     /**
84      * Handle the fact that this action was activated. The default here is to end the
85      * current form processing and redisplay the form, which means that actual behaviour
86      * should be implemented in event listeners.
87      */

88     protected void handleActivate() {
89         getForm().endProcessing(true);
90     }
91
92     /**
93      * Always return <code>true</code> (an action has no validation)
94      *
95      * <br>TODO is there a use case for actions having validators?
96      */

97     public boolean validate() {
98         return true;
99     }
100
101     /**
102      * @see org.apache.cocoon.forms.formmodel.Widget#isValid()
103      */

104     public boolean isValid() {
105         return true;
106     }
107
108     private static final String JavaDoc ACTION_EL = "action";
109
110     /**
111      * @return "action"
112      */

113     public String JavaDoc getXMLElementName() {
114         return ACTION_EL;
115     }
116
117     /**
118      * Adds a ActionListener to this widget instance. Listeners defined
119      * on the widget instance will be executed in addtion to any listeners
120      * that might have been defined in the widget definition.
121      */

122     public void addActionListener(ActionListener listener) {
123         this.listener = WidgetEventMulticaster.add(this.listener, listener);
124     }
125
126     public void removeActionListener(ActionListener listener) {
127         this.listener = WidgetEventMulticaster.remove(this.listener, listener);
128     }
129
130     private void fireActionEvent(ActionEvent event) {
131         if (this.listener != null) {
132             this.listener.actionPerformed(event);
133         }
134     }
135
136     public void broadcastEvent(WidgetEvent event) {
137         if (event instanceof ActionEvent) {
138             this.definition.fireActionEvent((ActionEvent)event);
139             fireActionEvent((ActionEvent)event);
140         } else {
141             // Other kinds of events
142
super.broadcastEvent(event);
143         }
144     }
145 }
146
Popular Tags