KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > link > DirectLink


1 // Copyright 2004, 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.link;
16
17 import java.util.List JavaDoc;
18
19 import org.apache.tapestry.IActionListener;
20 import org.apache.tapestry.IDirect;
21 import org.apache.tapestry.IRequestCycle;
22 import org.apache.tapestry.Tapestry;
23 import org.apache.tapestry.engine.DirectServiceParameter;
24 import org.apache.tapestry.engine.ILink;
25 import org.apache.tapestry.listener.ListenerInvoker;
26
27 /**
28  * A component for creating a link using the direct service; used for actions that are not dependant
29  * on dynamic page state. [ <a HREF="../../../../../ComponentReference/DirectLink.html">Component
30  * Reference </a>]
31  *
32  * @author Howard Lewis Ship
33  */

34
35 public abstract class DirectLink extends AbstractLinkComponent implements IDirect
36 {
37     public abstract IActionListener getListener();
38
39     /**
40      * Returns true if the stateful parameter is bound to a true value. If stateful is not bound,
41      * also returns the default, true.
42      */

43
44     public abstract boolean isStateful();
45
46     public ILink getLink(IRequestCycle cycle)
47     {
48         Object JavaDoc[] serviceParameters = constructServiceParameters(getParameters());
49
50         DirectServiceParameter dsp = new DirectServiceParameter(this, serviceParameters);
51
52         return getLink(cycle, Tapestry.DIRECT_SERVICE, dsp);
53     }
54
55     /**
56      * Converts a service parameters value to an array of objects. This is used by the
57      * {@link DirectLink},{@link ServiceLink}and {@link ExternalLink}components.
58      *
59      * @param parameterValue
60      * the input value which may be
61      * <ul>
62      * <li>null (returns null)
63      * <li>An array of Object (returns the array)
64      * <li>A {@link List}(returns an array of the values in the List})
65      * <li>A single object (returns the object as a single-element array)
66      * </ul>
67      * @return An array representation of the input object.
68      * @since 2.2
69      */

70
71     public static Object JavaDoc[] constructServiceParameters(Object JavaDoc parameterValue)
72     {
73         if (parameterValue == null)
74             return null;
75
76         if (parameterValue instanceof Object JavaDoc[])
77             return (Object JavaDoc[]) parameterValue;
78
79         if (parameterValue instanceof List JavaDoc)
80         {
81             List JavaDoc list = (List JavaDoc) parameterValue;
82
83             return list.toArray();
84         }
85
86         return new Object JavaDoc[]
87         { parameterValue };
88     }
89
90     /**
91      * Invoked by the direct service to trigger the application-specific action by notifying the
92      * {@link IActionListener listener}.
93      *
94      * @throws org.apache.tapestry.StaleSessionException
95      * if the component is stateful, and the session is new.
96      */

97
98     public void trigger(IRequestCycle cycle)
99     {
100         IActionListener listener = getListener();
101
102         if (listener == null)
103             throw Tapestry.createRequiredParameterException(this, "listener");
104
105         getListenerInvoker().invokeListener(listener, this, cycle);
106     }
107
108     /** @since 2.2 * */
109
110     public abstract Object JavaDoc getParameters();
111
112     /**
113      * Injected.
114      *
115      * @since 4.0
116      */

117
118     public abstract ListenerInvoker getListenerInvoker();
119 }
Popular Tags