简体   繁体   English

在 Eclipse 启动时打开一个预定义 - 以编程方式

[英]Open a prespective on eclipse startup - programmatically

I am developing a eclipse plugin.我正在开发一个eclipse插件。 I need to open my prespective when we open the eclipse at first time.当我们第一次打开日食时,我需要打开我的预想。 Any ways to achieve this?有什么方法可以实现这一目标吗? i guess some listener must be available but could not trace out.我想一定有一些听众可用,但无法追踪。

We can open a prespective after eclipse start using PlatformUI.getWorkbench().showPrespective(<prespective id>)我们可以在 eclipse 开始使用PlatformUI.getWorkbench().showPrespective(<prespective id>)

Similarly is there a way to open the prespective on eclipse startup, so that our desired prespective gets opened when starting the eclipse.类似地,有没有办法在 eclipse 启动时打开预定义,以便在启动 eclipse 时打开我们想要的预定义。

You can use the org.eclipse.ui.startup extension point in your plugin.您可以在插件中使用org.eclipse.ui.startup扩展点。 When the plugin is activated, check/set a preference to decide if you want to switch perspectives and then schedule a UIJob do do it.激活插件后,检查/设置首选项以决定是否要切换视角,然后安排UIJob执行此操作。

  1. Implement the extension point.实现扩展点。 Some class in the plugin needs implements org.eclipse.ui.IStartup .插件中的某些类需要implements org.eclipse.ui.IStartup The activator class is fine in this case.在这种情况下,激活器类很好。 Particularly, since you don't need anything in the earlyStartup method.特别是,因为您不需要earlyStartup方法中的任何内容。

  2. In the start method, make the decision to switch and schedule it:start方法中,做出切换和调度的决定:

     public void start(BundleContext context) throws Exception { super.start(context); plugin = this; final boolean switchPerpective = processPluginUpgrading(); if (switchPerpective) { final IWorkbench workbench = PlatformUI.getWorkbench(); new UIJob("Switching perspectives"){ @Override public IStatus runInUIThread(IProgressMonitor monitor) { try { workbench.showPerspective(perspectiveId, workbench.getActiveWorkbenchWindow()); } catch (WorkbenchException e) { return new Status(IStatus.ERROR,PLUGIN_ID,"Error while switching perspectives", e); } return Status.OK_STATUS; }} .run(new NullProgressMonitor()); } }
  3. Use the preference store to keep data for your decision logic.使用首选项存储为您的决策逻辑保留数据。 In this implementation, the perspective is switched once per workspace whenever the plugin is upgraded.在此实现中,每当插件升级时,每个工作区都会切换一次透视图。 The data recorded in the preference store will allow a future version to have a difference policy.偏好存储中记录的数据将允许未来版本具有差异策略。 It uses the getPreferenceStore from AbstractUIPlugin so it is scoped per workspace.它使用来自AbstractUIPlugingetPreferenceStore ,因此它的范围是每个工作区。 If you want to use other scopes, see the FAQ .如果您想使用其他范围,请参阅常见问题解答

     private Boolean processPluginUpgrading() { final Version version = getDefault().getBundle().getVersion(); final IPreferenceStore preferenceStore = getDefault().getPreferenceStore(); final String preferenceName = "lastVersionActivated"; final String lastVersionActivated = preferenceStore.getString(preferenceName); final boolean upgraded = "".equals(lastVersionActivated) || (version.compareTo(new Version(lastVersionActivated)) > 0); preferenceStore.setValue(preferenceName, version.toString()); return upgraded; }

One thing I am doing to open my custom perspective in my plugin is to configure it in config.ini in eclipe's installation folder as below:为了在插件中打开自定义透视图,我正在做的一件事是在 eclipe 安装文件夹中的config.ini中对其进行配置,如下所示:

-perspective <my perspective id>

and it is working fine.它工作正常。 I got this information from Lars Vogel's tutorial, which you can find here .我从 Lars Vogel 的教程中得到了这些信息,你可以在这里找到。 Hope this helps.希望这可以帮助。

Other way:另一种方式:

org.eclipse.ui.IPerspectiveRegistry.setDefaultPerspective(id) this sets default perspective to the given id. org.eclipse.ui.IPerspectiveRegistry.setDefaultPerspective(id)这将默认透视图设置为给定的 id。 API Docs for the same. 相同的 API 文档。

Go to

D:\{MyTestSpace}\eclipse\features\myCustom.plugin.feature_3.1.0.201607220552

you can see feature.xml under plugin tag you get the id.你可以在插件标签下看到feature.xml你得到 id。

Use this id in config.ini which you can find underconfig.ini使用此 ID,您可以在下面找到

D:\\{MyTestSpace}\\eclipse\\configuration

As作为

-perspective <myCustum.plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM