`
sunway
  • 浏览: 113146 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
From:http://www.blog.edu.cn/user1/19180/archives/2005/372845.shtml
1 插件主类

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

java 代码
package pku.oo.notationBuilder;

import org.eclipse.ui.plugin.*;
import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.BundleContext;

public class NotationBuilderPlugin extends AbstractUIPlugin {

//The shared instance.
private static NotationBuilderPlugin plugin;

/**
* The constructor.
*/
public NotationBuilderPlugin() {
plugin = this;
}

/**
* This method is called upon plug-in activation
*/
public void start(BundleContext context) throws Exception {
super.start(context);
}

/**
* This method is called when the plug-in is stopped
*/
public void stop(BundleContext context) throws Exception {
super.stop(context);
plugin = null;
}

/**
* Returns the shared instance.
*/
public static NotationBuilderPlugin getDefault() {
return plugin;
}

/**
* Returns an image descriptor for the image file at the given
* plug-in relative path.
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("NotationBuilder", path);
}
}



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

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 代码
package pku.oo.ui;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.parts.ContentOutlinePage;
import org.eclipse.gef.ui.parts.GraphicalEditorWithPalette;
import org.eclipse.gef.ui.parts.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.IPageSite;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;

import pku.oo.model.Diagram;
import pku.oo.part.PartFactory;
import pku.oo.part.TreePartFactory;
import pku.oo.util.PaletteFactory;

public class NotationBuilderEditor extends GraphicalEditorWithPalette {

private Diagram diagram = new Diagram();

public NotationBuilderEditor() {
super();
this.setEditDomain(new DefaultEditDomain(this));
}
public Diagram getDiagram() {
return this.diagram;
}

protected void configureGraphicalViewer() {
super.configureGraphicalViewer();
getGraphicalViewer().setRootEditPart(new ScalableFreeformRootEditPart());
getGraphicalViewer().setEditPartFactory(new PartFactory());
}
private PaletteRoot paletteRoot;

protected PaletteRoot getPaletteRoot() {
if (this.paletteRoot == null) {
this.paletteRoot = PaletteFactory.createPalette();
}
return this.paletteRoot;
}

protected void initializeGraphicalViewer() {
getGraphicalViewer().setContents(this.diagram);
}

public void doSave(IProgressMonitor monitor) {
}

public boolean isDirty() {
return getCommandStack().isDirty();
}

public void doSaveAs() {
}
public Object getAdapter(Class type) {
if (type == IContentOutlinePage.class)
return new OutlinePage();
return super.getAdapter(type);
}

public boolean isSaveAsAllowed() {
return false;
}


class OutlinePage extends ContentOutlinePage {
public OutlinePage() {
super(new TreeViewer());
}

public void init(IPageSite pageSite) {
super.init(pageSite);
}

public void createControl(Composite parent) {
super.createControl(parent);
getSelectionSynchronizer().addViewer(getViewer());
getViewer().setEditDomain(getEditDomain());
getViewer().setEditPartFactory(new TreePartFactory());
getViewer().setContents(getDiagram());
}

public void dispose() {
getSelectionSynchronizer().removeViewer(getViewer());
super.dispose();
}
}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics