我正在为Eclipse编写一个插件,它为我们的软件堆栈中使用的特定预处理器指令提供语法突出显示。在编译实际代码之前,这些代码会被外部程序取代(想象一下像<% FOO %>
这样的标记会被其他东西替换,比如版本字符串。)
因此,该插件为每个标记提供注释,以便快速识别这些标记。不是实现自己的编辑器,而是为已有的编辑器提供注释,如JavaEditor或PHP-Editor。
现在的问题是,外部程序处理的文件都具有相同的文件扩展名,比如说.inc
。它们可以包含Java代码或PHP代码(将来也可以使用其他语言)。
我已成功将内容类型添加到我的插件中,并且可以根据某些条件区分不同的文件。所以,当我有一个java .inc
文件并分配内容类型inc file (java)
时。
但是,用户应该能够覆盖此自动检测(有时,自动检测可能会失败)。所以,我希望能够用不同的编辑器(Java编辑器,PHP编辑器......)打开一个文件(foo.inc
),并能够保存这种关联。
我目前正在考虑的方法:
有更好的解决方案吗?您对我列出的任何方法有更多了解吗?
答案 0 :(得分:3)
我找到了如何覆盖文件打开操作:注册actionProvider
覆盖org.eclipse.ui.navigator.resources.OpenActions
。我提供了所有代码,因为将所有这些不同的东西放在一起并让它工作起来相对困难。
让我们从plugin.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);
}
}
答案 1 :(得分:0)
您可以通过编程方式直接攻击IEditorRegistry,方法是注册特定的文件名而不仅仅是扩展名。
请看:&#34;帮助IEditorRegistry&#34;
方法:
void setDefaultEditor(String fileNameOrExtension, String editorId)
Sets the default editor id for the files that match
that specified file name or extension.
接受全名和/或通配符。大多数openEditor调用(菜单,工具栏等...)最终都会调用此注册表来获取相应的编辑器。 在打开编辑器时设置一个挂钩,专门注册该文件名。
如果您有两个名称相同且语言不同的文件,但与您的方法相比,它快速且易于实施,这并非万无一失。