X

Geertjan's Blog

  • October 11, 2011

Alphabetical Flow Chart

Geertjan Wielenga
Product Manager

Here's an alphabetical flow chart created via the NetBeans Visual Library:

Here's how:

public final class AlphabeticalFlowTopComponent extends TopComponent {
private static final String[] unsortedLabels = new String[] {
"For",
"Zoo",
"Fat",
"Jen",
"Wet",
"Pot",
"Bat",
"Fit",
"Cow",
"Dog",
"Jon",
"Cat",
"Pet",
"Hat",
"And"
};
public AlphabeticalFlowTopComponent() {
initComponents();
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 AlphabeticalFlowLayout(scene, connectionLayer));
scene.addChild(baseLayer);
scene.addChild(connectionLayer);
add(scene.createView(), BorderLayout.CENTER);
}
private final class AlphabeticalFlowLayout implements Layout {
private final LayerWidget connectionLayer;
private final Scene scene;
private AlphabeticalFlowLayout(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);
if (i == 0) {
child.resolveBounds(new Point(10, 20), null);
} else if (i < 5) {
x += 80;
child.resolveBounds(new Point(x, 20), null);
connectionLayer.addChild(new MyConnectionWidget(scene,
widgets.get(i - 1), widgets.get(i)));
} else if (i == 5) {
child.resolveBounds(new Point(x, 80), null);
connectionLayer.addChild(new MyConnectionWidget(scene,
widgets.get(i - 1), widgets.get(i)));
} else if (i < 9) {
x -= 80;
child.resolveBounds(new Point(x, 80), null);
connectionLayer.addChild(new MyConnectionWidget(scene,
widgets.get(i - 1), widgets.get(i)));
} else if (i == 9) {
x -= 80;
child.resolveBounds(new Point(x+10, 80), null);
connectionLayer.addChild(new MyConnectionWidget(scene,
widgets.get(i - 1), widgets.get(i)));
} else if (i == 10) {
child.resolveBounds(new Point(x+10, 140), null);
connectionLayer.addChild(new MyConnectionWidget(scene,
widgets.get(i - 1), widgets.get(i)));
} else if (i < 15) {
x += 80;
child.resolveBounds(new Point(x, 140), null);
connectionLayer.addChild(new MyConnectionWidget(scene,
widgets.get(i - 1), widgets.get(i)));
}
}
}
@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);
setTargetAnchorShape(AnchorShape.TRIANGLE_HOLLOW);
setSourceAnchor(AnchorFactory.createRectangularAnchor(source));
setTargetAnchor(AnchorFactory.createRectangularAnchor(target));
}
}
...
...
...

Some of the calculations could be prettier, but the approach should be clear.

Join the discussion

Comments ( 2 )
  • guest Tuesday, October 11, 2011

    nice work.


  • Leandro Tuesday, October 11, 2011

    Hi. Please, provide the import statements in your code. Does it only run in NetBeans? Which version?


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