KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > AbstractSubmit


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

15 package org.apache.tapestry.form;
16
17 import java.util.Collection JavaDoc;
18
19 import org.apache.tapestry.IActionListener;
20 import org.apache.tapestry.IForm;
21 import org.apache.tapestry.IMarkupWriter;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.listener.ListenerInvoker;
24
25 /**
26  * Superclass for components submitting their form.
27  *
28  * @author Richard Lewis-Shell
29  * @since 4.0
30  */

31
32 abstract class AbstractSubmit extends AbstractFormComponent
33 {
34     /**
35      * Determine if this submit component was clicked.
36      *
37      * @param cycle
38      * @param name
39      * @return true if this submit was clicked
40      */

41     protected abstract boolean isClicked(IRequestCycle cycle, String JavaDoc name);
42
43     /**
44      * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
45      */

46     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
47     {
48         if (isClicked(cycle, getName()))
49             handleClick(cycle, getForm());
50     }
51
52     void handleClick(final IRequestCycle cycle, IForm form)
53     {
54         if (isParameterBound("selected"))
55             setSelected(getTag());
56
57         final IActionListener listener = getListener();
58
59         if (listener == null)
60             return;
61
62         final ListenerInvoker listenerInvoker = getListenerInvoker();
63
64         Object JavaDoc parameters = getParameters();
65         if (parameters != null)
66         {
67             if (parameters instanceof Collection JavaDoc)
68             {
69                 cycle.setListenerParameters(((Collection JavaDoc) parameters).toArray());
70             }
71             else
72             {
73                 cycle.setListenerParameters(new Object JavaDoc[]
74                 { parameters });
75             }
76         }
77
78         // Have a listener; notify it now, or defer for later?
79

80         Runnable JavaDoc notify = new Runnable JavaDoc()
81         {
82             public void run()
83             {
84                 listenerInvoker.invokeListener(listener, AbstractSubmit.this, cycle);
85             }
86         };
87
88         if (getDefer())
89             form.addDeferredRunnable(notify);
90         else
91             notify.run();
92     }
93
94     /** parameter */
95     public abstract IActionListener getListener();
96
97     /** parameter */
98     public abstract Object JavaDoc getTag();
99
100     /** parameter */
101     public abstract void setSelected(Object JavaDoc tag);
102
103     /** parameter */
104     public abstract boolean getDefer();
105
106     /** parameter */
107     public abstract Object JavaDoc getParameters();
108
109     /** Injected */
110     public abstract ListenerInvoker getListenerInvoker();
111 }
112
Popular Tags