关键字:GEF Connection 选中后显示连线
GEF(Graphics Editor Framework)是一个挺复杂的图形编辑的框架结构。基本的概念就很多,我是参考《Eclipse插件开发》这本书学习的,这本书对与GEF的内容讲解的不好。 下个星期自己把这部分的内容整理一下。
说说连线 后显示的问题:
第一步:
覆盖refresh()函数,不让其调用refreshSourceConnections();和refreshTargetConnections()函数。
第二步:
在editorpart中增加
installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new SelectionEditPolicy());
的EditorPolicy()。
在SelectionEditPolicy 中实现选择时刷新连线。
public class SelectionEditPolicy extends SelectionEditPolicy {
protected void hideSelection() {
BlockEditpart editpart = (BlockEditpart)getHost();
setConnectionVisible(editpart.getSourceConnections(),false,null);
setConnectionVisible(editpart.getTargetConnections(),false,null);
}
protected void showPrimarySelection() {
BlockEditpart editpart = (BlockEditpart)getHost();
editpart.refreshConnections();
setConnectionVisible(editpart.getSourceConnections(),true,ColorConstants.red);
setConnectionVisible(editpart.getTargetConnections(),true,ColorConstants.red);
}
protected void showSelection() {
BlockEditpart editpart = (BlockEditpart)getHost();
setConnectionVisible(editpart.getSourceConnections(),true,ColorConstants.green);
setConnectionVisible(editpart.getTargetConnections(),true,ColorConstants.green);
}
private void setConnectionVisible(List<?> Connections, boolean b, Color fg) {
for(Object o : Connections) {
if(o instanceof ConnectionEditPart) {
ConnectionEditPart cep = (ConnectionEditPart)o;
cep.setVisible(b,fg);
}
}
}
}
这里要注意:refreshConnections中除了调用refreshSourceConnections(); refreshTargetConnections();还要手工写一个刷新连线对应的Editor,因为前面没有调用过,不然线就连不上。
private void refreshRelateConnection() {
Map<Object,EditPart> modelToEditPart = new HashMap<Object,EditPart>();
List<?> editParts = this.getParent().getChildren();
for(Object o : editParts) {
EditPart editPart = (EditPart)o;
if(!(editPart instanceof BlockEditpart)) {
continue;
}
modelToEditPart.put(editPart.getModel(), editPart);
}
List<?> ee = getSourceConnections();
for (int i = 0; i < ee.size(); i++) {
ConnectionModel c = getModelSourceConnections().get(i);
BlockEditpart e = (BlockEditpart)modelToEditPart.get(c.getTarget());
ConnectionEditPart cep = (ConnectionEditPart)ee.get(i);
e.addTargetConnection(cep, 0);
}
ee = getTargetConnections();
for (int i = 0; i < ee.size(); i++) {
ConnectionModel c = getModelTargetConnections().get(i);
BlockEditpart e = (BlockEditpart)modelToEditPart.get(c.getSource());
ConnectionEditPart cep = (ConnectionEditPart)ee.get(i);
e.addSourceConnection(cep, 0);
}
}
刷新对应连线,有连个注意点,第一,如何得到对应Node的Editorpart,这个采用了最蠢的方法,遍历所有的Editorparts,不知道有没有更好的方法;第二,不能用到的Editorpart直接调用refreshSourceConnections(); refreshTargetConnections();,因为这样使用的话,原始Node的所有连线是画好了,相关Node的连线又没有画好。
办法有点儿蠢,不过效果达到了。