Package com.cedarsoft.spring.rcp.commands

Source Code of com.cedarsoft.spring.rcp.commands.DeleteCommand$Callback

package com.cedarsoft.spring.rcp.commands;

import com.cedarsoft.spring.SpringSupport;
import com.cedarsoft.spring.rcp.async.BackgroundAction;
import com.cedarsoft.spring.rcp.async.BlockingBackgroundActionRunner;
import com.cedarsoft.spring.rcp.id.IdStrategy;
import com.cedarsoft.spring.rcp.tbpanel.aspects.AddRemoveAspect;
import com.cedarsoft.ObjectAccess;
import com.cedarsoft.ObjectRemove;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.richclient.command.ActionCommand;
import org.springframework.richclient.dialog.ConfirmationDialog;

import java.util.List;

/**
* A delete command.
*/
public class DeleteCommand<T> extends ActionCommand {
  @NotNull
  @NonNls
  public static final String DELETE_COMMAND_ID = "deleteCommand";

  @NotNull
  private final ObjectAccess<? extends T> objectAccess;
  @NotNull
  private final ObjectRemove<T> objectRemove;
  @NotNull
  private final IdStrategy idStrategy;

  public DeleteCommand( @NotNull ObjectAccess<? extends T> objectAccess, @NotNull ObjectRemove<T> objectRemove, @NotNull IdStrategy idStrategy ) {
    super( DELETE_COMMAND_ID );
    this.objectAccess = objectAccess;
    this.objectRemove = objectRemove;
    this.idStrategy = idStrategy;
  }

  @Override
  protected void doExecuteCommand() {
    String title = SpringSupport.INSTANCE.getMessage( DELETE_COMMAND_ID + ".title" );
    String message = SpringSupport.INSTANCE.getMessage( DELETE_COMMAND_ID + ".message" );
    ConfirmationDialog dlg = new ConfirmationDialog( title, message ) {
      @Override
      protected void onConfirm() {
        final List<? extends T> objectsToDelete = objectAccess.getElements();

        new BlockingBackgroundActionRunner( SpringSupport.INSTANCE.getActiveApplicationWindow(), new BackgroundAction( idStrategy.getBaseId() + AddRemoveAspect.KEY_DELETE_BEAN ) {
          @Override
          protected boolean confirm() {
            if ( callback != null ) {
              return callback.confirm();
            }
            return true;
          }

          @Override
          protected void executeInBackground() throws Exception {
            for ( T entry : objectsToDelete ) {
              objectRemove.remove( entry );
              // And notify the rest of the application of the change
              SpringSupport.INSTANCE.publishDeleted( entry );
            }
          }
        } ).run();
      }
    };
    dlg.showDialog();
  }

  @Nullable
  private Callback callback;

  @Nullable
  public Callback getCallback() {
    return callback;
  }

  public void setCallback( @Nullable Callback callback ) {
    this.callback = callback;
  }

  public interface Callback {
    boolean confirm();
  }
}
TOP

Related Classes of com.cedarsoft.spring.rcp.commands.DeleteCommand$Callback

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.