RCP学习:如何重写WorkbenchPage

简介: 重写WorkbenchPage的必要性在哪里? 比如有一个需求,比如屏蔽编辑器的关闭功能,或者把关闭编辑器按钮的实际功能转为隐藏编辑器 前一个功能还可以通过重写一系列的类来完成,后面这个功能几乎是无法完成的。

重写WorkbenchPage的必要性在哪里?

比如有一个需求,比如屏蔽编辑器的关闭功能,或者把关闭编辑器按钮的实际功能转为隐藏编辑器

前一个功能还可以通过重写一系列的类来完成,后面这个功能几乎是无法完成的。

 

我们可以通过扩展org.eclipse.ui.internalTweaklets来完成

我们来看WorkbenchPage的初始化是怎样的:

/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.ui.internal.tweaklets;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.internal.Perspective;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.internal.registry.PerspectiveDescriptor;

/**
 * @since 3.4
 *
 */
public class Workbench3xImplementation extends WorkbenchImplementation {

    /* (non-Javadoc)
     * @see org.eclipse.ui.internal.tweaklets.WorkbenchImplementation#createWBW(int)
     */
    public WorkbenchWindow createWorkbenchWindow(int newWindowNumber) {
        return new WorkbenchWindow(newWindowNumber);
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.internal.tweaklets.WorkbenchImplementation#createWBPage(org.eclipse.ui.internal.WorkbenchWindow, java.lang.String, org.eclipse.core.runtime.IAdaptable)
     */
    public WorkbenchPage createWorkbenchPage(WorkbenchWindow workbenchWindow,
            String perspID, IAdaptable input) throws WorkbenchException {
        return new WorkbenchPage(workbenchWindow, perspID, input);
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.internal.tweaklets.WorkbenchImplementation#createWBPage(org.eclipse.ui.internal.WorkbenchWindow, org.eclipse.core.runtime.IAdaptable)
     */
    public WorkbenchPage createWorkbenchPage(WorkbenchWindow workbenchWindow,
            IAdaptable finalInput) throws WorkbenchException {
        return new WorkbenchPage(workbenchWindow, finalInput);
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.internal.tweaklets.WorkbenchImplementation#createPerspective(org.eclipse.ui.internal.registry.PerspectiveDescriptor, org.eclipse.ui.internal.WorkbenchPage)
     */
    public Perspective createPerspective(PerspectiveDescriptor desc,
            WorkbenchPage workbenchPage) throws WorkbenchException {
        return new Perspective(desc, workbenchPage);
    }

}

 

该类不止负责WorkbenchPage生成,重写WorkbenchWindow,重写Perspective也可以通过它。

这个类是可以通过扩展点配置出来的,见代码节选:

public abstract class WorkbenchImplementation {
    public static TweakKey KEY = new Tweaklets.TweakKey(WorkbenchImplementation.class);

    static {
        Tweaklets.setDefault(WorkbenchImplementation.KEY, new Workbench3xImplementation());
    }


}

 

private static Object createTweaklet(TweakKey definition) {
        IConfigurationElement[] elements = Platform
                .getExtensionRegistry()
                .getConfigurationElementsFor("org.eclipse.ui.internalTweaklets"); //$NON-NLS-1$
        for (int i = 0; i < elements.length; i++) {
            if (definition.tweakClass.getName().equals(
                    elements[i].getAttribute("definition"))) { //$NON-NLS-1$
                try {
                    Object tweaklet = elements[i].createExecutableExtension("implementation"); //$NON-NLS-1$
                    tweaklets.put(definition, tweaklet);
                    return tweaklet;
                } catch (CoreException e) {
                    StatusManager.getManager().handle(
                            StatusUtil.newStatus(IStatus.ERROR,
                                    "Error with extension " + elements[i], e), //$NON-NLS-1$
                            StatusManager.LOG);
                }
            }
        }
        return null;
    }

我现在实现一个自己的WorkbenchImplementation,如下

package galaxy.ide.application.test;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.internal.tweaklets.Workbench3xImplementation;

public class MyWorkbenchImplementation extends Workbench3xImplementation {
    public MyWorkbenchImplementation() {
        super();
    }

    public WorkbenchPage createWorkbenchPage(WorkbenchWindow workbenchWindow,
            String perspID, IAdaptable input) throws WorkbenchException {
        return new WorkbenchPage(workbenchWindow, perspID, input) {
            public boolean closeEditors(IEditorReference[] refArray,
                    boolean save) {
                System.out.println("执行关闭!!!!");
                return super.closeEditors(refArray, save);
            }
        };
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.internal.tweaklets.WorkbenchImplementation#createWBPage
     * (org.eclipse.ui.internal.WorkbenchWindow,
     * org.eclipse.core.runtime.IAdaptable)
     */
    public WorkbenchPage createWorkbenchPage(WorkbenchWindow workbenchWindow,
            IAdaptable finalInput) throws WorkbenchException {
        return new WorkbenchPage(workbenchWindow, finalInput) {
            public boolean closeEditors(IEditorReference[] refArray,
                    boolean save) {
                System.out.println("执行关闭!!!!");
                return super.closeEditors(refArray, save);
            }
        };
    }
}

为了让该类执行,扩展点定义如下:

<extension
         point="org.eclipse.ui.internalTweaklets">
      <tweaklet
            definition="org.eclipse.ui.internal.tweaklets.WorkbenchImplementation"
            description="galaxy.ide.application.test.MyWorkbenchImplementation"
            id="galaxy.ide.application.test.my"
            implementation="galaxy.ide.application.test.MyWorkbenchImplementation"
            name="galaxy.ide.application.test.MyWorkbenchImplementation">
      </tweaklet>
   </extension>

如此,即可。

目录
相关文章
|
Apache 开发工具 SEO
Apache Typecho框架启用地址重写
地址重写有利于SEO优化,开启地址重写可以去掉Typecho框架中的index.php后缀,该后缀如下。
320 0
Apache Typecho框架启用地址重写
|
API Swift Python
SwiftUI极简教程11:Path路径的使用
SwiftUI极简教程11:Path路径的使用
805 0
SwiftUI极简教程11:Path路径的使用
|
数据安全/隐私保护
SAP Spartacus 找不到登录入口的问题 - 如何使用 Schematics 安装 user package
SAP Spartacus 找不到登录入口的问题 - 如何使用 Schematics 安装 user package
SAP Spartacus 找不到登录入口的问题 - 如何使用 Schematics 安装 user package
|
数据库 数据安全/隐私保护 Windows
Windows系统命令行net user命令用法
原文:Windows系统命令行net user命令用法 在Windows渗透测试过程中,最常用的要数net user 命令了,但是非常多的时候我们都是对Linux命令非常熟悉,对Windows命令非常熟悉或者了解用法的少只有少,为了以后工作方便,这里记录一下Windows系统中的 net user 命令的用法。
2154 0
|
PHP
YII2 配置gii之后页面404 解决 2点=1 要加载model,2 要设置环境为dev,如下截图 3次要---有时候可能需要 执行composer dump-autoload 重新加载类
YII2 配置gii之后页面404  解决 2点=1 要加载model,2 要设置环境为dev,如下截图 解决成功     我的是这么解决的
1136 0
|
存储 安全 API
Metasploit-page1
版权声明:转载请注明出处:http://blog.csdn.net/dajitui2024 https://blog.csdn.net/dajitui2024/article/details/79396329 硬件需求: 存储: 10G存储空间。
1215 0