Gef最简单入门-HelloWold(2)
ok 。上代码
模型类
package testgef.model;
public class HelloModel {
private String text = "Hello word";
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
这个不多说。提供一个文本hellowold
下一步。控制器。GEF中主要的控制器就是EditPart。这里继承一个AbstractGraphicalEitPart派生。注意。继承的时候会看到报错说找不到这个类。这个是没有导包的原因自己手动添加并且注意下导包是否正确
package testgef.controller;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import testgef.model.HelloModel;
//控制器
public class HelloEditorPart extends AbstractGraphicalEditPart{
@Override
protected IFigure createFigure() {
// TODO Auto-generated method stub
HelloModel model=(HelloModel)getModel();
Label label=new Label();
label.setText(model.getText());
return label;
}
@Override
protected void createEditPolicies() {
// TODO Auto-generated method stub
}
}
然后分析下这个类。createFigure()方法故名思意肯定是创建一个图形。getmodel获取这个模型。然后。在次方法里创建一个i额Label图形。ok。把model放入这个控件中。返回这个控件。
继续:工厂类。连接模型跟控制器。
package testgef.controller;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import testgef.model.HelloModel;
public class PartFactory implements EditPartFactory {
@Override
public EditPart createEditPart(EditPart context, Object model) {
// TODO Auto-generated method stub
EditPart part = getPartForElement(model);
part.setModel(model);
return part;
}
private EditPart getPartForElement(Object modelElement) {
if (modelElement instanceof HelloModel)
// TODO Auto-generated method stub
return new HelloEditorPart();
throw new RuntimeException("cant't create part for model element");
}
}
继续View注意id要跟plugin.xml里面的id
package hellowoldgef.editors;
import hellowoldgef.controller.PartFactory;
import hellowoldgef.model.HelloModel;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.ui.parts.GraphicalEditor;
public class HelloWoldEditor extends GraphicalEditor {
public static final String ID = "hellowoldgef.editors.HelloWoldEditor";
GraphicalViewer viewer;
public HelloWoldEditor() {
setEditDomain(new DefaultEditDomain(this));
}
protected void configureGraphicalViewer() {
super.configureGraphicalViewer();
viewer = getGraphicalViewer();
viewer.setEditPartFactory(new PartFactory());
}
//设置内容
@Override
protected void initializeGraphicalViewer() {
// TODO Auto-generated method stub
viewer.setContents(new HelloModel());
}
@Override
public void doSave(IProgressMonitor monitor) {
// TODO Auto-generated method stub
}
}
over。运行工程。在运行平台上随便新建一个java工程。在src新建一个文件。后缀名为hellowordgef。ok。亲爱的hello出来了
好吧。做完HelloWord的GEF出来之后。得出一个结论。嗯。够绕。不过总算最最基础的流程走了下。下面慢慢来吧。
提供一个这个小demo的下载地址。记得可能有导包的问题哦。
http://download.csdn.net/detail/wm198966/5884511