GEF + EMF:为什么在refreshChildren()被调用时,我的编辑器不会删除被删除对象的图? [英] GEF + EMF: Why doesn't my editor remove the Figure for a removed object when refreshChildren() is called?
问题描述
我已经为图形的EMF模型实现了一个GEF编辑器,并在图中显示了某种类型的节点的remove命令。我想我已经完成了所有必要的步骤,以使其设置工作( vainolo 的博客已经非常有帮助)。
I have implemented a GEF editor for a graph-like EMF model, with a remove command for a certain type of node in the graph. I think I've done all the necessary steps in order to make this set up work (vainolo's blog has been a great help).
但是,当我删除模型元素时,视图不会刷新,即,模型元素的图形不会从编辑器视图中删除,我不知道为什么。如果有人可以看看我的来源,并指出任何问题(可能的解决方案:),我将非常感激。非常感谢您提前!
However, when I'm deleting a model element, the view doesn't get refreshed, i.e., the figure for the model element isn't removed from the editor view, and I have no idea why. I'd be extremely grateful if somebody could have a look at my sources and point me to any problems (and possibly solutions :)). Many thanks in advance!
以下是我认为是这个问题的重要课程。请允许我知道,如果我添加进一步的代码/编辑代码等(我已经省略了我认为没有帮助的代码,例如getter和setter,类变量)。谢谢!
DiagramEditPart
public class DiagramEditPart extends AbstractGraphicalEditPart {
public DiagramEditPart(Diagram model) {
this.setModel(model);
adapter = new DiagramAdapter();
}
@Override protected IFigure createFigure() {
Figure figure = new FreeformLayer();
return figure;
}
@Override protected void createEditPolicies() {
installEditPolicy(EditPolicy.LAYOUT_ROLE, new DiagramXYLayoutPolicy());
}
@Override protected List<EObject> getModelChildren() {
List<EObject> allModelObjects = new ArrayList<EObject>();
if (((Diagram) getModel()).getMyNodes() != null)
allModelObjects.addAll(((Diagram) getModel()).getMyNodes());
return allModelObjects;
}
@Override public void activate() {
if(!isActive()) {
((Diagram) getModel()).eAdapters().add(adapter);
}
super.activate();
}
@Override public void deactivate() {
if(isActive()) {
((Diagram) getModel()).eAdapters().remove(adapter);
}
super.deactivate();
}
public class DiagramAdapter implements Adapter {
@Override public void notifyChanged(Notification notification) {
switch (notification.getEventType()) {
case Notification.REMOVE: refreshChildren();
break;
default:
break;
}
}
@Override public Notifier getTarget() {
return (Diagram) getModel();
}
@Override public void setTarget(Notifier newTarget) {
// Do nothing.
}
@Override public boolean isAdapterForType(Object type) {
return type.equals(Diagram.class);
}
}
}
MyNodeEditPart
public class MyNodeEditPart extends AbstractGraphicalEditPart {
public MyNodeEditPart(MyNode model) {
this.setModel(model);
adapter = new MyNodeAdapter();
}
@Override protected IFigure createFigure() {
return new MyNodeFigure();
}
@Override protected void createEditPolicies() {
installEditPolicy(EditPolicy.COMPONENT_ROLE, new MyNodeComponentEditPolicy());
}
@Override protected void refreshVisuals() {
MyNodeFigure figure = (MyNodeFigure) getFigure();
DiagramEditPart parent = (DiagramEditPart) getParent();
Dimension labelSize = figure.getLabel().getPreferredSize();
Rectangle layout = new Rectangle((getParent().getChildren().indexOf(this) * 50),
(getParent().getChildren().indexOf(this) * 50), (labelSize.width + 20),
(labelSize.height + 20));
parent.setLayoutConstraint(this, figure, layout);
}
public List<Edge> getModelSourceConnections() {
if ((MyNode) getModel() != null && ((MyNode) getModel()).getDiagram() != null) {
ArrayList<Edge> sourceConnections = new ArrayList<Edge>();
for (Edge edge : ((MyNode) getModel()).getDiagram().getOutEdges(((MyNode) getModel()).getId())) {
sourceConnections.add(edge);
}
return sourceConnections;
}
return null;
}
// + the same method for targetconnections
@Override public void activate() {
if (!isActive()) {
((MyNode) getModel()).eAdapters().add(adapter);
}
super.activate();
}
@Override public void deactivate() {
if (isActive()) {
((MyNode) getModel()).eAdapters().remove(adapter);
}
super.deactivate();
}
public class MyNodeAdapter implements Adapter {
@Override
public void notifyChanged(Notification notification) {
refreshVisuals();
}
@Override
public Notifier getTarget() {
return (MyNode) getModel();
}
@Override
public void setTarget(Notifier newTarget) {
// Do nothing
}
@Override
public boolean isAdapterForType(Object type) {
return type.equals(MyNode.class);
}
}
}
MyNodeComponentEditPolicy
public class MyNodeComponentEditPolicy extends ComponentEditPolicy {
@Override
protected Command createDeleteCommand(GroupRequest deleteRequest) {
DeleteMyNodeCommand nodeDeleteCommand = new DeleteMyNodeCommand((MyNode) getHost().getModel());
return nodeDeleteCommand;
}
}
DeleteMyNodeCommand
public class DeleteMyNodeCommand extends Command {
public DeleteMyNodeCommand(MyNode model) {
this.node = model;
this.graph = node.getDiagram();
}
@Override public void execute() {
getMyNode().setDiagram(null);
System.out.println("Is the model still present in the graph? " + getGraph().getMyNodes().contains(getMyNode()));
// Returns false, i.e., graph doesn't contain model object at this point!
}
@Override public void undo() {
getMyNode().setDiagram(getGraph());
}
}
EDIT
重新执行execc的注释:是的,$ code> refreshChildren()正在被调用。我已经通过覆盖它并添加了一个简单的 System.err
行来测试这一行,该行在删除节点时在控制台上显示:
EDIT
Re execc's comment: Yes, refreshChildren()
is being called. I've tested this by overriding it and adding a simple System.err
line, which is being displayed on the console on deletion of a node:
@Override
public void refreshChildren() {
super.refreshChildren();
System.err.println("refreshChildren() IS being called!");
}
EDIT 2
有趣的事情是,当我关闭编辑器并保留模型,然后重新打开相同的文件,节点不再被绘制,不存在于模型中。但这是什么意思?我在一个陈旧的模型上工作吗?或者是让孩子们看起来更好吗?
EDIT 2
The funny (well...) thing is, when I close the editor and persist the model, then re-open the same file, the node isn't painted anymore, and is not present in the model. But what does this mean? Am I working on a stale model? Or is refreshing/getting the model children not working properly?
我刚刚发现了一个特殊的东西,哪个可能解释我有的东西?在 getModelChildren()
方法中,我调用 allModelObjects.addAll((())getModel())。getMyNodes());
和 getMyNodes()
返回不可修改 EList
。我在delete命令中尝试沿着((Diagram)getModel())。getMyNodes()。remove(getMyNode())
并且它抛出一个 UnsupportedOperationException
... Hm。
I've just found a peculiar thing, which might explain the isues I have? In the getModelChildren()
method I call allModelObjects.addAll(((Diagram) getModel()).getMyNodes());
, and getMyNodes()
returns an unmodifiable EList
. I found out when I tried to do something along the lines of ((Diagram) getModel()).getMyNodes().remove(getMyNode())
in the delete command, and it threw an UnsupportedOperationException
... Hm.
呃,有人杀我吗?
我已经仔细检查过我是否处理相同的图表
对象,而在这样做的时候,我偶然发现了一个非常尴尬的 / strong>东西:
Er, somebody kill me please?
I've double-checked whether I'm handling the same Diagram
object at all times, and while doing this I stumbled across a very embarassing thing:
DiagramEditPart $ c中的
getModelChildren()
$ c>在最后一个版本读约像这样:
The getModelChildren()
method in DiagramEditPart
in the last version read approx. like this:
@Override protected List<EObject> getModelChildren() {
List<EObject> allModelObjects = new ArrayList<EObject>();
EList<MyNode> nodes = ((Diagram) getModel()).getMyNodes();
for (MyNode node : nodes) {
if (node.getDiagram() != null); // ### D'Uh! ###
allModelObjects.add(node);
}
return allModelObjects;
}
我想为窃取大家的时间道歉!您的建议非常有帮助,确实帮助我最终追踪错误!
I'd like to apologize for stealing everyone's time! Your suggestions were very helpful, and indeed helped my to finally track down the bug!
我还学到了一些课程,其中包括:始终粘贴原始代码过度简单可能会掩盖您的错误!我学到了很多关于EMF,适配器
和GEF。仍然:
I've also learned a number of lessons, amongst them: Always paste the original code, over-simplifaction may cloak your bugs! And I've learned a lot about EMF, Adapter
, and GEF. Still:
推荐答案
以下部分代码中的第5行中有一个分号太多,即if语句之后: if(node.getDiagram()!= null);
:
There is one semi-colon too many in line 5 of the following part of the code, namely after the if statement: if (node.getDiagram() != null);
:
1 @Override protected List<EObject> getModelChildren() {
2 List<EObject> allModelObjects = new ArrayList<EObject>();
3 EList<MyNode> nodes = ((Diagram) getModel()).getMyNodes();
4 for (MyNode node : nodes) {
5 if (node.getDiagram() != null);
6 allModelObjects.add(node);
7 }
8 return allModelObjects;
9 }
这篇关于GEF + EMF:为什么在refreshChildren()被调用时,我的编辑器不会删除被删除对象的图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!