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.ajax.markup.html;
018:
019: import org.apache.wicket.ajax.AjaxEventBehavior;
020: import org.apache.wicket.ajax.AjaxRequestTarget;
021: import org.apache.wicket.ajax.IAjaxCallDecorator;
022: import org.apache.wicket.ajax.calldecorator.CancelEventIfNoAjaxDecorator;
023: import org.apache.wicket.markup.ComponentTag;
024: import org.apache.wicket.markup.html.link.Link;
025: import org.apache.wicket.model.IModel;
026:
027: /**
028: * An ajax link that will degrade to a normal request if ajax is not available
029: * or javascript is disabled
030: *
031: * @since 1.2
032: *
033: * @author Igor Vaynberg (ivaynberg)
034: *
035: */
036: public abstract class AjaxFallbackLink extends Link implements
037: IAjaxLink {
038: /** */
039: private static final long serialVersionUID = 1L;
040:
041: /**
042: * Construct.
043: *
044: * @param id
045: */
046: public AjaxFallbackLink(final String id) {
047: this (id, null);
048: }
049:
050: /**
051: * Construct.
052: *
053: * @param id
054: * @param model
055: */
056: public AjaxFallbackLink(final String id, final IModel model) {
057: super (id, model);
058:
059: add(new AjaxEventBehavior("onclick") {
060: private static final long serialVersionUID = 1L;
061:
062: protected void onEvent(AjaxRequestTarget target) {
063: onClick(target);
064: }
065:
066: protected IAjaxCallDecorator getAjaxCallDecorator() {
067: return new CancelEventIfNoAjaxDecorator(
068: AjaxFallbackLink.this .getAjaxCallDecorator());
069: }
070:
071: protected void onComponentTag(ComponentTag tag) {
072: // only render handler if link is enabled
073: if (isLinkEnabled()) {
074: super .onComponentTag(tag);
075: }
076: }
077: });
078: }
079:
080: protected IAjaxCallDecorator getAjaxCallDecorator() {
081: return null;
082: }
083:
084: /**
085: *
086: * @see org.apache.wicket.markup.html.link.Link#onClick()
087: */
088: public final void onClick() {
089: onClick(null);
090: }
091:
092: /**
093: * Callback for the onClick event. If ajax failed and this event was
094: * generated via a normal link the target argument will be null
095: *
096: * @param target
097: * ajax target if this linked was invoked using ajax, null
098: * otherwise
099: */
100: public abstract void onClick(final AjaxRequestTarget target);
101: }
|