X

Geertjan's Blog

  • October 11, 2011

Alphabetical Hierarchy Chart

Geertjan Wielenga
Product Manager

Here's a different kind of chart you can make with the NetBeans Visual Library, using some dummy data (i.e., as far as I know there's not much actual correlation between "zen" and "dog"):

Here's how:

public final class AlphabeticalHierarchicalTopComponent extends TopComponent {
    private static final String[] unsortedLabels = new String[]{
        "Jon",
        "Cat",
        "Pet",
        "Sim",
        "Zen",
        "Fat",
        "Dog",
        "Hat",
        "Tim",
        "Sid",
        "And"
    };
    public AlphabeticalHierarchicalTopComponent() {
        initComponents();
        setName("Alphabetical Hierarchy Layout");
        setLayout(new BorderLayout());
        Scene scene = new Scene();
        LayerWidget baseLayer = new LayerWidget(scene);
        LayerWidget connectionLayer = new LayerWidget(scene);
        for (String label : unsortedLabels) {
            LabelWidget lw = new LabelWidget(scene, label);
            lw.setBorder(
                    BorderFactory.createRoundedBorder(5, 5, Color.CYAN, Color.BLACK));
            baseLayer.addChild(lw, label);
        }
        baseLayer.setLayout(new AlphabeticalHierarchyLayout(scene, connectionLayer));
        scene.addChild(baseLayer);
        scene.addChild(connectionLayer);
        add(scene.createView(), BorderLayout.CENTER);
    }
    private final class AlphabeticalHierarchyLayout implements Layout {
        private final LayerWidget connectionLayer;
        private final Scene scene;
        private AlphabeticalHierarchyLayout(Scene scene, LayerWidget connectionLayer) {
            this.scene = scene;
            this.connectionLayer = connectionLayer;
        }
        @Override
        public void layout(final Widget widget) {
            ArrayList<Widget> widgets = new ArrayList<Widget>(widget.getChildren());
            Collections.sort(widgets, new Comparator<Widget>() {
                @Override
                public int compare(Widget o1, Widget o2) {
                    return ((String) widget.getChildConstraint(o1)).compareTo((String) widget.getChildConstraint(o2));
                }
            });
            int x = 0;
            for (int i = 0; i < widgets.size(); i++) {
                Widget child = widgets.get(i);
                Widget rootChild = widgets.get(0);
                if (i == 0) {
                    child.resolveBounds(new Point(170, 20), null);
                } else if (i < 5) {
                    x += 70;
                    child.resolveBounds(new Point(x, 80), null);
                    connectionLayer.addChild(new MyConnectionWidget(scene,
                            rootChild, widgets.get(i)));
                } else if (i == 5) {
                    x -= 100;
                    child.resolveBounds(new Point(x, 140), null);
                    connectionLayer.addChild(new MyConnectionWidget(scene,
                            widgets.get(3), widgets.get(5)));
                } else if (i == 6) {
                    x += 70;
                    child.resolveBounds(new Point(x, 140), null);
                    connectionLayer.addChild(new MyConnectionWidget(scene,
                            widgets.get(3), widgets.get(6)));
                } else if (i == 7) {
                    x += 70;
                    child.resolveBounds(new Point(x, 140), null);
                    connectionLayer.addChild(new MyConnectionWidget(scene,
                            widgets.get(3), widgets.get(7)));
                } else if (i == 8) {
                    x += 70;
                    child.resolveBounds(new Point(x, 140), null);
                    connectionLayer.addChild(new MyConnectionWidget(scene,
                            widgets.get(1), widgets.get(8)));
                } else if (i == 9) {
                    x += 70;
                    child.resolveBounds(new Point(x, 140), null);
                    connectionLayer.addChild(new MyConnectionWidget(scene,
                            widgets.get(1), widgets.get(9)));
                } else if (i == 10) {
                    x -= 70;
                    child.resolveBounds(new Point(x, 210), null);
                    connectionLayer.addChild(new MyConnectionWidget(scene,
                            widgets.get(8), widgets.get(10)));
                    connectionLayer.addChild(new MyConnectionWidget(scene,
                            widgets.get(10), widgets.get(2)));
                }
            }
        }
        @Override
        public boolean requiresJustification(Widget widget) {
            return false;
        }
        @Override
        public void justify(Widget widget) {
        }
    }
    public class MyConnectionWidget extends ConnectionWidget {
        public MyConnectionWidget(Scene scene, Widget source, Widget target) {
            super(scene);
            setSourceAnchor(AnchorFactory.createDirectionalAnchor(
                    source, AnchorFactory.DirectionalAnchorKind.VERTICAL));
            setTargetAnchor(AnchorFactory.createDirectionalAnchor(
                    target, AnchorFactory.DirectionalAnchorKind.VERTICAL));
            setRouter(RouterFactory.createOrthogonalSearchRouter());
            setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        }
    }
...
...
...

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.