Search code examples
javaprefuse

Prefuse. When hovering over a node, how can I change the connected edges' visualization?


I use this typical snippet (from the prefuse examples) to change the color of one of my nodes when the mouse is over it:

ColorAction nFill = new ColorAction(NODES, VisualItem.FILLCOLOR);
nFill.setDefaultColor(ColorLib.gray(255));
nFill.add("_hover", ColorLib.gray(200));

I'd like to set the color for the edges in & out of this node to a different color too, preferably a different color for the ins than for the outs, but I can't find the right predicate to use.

I'm using a directed graph, in case it matters.

Is there a way to iterate over the children/parents of the current node/edge in the predicate API ? Do you have a solution to my actual issue ?


Solution

  • I found a way to do it without predicates, but by creating my own ColorAction subclass:

    class ConnectedEdgeColorAction extends ColorAction {
    
        final int outgoing = ColorLib.rgb(255, 100, 100);
        final int incoming = ColorLib.rgb(100, 255, 100);
        final int none = ColorLib.gray(100);
    
        public ConnectedEdgeColorAction(String group, String field) {
            super(group, field);
        }
    
        @Override
        public int getColor(VisualItem item) {
            if (item instanceof EdgeItem) {
                if (((EdgeItem) item).getSourceItem().isHover()) {
                    return outgoing;
                } else if (((EdgeItem) item).getTargetItem().isHover()) {
                    return incoming;
                }
            }
    
            return none;
        }
    
    }
    

    Then, I use that as the main color action for my edges:

    ColorAction nEdges = new ConnectedEdgeColorAction(EDGES, VisualItem.STROKECOLOR);
    

    I don't know if it's the "preferred" way to do it, but it works well enough for me.