Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » View is not refreshing after model element has been removed & refreshChildren() called(The view is only refrehed after I close and re-open the editor...)
icon9.gif  View is not refreshing after model element has been removed & refreshChildren() called [message #892379] Wed, 27 June 2012 23:09 Go to next message
Stephan Druskat is currently offline Stephan DruskatFriend
Messages: 104
Registered: October 2011
Location: Berlin, Germany
Senior Member

Dear All,

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.

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. However, when I close the editor (and persist the model), and re-open it, the view is being updated, and the figure for the model element is gone... I'd be extremely grateful if somebody could have a look at my sources and point me to any problems (and possibly solutions Smile). Many thanks in advance!

Below are what I think are the important classes for this issue. Please do let me know should I add further code/edit the code, etc. (I've left out code that I thought doesn't help, e.g., getters and setters, class variables). Thanks!

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().getSNodes().contains(getMyNode())); 
            // Returns false, i.e., graph doesn't contain model object at this point!
    }

    @Override public void undo() {
        getMyNode().setDiagram(getGraph());
    }

}
Re: View is not refreshing after model element has been removed & refreshChildren() called [message #892423 is a reply to message #892379] Thu, 28 June 2012 08:03 Go to previous messageGo to next message
Jan Krakora is currently offline Jan KrakoraFriend
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Hi,

are you sure that the refreshChildren() in your adapter is called?

If yes, you should check whether the allModelObjects you return from the getModelchildren() really doesn't contain the deleted node.

If so, I would try to debug the refreshChildren.

[Updated on: Thu, 28 June 2012 08:09]

Report message to a moderator

Re: View is not refreshing after model element has been removed & refreshChildren() called [message #892524 is a reply to message #892423] Thu, 28 June 2012 14:26 Go to previous messageGo to next message
Stephan Druskat is currently offline Stephan DruskatFriend
Messages: 104
Registered: October 2011
Location: Berlin, Germany
Senior Member

Thank you, Jan, for your reply!

Jan Krakora wrote on Thu, 28 June 2012 10:03
are you sure that the refreshChildren() in your adapter is called?


Yes it is, I've overridden refreshChildren(), called super.refreshChildren() from it and added a System.out feedback which does get displayed when I remove a node.

Jan Krakora wrote on Thu, 28 June 2012 10:03
If yes, you should check whether the allModelObjects you return from the getModelchildren() really doesn't contain the deleted node.


Bingo. It does in fact still contain the deleted node. Thanks for pointing that out. I'll investigate further and will get back here should I have further queries.

Thanks so much for your help!
Re: View is not refreshing after model element has been removed & refreshChildren() called [message #892561 is a reply to message #892524] Thu, 28 June 2012 15:50 Go to previous messageGo to next message
Stephan Druskat is currently offline Stephan DruskatFriend
Messages: 104
Registered: October 2011
Location: Berlin, Germany
Senior Member

sted 13 wrote on Thu, 28 June 2012 16:26
I'll investigate further and will get back here should I have further queries.


Hm, I really cannot figure out why the node is still there! Feels as if the model object is read-only!
Re: View is not refreshing after model element has been removed & refreshChildren() called [message #892568 is a reply to message #892561] Thu, 28 June 2012 16:01 Go to previous messageGo to next message
Jan Krakora is currently offline Jan KrakoraFriend
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Well, I think it's an EMF issue and I don't know the EMF at all. So, it's up to you.
Re: View is not refreshing after model element has been removed & refreshChildren() called [message #892571 is a reply to message #892568] Thu, 28 June 2012 16:05 Go to previous message
Stephan Druskat is currently offline Stephan DruskatFriend
Messages: 104
Registered: October 2011
Location: Berlin, Germany
Senior Member

Jan Krakora wrote on Thu, 28 June 2012 18:01
Well, I think it's an EMF issue and I don't know the EMF at all. So, it's up to you.


Okay, no problem. Thanks a lot for your help so far!
Previous Topic:graphicalViewer.reveal(editPart) draws over other views
Next Topic:[Zest]Question about dynamically refreshing a graph
Goto Forum:
  


Current Time: Tue Mar 19 05:51:22 GMT 2024

Powered by FUDForum. Page generated in 0.02085 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top