cn.dockerfoundry.ide.eclipse.server.ui.internal.DebugUIProvider.java Source code

Java tutorial

Introduction

Here is the source code for cn.dockerfoundry.ide.eclipse.server.ui.internal.DebugUIProvider.java

Source

/*******************************************************************************
 * Copyright (c) 2015 Pivotal Software, Inc. 
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of 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.
 *  
 *  Contributors:
 *     Pivotal Software, Inc. - initial API and implementation
 ********************************************************************************/
package cn.dockerfoundry.ide.eclipse.server.ui.internal;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wst.server.core.IModule;

import cn.dockerfoundry.ide.eclipse.server.core.internal.ApplicationAction;
import cn.dockerfoundry.ide.eclipse.server.core.internal.DockerFoundryServer;
import cn.dockerfoundry.ide.eclipse.server.core.internal.client.DockerFoundryApplicationModule;
import cn.dockerfoundry.ide.eclipse.server.core.internal.debug.DockerFoundryProperties;
import cn.dockerfoundry.ide.eclipse.server.core.internal.debug.DebugConnectionDescriptor;
import cn.dockerfoundry.ide.eclipse.server.core.internal.debug.IDebugProvider;

/**
 * Wrapper around a {@link IDebugProvider} that performs UI-aware provider
 * operations, for example prompting the user for additional information via
 * dialogues.
 */
class DebugUIProvider implements IDebugProvider {

    private final IDebugProvider provider;

    public DebugUIProvider(IDebugProvider provider) {
        this.provider = provider;
    }

    @Override
    public DebugConnectionDescriptor getDebugConnectionDescriptor(DockerFoundryApplicationModule appModule,
            DockerFoundryServer cloudServer, IProgressMonitor monitor) throws CoreException {
        return provider.getDebugConnectionDescriptor(appModule, cloudServer, monitor);
    }

    @Override
    public boolean canLaunch(DockerFoundryApplicationModule appModule, DockerFoundryServer cloudServer,
            IProgressMonitor monitor) throws CoreException {
        return provider.canLaunch(appModule, cloudServer, monitor);
    }

    @Override
    public boolean isDebugSupported(DockerFoundryApplicationModule appModule, DockerFoundryServer cloudServer) {
        return provider.isDebugSupported(appModule, cloudServer);
    }

    @Override
    public String getLaunchConfigurationID() {
        return provider.getLaunchConfigurationID();
    }

    @Override
    public boolean configureApp(DockerFoundryApplicationModule appModule, DockerFoundryServer cloudServer,
            IProgressMonitor monitor) throws CoreException {
        IModule[] mod = new IModule[] { appModule.getLocalModule() };

        boolean shouldRestart = DockerFoundryProperties.isModuleStopped.testProperty(mod, cloudServer);

        // If the application cannot yet launch then prompt the user to restart
        // the app in order for
        // any changes that enables debug to be set in the Cloud container
        if (!provider.canLaunch(appModule, cloudServer, monitor)) {

            final boolean[] shouldProceed = new boolean[] { true };

            // Ask if the module should be restarted in its current state.
            Display.getDefault().syncExec(new Runnable() {

                public void run() {
                    Shell shell = CloudUiUtil.getShell();
                    shouldProceed[0] = MessageDialog.openQuestion(shell, Messages.DebugUIProvider_DEBUG_TITLE,
                            Messages.DebugUIProvider_DEBUG_APP_RESTART_MESSAGE);
                }

            });

            shouldRestart = shouldProceed[0];
            if (!shouldProceed[0]) {
                return false;
            }
        }
        provider.configureApp(appModule, cloudServer, monitor);

        if (shouldRestart) {
            // Perform a full push and start
            cloudServer.getBehaviour().operations().applicationDeployment(mod, ApplicationAction.START)
                    .run(monitor);
        }
        return true;

    }
}