Skip to content
Advertisement

override graphstream’s DefaultMouseManager

I’m using GraphStream to show a map of an area and I’ve tried to inherit from the default MouseManager DefaultMouseManager and to override the mouseClicked method so that when clicking on a node the following will happened:

  1. node’s color will change.
  2. node’s label will show.
  3. node’s data will show in terminal.

I do know that the method works because the node’s data does get printed to terminal, but I think some other mouse event repaint the node and rehide the label so they doesn’t change when clicking on a node.

here is my MouseManager’s code:

public class CustomMouseManager2 extends DefaultMouseManager {
    protected View view;
    protected GraphicGraph graph;
    private GraphicElement focusedNode;

    @Override
    public void init(GraphicGraph graph, View view) {
        super.init(graph, view);
        .
        .
        .
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        GraphicElement currentNode = view.findNodeOrSpriteAt(e.getX(), e.getY());

        if(currentNode != null){
            OGraph graph = OGraph.getInstance();

            Random r = new Random();
            currentNode.setAttribute("ui.style", "fill-color: red; text-mode: normal;");
            ONode oNode = graph.getNode(Long.parseLong(currentNode.getLabel()));
            System.out.println(oNode.toString());

        }

        if(focusedNode!= null)
            focusedNode.setAttribute("ui.style", "fill-color: black;size: 10px, 10px; text-mode: hidden;");

        focusedNode = currentNode;
    }
}

I’ve tried to check what methods from the base class DefaultMouseManager are called after my mouseClicked is called so I could override them too, but there was to many of them to follow.

Is there an elegant way to make sure my changes will execute after all other method from the base class?

Advertisement

Answer

So, something similar to this question has happened, the mouseClicked() method was called twice.

In my code, I repaint black the previous node and hide its label after a new node is clicked. And for that reason, when the mouseClicked() method was called twice then the first call changed the node`s appearance and the second one changed it back.

In that case, an easy fix will be to check if the previous node and current node are the same. replace this if(focusedNode!= null) with this

if(focusedNode!= null && focusedNode != currentNode)

but a more straightforward solution will be to understand why the method is been called twice.
My guess is that it has something to do with the inheritance but I’m not sure.

Advertisement