替代方法是,您應(yīng)當(dāng)編寫自己的 Figure 子類,該子類掩藏了圖形結(jié)構(gòu)的詳細(xì)信息。然后定義這個(gè)子類最少數(shù)量的 API,EditPart(控制器)用這些 API 來更新視圖。這種實(shí)踐(稱為 關(guān)注分離(separation of concerns))可提高重用機(jī)會并使錯(cuò)誤更少。
public class MyNodeEditPart extends AbstractGraphicalEditPart implements NodeEditPart { ... public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) { return new ChopboxAnchor(getFigure()); } public ConnectionAnchor getSourceConnectionAnchor(Request request) { return new ChopboxAnchor(getFigure()); } public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) { return new ChopboxAnchor(getFigure()); } public ConnectionAnchor getTargetConnectionAnchor(Request request) { return new ChopboxAnchor(getFigure()); }
public class MyNodeEditPart extends AbstractGraphicalEditPart implements NodeEditPart, ModelListener { ...
public void activate() { super.activate(); ((MyModel)getModel()).addModelListener(this); }
public void deactivate() { ((MyModel)getModel()).removeModelListener(this); super.deactivate(); }
public void modelChanged(ModelEvent event) { if (event.getChange().equals("outgoingConnections")) refreshSourceConnections(); else if (event.getChange().equals("incomingConnections")) refreshTargetConnections(); else if (event.getChange().equals("icon") || event.getChange().equals("name")) refreshVisuals(); }
public class MyEditor extends GraphicalEditor { public MyEditor() { setEditDomain(new DefaultEditDomain(this)); }
protected void configureGraphicalViewer() { super.configureGraphicalViewer(); //Sets the viewers background to System "white" getGraphicalViewer().setEditPartFactory(new MyGraphicalEditpartFactory()); }
protected void initializeGraphicalViewer() { getGraphicalViewer().setContents(MagicHelper.constructSampleDiagram()); } public void doSave(IProgressMonitor monitor) { ... } public void doSaveAs() { ... } public void gotoMarker(IMarker marker) { ... } public boolean isDirty() { ... } public boolean isSaveAsAllowed() { ... } }