⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emulatorlaunchconfigdelegate.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2004 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.internal.launching;

import java.io.File;
import java.util.HashMap;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
import org.eclipse.jdt.launching.IVMRunner;
import org.eclipse.jdt.launching.VMRunnerConfiguration;

import eclipseme.core.EclipseMECoreStrings;
import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.launching.ILaunchConstants;
import eclipseme.core.model.ApplicationDescriptor;
import eclipseme.core.model.IMidletSuiteProject;
import eclipseme.core.model.MidletSuiteFactory;
import eclipseme.core.model.device.DeviceRegistry;
import eclipseme.core.model.device.IDevice;
import eclipseme.core.persistence.PersistenceException;


/**
 * Launch configuration for launching a Wireless Toolkit emulator
 * that launches as an executable rather than a Java class.
 * <p />
 * Copyright (c) 2003 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.8 $
 * <br>
 * $Date: 2006/02/11 21:26:57 $
 * <br>
 * @author Craig Setera
 */
public class EmulatorLaunchConfigDelegate extends
	AbstractJavaLaunchConfigurationDelegate 
{
	/** Status code for which the no midlet status is registered. */
	private static final IStatus NO_MIDLET_STATUS = new Status(
			IStatus.ERROR, 
			IEclipseMECoreConstants.PLUGIN_ID, 
			IEclipseMECoreConstants.ERR_OTA_NO_MIDLETS, 
			"", null);
	
	/**
	 * Construct a new Executable emulator launch config delegate. 
	 */
	public EmulatorLaunchConfigDelegate() {
		super();
	}

	/**
	 * @see eclipseme.core.launching.AbstractEmulatorLaunchConfigDelegate#getVMRunner(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
	 */
	protected IVMRunner getVMRunner(
			ILaunchConfiguration launchConfig,
			IDevice device,
			String mode) 
		throws CoreException 
	{
		// Set up the VM runner
		IMidletSuiteProject suite = getMidletSuite(launchConfig);
		return new EmulatorRunner(suite, device, mode);
	}

	/**
	 * @see eclipseme.core.launching.AbstractEmulatorLaunchConfigDelegate#getVMRunnerConfiguration(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
	 */
	protected VMRunnerConfiguration getVMRunnerConfiguration(
		ILaunchConfiguration launchConfig, 
		IDevice device,
		String mode) 
			throws CoreException 
	{
		File workingDir = verifyWorkingDirectory(launchConfig);
		String workingDirName = null;
		if (workingDir != null) {
			workingDirName = workingDir.getAbsolutePath();
		}
		
		// Create VM config
		VMRunnerConfiguration runConfig = 
			new VMRunnerConfiguration(device.getName(), new String[0]);
		runConfig.setVMArguments(new String[0]);
		runConfig.setWorkingDirectory(workingDirName);
		runConfig.setVMSpecificAttributesMap(new HashMap());

		return runConfig;
	}

	/**
	 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void launch(
			ILaunchConfiguration launchConfig, 
			String mode, 
			ILaunch launch, 
			IProgressMonitor monitor) 
				throws CoreException 
	{
		// Make sure we have a progress monitor
		if (monitor == null) {
			monitor = new NullProgressMonitor();
		}
		
		// Get the platform definition and test that it isn't
		// the unspecified platform definition
		IDevice device = getDevice(launchConfig);
		if (device == null) {
			// The platform definition isn't available, so we can't
			// launch
			EclipseMECorePlugin.throwCoreException(
					IStatus.ERROR, -999, 
					"Device is unspecified or unavailable");
		}
		
		monitor.beginTask(
			EclipseMECoreStrings.getString(
				"launchdelegate.launching", 
				new Object[] { launchConfig.getName() }), 
			3);
		
		// check for cancellation
		if (monitor.isCanceled()) {
			return;
		}
		
		// Set up the VM runner
		monitor.subTask(
				EclipseMECoreStrings.getString("launchdelegate.verifying_attrs"));
		IVMRunner runner = getVMRunner(launchConfig, device, mode);
		
		// Create VM config
		VMRunnerConfiguration runConfig = 
			getVMRunnerConfiguration(launchConfig, device, mode);
		
		// check for cancellation
		if (monitor.isCanceled()) {
			return;
		}		
		
		// done the verification phase
		monitor.worked(1);
		
		// Handle the source locator support
		monitor.subTask(
				EclipseMECoreStrings.getString("launchdelegate.source_locator"));
	
		setDefaultSourceLocator(launch, launchConfig);
		monitor.worked(1);		
		
		// Launch the configuration - 1 unit of work
		((EmulatorRunner) runner).run(runConfig, launchConfig, launch, monitor);
		
		// check for cancellation
		if (!monitor.isCanceled()) {
			monitor.done();
		}	
	}

	/**
	 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate2#preLaunchCheck(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
	 */
	public boolean preLaunchCheck(
		ILaunchConfiguration configuration,
		String mode, 
		IProgressMonitor monitor) 
			throws CoreException 
	{
		boolean continueLaunch = super.preLaunchCheck(configuration, mode, monitor);
		if (continueLaunch) {
			continueLaunch = verifyEmulationSettings(configuration, monitor);
		}
		
		return continueLaunch;
	}

	/**
	 * Return the device selected by the launch configuration.
	 * 
	 * @param launchConfig
	 * @return
	 * @throws CoreException
	 */
	private IDevice getDevice(ILaunchConfiguration launchConfig)
		throws CoreException
	{
		IDevice device = null;
		
		if (launchConfig.getAttribute(ILaunchConstants.USE_PROJECT_DEVICE, true)) {
			device = getMidletSuite(launchConfig).getDevice();
		} else {
			String toolkitName = 
				launchConfig.getAttribute(ILaunchConstants.EMULATED_DEVICE_GROUP, "");
			String deviceName =
				launchConfig.getAttribute(ILaunchConstants.EMULATED_DEVICE, "");
			
			try {
				device = DeviceRegistry.singleton.getDevice(toolkitName, deviceName);
			} catch (PersistenceException e) {
				EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
			}
		}
		
		return device;
	}
	
	/**
	 * Return the midlet suite that is selected for this launch.
	 * 
	 * @param launchConfig
	 * @return
	 * @throws CoreException
	 */
	private IMidletSuiteProject getMidletSuite(ILaunchConfiguration launchConfig) 
		throws CoreException
	{
		IJavaProject javaProject = getJavaProject(launchConfig);
		return MidletSuiteFactory.getMidletSuiteProject(javaProject);
	}
	
	/**
	 * Verify the current emulation settings before launching.  Return a
	 * boolean indicating whether the specified settings are valid.
	 * 
	 * @param configuration
	 * @param monitor
	 * @return
	 * @throws CoreException
	 */
	private boolean verifyEmulationSettings(
		ILaunchConfiguration configuration, 
		IProgressMonitor monitor) 
			throws CoreException 
	{
		boolean valid = true;
		
		// If this is OTA, there should be at least one midlet
		// defined in the JAD file.
		boolean doOTA = configuration.getAttribute(ILaunchConstants.DO_OTA, false);
		if (doOTA) {
			IJavaProject javaProject = getJavaProject(configuration);
			if (javaProject != null) {
				IMidletSuiteProject suite = MidletSuiteFactory.getMidletSuiteProject(javaProject);
				ApplicationDescriptor desc = suite.getApplicationDescriptor();
				if (desc != null) {
					valid = (desc.getMidletCount() > 0);
					if (!valid) {
						valid = promptWhetherToContinue();
					}
				}
			}
		}
		
		return valid;
	}

	/**
	 * Return a boolean concerning whether the user wants to continue the launch.
	 * 
	 * @return
	 * @throws CoreException
	 */
	private boolean promptWhetherToContinue()
		throws CoreException
	{
		Boolean shouldContinue = 
			(Boolean) EclipseMECorePlugin.statusPrompt(NO_MIDLET_STATUS, this);
		return (shouldContinue != null) ? shouldContinue.booleanValue() : false;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -