[zt]第二章 主类和主界面类的实现(1)

FROM:http://www.blog.edu.cn/user1/19180/archives/2005/372845.shtml

1 插件主类

    所谓的插件主类,就等同于一般Java程序中的main函数。它实现了如何启动、停止插件等动作。这个类实际上就是一个实现了org.eclipse.ui.plugin.AbstractUIPlugin的类,这个类的代码比较简单,利用向导生成的默认代码就可以了。如果你删除了默认的文件,下面的示例可以帮助你编写新的类:

java 代码
  1. package pku.oo.notationBuilder;   
  2.   
  3. import org.eclipse.ui.plugin.*;   
  4. import org.eclipse.jface.resource.ImageDescriptor;   
  5. import org.osgi.framework.BundleContext;   
  6.   
  7. public class NotationBuilderPlugin extends AbstractUIPlugin {   
  8.   
  9.     //The shared instance.   
  10.     private static NotationBuilderPlugin plugin;   
  11.        
  12.     /**  
  13.      * The constructor.  
  14.      */  
  15.     public NotationBuilderPlugin() {   
  16.         plugin = this;   
  17.     }   
  18.   
  19.     /**  
  20.      * This method is called upon plug-in activation  
  21.      */  
  22.     public void start(BundleContext context) throws Exception {   
  23.         super.start(context);   
  24.     }   
  25.   
  26.     /**  
  27.      * This method is called when the plug-in is stopped  
  28.      */  
  29.     public void stop(BundleContext context) throws Exception {   
  30.         super.stop(context);   
  31.         plugin = null;   
  32.     }   
  33.   
  34.     /**  
  35.      * Returns the shared instance.  
  36.      */  
  37.     public static NotationBuilderPlugin getDefault() {   
  38.         return plugin;   
  39.     }   
  40.   
  41.     /**  
  42.      * Returns an image descriptor for the image file at the given  
  43.      * plug-in relative path.  
  44.      *  
  45.      * @param path the path  
  46.      * @return the image descriptor  
  47.      */  
  48.     public static ImageDescriptor getImageDescriptor(String path) {   
  49.         return AbstractUIPlugin.imageDescriptorFromPlugin("NotationBuilder", path);   
  50.     }   
  51. }   

 

    注释已经很清晰了,大部分代码只要拷贝就可以了。

2 编写插件界面主类

    插件界面主类实现了如何创建插件的主界面,但是一个GEF编辑器插件的主界面到底是什么呢?我下面先详细介绍一下。

    一个GEF编辑器插件的主界面一般包括三个部分:Palette,调色板,就好像是eclipse中WindowsBuilder Pro中的控件工具栏;Outline Page,大纲页,好像是eclipse的Package Explorer;主编辑区域,顾名思义了。

    当然,一个GEF编辑器插件并非一个需要这三个,对于Palette和Outline Page是可选的,而我们将要创建的界面主类,则是用来创建主编辑区域的,因此它是必选的。在后面的分析中我们会看到,Outline Page和主编辑区域的工作原理十分相似,地位上他们是平等的(这就是说,他们都是一种编辑区域,都可以对我们的文件内容进行编辑),这与Palette不同。

    如果要编写一个支持Palette的界面主类,我们需要一个继承自GraphicalEditorWithPalette的类。同时我们应该实现或覆盖下面的函数或接口:
protected void configureGraphicalViewer();
    这个函数告诉eclipse如何配置编辑器的主界面,在这个函数里我们应该编写创建主界面的代码,并设置EditPart的工厂,关于EditPart的概念,稍后详述,而EditPart的工厂类,主要作用是根据模型创建EditPart,这其中牵扯到GEF的框架以及各个部分的创建顺序(先创建模型,再创建EditPart、最后创建Figure,这些概念都在后面叙述)。
protected PaletteRoot getPaletteRoot();
    这个接口用来创建调色板。调色板的结构实际就是一个颗树,这里用来获得树根。
protected void initializeGraphicalViewer();
    这个接口用于为编辑器创建context,实际就是编辑器的模型部分。然后eclipse会根据这个模型创建编辑器的EditPart,创建的原则由EditPart的工厂给出。这些概念都在以后说明。
public void doSave(IProgressMonitor monitor);
public void doSaveAs();
    这两个接口都是顾名思义,不做过多解释。只是这个参数的作用还没有搞明白。
public boolean isDirty();
    这个函数用于返回命令栈是不是“脏的”(虽然我不太喜欢这么翻译)。
