1
28 package demo.layout.tree;
29
30 import demo.view.DemoBase;
31 import demo.view.DemoDefaults;
32
33 import y.base.Node;
34 import y.base.NodeList;
35 import y.layout.tree.GenericTreeLayouter;
36 import y.view.Graph2D;
37 import y.view.NavigationMode;
38
39 import javax.swing.AbstractAction;
40 import javax.swing.ButtonGroup;
41 import javax.swing.JToggleButton;
42 import javax.swing.JToolBar;
43 import java.awt.EventQueue;
44 import java.awt.event.ActionEvent;
45 import java.net.URL;
46 import java.util.Locale;
47
48
54 public class TreeLayoutConfigurationDemo extends DemoBase {
55 public static void main(String[] args) {
56 EventQueue.invokeLater(new Runnable() {
57 public void run() {
58 Locale.setDefault(Locale.ENGLISH);
59 initLnF();
60 new TreeLayoutConfigurationDemo().start();
61 }
62 });
63 }
64
65 private void layout(TreeLayoutConfiguration configuration) {
66 GenericTreeLayouter genericTreeLayouter = new GenericTreeLayouter();
67 configuration.layout(genericTreeLayouter, view.getGraph2D());
68 view.fitContent();
69 view.updateView();
70 }
71
72 protected void initialize() {
73 loadGraph("resource/dfb2004.graphml");
74 layout(TreeLayoutConfiguration.PLAYOFFS);
75 }
76
77 protected void registerViewModes() {
78 view.addViewMode(new NavigationMode());
79 }
80
81 protected void loadGraph(URL resource) {
82 super.loadGraph(resource);
83 DemoDefaults.applyRealizerDefaults(view.getGraph2D());
84 }
85
86 protected JToolBar createToolBar() {
87 final AbstractAction[] actions = {
88 new AbstractAction("Playoffs") {
89 public void actionPerformed(ActionEvent e) {
90 view.getGraph2D().clear();
91 loadGraph("resource/dfb2004.graphml");
92 layout(TreeLayoutConfiguration.PLAYOFFS);
93 }
94 },
95 new AbstractAction("Playoffs Double") {
96 public void actionPerformed(ActionEvent e) {
97 view.getGraph2D().clear();
98 loadGraph("resource/dfb2004.graphml");
99 layout(TreeLayoutConfiguration.PLAYOFFS_DOUBLE);
100 }
101 },
102 new AbstractAction("Double Line") {
103 public void actionPerformed(ActionEvent e) {
104 view.getGraph2D().clear();
105 createTree(view.getGraph2D(), new int[]{1, 4, 6, 8});
106 layout(TreeLayoutConfiguration.DOUBLE_LINE);
107 }
108 },
109 new AbstractAction("Bus") {
110 public void actionPerformed(ActionEvent e) {
111 view.getGraph2D().clear();
112 createTree(view.getGraph2D(), new int[]{1, 4, 3, 8});
113 layout(TreeLayoutConfiguration.BUS);
114 }
115 },
116 new AbstractAction("Layered Tree") {
117 public void actionPerformed(ActionEvent e) {
118 view.getGraph2D().clear();
119 createTree(view.getGraph2D(), new int[]{1, 4, 4, 4});
120 layout(TreeLayoutConfiguration.LAYERED_TREE);
121 }
122 },
123 new AbstractAction("Default Delegating") {
124 public void actionPerformed(ActionEvent e) {
125 view.getGraph2D().clear();
126 createTree(view.getGraph2D(), new int[]{1, 4, 3, 8});
127 layout(TreeLayoutConfiguration.DEFAULT_DELEGATING);
128 }
129 },
130 };
131
132 final JToolBar jtb = super.createToolBar();
133 jtb.addSeparator();
134 final ButtonGroup group = new ButtonGroup();
135 for (int i = 0; i < actions.length; ++i) {
136 final JToggleButton jb = new JToggleButton(actions[i]);
137 jb.setSelected(i == 0);
138 jtb.add(jb);
139 group.add(jb);
140 }
141 return jtb;
142 }
143
144
147 protected boolean isUndoRedoEnabled() {
148 return false;
149 }
150
151
154 protected boolean isClipboardEnabled() {
155 return false;
156 }
157
158
161 public static Graph2D createTree(Graph2D graph, int[] childrenCountPerLayer) {
162 if (childrenCountPerLayer.length == 0) {
163 return graph;
164 }
165 if (childrenCountPerLayer[0] != 1) {
166 throw new IllegalArgumentException("The first layer must contain 1 node");
167 }
168
169 NodeList lastLayerContent = new NodeList();
170
171 Node node = graph.createNode();
173 lastLayerContent.add(node);
174
175 for (int i = 1; i < childrenCountPerLayer.length; i++) {
176 int childrenCount = childrenCountPerLayer[i];
177
178 NodeList newLayerContent = new NodeList();
179 for (int j = 0; j < lastLayerContent.size(); j++) {
180 Node parent = (Node) lastLayerContent.get(j);
181
182 for (int k = 0; k < childrenCount; k++) {
183 Node child = graph.createNode();
184 newLayerContent.add(child);
185 graph.setLabelText(child, String.valueOf(graph.N()));
186 graph.createEdge(parent, child);
187 }
188 }
189
190 lastLayerContent = newLayerContent;
191 }
192 return graph;
193 }
194
195 }
196