eclipseeclipse更换工作区间是什么意思

下次自动登录
现在的位置:
& 综合 & 正文
Eclipse 中实现工作区的切换
说到切换工作区,很快会想到
Switch Workspace
”按钮的功能,他能够从另一个工作区加载用户的数据,如果我们在
的开发中要实现此类似的功能的话,不妨可以参考一下
本身的实现。
此功能的实现在
org.eclipse.ui.internal.ide.actions.OpenWorkspaceAction
类中,位于
org.eclipse.ui.ide
插件里,要查看的话,可以从
Eclipse IDE
的插件列表视图中将此插件导出为源文件项目即可。
下面来看看
OpenWorkspaceAction
的做法是如何实现的:
通过用户的输入(工作空间选择对话框)信息来拼装一个启动命令,通过这个命令直接可以启动一个
平台的实例;
System..setProperty
”eclipse.exitdata”
)将这个启动命令存储在
eclipse.exitdata
eclipse.exitcode
属性设置为
eclipse.exitcode
可以有三种状态,在
IApplication
中注册的,分别是
EXIT_RESTART
, 24(EXIT_RELAUNCH),
具体每个状态的含义可以参考
IApplication
window.getWorkbench().restart()
方法重新启动。
明白了整个的实现过程后,再来看看关键部分的功能实现,首先是
OpenWorkspaceAction
buildCommansLinke
String workspace
private String buildCommandLine(String workspace) {
String property = System.getProperty(PROP_VM);
if (property == null) {
MessageDialog.openError(window.getShell(),
IDEWorkbenchMessages.OpenWorkspaceAction_errorTitle,NLS
IDEWorkbenchMessages.OpenWorkspaceAction_errorMessage,
PROP_VM));
StringBuffer result = new StringBuffer(512);
result.append(property);
result.append(NEW_LINE);
// append the vmargs and commands. Assume that these already end in /n
String vmargs = System.getProperty(PROP_VMARGS);
if (vmargs != null) {
result.append(vmargs);
// append the rest of the args, replacing or adding -data as required
property = System.getProperty(PROP_COMMANDS);
if (property == null) {
result.append(CMD_DATA);
result.append(NEW_LINE);
result.append(workspace);
result.append(NEW_LINE);
// find the index of the arg to replace its value
int cmd_data_pos = property.lastIndexOf(CMD_DATA);
if (cmd_data_pos != -1) {
cmd_data_pos += CMD_DATA.length() + 1;
result.append(property.substring(0, cmd_data_pos));
result.append(workspace);
result.append(property.substring(property.indexOf('/n',
cmd_data_pos)));
result.append(CMD_DATA);
result.append(NEW_LINE);
result.append(workspace);
result.append(NEW_LINE);
result.append(property);
// put the vmargs back at the very end (mands property
// already contains the -vm arg)
if (vmargs != null) {
result.append(CMD_VMARGS);
result.append(NEW_LINE);
result.append(vmargs);
return result.toString();
再来看方法
promptForWorkspace(),
作用是提示选择工作空间:
String promptForWorkspace() {
// get the current workspace as the default
ChooseWorkspaceData data = new ChooseWorkspaceData(Platform
.getInstanceLocation().getURL());
ChooseWorkspaceDialog dialog = new ChooseWorkspaceWithSettingsDialog(
window.getShell(), data, true, false);
dialog.prompt(true);
// return null if the user changed their mind
String selection = data.getSelection();
if (selection == null) {
// otherwise store the new selection and return the selection
data.writePersistedData();
void restart(String path) {
String command_line = buildCommandLine(path);
if (command_line == null) {
void run() {
String path = promptForWorkspace();
if (path == null) {
restart(path);
System.setProperty(PROP_EXIT_CODE, Integer.toString(24));
System.setProperty(PROP_EXIT_DATA, command_line);
window.getWorkbench().restart();
&&&&推荐文章:
【上篇】【下篇】

我要回帖

更多关于 eclipse 切换工作区 的文章

 

随机推荐