Package com.cedarsoft.wicket.confirm

Source Code of com.cedarsoft.wicket.confirm.ConfirmationLink

/**
* 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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;

/**
* A confirmation link that uses JavaScript if available.
*/
public class ConfirmationLink extends AjaxFallbackLink<Object> {
  @NotNull
  @NonNls
  private final Model<? extends String> message;

  @NotNull
  private final Action action;

  public ConfirmationLink( @NotNull @NonNls String id, @NotNull @NonNls Model<? extends String> message, @NotNull 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 );
  }
}
TOP

Related Classes of com.cedarsoft.wicket.confirm.ConfirmationLink

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.