|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DirectLink.java | 87.5% | 93.3% | 100% | 92.3% |
|
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; | |
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 | 25 | public ILink getLink(IRequestCycle cycle) |
47 | { | |
48 | 25 | Object[] serviceParameters = constructServiceParameters(getParameters()); |
49 | ||
50 | 25 | DirectServiceParameter dsp = new DirectServiceParameter(this, serviceParameters); |
51 | ||
52 | 25 | 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 | 61 | public static Object[] constructServiceParameters(Object parameterValue) |
72 | { | |
73 | 61 | if (parameterValue == null) |
74 | 48 | return null; |
75 | ||
76 | 13 | if (parameterValue instanceof Object[]) |
77 | 2 | return (Object[]) parameterValue; |
78 | ||
79 | 11 | if (parameterValue instanceof List) |
80 | { | |
81 | 8 | List list = (List) parameterValue; |
82 | ||
83 | 8 | return list.toArray(); |
84 | } | |
85 | ||
86 | 3 | return new Object[] |
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 | 23 | public void trigger(IRequestCycle cycle) |
99 | { | |
100 | 23 | IActionListener listener = getListener(); |
101 | ||
102 | 23 | if (listener == null) |
103 | 0 | throw Tapestry.createRequiredParameterException(this, "listener"); |
104 | ||
105 | 23 | getListenerInvoker().invokeListener(listener, this, cycle); |
106 | } | |
107 | ||
108 | /** @since 2.2 * */ | |
109 | ||
110 | public abstract Object getParameters(); | |
111 | ||
112 | /** | |
113 | * Injected. | |
114 | * | |
115 | * @since 4.0 | |
116 | */ | |
117 | ||
118 | public abstract ListenerInvoker getListenerInvoker(); | |
119 | } |
|