我發現如何覆蓋文件打開操作:通過註冊actionProvider
,它覆蓋org.eclipse.ui.navigator.resources.OpenActions
。我提供了所有的代碼,因爲將所有這些不同的東西放在一起並使其工作起來相對困難。
讓我們從插件開始。XML條目:
<!-- overwrite OpenActions -->
<extension
point="org.eclipse.ui.navigator.navigatorContent">
<actionProvider
class="myplugin.navigator.OpenActionProvider"
id="myplugin.navigator.actions.open"
overrides="org.eclipse.ui.navigator.resources.OpenActions"
priority="highest">
<enablement>
<and>
<instanceof
value="org.eclipse.core.resources.IFile">
</instanceof>
<test
property="org.eclipse.core.resources.extension"
value="frm">
</test>
</and>
</enablement>
</actionProvider>
</extension>
<extension
point="org.eclipse.ui.navigator.viewer">
<viewerActionBinding
viewerId="org.eclipse.ui.navigator.ProjectExplorer">
<includes>
<actionExtension
pattern="myplugin.navigator.actions.open">
</actionExtension>
</includes>
</viewerActionBinding>
</extension>
的OpenActionProvider看起來是這樣的:
package myplugin.navigator;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.ICommonActionConstants;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite;
public class OpenActionProvider extends CommonActionProvider {
private OpenEditorActionGroup fOpenGroup;
@Override
public void init(ICommonActionExtensionSite site) {
ICommonViewerWorkbenchSite workbenchSite = null;
if (site.getViewSite() instanceof ICommonViewerWorkbenchSite) {
workbenchSite = (ICommonViewerWorkbenchSite) site.getViewSite();
}
if (workbenchSite != null) {
if (workbenchSite.getPart() != null && workbenchSite.getPart() instanceof IViewPart) {
IViewPart viewPart = (IViewPart) workbenchSite.getPart();
fOpenGroup = new OpenEditorActionGroup(viewPart);
}
}
}
@Override
public void dispose() {
if (fOpenGroup != null) {
fOpenGroup.dispose();
fOpenGroup = null;
}
super.dispose();
}
@Override
public void fillActionBars(IActionBars actionBars) {
if (fOpenGroup == null)
return;
fOpenGroup.updateActionBars();
actionBars.setGlobalActionHandler(ICommonActionConstants.OPEN, fOpenGroup.getOpenAction());
}
@Override
public void fillContextMenu(IMenuManager menu) {
if (fOpenGroup == null)
return;
fOpenGroup.fillContextMenu(menu);
}
@Override
public void setContext(ActionContext context) {
super.setContext(context);
if (fOpenGroup == null)
return;
fOpenGroup.setContext(context);
}
}
的OpenEditorActionGroup看起來是這樣的:
package myplugin.navigator;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.actions.OpenFileAction;
import org.eclipse.ui.actions.OpenWithMenu;
import org.eclipse.ui.navigator.ICommonMenuConstants;
public class OpenEditorActionGroup extends AbstractActionGroup {
private OpenFileAction fOpenFileAction;
public OpenEditorActionGroup(IViewPart viewPart) {
super(viewPart);
}
@Override
protected void makeActions() {
fOpenFileAction= new OpenGenElementAction(getViewPart().getSite().getPage());
}
public static IStructuredSelection convertSelectionToResources(ISelection s) {
List<Object> converted = new ArrayList<Object>();
if (s instanceof StructuredSelection) {
Object[] elements = ((StructuredSelection) s).toArray();
for (int i = 0; i < elements.length; i++) {
Object e = elements[i];
if (e instanceof IResource) {
converted.add(e);
} else if (e instanceof IAdaptable) {
IResource r = (IResource) ((IAdaptable) e).getAdapter(IResource.class);
if (r != null) {
converted.add(r);
}
}
}
}
return new StructuredSelection(converted.toArray());
}
@Override
public void fillContextMenu(IMenuManager menu) {
System.out.println("fillcontextmenu");
if (getContext() == null)
return;
IStructuredSelection celements = (IStructuredSelection)getContext().getSelection();
IStructuredSelection selection = convertSelectionToResources(celements);
fOpenFileAction.selectionChanged(celements);
if (!fOpenFileAction.isEnabled())
return;
menu.appendToGroup(ICommonMenuConstants.GROUP_OPEN, fOpenFileAction);
fillOpenWithMenu(menu, selection);
}
的AbstractActionGroup是隻是一個包裝,你應該想實現更多的這些:
package myplugin.navigator;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.actions.ActionGroup;
public abstract class AbstractActionGroup extends ActionGroup {
private final IViewPart fViewPart;
public AbstractActionGroup(IViewPart viewPart) {
Assert.isNotNull(viewPart);
fViewPart = viewPart;
makeActions();
}
protected IViewPart getViewPart() {
return fViewPart;
}
protected ImageDescriptor getImageDescriptor(String relativePath) {
return ImageDescriptor.createFromURL(null);
}
protected abstract void makeActions();
@Override
public abstract void fillContextMenu(IMenuManager menu);
@Override
public abstract void fillActionBars(IActionBars actionBars);
@Override
public abstract void updateActionBars();
}
最後的OpenGenElementAction本身:
package myplugin.navigator;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.OpenFileAction;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.part.FileEditorInput;
public class OpenGenElementAction extends OpenFileAction {
private IFile selectedFile = null;
private final IWorkbenchPage workbenchPage;
public OpenGenElementAction(IWorkbenchPage page) {
super(page);
workbenchPage = page;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public void run() {
System.out.println("RUN");
}
@Override
protected boolean updateSelection(IStructuredSelection selection) {
if (selection.size() != 1)
return super.updateSelection(selection);
Object element = selection.getFirstElement();
if (element instanceof IFile) {
selectedFile = (IFile)element;
}
return selectedFile != null || super.updateSelection(selection);
}
}
之後,我發現這個代碼在**包裝資源管理器和項目導航器中都不起作用:(。 – Michael 2011-05-15 20:57:42
感謝您發佈這個! – ernesto 2013-02-06 11:34:42