com.cloudbees.eclipse.run.ui.popup.actions.StopAction.java Source code

Java tutorial

Introduction

Here is the source code for com.cloudbees.eclipse.run.ui.popup.actions.StopAction.java

Source

/*******************************************************************************
 * Copyright (c) 2013 Cloud Bees, 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:
 *    Cloud Bees, Inc. - initial API and implementation 
 *******************************************************************************/
package com.cloudbees.eclipse.run.ui.popup.actions;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.internal.ObjectPluginAction;

import com.cloudbees.api.ApplicationInfo;
import com.cloudbees.eclipse.run.core.BeesSDK;
import com.cloudbees.eclipse.run.ui.CBRunUiActivator;
import com.cloudbees.eclipse.ui.CloudBeesUIPlugin;

@SuppressWarnings("restriction")
public class StopAction implements IObjectActionDelegate {

    private final class IRunnableWithProgressImplementation implements IRunnableWithProgress {
        private final ISelection selection;

        private IRunnableWithProgressImplementation(ISelection selection) {
            this.selection = selection;
        }

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            monitor.beginTask("Stopping RUN@cloud server", 1);
            Object firstElement = ((StructuredSelection) this.selection).getFirstElement();

            if (firstElement instanceof ApplicationInfo) {
                try {
                    ApplicationInfo appInfo = (ApplicationInfo) firstElement;

                    String id = appInfo.getId();
                    int i = id.indexOf("/");
                    BeesSDK.stop(id.substring(0, i), id.substring(i + 1));
                    monitor.done();

                } catch (Exception e) {
                    CBRunUiActivator.logErrorAndShowDialog(e);
                    monitor.done();
                }
            }
        }
    }

    /**
     * Constructor for Action1.
     */
    public StopAction() {
        super();
    }

    /**
     * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
     */
    @Override
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    }

    /**
     * @see IActionDelegate#run(IAction)
     */
    @Override
    public void run(IAction action) {
        if (action instanceof ObjectPluginAction) {

            final ISelection selection = ((ObjectPluginAction) action).getSelection();

            if (selection instanceof StructuredSelection) {
                ProgressMonitorDialog monitor = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
                try {
                    monitor.run(false, false, new IRunnableWithProgressImplementation(selection));
                    CloudBeesUIPlugin.getDefault().fireApplicationInfoChanged();
                } catch (Exception e) {
                    CBRunUiActivator.logError(e);
                }
            }
        }
    }

    /**
     * @see IActionDelegate#selectionChanged(IAction, ISelection)
     */
    @Override
    public void selectionChanged(IAction action, ISelection selection) {
    }

}