RCP学习:如何重写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>
如此,即可。