Package org.fusesource.ide.sap.ui.command

Source Code of org.fusesource.ide.sap.ui.command.DeleteHandler

/*******************************************************************************
* Copyright (c) 2014 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
* William Collins punkhornsw@gmail.com
******************************************************************************/
package org.fusesource.ide.sap.ui.command;

import java.util.Collections;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.DeleteCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISources;
import org.eclipse.ui.handlers.HandlerUtil;
import org.fusesource.camel.component.sap.model.rfc.DestinationData;
import org.fusesource.camel.component.sap.model.rfc.RfcPackage;
import org.fusesource.camel.component.sap.model.rfc.ServerData;
import org.fusesource.camel.component.sap.model.rfc.impl.DestinationDataStoreEntryImpl;
import org.fusesource.camel.component.sap.model.rfc.impl.ServerDataStoreEntryImpl;

public class DeleteHandler extends AbstractHandler implements IHandler {

  private EditingDomain editingDomain;
  private CompoundCommand command;

  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    editingDomain.getCommandStack().execute(command);
    return null;
  }

  @Override
  public void setEnabled(Object evaluationContext) {
    Command deleteEntryCommand = null;
    Command removeValueCommand = null;
    setBaseEnabled(false);
    Object obj = HandlerUtil.getVariable(evaluationContext, ISources.ACTIVE_CURRENT_SELECTION_NAME);
    if (obj instanceof IStructuredSelection) {
      IStructuredSelection selection = (IStructuredSelection) obj;
      if (selection.size() == 1) {
        obj = selection.getFirstElement();
        if (obj instanceof EObject) {
          EObject eObject = (EObject) obj;
          editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(eObject);
          if (editingDomain != null) {
            deleteEntryCommand = DeleteCommand.create(editingDomain, Collections.singletonList(eObject));
            if (eObject instanceof DestinationDataStoreEntryImpl) {
              DestinationData destinationData = ((DestinationDataStoreEntryImpl)eObject).getValue();
              removeValueCommand = RemoveCommand.create(editingDomain, destinationData.eContainer(), RfcPackage.Literals.DESTINATION_DATA_STORE__DESTINATION_DATA, Collections.singletonList(destinationData));
            } else if (eObject instanceof ServerDataStoreEntryImpl) {
              ServerData serverData = ((ServerDataStoreEntryImpl)eObject).getValue();
              removeValueCommand = RemoveCommand.create(editingDomain, serverData.eContainer(), RfcPackage.Literals.SERVER_DATA_STORE__SERVER_DATA, Collections.singletonList(serverData));
            }
            if (canDelete(selection)) {
              command = new CompoundCommand();
              command.append(deleteEntryCommand);
              command.append(removeValueCommand);
              setBaseEnabled(true);
            }
          }
        }
      }
    }
  }

  protected boolean canDelete(IStructuredSelection selection) {
    if (selection.size() > 0) {
      for (Object obj: selection.toList()) {
        if (obj instanceof ServerDataStoreEntryImpl || (obj instanceof DestinationDataStoreEntryImpl)) {
          continue;
        }
        return false;
      }
      return true;
    }
    return false;
  }
}
TOP

Related Classes of org.fusesource.ide.sap.ui.command.DeleteHandler

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.