作者:谢绝沟通 | 来源:互联网 | 2023-09-17 13:29
1.新建一个Plug-inProject,名为FlowPlugin,Templates选择Multi-pageeditor,后缀指定为flow2.分为2个页签:源码和设计,源码使
1.新建一个Plug-in Project, 名为FlowPlugin, Templates选择Multi-page editor, 后缀指定为flow
2.分为2个页签: 源码和设计, 源码使用XMLEditor作为EditorPart(XMLEditor可以使用向导生成后复制过来),
private XMLEditor srcEditor;void createSrcPage() {try {srcEditor = new XMLEditor();int index = addPage(srcEditor, getEditorInput());setPageText(index, " 源 码 ");} catch (PartInitException e) {ErrorDialog.openError(getSite().getShell(),"Error creating nested text editor",null,e.getStatus());}}
设计使用GraphicalEditorWithFlyoutPalette作为EditorPart(这是内置好的画板工具栏)
private GraphicalEditorWithFlyoutPalette designEditor;
void createDesignPage() {try {designEditor = new FlowDiagramEditor();IPath path = ((IFileEditorInput) getEditorInput()).getFile().getProjectRelativePath();IEditorInput input = new FlowDiagramEditorInput(path);int index = addPage(designEditor, input);setPageText(index, " 设 计 ");setPartName(getEditorInput().getName()); // 设置标题} catch (PartInitException e) {ErrorDialog.openError(getSite().getShell(),"Error creating nested text editor",null,e.getStatus());}}
3.自定义FlowDiagramEditor继承GraphicalEditorWithFlyoutPalette添加我们的模型组件工具箱
import flowplugin.Activator;
import flowplugin.factory.EditPartFactory;
import flowplugin.model.HelloModel;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.SnapToGrid;
import org.eclipse.gef.palette.CreationToolEntry;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.SelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.SimpleFactory;
import org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;public class FlowDiagramEditor extends GraphicalEditorWithFlyoutPalette {
//1.添加ID,与扩展点ID保持一致,因此为了避免出错,建议ID统一为全类名public static final String ID = "flowplugin.editors.FlowDiagramEditor";// 2.视图GraphicalViewer viewer;public FlowDiagramEditor() {setEditDomain(new DefaultEditDomain(this));}/*** 1.首先配置视图* */@Overrideprotected void configureGraphicalViewer() {super.configureGraphicalViewer();// 1.1 获取视图viewer = getGraphicalViewer();viewer.setProperty(SnapToGrid.PROPERTY_GRID_ENABLED, true); // 启用网格viewer.setProperty(SnapToGrid.PROPERTY_GRID_VISIBLE, true); // 显示网格// 1.2 设置EditPartFactoryviewer.setEditPartFactory(new EditPartFactory());}/*** 3.配置调色板*/@Overrideprotected PaletteRoot getPaletteRoot() {// 在 Palette 中添加工具箱// 1.创建一个 Palette的rootPaletteRoot root = new PaletteRoot();// 2.创建一个工具组用来放置常规ToolPaletteGroup toolGroup = new PaletteGroup("ToolGroup");// 3.创建一个GEF提供的Selection工具并将其放到ToolGroup中ToolEntry tool = new SelectionToolEntry();toolGroup.add(tool);root.setDefaultEntry(tool); // 设置该工具是缺省被选择的工具// 4.创建一个GEF提供的"Marquee多选"工具并将其放到toolGroup中tool = new MarqueeToolEntry();toolGroup.add(tool);// 5.创建一个Drawer(抽屉)放置绘图工具,该抽屉名称为"画图"PaletteDrawer drawer = new PaletteDrawer("画图");// 指定"创建HelloModel模型"工具所对应的图标ImageDescriptor imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "/gar.ico");// 6.创建"创建HelloModel模型"工具CreationToolEntry creationEntry = new CreationToolEntry("绘制HelloModel", // The character string displayed on a palette"创建HelloModel模型", // Tool 提示new SimpleFactory(HelloModel.class), // The factory which createsimageDescriptor, // The image of 16X16 displayed on a paletteimageDescriptor);// The image of 24X24 displayed on a palettedrawer.add(creationEntry); // (7)将其加到前面创建的抽屉中// 8.最后将创建的两组工具加到root上.root.add(toolGroup);root.add(drawer);return root;}@Overridepublic void doSave(IProgressMonitor arg0) {// TODO Auto-generated method stub}}
4.自定义FlowDiagramEditorInput继承IPathEditorInput, 来设置编辑器输入来源
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IPersistableElement;public class FlowDiagramEditorInput implements IPathEditorInput {private IPath path;public FlowDiagramEditorInput(IPath path) {this.path = path;}@Overridepublic boolean exists() {// TODO Auto-generated method stubreturn false;}@Overridepublic ImageDescriptor getImageDescriptor() {// TODO Auto-generated method stubreturn null;}@Overridepublic String getName() {// TODO Auto-generated method stubreturn path.toString();}@Overridepublic IPersistableElement getPersistable() {// TODO Auto-generated method stubreturn null;}@Overridepublic String getToolTipText() {// TODO Auto-generated method stubreturn null;}@Overridepublic T getAdapter(Class arg0) {// TODO Auto-generated method stubreturn null;}@Overridepublic IPath getPath() {// TODO Auto-generated method stubreturn path;}}
5.重写编辑器工程EditPartFactory,将我们的模型组件放入工厂中代管理
import flowplugin.control.ContentsEditPart;
import flowplugin.model.HelloModel;
import org.eclipse.gef.EditPart;public class EditPartFactory implements org.eclipse.gef.EditPartFactory {@Overridepublic EditPart createEditPart(EditPart context, Object model) {// 1.根据模型创建其对应的控制器EditPart part = getPartForElement(model);// 2.将模型存入到控制器中part.setModel(model);return part;}/*** Maps an object to an EditPart.* * @throws RuntimeException if no match was found (programming error)*/private EditPart getPartForElement(Object modelElement) {// 根据模型创建其对应的控制器if (modelElement instanceof HelloModel)return new ContentsEditPart();throw new RuntimeException("Can't create part for model element: "+ ((modelElement != null) ? modelElement.getClass().getName() : "null"));}
}
6.创建图形编辑器ContentsEditPart继承AbstractGraphicalEditPart, 可以将初始化图形添加到画面上
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Layer;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;public class ContentsEditPart extends AbstractGraphicalEditPart {/*** 1.创建图形元素*/@Overrideprotected IFigure createFigure() {Layer figure = new Layer();return figure;}@Overrideprotected void createEditPolicies() {// TODO Auto-generated method stub}}
7.创建自定义模型HelloModel
public class HelloModel {/*** 1.展示的文本*/private String text = "Hello World";public String getText() {return text;}public void setText(String text) {this.text = text;}}
最终运行效果
![](https://img.php1.cn/3cd4a/1eebe/cd5/eec57030b649a106.webp?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N2eWdoMTIz,size_16,color_FFFFFF,t_70)
源码 https://download.csdn.net/download/svygh123/12035876