/**
* Copyright (C) cedarsoft GmbH.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cedarsoft.wicket.confirm;
import com.cedarsoft.wicket.JavaScriptEventConfirmation;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.model.Model;
import org.jetbrains.annotations.NonNls;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Nullable;
import java.io.Serializable;
/**
* A confirmation link that uses JavaScript if available.
*/
public class ConfirmationLink extends AjaxFallbackLink<Object> {
@Nonnull
@NonNls
private final Model<? extends String> message;
@Nonnull
private final Action action;
public ConfirmationLink( @Nonnull @NonNls String id, @Nonnull @NonNls Model<? extends String> message, @Nonnull Action action ) {
super( id );
this.message = message;
this.action = action;
add( new JavaScriptEventConfirmation( JavaScriptEventConfirmation.EVENT_ON_CLICK, message ) );
}
@Override
public void onClick( @Nullable AjaxRequestTarget target ) {
if ( target == null ) {
//Fallback!
MarkupContainer parent = getParent();
if ( parent == null ) {
throw new IllegalStateException( "No parent found!" );
}
FallbackConfirmationLinkPanel.replace( parent, message, action );
} else {
//Javascript
action.execute( target );
}
}
public interface Action extends Serializable {
/**
* Executes the action
* @param target the ajax request target (if there is one)
*/
void execute( @Nullable AjaxRequestTarget target );
}
}