参考博客http://blog.csdn.net/andywangcn/article/details/8257961实现自定义节点的解析
不用多说,如何在 org.eclipse.ui.views扩展点中添加Navigator很简单,这里只给出配置,对应Class类可根据自己需要对org.eclipse.ui.navigator.CommonNavigator类做进一步扩展
<extension
point="org.eclipse.ui.views">
<view
class="org.eclipse.ui.navigator.CommonNavigator"
icon="icons/sample.gif"
id="nwpu.edu.gnc.CNFnavigator"
name="Navigator"
restorable="true">
</view>
</extension>
紧接着配置 org.eclipse.ui.navigator.navigatorContent扩展点,自己的配置如下:
<extension
point="org.eclipse.ui.navigator.navigatorContent">
<navigatorContent
activeByDefault="true"
contentProvider="nwpu.edu.gnc.navigator.NavigatorContentProvider" //指定节点的content如何获取,自己在这个类中实现的是对XML文件的解析及相关操作
icon="icons/alt_window_16.gif"
id="nwpu.edu.gnc.cnf.navigatorContent" //id必须被指定,此id会被在org.eclipse.ui.navigator.viewer扩展点下的viewerContentBinding中作为新的可展示元素添加进来
labelProvider="nwpu.edu.gnc.navigator.NavigatorLableProvider" //提供展示节点的显示图片和文字
name="XML Navigator Content"
priority="normal">
<triggerPoints> //触发点
<or>
<and>
<instanceof
value="org.eclipse.core.resources.IFile"> //提供文件的展示
</instanceof>
<test //设置在指定扩展名为XML的文件处进行节点展开
forcePluginActivation="true"
property="org.eclipse.core.resources.extension"
value="xml">
</test>
</and>
<instanceof
value="nwpu.edu.gnc.project.entity.EntityElement">
</instanceof>
</or>
</triggerPoints>
<possibleChildren>
<or>
<instanceof
value="nwpu.edu.gnc.project.entity.EntityElement"> //设置展开展开元素的类型
</instanceof>
</or>
</possibleChildren>
<actionProvider //提供扩展节点的右键菜单
class="nwpu.edu.gnc.navigator.NodeCommonActionProvider"
id="nwpu.edu.gnc.cnf.navigatorContent.actions" //id 会被org.eclipse.ui.navigator.viewer扩展点下的actionExtension引用
priority="normal">
<enablement>
<or>
<instanceof
value="nwpu.edu.gnc.project.entity.EntityElement"> //指定设置这种Action的节点,这里如果有多个元素不能设置为nwpu.edu.gnc.project.entity.*必须提供一个可以转换所有Entity的一个类,获取到的节点类型就是EntityElement
</instanceof></or></enablement></actionProvider> </navigatorContent> </extension>
接着修改扩展点org.eclipse.ui.navigator.viewer,该扩展点主要实现Action和Content的绑定
<extension
point="org.eclipse.ui.navigator.viewer">
<viewerActionBinding
viewerId="nwpu.edu.gnc.CNFnavigator"> //指定action绑定的视图ID为之前定义好的Navigator的ID
<includes>
<actionExtension
pattern="org.eclipse.ui.navigator.resources.*">
</actionExtension>
<actionExtension
pattern="nwpu.edu.gnc.cnf.navigatorContent.actions"> //为节点自定义的Action
</actionExtension>
</includes>
</viewerActionBinding>
<viewerContentBinding
viewerId="nwpu.edu.gnc.CNFnavigator"> //指定content绑定的视图ID为之前定义好的Navigator的ID
<includes>
<contentExtension
pattern="org.eclipse.ui.navigator.resourceContent">
</contentExtension>
<contentExtension
pattern="org.eclipse.ui.navigator.resources.filters.*">
</contentExtension>
<contentExtension
pattern="org.eclipse.ui.navigator.resources.linkHelper">
</contentExtension>
<contentExtension
pattern="org.eclipse.ui.navigator.resources.workingSets">
</contentExtension>
<contentExtension
pattern="nwpu.edu.gnc.cnf.navigatorContent"> //扩展navigator内容为自定义的navigatorContent扩展的ID
</contentExtension>
</includes>
</viewerContentBinding>
<viewer
viewerId="nwpu.edu.gnc.CNFnavigator">
</viewer>
</extension>
以上,基本完成了所有的配置;下面给出部分相关代码:
NavigatorContentProvider.java实现如下
/**
*
*/
package nwpu.edu.gnc.navigator;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import nwpu.edu.gnc.XMLparseTree.PraseProjectTree;
import nwpu.edu.gnc.project.entity.EntityElement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
/**
* @author ww
*
*/
public class NavigatorContentProvider implements ITreeContentProvider{
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
}
@Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
if (inputElement instanceof IFile) {
try {
InputStream is = ((IFile) inputElement).getContents();
EntityElement element = parseXML(is);
System.out.println(element.getChildren().size());
is.close();
return new Object[] { element };
} catch (Exception e) {
return null;
}
}
return null;
//return ((EntityElement) inputElement).getChildren().toArray();
//
}
//解析文件
private EntityElement parseXML(InputStream is) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(is);
Element root = document.getRootElement();
EntityElement entityElement = PraseProjectTree.parseProjectTree(root);
return entityElement;
}
@Override
public Object[] getChildren(Object parentElement) {
// TODO Auto-generated method stub
if(parentElement instanceof IFile){
return getElements(parentElement);
}else if(parentElement instanceof EntityElement){
return ((EntityElement) parentElement).getChildren().toArray();
}
return null;
//return ((EntityElement) parentElement).getChildren().toArray();
}
@Override
public Object getParent(Object element) {
// TODO Auto-generated method stub
if(element instanceof EntityElement){
return ((EntityElement) element).getParent();
}
return null;
//return ((EntityElement) element).getParent();
}
@Override
public boolean hasChildren(Object element) {
// TODO Auto-generated method stub
// if(element instanceof EntityElement){
// return ((EntityElement) element).getChildren().size()>0;
// }
return true;
}
}
NavigatorLabelProvider.java类实现如下:
package nwpu.edu.gnc.navigator;
import java.util.HashMap;
import java.util.Map;
import nwpu.edu.gnc.Activator;
import nwpu.edu.gnc.project.entity.*;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.navigator.IDescriptionProvider;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.w3c.dom.Node;
public class NavigatorLableProvider extends LabelProvider implements ILabelProvider,IDescriptionProvider{
private Map<ImageDescriptor,Image> imageCache = new HashMap<ImageDescriptor,Image>(20);
public void addListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public Image getImage(Object object) {
// TODO Auto-generated method stub
if(object instanceof EntityElement){
return Activator.getImageDescriptor("icons/alt_window_16.gif").createImage();
}
return super.getImage(object);
}
@Override
public String getText(Object object) {
// TODO Auto-generated method stub
String textString = new String();
Object element=((EntityElement)object).getEntityObject();
System.err.println(element.toString()+ " "+element.getClass());
if(element instanceof Project){
textString=((Project) element).getName().getName();
System.out.println("textString:::"+textString);
}else if (element instanceof Folder) {
textString=((Folder) element).getName().getName();
}else if (element instanceof Model) {
textString=((Model) element).getName().getName();
}else if (element instanceof Atom) {
textString=((Atom) element).getName().getName();
}else if (element instanceof Reference) {
textString=((Reference) element).getName().getName();
}else if (element instanceof Set) {
textString=((Set) element).getName().getName();
}
return textString;
}
protected RuntimeException unknownElement(Object element) {
return new RuntimeException(
"Unknown type of element in tree of type " + element.getClass().getName());
}
public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}
public void removeListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
@Override
public String getDescription(Object anElement) {
// TODO Auto-generated method stub
return null;
}
}
最后附上运行截图:
NodeCommonActionProvider.java 实现如下:
/**
*
*/
package nwpu.edu.gnc.navigator;
import nwpu.edu.gnc.popupAction.AttachLibraryAction;
import nwpu.edu.gnc.popupAction.CheckGuidAction;
import nwpu.edu.gnc.popupAction.ConstraintsAction;
import nwpu.edu.gnc.popupAction.CopyAction;
import nwpu.edu.gnc.popupAction.CopyClosureAction;
import nwpu.edu.gnc.popupAction.CopySmartAction;
import nwpu.edu.gnc.popupAction.CreateProjectAction;
import nwpu.edu.gnc.popupAction.DeleteAction;
import nwpu.edu.gnc.popupAction.FollowReferenceAction;
import nwpu.edu.gnc.popupAction.HelpAction;
import nwpu.edu.gnc.popupAction.InsertAtomAction;
import nwpu.edu.gnc.popupAction.InsertFolderAction;
import nwpu.edu.gnc.popupAction.InsertModelAction;
import nwpu.edu.gnc.popupAction.InterpretAction;
import nwpu.edu.gnc.popupAction.PasteAction;
import nwpu.edu.gnc.popupAction.PasteSpecialAction;
import nwpu.edu.gnc.popupAction.ReadOnlyAction;
import nwpu.edu.gnc.popupAction.RegistryAction;
import nwpu.edu.gnc.popupAction.ShowAttributesAction;
import nwpu.edu.gnc.popupAction.ShowPreferencesAction;
import nwpu.edu.gnc.popupAction.ShowPropertiesActions;
import nwpu.edu.gnc.popupAction.TreeBrowserOptionsAction;
import nwpu.edu.gnc.popupAction.ViewInParentAction;
import nwpu.edu.gnc.popupAction.WriteOnlyAction;
import nwpu.edu.gnc.project.entity.EntityElement;
import nwpu.edu.gnc.project.entity.Model;
import nwpu.edu.gnc.project.entity.Project;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonViewerSite;
/**
* @author ww
* 该类用于控制.xml文件下的节点处的右键菜单显示
*/
public class NodeCommonActionProvider extends CommonActionProvider {
private EntityElement element;
private ICommonViewerSite viewSite;
private Action propertyAction;
private IWorkbenchWindow window;
private ReadOnlyAction readOnlyAction=new ReadOnlyAction();
private WriteOnlyAction writeOnlyAction=new WriteOnlyAction();
private ConstraintsAction constraintsAction=new ConstraintsAction();
private CopyAction copyAction =new CopyAction();
private CopyClosureAction copyClosureAction=new CopyClosureAction();
private CopySmartAction copySmartAction=new CopySmartAction();
private DeleteAction deleteAction=new DeleteAction();
private HelpAction helpAction=new HelpAction();
private InterpretAction interpretAction=new InterpretAction();
private PasteAction pasteAction=new PasteAction();
private PasteSpecialAction pasteSpecialAction=new PasteSpecialAction();
private ShowAttributesAction showAttributesAction=new ShowAttributesAction(window);
private ShowPreferencesAction showPreferencesAction=new ShowPreferencesAction(window);
private ShowPropertiesActions showPropertiesAction=new ShowPropertiesActions(window);
private TreeBrowserOptionsAction treeBrowserOptionsAction=new TreeBrowserOptionsAction();
private ViewInParentAction viewInParentAction=new ViewInParentAction();
private RegistryAction registryAction=new RegistryAction();
private FollowReferenceAction followReferenceAction=new FollowReferenceAction();
private CheckGuidAction checkGuidAction=new CheckGuidAction();
private InsertAtomAction insertAtomAction=new InsertAtomAction();
private InsertModelAction insertModelAction=new InsertModelAction();
private AttachLibraryAction attachLibraryAction=new AttachLibraryAction();
private InsertFolderAction insertFolderAction=new InsertFolderAction();
private CreateProjectAction createProjectAction = new CreateProjectAction();
public NodeCommonActionProvider() {
}
@Override
public void init(ICommonActionExtensionSite site) {
super.init(site);
viewSite = site.getViewSite();
propertyAction = new Action("Show Property") {
@Override
public void run() {
IStructuredSelection selection = (IStructuredSelection) viewSite
.getSelectionProvider().getSelection();
Object firstElement = selection.getFirstElement();
if (firstElement instanceof EntityElement) {
EntityElement selectedNode = (EntityElement) firstElement;
Object object=((EntityElement)selectedNode).getEntityObject();
if (object instanceof Project) {
Project project = ((Project)object);
String property = "项目名称:" + project.getName() + "\n"
+ "GUID:" + project.getGuid() + "\n"
+ "创建日期:" + project.getCdate() + "\n"
+ "修改日期:" + project.getMdate() + "\n"
+ "版本:" + project.getVersion() + "\n"
+ "作者:" + project.getAuthor() + "\n";
MessageDialog.openInformation(viewSite.getShell(),
"Property", property);
}else {
MessageDialog.openInformation(viewSite.getShell(),
"Property", object.toString());
}
}
}
};
}
@Override
public void fillContextMenu(IMenuManager menu) {
menu.add(propertyAction);
menu.add(showPreferencesAction);
menu.add(showPropertiesAction);
menu.add(showAttributesAction);
menu.add(registryAction);
menu.add(attachLibraryAction);
menu.add(checkGuidAction);
menu.add(new Separator());
menu.add(insertFolderAction);
menu.add(copyAction);
menu.add(copyClosureAction);
menu.add(copySmartAction);
menu.add(pasteAction);
menu.add(pasteSpecialAction);
menu.add(new Separator());
menu.add(deleteAction);
menu.add(new Separator());
menu.add(constraintsAction);
menu.add(interpretAction);
MenuManager accessMenu =new MenuManager("&Access", "access");
accessMenu.add(readOnlyAction);
accessMenu.add(writeOnlyAction);
menu.add(accessMenu);
menu.add(viewInParentAction);
menu.add(new Separator());
menu.add(helpAction);
menu.add(new Separator());
menu.add(treeBrowserOptionsAction);
}
}
EntityElement.java实现如下:
package nwpu.edu.gnc.project.entity;
import java.util.ArrayList;
import java.util.List;
public class EntityElement{
private EntityElement parent;
List<EntityElement> children = new ArrayList<EntityElement>();
private Object entityObject;
public EntityElement getParent() {
return parent;
}
public void setParent(EntityElement parent) {
this.parent = parent;
}
public List<EntityElement> getChildren() {
return children;
}
public void addChildren(EntityElement child) {
//child.setParent(this);
children.add(child);
}
public void removeChildren(EntityElement child) {
children.remove(child);
}
public Object getEntityObject() {
return entityObject;
}
public void setEntityObject(Object entityObject) {
this.entityObject = entityObject;
}
public void setChildren(List<EntityElement> children) {
this.children = children;
}
public boolean hasChildren(){
if (children.size()>0) {
return true;
}else {
return false;
}
}
}
其他实体类型过多就不在这提出代码了
随后附上运行截图: