简体   繁体   中英

JUNG collapse vertices based on vertex attribute

I have added to my GUI the collapse vertex function like:

private void collapseByVillageButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
    // TODO add your handling code here:
    showReligionCheckBox.setEnabled(false);
    showReligionCheckBox.setSelected(false);
    showVillageCheckBox.setEnabled(false);
    showVillageCheckBox.setSelected(false);
    collapseByVillageButton.setEnabled(false);
    collapseByReligionButton.setEnabled(false);
    vv.getRenderContext().setVertexShapeTransformer(religion_none);
    vv.getRenderContext().setVertexFillPaintTransformer(village_none);
    String[] villages = {"a", "b", "c", "d", "e"};
    for (String s : villages) {
        Collection picked = new HashSet();
        for (Potter p : g.getVertices()) {
            if (p.village.equals(s)) {
                picked.add(p);
            }
        }
        if (picked.size() > 1) {
            Graph inGraph = layout.getGraph();
            Graph clusterGraph = collapser.getClusterGraph(inGraph, picked);
            Graph graph = collapser.collapse(layout.getGraph(), clusterGraph);
            double sumx = 0;
            double sumy = 0;
            for (Object v : picked) {
                Point2D p = (Point2D) layout.transform((Potter) v);
                sumx += p.getX();
                sumy += p.getY();
            }
            Point2D cp = new Point2D.Double(sumx / picked.size(), sumy / picked.size());
            vv.getRenderContext().getParallelEdgeIndexFunction().reset();
            layout.setGraph(graph);
            layout.setLocation(clusterGraph, cp);
            vv.getPickedVertexState().clear();
            vv.repaint();
        }
    }

When I press the button, JUNG collapse the vertices according to the value of the property "village". This works, although I don't know if this is the right way to do it. One thing that I don't understand is: how do I change the new vertices labels in order to display something nicer that the list of names of the collapsed vertices?

EDIT:

I think this could do the trick in my case:

vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller() {
        @Override
        public String transform(Object v) {
            if (v instanceof Graph) {
                Collection vertices = ((Graph) v).getVertices();
                Potter next = (Potter) vertices.iterator().next();
                return next.village;
            }
            return super.transform(v);
        }
    });

Any hint more than welcome!

Best regards, Simone

You supply the labels for the collapsed vertices, so they can be whatever you like. By default you are probably using a ToStringLabeller but you can swap it out by providing a different vertex to label transformer to the pluggable renderer context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM