Posted on 2008-01-03 08:54
笑看人生 阅读(2847)
评论(3) 编辑 收藏 所属分类:
Java插件开发
为了实现流程编辑器,光有我们建立的模型,控制器和视图还不够,我们还必须新建一个编辑器类,为此,我们要新建一个插件项目,我们新建的插件项目com.example.workflow结构如下:
为了实现一个编辑器,我们必须扩展org.eclipse.ui.editors这个扩展点,这个可以在plugin.xm定义,定义后的文件如下:
<extension
point="org.eclipse.ui.editors">
<editor
class="com.example.workflow.ui.WorkflowProcessEditor"
default="true"
extensions="workflow"
icon="src/com/example/workflow/icons/shapes.gif"
id="com.example.workflow.WorkflowProcessEditor"
name="流程设计器"/>
</extension>
其中,class属性指定我们编辑器对应的类;
Extensions属性指定编辑器打开文件的扩展名为workflow;
Default属性指出该编辑器是扩展名为workflow文件的默认打开方式;
Icon为扩展名为workflow的文件和编辑器的图标;
Id属性为编辑器的唯一标识
Name为编辑器显示的名称
同时,我们还必须定义插件运行时要依赖的包,打开META-INF中的文件MANIFEST.MF ,添加依赖项就可以,最后文件内容如下:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Workflow Plug-in
Bundle-SymbolicName: com.example.workflow;singleton:=true
Bundle-Version: 1.0.0
Bundle-Activator: com.example.workflow.Activator
Bundle-Vendor: EXAMPLE
Bundle-Localization: plugin
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.gef,
org.eclipse.ui.editors,
org.eclipse.ui.ide,
org.eclipse.core.resources,
org.eclipse.ui.views
Eclipse-LazyStart: true
现在我们就可以来定义编辑器对应的类了WorkflowProcessEditor,代码如下:
package com.example.workflow.ui;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette;
import org.eclipse.ui.IEditorInput;
import com.example.workflow.model.WorkflowProcess;
import com.example.workflow.parts.WorkflowProcessEditPartFactory;
public class WorkflowProcessEditor extends GraphicalEditorWithFlyoutPalette{
private WorkflowProcess process;
private static PaletteRoot PALETTE_MODEL;
public WorkflowProcessEditor(){
setEditDomain(new DefaultEditDomain(this));
}
private WorkflowProcess getModel(){
return this.process;
}
protected void initializeGraphicalViewer() {
super.initializeGraphicalViewer();
GraphicalViewer viewer = getGraphicalViewer();
viewer.setContents(getModel());
}
protected void configureGraphicalViewer() {
super.configureGraphicalViewer();
GraphicalViewer viewer = getGraphicalViewer();
viewer.setEditPartFactory(new WorkflowProcessEditPartFactory());
viewer.setRootEditPart(new ScalableFreeformRootEditPart());
}
protected void setInput(IEditorInput input) {
super.setInput(input);
process = new WorkflowProcess();
}
protected PaletteRoot getPaletteRoot() {
if (PALETTE_MODEL == null)
PALETTE_MODEL = WorkflowProcessEditorPaletteFactory.createPalette();
return PALETTE_MODEL;
}
public void doSave(IProgressMonitor monitor) {
// TODO Auto-generated method stub
}
}
这个类继承了GraphicalEditorWithFlyoutPalette,带有面板的图形化编辑器。在这个类getPaletteRoot()方法中,我们定创建了面板,面板类详细信息,接下来将介绍。在setInput方法中,我们定义了一个流程对象,在这里我们也可以从外面文件读入一个对象,作为编辑器的输入,在构造函数中,setEditDomain(new DefaultEditDomain(this));就是给当前编辑器设置一个默认编辑域,这个域跟命令堆栈有关。在initializeGraphicalViewer()方法中,我们把GraphicalViewer的内容设计为流程,说明我们以后编辑的是一个流程对象,在configureGraphicalViewer(),我们把第二讲中定义的模型和控制器映射的类注册进来,这样编辑器就知道模型和控制器的映射关系了。
接下来我们再看一下面板对应的类:
package com.example.workflow.ui;
import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteSeparator;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.gef.requests.SimpleFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import com.example.workflow.Activator;
import com.example.workflow.model.Activity;
import com.example.workflow.model.EndActivity;
import com.example.workflow.model.StartActivity;
/** *//**
*UtilityclassthatcancreateaGEFPalette.
*@see#createPalette()
*/
finalclass WorkflowProcessEditorPaletteFactory {
/** *//**
*CreatesthePaletteRootandaddsallpaletteelements.
*Usethisfactorymethodtocreateanewpaletteforyourgraphicaleditor.
*@returnanewPaletteRoot
*/
static PaletteRoot createPalette(){
PaletteRoot palette = new PaletteRoot();
palette.add(createToolsGroup(palette));
palette.add(createActivitiesDrawer());
return palette;
}
/** *//**Createthe"Activities"drawer.*/
privatestatic PaletteContainer createActivitiesDrawer() {
PaletteDrawer componentsDrawer = new PaletteDrawer("Process");
CombinedTemplateCreationEntry component = new CombinedTemplateCreationEntry(
"Start",
"Create a StartActivity",
StartActivity.class,
new SimpleFactory(StartActivity.class),
ImageDescriptor.createFromFile(Activator.class, "icons/start16.gif"),
ImageDescriptor.createFromFile(Activator.class, "icons/start24.gif"));
componentsDrawer.add(component);
component = new CombinedTemplateCreationEntry(
"Activity",
"Create a Activity",
Activity.class,
new SimpleFactory(Activity.class),
ImageDescriptor.createFromFile(Activator.class, "icons/activity16.gif"),
ImageDescriptor.createFromFile(Activator.class, "icons/activity24.gif"));
componentsDrawer.add(component);
component = new CombinedTemplateCreationEntry(
"End",
"Create a EndActivity",
Activity.class,
new SimpleFactory(EndActivity.class),
ImageDescriptor.createFromFile(Activator.class, "icons/end16.gif"),
ImageDescriptor.createFromFile(Activator.class, "icons/end24.gif"));
componentsDrawer.add(component);
return componentsDrawer;
}
/** *//**Createthe"Tools"group.*/
privatestatic PaletteContainer createToolsGroup(PaletteRoot palette) {
PaletteGroup toolGroup = new PaletteGroup("Tools");
// Add a selection tool to the group
ToolEntry tool = new PanningSelectionToolEntry();
toolGroup.add(tool);
palette.setDefaultEntry(tool);
// Add a marquee tool to the group
toolGroup.add(new MarqueeToolEntry());
// Add a (unnamed) separator to the group
toolGroup.add(new PaletteSeparator());
tool = new ConnectionCreationToolEntry(
"Transition",
"Create a Transiton",
new CreationFactory() {
public Object getNewObject() { returnnull; }
public Object getObjectType() { returnnew Integer(1); }
},
ImageDescriptor.createFromFile(Activator.class, "icons/transition16.gif"),
ImageDescriptor.createFromFile(Activator.class, "icons/transition24.gif"));
toolGroup.add(tool);
return toolGroup;
}
}
这样,我们的gef项目就可以运行起来了,效果如下图:
对照面板的外观,读者应该很容易明白面板类的含义,下一节我们将介绍如何在编辑器中创建活动。
代码下载