descent.internal.ui.refactoring.reorg.DeleteAction.java Source code

Java tutorial

Introduction

Here is the source code for descent.internal.ui.refactoring.reorg.DeleteAction.java

Source

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package descent.internal.ui.refactoring.reorg;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.DeleteResourceAction;

import descent.internal.corext.refactoring.RefactoringAvailabilityTester;
import descent.internal.corext.refactoring.RefactoringExecutionStarter;
import descent.internal.corext.refactoring.reorg.ReorgUtils;
import descent.internal.corext.util.JavaModelUtil;
import descent.internal.ui.IJavaHelpContextIds;
import descent.internal.ui.JavaPlugin;
import descent.internal.ui.refactoring.RefactoringMessages;
import descent.internal.ui.util.ExceptionHandler;
import descent.ui.actions.SelectionDispatchAction;

public class DeleteAction extends SelectionDispatchAction {

    public DeleteAction(IWorkbenchSite site) {
        super(site);
        setText(ReorgMessages.DeleteAction_3);
        setDescription(ReorgMessages.DeleteAction_4);
        ISharedImages workbenchImages = JavaPlugin.getDefault().getWorkbench().getSharedImages();
        setDisabledImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED));
        setImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
        setHoverImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));

        PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.DELETE_ACTION);
    }

    /*
     * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
     */
    public void selectionChanged(IStructuredSelection selection) {
        if (ReorgUtils.containsOnlyProjects(selection.toList())) {
            setEnabled(createWorkbenchAction(selection).isEnabled());
            return;
        }
        try {
            setEnabled(RefactoringAvailabilityTester.isDeleteAvailable(selection.toArray()));
        } catch (CoreException e) {
            //no ui here - this happens on selection changes
            // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
            if (JavaModelUtil.isExceptionToBeLogged(e))
                JavaPlugin.log(e);
            setEnabled(false);
        }
    }

    private IAction createWorkbenchAction(IStructuredSelection selection) {
        DeleteResourceAction action = new DeleteResourceAction(getShell());
        action.selectionChanged(selection);
        return action;
    }

    /* (non-Javadoc)
     * @see descent.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
     */
    public void run(IStructuredSelection selection) {
        if (ReorgUtils.containsOnlyProjects(selection.toList())) {
            createWorkbenchAction(selection).run();
            return;
        }
        try {
            RefactoringExecutionStarter.startDeleteRefactoring(selection.toArray(), getShell());
        } catch (CoreException e) {
            ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring,
                    RefactoringMessages.OpenRefactoringWizardAction_exception);
        }
    }
}