KickJava   Java API By Example, From Geeks To Geeks.

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


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

40 public class Action extends AbstractWidget {
41     protected ActionDefinition definition;
42
43     public Action(ActionDefinition definition) {
44         this.definition = definition;
45         setLocation(definition.getLocation());
46     }
47
48     public String JavaDoc getId() {
49         return definition.getId();
50     }
51
52     public void readFromRequest(final FormContext formContext) {
53         Form form = getForm();
54         
55         // Set the submit widget if we can determine it from the request
56
String JavaDoc fullId = getFullyQualifiedId();
57         Request request = formContext.getRequest();
58         
59         String JavaDoc value = request.getParameter(fullId);
60         if (value != null && value.length() > 0) {
61             form.setSubmitWidget(this);
62             
63         } else {
64             // Special workaround an IE bug for <input type="image" name="foo"> :
65
// in that case, IE only sends "foo.x" and "foo.y" and not "foo" whereas
66
// standards-compliant browsers such as Mozilla do send the "foo" parameter.
67
//
68
// Note that since actions are terminal widgets, there's no chance of conflict
69
// with a child "x" or "y" widget.
70
value = request.getParameter(fullId + ".x");
71             if ((value != null) && value.length() > 0) {
72                 form.setSubmitWidget(this);
73             }
74         }
75         
76         if (form.getSubmitWidget() == this) {
77             form.addWidgetEvent(new ActionEvent(this, definition.getActionCommand()));
78             
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      * @todo is there a use case for actions having validators?
96      */

97     public boolean validate(FormContext formContext) {
98         return true;
99     }
100
101     private static final String JavaDoc ACTION_EL = "action";
102
103     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
104         AttributesImpl buttonAttrs = new AttributesImpl();
105         buttonAttrs.addCDATAAttribute("id", getFullyQualifiedId());
106         contentHandler.startElement(Constants.WI_NS, ACTION_EL, Constants.WI_PREFIX_COLON + ACTION_EL, buttonAttrs);
107         // generate label, help, hint, etc.
108
definition.generateDisplayData(contentHandler);
109         contentHandler.endElement(Constants.WI_NS, ACTION_EL, Constants.WI_PREFIX_COLON + ACTION_EL);
110     }
111
112     public void generateLabel(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
113         definition.generateLabel(contentHandler);
114     }
115     
116     public void broadcastEvent(WidgetEvent event) {
117         this.definition.fireActionEvent((ActionEvent)event);
118     }
119 }
120
Popular Tags