1
14 package demo.view.layout;
15
16 import demo.view.DemoBase;
17 import y.base.Edge;
18 import y.base.Node;
19 import y.layout.BufferedLayouter;
20 import y.layout.CanonicMultiStageLayouter;
21 import y.layout.EdgeLabelLayout;
22 import y.layout.GraphLayout;
23 import y.layout.Layouter;
24 import y.layout.OrientationLayouter;
25 import y.layout.circular.CircularLayouter;
26 import y.layout.hierarchic.HierarchicLayouter;
27 import y.layout.organic.SmartOrganicLayouter;
28 import y.layout.orthogonal.OrthogonalLayouter;
29 import y.option.OptionHandler;
30 import y.util.D;
31 import y.view.Arrow;
32 import y.view.EdgeLabel;
33 import y.view.Graph2D;
34 import y.view.LayoutMorpher;
35 import y.anim.AnimationFactory;
36 import y.anim.AnimationPlayer;
37
38 import javax.swing.AbstractAction;
39 import javax.swing.JToolBar;
40 import javax.swing.SwingUtilities;
41 import javax.swing.JRootPane;
42 import java.awt.event.ActionEvent;
43 import java.util.Arrays;
44 import java.util.List;
45 import java.util.Random;
46
47
69 public class LayoutDemo extends DemoBase
70 {
71 private AnimationPlayer animationPlayer = new AnimationPlayer();
72 OptionHandler layoutOptions;
73
74 public LayoutDemo()
75 {
76 this(new String[]{"hierarchic", "anim", "label"});
77 }
78
79 public LayoutDemo(String[] args) {
80 animationPlayer.addAnimationListener( view );
81 initLayoutOptions( args );
82
83 view.getGraph2D().getDefaultEdgeRealizer().setArrow( Arrow.STANDARD );
85
86 buildGraph( view.getGraph2D() );
88 }
89
90
95 void initLayoutOptions(String[] args) {
96 boolean anim = true;
98 boolean label = true;
99 String layout = "hierarchic";
100 if (args.length > 0) {
101 layout = args[0];
102 List list = Arrays.asList(args);
103 anim = list.contains("anim");
104 label = list.contains("label");
105 }
106
107 layoutOptions = new OptionHandler("Settings");
108 final String[] algoEnum = {"hierarchic", "orthogonal", "organic", "circular" };
109 layoutOptions.addEnum("Layout Style", algoEnum, layout, null);
110 layoutOptions.addBool("Activate Generic Labeling", label);
111 layoutOptions.addBool("Activate Layout Morphing", anim);
112
113 if(args.length == 0) {
114 layoutOptions.showEditor();
115 }
116 }
117
118
119
120
121 void buildGraph(Graph2D graph)
122 {
123 graph.clear();
124 Node nodes[] = new Node[10];
125 for(int i = 0; i < nodes.length; i++)
126 {
127 nodes[i] = graph.createNode();
128 graph.getRealizer(nodes[i]).setLabelText(""+i);
129 }
130
131 Random random = new Random(0);
132 for ( int i = 0; i < nodes.length; i++ ) {
133 for ( int j = i + 1; j < nodes.length; j++ ) {
134 if ( random.nextDouble() > 0.75 ) {
135 Edge edge = graph.createEdge( nodes[ i ], nodes[ j ] );
136 EdgeLabel edgeLabel = new EdgeLabel( i + " -> " + j );
137 edgeLabel.setModel( EdgeLabel.SIDE_SLIDER );
138 edgeLabel.setPreferredPlacement( EdgeLabelLayout.PLACE_AT_CENTER );
139 graph.getRealizer( edge ).addLabel( edgeLabel );
140 }
141 }
142 }
143 }
144
145
148 protected JToolBar createToolBar() {
149 JToolBar bar = super.createToolBar();
150 bar.addSeparator();
151 bar.add(new LayoutAction());
152 return bar;
153 }
154
155
158 class LayoutAction extends AbstractAction {
159 LayoutAction() {
160 super("Auto-Layout Graph");
161 }
162
163 public void actionPerformed(ActionEvent e) {
164 if(layoutOptions.showEditor()) {
165 applyLayout();
166 }
167 }
168 }
169
170
173 void applyLayout() {
174 Layouter layouter = createLayouter(layoutOptions);
175 applyLayout(layouter, layoutOptions.getBool("Activate Layout Morphing"));
176 }
177
178
181 Layouter createLayouter(OptionHandler layoutOptions) {
182
183 String layout = layoutOptions.getString("Layout Style");
184 boolean label = layoutOptions.getBool("Activate Generic Labeling");
185
186 CanonicMultiStageLayouter layouter = null;
187
188 if (layout.equals("circular")) {
189 CircularLayouter cl = new CircularLayouter();
190 cl.getSingleCycleLayouter().setMinimalNodeDistance(100);
191 layouter = cl;
192 } else if (layout.equals("hierarchic")) {
193 HierarchicLayouter hl = new HierarchicLayouter();
194 hl.setMinimalLayerDistance(60);
196 hl.setMinimalNodeDistance(20);
197
198 OrientationLayouter ol = new OrientationLayouter();
200 ol.setOrientation(OrientationLayouter.LEFT_TO_RIGHT);
201 hl.setOrientationLayouter(ol);
202
203 layouter = hl;
204 } else if (layout.equals("organic")) {
205 SmartOrganicLayouter ol = new SmartOrganicLayouter();
206 ol.setPreferredEdgeLength(80);
208 ol.setQualityTimeRatio(1.0);
209 ol.setNodeOverlapsAllowed(false);
210 layouter = ol;
211 } else if (layout.equals("orthogonal")) {
212 OrthogonalLayouter ol = new OrthogonalLayouter();
213 layouter = ol;
215 }
216
217 if (layouter == null) usage();
218
219 if (label) {
220 layouter.setLabelLayouterEnabled(true);
222 }
223
224 return layouter;
225 }
226
227
233 void applyLayout(Layouter layouter, boolean animated) {
234 if (animated) {
235 GraphLayout gl = new BufferedLayouter(layouter).calcLayout(view.getGraph2D());
236
237 LayoutMorpher morpher = new LayoutMorpher( view, gl );
238 morpher.setPreferredDuration( 800 );
239 animationPlayer.animate( AnimationFactory.createEasedAnimation( morpher ) );
240 } else {
241 layouter.doLayout(view.getGraph2D());
242
243 view.fitContent();
246
247
251
253 view.updateView();
254 }
255 }
256
257
258 void usage() {
259 D.bug("USAGE: java demo.view.layout.LayoutDemoTmp " +
260 "{organic,circular,random,hierarchic,orthogonal} [label] [anim]");
261 System.exit(0);
262 }
263
264 public void addContentTo( final JRootPane rootPane ) {
265 super.addContentTo( rootPane );
266 SwingUtilities.invokeLater(new Runnable() {
267 public void run() {
268 applyLayout();
269 }
270 });
271 }
272
273 public static void main(String[] args)
274 {
275 initLnF();
276 final LayoutDemo demo = new LayoutDemo(args);
277
278 demo.start(demo.getClass().getName());
279
280
285 SwingUtilities.invokeLater(new Runnable() {
286 public void run() {
287 demo.applyLayout();
288 }
289 });
290 }
291 }
292