【问题标题】:Open a prespective on eclipse startup - programmatically打开 Eclipse 启动的视角 - 以编程方式
【发布时间】:2013-06-01 15:04:04
【问题描述】:

我正在开发一个 Eclipse 插件。当我们第一次打开 Eclipse 时,我需要打开我的预想。有什么方法可以实现这一目标?我想一定有一些听众可用,但无法追踪。

我们可以在eclipse启动后使用PlatformUI.getWorkbench().showPrespective(<prespective id>)打开一个preferive

同样有一种方法可以在 eclipse 启动时打开预置,以便在启动 Eclipse 时打开我们想要的预置。

【问题讨论】:

  • 您的插件是用于您自己的 Eclipse 产品、RCP 应用程序还是用于用户选择安装的任何典型 Eclipse 安装?

标签: java eclipse plugins eclipse-plugin


【解决方案1】:

您可以在插件中使用org.eclipse.ui.startup 扩展点。激活插件后,检查/设置首选项以决定是否要切换视角,然后安排UIJob 执行此操作。

  1. 实现扩展点。插件中的某些类需要implements org.eclipse.ui.IStartup。在这种情况下,激活器类很好。特别是,因为您不需要 earlyStartup 方法中的任何内容。

  2. 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. 使用首选项存储为您的决策逻辑保留数据。在此实现中,每当升级插件时,每个工作区都会切换一次透视图。偏好存储中记录的数据将允许未来版本具有不同的策略。它使用来自AbstractUIPlugingetPreferenceStore,因此它的范围是每个工作区。如果您想使用其他范围,请参阅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;
    }
    

【讨论】:

    【解决方案2】:

    我在插件中打开自定义透视图的一件事是在 ecipe 的安装文件夹中的 config.ini 中配置它,如下所示:

    -perspective <my perspective id>
    

    它工作正常。我从 Lars Vogel 的教程中获得了这些信息,您可以找到 here。希望这可以帮助。

    其他方式:

    org.eclipse.ui.IPerspectiveRegistry.setDefaultPerspective(id) 这会将默认透视设置为给定的 id。 API Docs for the same.

    【讨论】:

    • 感谢 Pradeep 的解决方案。有用。但我需要它以编程方式完成。因为我正在开发一个插件,我所有的文件都在一个 jar 中。因此,一旦 Eclipse 启动,jar 就会加载,此时我需要将默认值设置为我自己的值。有什么想法吗?
    • @Simz : 如何获取视角 ID?就我而言,我想要 C/C++ 透视图 ID?谢谢。
    【解决方案3】:

    转到

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

    你可以在插件标签下看到feature.xml你得到了id。

    config.ini 中使用此 ID,您可以在

    下找到该 ID

    D:\{MyTestSpace}\eclipse\configuration

    作为

    -perspective <myCustum.plugin>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2013-11-18
      • 2013-03-02
      • 1970-01-01
      相关资源
      最近更新 更多