001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.behavior;
018:
019: import org.apache.wicket.Component;
020: import org.apache.wicket.RequestListenerInterface;
021: import org.apache.wicket.Response;
022: import org.apache.wicket.markup.ComponentTag;
023: import org.apache.wicket.markup.html.IHeaderContributor;
024: import org.apache.wicket.markup.html.IHeaderResponse;
025: import org.apache.wicket.protocol.http.request.WebRequestCodingStrategy;
026: import org.apache.wicket.util.string.AppendingStringBuffer;
027:
028: /**
029: * Abstract class for handling Ajax roundtrips. This class serves as a base for
030: * javascript specific implementations, like ones based on Dojo or
031: * Scriptaculous, or Wicket's default.
032: *
033: * @author Eelco Hillenius
034: * @author Ralf Ebert
035: * @author Igor Vaynberg
036: */
037: public abstract class AbstractAjaxBehavior extends AbstractBehavior
038: implements IBehaviorListener, IHeaderContributor {
039: /** the component that this handler is bound to. */
040: private Component component;
041:
042: /**
043: * Construct.
044: */
045: public AbstractAjaxBehavior() {
046: }
047:
048: /**
049: * Bind this handler to the given component.
050: *
051: * @param hostComponent
052: * the component to bind to
053: */
054: public final void bind(final Component hostComponent) {
055: if (hostComponent == null) {
056: throw new IllegalArgumentException(
057: "Argument hostComponent must be not null");
058: }
059:
060: if (this .component != null) {
061: throw new IllegalStateException(
062: "this kind of handler cannot be attached to "
063: + "multiple components; it is already attached to component "
064: + this .component + ", but component "
065: + hostComponent
066: + " wants to be attached too");
067:
068: }
069:
070: this .component = hostComponent;
071:
072: // call the calback
073: onBind();
074: }
075:
076: /**
077: * Gets the url that references this handler.
078: *
079: * @return the url that references this handler
080: */
081: public CharSequence getCallbackUrl() {
082: return getCallbackUrl(true);
083: }
084:
085: /**
086: * Gets the url that references this handler.
087: *
088: * @param onlyTargetActivePage
089: * if true the callback to this behavior will be ignore if the
090: * page is not the last one the user accessed
091: *
092: * @return the url that references this handler
093: */
094: public final CharSequence getCallbackUrl(
095: final boolean onlyTargetActivePage) {
096: if (getComponent() == null) {
097: throw new IllegalArgumentException(
098: "Behavior must be bound to a component to create the URL");
099: }
100:
101: final RequestListenerInterface rli;
102:
103: rli = IBehaviorListener.INTERFACE;
104:
105: AppendingStringBuffer url = new AppendingStringBuffer(
106: getComponent().urlFor(this , rli));
107:
108: if (onlyTargetActivePage) {
109: url
110: .append("&")
111: .append(
112: WebRequestCodingStrategy.IGNORE_IF_NOT_ACTIVE_PARAMETER_NAME)
113: .append("=true");
114: }
115:
116: return url;
117: }
118:
119: /**
120: * @see org.apache.wicket.behavior.IBehavior#onComponentTag(org.apache.wicket.Component,
121: * org.apache.wicket.markup.ComponentTag)
122: */
123: public final void onComponentTag(final Component component,
124: final ComponentTag tag) {
125: onComponentTag(tag);
126: }
127:
128: /**
129: * @see org.apache.wicket.behavior.AbstractBehavior#onRendered(org.apache.wicket.Component)
130: */
131: public final void onRendered(final Component hostComponent) {
132: onComponentRendered();
133: }
134:
135: /**
136: * @see org.apache.wicket.markup.html.IHeaderContributor#renderHead(IHeaderResponse)
137: */
138: public void renderHead(final IHeaderResponse response) {
139: }
140:
141: /**
142: * Gets the component that this handler is bound to.
143: *
144: * @return the component that this handler is bound to
145: */
146: protected final Component getComponent() {
147: return component;
148: }
149:
150: /**
151: * Called any time a component that has this handler registered is rendering
152: * the component tag. Use this method e.g. to bind to javascript event
153: * handlers of the tag
154: *
155: * @param tag
156: * the tag that is rendered
157: */
158: protected void onComponentTag(final ComponentTag tag) {
159: }
160:
161: /**
162: * Called when the component was bound to it's host component. You can get
163: * the bound host component by calling getComponent.
164: */
165: protected void onBind() {
166: }
167:
168: /**
169: * Called to indicate that the component that has this handler registered
170: * has been rendered. Use this method to do any cleaning up of temporary
171: * state
172: */
173: protected void onComponentRendered() {
174: }
175:
176: /**
177: * @see org.apache.wicket.behavior.AbstractBehavior#getStatelessHint(Component)
178: */
179: public boolean getStatelessHint(Component component) {
180: return false;
181: }
182:
183: // TODO the next three methods will be removed with next commit. Here as
184: // final to help with refactoring
185:
186: protected final String getImplementationId() {
187: return "foo";
188: }
189:
190: protected final void onRenderHeadContribution(
191: final Response response) {
192: }
193:
194: protected final void onRenderHeadInitContribution(
195: final Response response) {
196: }
197: }
|