RCP学习:如何重写WorkbenchPage

RCP学习:如何重写WorkbenchPage

原文地址:http://www.cnblogs.com/anrainie/archive/2012/11/06/2757438.html

 

重写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>
复制代码

如此,即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值