开发基于eclipse的launch

一、 基本概念
LaunchConfigurations和LaunchConfigurationTypes:LaunchConfigurations表示一个具体的用户launch而LaunchConfigurationTypes表示能生成该类用户launch配置的工具。LaunchConfigurationTypes与LaunchConfigurations的关系有点类似于类与他所对应的对象的关系。用下图来说明:当我们编写一个SWT的应用程序的时候,在launch该程序时,选择SWT Application(对应于LaunchConfigurationTypes),然后生成一个对应于本程序的swt的launch选项New_swt_config(对应于LaunchConfigurations)

说明:
1) LaunchConfigurations必须实现org.eclipse.debug.core.ILaunchConfiguration接口,该接口提供了一些方法用来获取config的配置信息,但不可改变这些配置信息,如果我们想改变这些配置信息,那LaunchConfigurations必须实现org.eclipse.debug.core.ILaunchConfigurationWorkingCopy接口
二、 需要引进的包
如果要使用LaunchConfigurationTypes则需引进org.eclipse.debug.core,如果还有使用UI则需要引进org.eclipse.debug.ui
三、 具体操作步骤
1) 声明一个LaunchConfigurationTypes




说明:
[1]delegate项指定了该LaunchConfigurationTypes所对应的类,该类必须实现org.eclipse.debug.core.model.ILaunchConfigurationDelegate接口的launch()方法。
[2]modes项指定了该LaunchConfigurationTypes所支持的的模式,如果指定为"run, debug",则需要在[1]中的launch方法中判别并分别处理这两种模式。
[3]public指定该LaunchConfigurationTypes是否在UI中出现
2)声明一个LaunchConfigurationTypes的图标




说明;
[1] configTypeID为1)中指定的id
3)在Configuration中声明一个tab group








说明:
[1]class指定的类必须实现org.eclipse.debug.ui.ILaunchConfigurationTabGroup接口
4)实现该tab group
public void createTabs(ILaunchConfigurationDialog dialog, String mode) {
ILaunchConfigurationTab tabs[] = {
new PMainTab(),
new PArgumentsTab(),
new EnvironmentTab(),
new ParallelTab(),
new PDebuggerTab(false),
new PCommonTab(),
new PSelectNodeTab()};
setTabs(tabs);
}
说明:
[1]所有的tab group应当包含org.eclipse.debug.ui.CommonTab这个tab页

5)实现每个tab
说明:
[1]一个tab最重要的方法为下列三个:
public void setDefaults(ILaunchConfigurationWorkingCopy configuration);
public void performApply(ILaunchConfigurationWorkingCopy configuration);
public void initializeFrom(ILaunchConfiguration configuration);
前两个函数把参数从tab拷贝到working copy中,后一个函数把参数从working copy拷贝到tab中,这里从三个函数的参数类型也可体会在第一节中的关于ILaunchConfigurationWorkingCopy和ILaunchConfiguration的概念。
[2] performApply函数示例
public void performApply(ILaunchConfigurationWorkingCopy configuration) {
configuration
.setAttribute(IPTPLaunchConfigurationConstants.MACHINE_NAME,
getMachineName());
configuration.setAttribute(
IPTPLaunchConfigurationConstants.NUMBER_OF_PROCESSES,
getFieldContent(numberOfProcessField));
}
[3] initializeFrom函数示例
public void initializeFrom(ILaunchConfiguration configuration) {
try {
numberOfProcessField.setStringValue(configuration.getAttribute(
IPTPLaunchConfigurationConstants.NUMBER_OF_PROCESSES,
EMPTY_STRING));
int idx = getMachineNameIndex(configuration.getAttribute(IPTPLaunchConfigurationConstants.MACHINE_NAME, EMPTY_STRING));
machineCombo.select(idx);
} catch (CoreException e) {
setErrorMessage(CoreMessages
.getFormattedResourceString(
“CommonTab.common.Exception_occurred_reading_configuration_EXCEPTION”,
e.getStatus().getMessage()));
}

}
[4] isValid函数示例
public boolean isValid(ILaunchConfiguration configuration) {
setErrorMessage(null);
setMessage(null);

if (!numberOfProcessField.isValid()) {
setErrorMessage(numberOfProcessField.getErrorMessage());
return false;
}
return true;
}
如果所有的参数都有合法的值,则返回true
6)实现launch的快捷方式






说明:
[1]class所指定的类必须实现org.eclipse.debug.ui.ILaunchShortcut接口

English Vesion: http://www.eclipse.org/articles/Article-Launch-Framework/launch.html