1

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?

Kfir Ettinger
  • 584
  • 4
  • 13

2 Answers2

1

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

Read the documentation and look at the code in DefaultMouseManager. I googled DefaultMouseManager, looked at the documentation, went through the inheritance of the different interfaces until I got to MouseListener, which describes the order of operations. Then I looked at mouseClicked and mouseReleased since they would be called last, mouseClicked is empty so that leaves mouseReleased and the methods that are called in it.

Don Pauer
  • 31
  • 2
  • 1
    I have looked there but there are many other methods that are called after mouseRelesed and mouseClicked. for example the methods from the class [DefaultView](https://github.com/graphstream/gs-ui-swing/blob/67888fae1f2bbc71b4d9ab50bdfd20e7ea391a67/src/org/graphstream/ui/swing_viewer/DefaultView.java) – Kfir Ettinger May 12 '22 at 09:06
  • The way I read the code is that mouseReleased calls a bunch of other methods (like I mentioned in my reply), the methods from DefaultView are probably part of that chain. I would just @override mouseReleased, copy the parts from DefaultMouseManager that you need and add your custom method to do what you want at the end of it, see where that gets you – Don Pauer May 13 '22 at 15:15
0

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.

Kfir Ettinger
  • 584
  • 4
  • 13