public Object getAdapter(Class type);
    这个函数用于获得指定类型的适配器类,一般重载这个函数创建自己的Outline Page。这个函数可以用来创建很多其它的东西,比如Property Sheet,你可以参考其父类的实现。
protected void initializePaletteViewer();
    这个函数用来初始化Palette,如果你需要给Palette增加什么新的功能,可以重载这个函数,一般情况下使用默认的就可以了。

    下面的示例代码能够帮助你了解如何编写这个类,你只需要注意函数的功能,至于中间的一些代码、出现的类我会在稍后说明:

java 代码
  1. package pku.oo.ui;   
  2.   
  3. import org.eclipse.core.runtime.IProgressMonitor;   
  4. import org.eclipse.gef.DefaultEditDomain;   
  5. import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;   
  6. import org.eclipse.gef.palette.PaletteRoot;   
  7. import org.eclipse.gef.ui.actions.ActionRegistry;   
  8. import org.eclipse.gef.ui.parts.ContentOutlinePage;   
  9. import org.eclipse.gef.ui.parts.GraphicalEditorWithPalette;   
  10. import org.eclipse.gef.ui.parts.TreeViewer;   
  11. import org.eclipse.swt.widgets.Composite;   
  12. import org.eclipse.ui.IActionBars;   
  13. import org.eclipse.ui.actions.ActionFactory;   
  14. import org.eclipse.ui.part.IPageSite;   
  15. import org.eclipse.ui.views.contentoutline.IContentOutlinePage;   
  16.   
  17. import pku.oo.model.Diagram;   
  18. import pku.oo.part.PartFactory;   
  19. import pku.oo.part.TreePartFactory;   
  20. import pku.oo.util.PaletteFactory;   
  21.   
  22. public class NotationBuilderEditor extends GraphicalEditorWithPalette {   
  23.   
  24.     private Diagram diagram = new Diagram();   
  25.        
  26.     public NotationBuilderEditor() {   
  27.         super();   
  28.         this.setEditDomain(new DefaultEditDomain(this));   
  29.     }   
  30.     public Diagram getDiagram() {   
  31.         return this.diagram;   
  32.     }   
  33.   
  34.     protected void configureGraphicalViewer() {   
  35.         super.configureGraphicalViewer();   
  36.         getGraphicalViewer().setRootEditPart(new ScalableFreeformRootEditPart());   
  37.         getGraphicalViewer().setEditPartFactory(new PartFactory());   
  38.     }   
  39.     private PaletteRoot paletteRoot;   
  40.        
  41.     protected PaletteRoot getPaletteRoot() {   
  42.         if (this.paletteRoot == null) {   
  43.             this.paletteRoot = PaletteFactory.createPalette();   
  44.         }   
  45.         return this.paletteRoot;   
  46.     }   
  47.   
  48.     protected void initializeGraphicalViewer() {   
  49.         getGraphicalViewer().setContents(this.diagram);   
  50.     }   
  51.   
  52.     public void doSave(IProgressMonitor monitor) {   
  53.     }   
  54.   
  55.     public boolean isDirty() {   
  56.         return getCommandStack().isDirty();   
  57.     }   
  58.   
  59.     public void doSaveAs() {   
  60.     }   
  61.     public Object getAdapter(Class type) {   
  62.         if (type == IContentOutlinePage.class)   
  63.             return new OutlinePage();   
  64.         return super.getAdapter(type);   
  65.     }   
  66.   
  67.     public boolean isSaveAsAllowed() {   
  68.         return false;   
  69.     }   
  70.   
  71.   
  72.     class OutlinePage extends ContentOutlinePage {   
  73.         public OutlinePage() {   
  74.             super(new TreeViewer());   
  75.         }   
  76.   
  77.         public void init(IPageSite pageSite) {   
  78.             super.init(pageSite);   
  79.         }   
  80.   
  81.         public void createControl(Composite parent) {   
  82.             super.createControl(parent);   
  83.             getSelectionSynchronizer().addViewer(getViewer());   
  84.             getViewer().setEditDomain(getEditDomain());   
  85.             getViewer().setEditPartFactory(new TreePartFactory());   
  86.             getViewer().setContents(getDiagram());   
  87.         }   
  88.   
  89.         public void dispose() {   
  90.             getSelectionSynchronizer().removeViewer(getViewer());   
  91.             super.dispose();   
  92.         }   
  93.     }   
  94.   
  95. }   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值