<commit_before>/*******************************************************************************
 * Copyright (c) 2013, 2014 Red Hat, Inc. 
 * 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:
 * 		 Red Hat Inc. - initial API and implementation and/or initial documentation
 *******************************************************************************/
package org.eclipse.thym.ui.launch;

import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.thym.core.HybridProject;
import org.eclipse.thym.core.HybridProjectLaunchConfigConstants;
import org.eclipse.thym.ui.HybridUI;
import org.eclipse.thym.ui.internal.status.StatusManager;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ide.ResourceUtil;
/**
 * General hybrid mobile project launch shortcut.
 * 
 * @author <NAME>
 *
 */
public abstract class HybridProjectLaunchShortcut implements ILaunchShortcut{

	@Override
	public void launch(ISelection selection, String mode) {
			IStructuredSelection ssel = (IStructuredSelection) selection;
			Object selected = ssel.getFirstElement();
			IResource res = (IResource) selected;
			launch(res.getProject());
	}

	@Override
	public void launch(IEditorPart editor, String mode) {
		IFile file = ResourceUtil.getFile(editor.getEditorInput());
		if (file != null) {
			IProject project = file.getProject();
			launch(project);
		}
	}

	private void launch(IProject project) {
		try {
			if( !validateBuildToolsReady() ||
					!shouldProceedWithLaunch(HybridProject.getHybridProject(project)))
				return;
			ILaunchConfiguration launchConfig = findOrCreateLaunchConfiguration(project);
			ILaunchConfigurationWorkingCopy wc = launchConfig.getWorkingCopy();
			updateLaunchConfiguration(wc);
			launchConfig = wc.doSave();
			DebugUITools.launch(launchConfig, "run");
			
		} catch (CoreException e) {
			if (e.getCause() instanceof IOException) {
				Status status = new Status(IStatus.ERROR, HybridUI.PLUGIN_ID,
						"Unable to complete the build for target plarform",
						e.getCause());
				StatusManager.handle(status);
			}else{
				StatusManager.handle(e);
			}
		}
	}
	
	
	
	/**
	 * Creates a new launch configuration for the given project if one does not exists.
	 * 
	 * @param project
	 * @return
	 * @throws CoreException
	 */
	private ILaunchConfiguration findOrCreateLaunchConfiguration(IProject project) throws CoreException{
		ILaunchManager lm = getLaunchManager();
		ILaunchConfigurationType configType = getLaunchConfigurationType();
		ILaunchConfiguration[] confs = lm.getLaunchConfigurations(configType);
		for (ILaunchConfiguration configuration : confs) {
			if(isCorrectLaunchConfiguration(project, configuration)){
				return configuration;
			}
		}<commit_msg>bug 472423 - ClassCastException in HybridProjectLaunchShortcut<commit_after>/*******************************************************************************
 * Copyright (c) 2013, 2014 Red Hat, Inc. 
 * 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:
 * 		 Red Hat Inc. - initial API and implementation and/or initial documentation
 *******************************************************************************/
package org.eclipse.thym.ui.launch;

import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.thym.core.HybridProject;
import org.eclipse.thym.core.HybridProjectLaunchConfigConstants;
import org.eclipse.thym.ui.HybridUI;
import org.eclipse.thym.ui.internal.status.StatusManager;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.ResourceUtil;
/**
 * General hybrid mobile project launch shortcut.
 * 
 * @author <NAME>
 *
 */
public abstract class HybridProjectLaunchShortcut implements ILaunchShortcut{

	@Override
	public void launch(ISelection selection, String mode) {
			IStructuredSelection ssel = (IStructuredSelection) selection;
			Object selected = ssel.getFirstElement();
			if(selected instanceof IAdaptable){
				IAdaptable adaptable = (IAdaptable) selected;
				IProject project = adaptable.getAdapter(IProject.class);
				if(project == null ){
					IResource resource = adaptable.getAdapter(IResource.class);
					if(resource != null ){
						project = resource.getProject();
					}
				}
				if(project != null){
					launch(project);
				}else{
					showEmptyError("Unable to determine a project to launch from selection.");
				}
			}
	}

	@Override
	public void launch(IEditorPart editor, String mode) {
		IFile file = ResourceUtil.getFile(editor.getEditorInput());
		if (file != null) {
			IProject project = file.getProject();
			launch(project);
		}
		else{
			showEmptyError("Unable to determine the project to launch for from the editor.");
		}
	}

	private void launch(IProject project) {
		try {
			if( !validateBuildToolsReady() ||
					!shouldProceedWithLaunch(HybridProject.getHybridProject(project)))
				return;
			ILaunchConfiguration launchConfig = findOrCreateLaunchConfiguration(project);
			ILaunchConfigurationWorkingCopy wc = launchConfig.getWorkingCopy();
			updateLaunchConfiguration(wc);
			launchConfig = wc.doSave();
			DebugUITools.launch(launchConfig, "run");
			
		} catch (CoreException e) {
			if (e.getCause() instanceof IOException) {
				Status status = new Status(IStatus.ERROR, HybridUI.PLUGIN_ID,
						"Unable to complete the build for target plarform",
						e.getCause());
				StatusManager.handle(status);
			}else{
				StatusManager.handle(e);
			}
		}
	}
	
	private void showEmptyError(String message){
		MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 
				"Launch Failed", message);
	}
	
	
	/**
	 * Creates a new launch configuration for the given project if one does not exists.
	 * 
	 * @param project
	 * @return
	 * @throws CoreException
	 */
	private ILaunchConfiguration findOrCreateLaunchConfiguration(IProject project) throws CoreException{
		ILaunchManager lm = getLaunchManager();
		ILaunchConfigurationType configType = getLaunchConfigurationType();
		ILaunchConfiguration[] confs = lm.getLaunchConfigurations(configType);
		for (ILaunchConfiguration configuration : confs) {
			if(isCorrectLaunchConfiguration(project, configuration)){
				return configuration;
			}
		}
{'fraction_non_alphanumeric': 0.13428430890309886, 'fraction_numerical': 0.0036891293654697493, 'mean_word_length': 5.042347696879643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '264077', 'n_tokens_mistral': 2562, 'n_tokens_neox': 2241, 'n_words': 523}