1
14 package demo.view.rendering;
15
16 import demo.view.DemoBase;
17 import y.base.Edge;
18 import y.base.Node;
19 import y.geom.YPoint;
20 import y.view.BridgeCalculator;
21 import y.view.DefaultGraph2DRenderer;
22 import y.view.Graph2D;
23
24 import javax.swing.JComboBox;
25 import javax.swing.JLabel;
26 import javax.swing.JToolBar;
27 import java.awt.EventQueue;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.util.Locale;
31
32
40 public class BridgeDemo extends DemoBase {
41 BridgeCalculator bridgeCalculator;
42
43 public BridgeDemo() {
44 bridgeCalculator = new BridgeCalculator();
46
47 ((DefaultGraph2DRenderer) view.getGraph2DRenderer()).setBridgeCalculator(bridgeCalculator);
49
50 final Graph2D graph = view.getGraph2D();
52 Node[] nodes = new Node[16];
53
54 int count = 0;
55 double hDist = graph.getDefaultNodeRealizer().getWidth() + 10;
56 double vDist = graph.getDefaultNodeRealizer().getHeight() + 10;
57
58 for (int i = 1; i < 5; i++) {
59 nodes[count++] = graph.createNode(50 + hDist * i, vDist*5+50);
60 nodes[count++] = graph.createNode(50 + hDist * i, 40);
61 nodes[count++] = graph.createNode(40, 50 + vDist * i);
62 nodes[count++] = graph.createNode(50+hDist*5, 50 + vDist * i);
63 }
64
65 graph.createEdge(nodes[0], nodes[1]);
66
67 Edge e = graph.createEdge(nodes[0], nodes[1]);
68 graph.setSourcePointRel(e, new YPoint(5, 0));
69 graph.setTargetPointRel(e, new YPoint(5, 0));
70
71 graph.createEdge(nodes[5], nodes[4]);
72
73 graph.createEdge(nodes[2], nodes[3]);
74 e = graph.createEdge(nodes[2], nodes[3]);
75 graph.setSourcePointRel(e, new YPoint(0, 5));
76 graph.setTargetPointRel(e, new YPoint(0, 5));
77
78 graph.createEdge(nodes[7], nodes[6]);
79
80 graph.createEdge(nodes[2 + 8], nodes[3 + 8]);
81 graph.createEdge(nodes[7 + 8], nodes[6 + 8]);
82
83 graph.createEdge(nodes[0 + 8], nodes[1 + 8]);
84 graph.createEdge(nodes[5 + 8], nodes[4 + 8]);
85 }
86
87 protected JToolBar createToolBar() {
88 final JComboBox crossingModeCB = new JComboBox(new Object[]{"Horizontal Crosses Vertical", "Vertical Crosses Horizontal", "Induced by Edge Ordering"});
89 crossingModeCB.setMaximumSize(crossingModeCB.getPreferredSize());
90 crossingModeCB.setSelectedIndex(0);
91 crossingModeCB.addActionListener(new ActionListener() {
92 public void actionPerformed(ActionEvent e) {
93 bridgeCalculator.setCrossingMode(getCrossingModeForIndex(crossingModeCB.getSelectedIndex()));
94 view.getGraph2D().updateViews();
95 }
96 });
97 final JComboBox crossingStyleCB = new JComboBox(new Object[]{"Gap", "Arc", "Square", "Two Sides", "Scaled Arc", "Scaled Square", "Scaled Two Sides"});
98 crossingStyleCB.setMaximumSize(crossingStyleCB.getPreferredSize());
99 crossingStyleCB.setSelectedIndex(1);
100 crossingStyleCB.addActionListener(new ActionListener() {
101 public void actionPerformed(ActionEvent e) {
102 bridgeCalculator.setCrossingStyle((short) crossingStyleCB.getSelectedIndex());
103 view.getGraph2D().updateViews();
104 }
105 });
106 final JComboBox orientationStyleCB = new JComboBox(
107 new Object[]{"Up", "Down", "Left", "Right", "Positive", "Negative", "Flow Left", "Flow Right"});
108 orientationStyleCB.setMaximumSize(orientationStyleCB.getPreferredSize());
109 orientationStyleCB.setSelectedIndex(4);
110 orientationStyleCB.addActionListener(new ActionListener() {
111 public void actionPerformed(ActionEvent e) {
112 bridgeCalculator.setOrientationStyle((short) (orientationStyleCB.getSelectedIndex() + 1));
113 view.getGraph2D().updateViews();
114 }
115 });
116
117 final JToolBar toolBar = super.createToolBar();
118 toolBar.addSeparator();
119 toolBar.add(new JLabel("Bridge Style: "));
120 toolBar.add(crossingStyleCB);
121 toolBar.addSeparator(TOOLBAR_SMALL_SEPARATOR);
122 toolBar.add(orientationStyleCB);
123 toolBar.addSeparator(TOOLBAR_SMALL_SEPARATOR);
124 toolBar.add(crossingModeCB);
125 return toolBar;
126 }
127
128 private static short getCrossingModeForIndex(int index) {
129 switch (index) {
130 case 0:
131 default:
132 return BridgeCalculator.CROSSING_MODE_HORIZONTAL_CROSSES_VERTICAL;
133 case 1:
134 return BridgeCalculator.CROSSING_MODE_VERTICAL_CROSSES_HORIZONTAL;
135 case 2:
136 return BridgeCalculator.CROSSING_MODE_ORDER_INDUCED;
137 }
138 }
139
140 public static void main(String[] args) {
141 EventQueue.invokeLater(new Runnable() {
142 public void run() {
143 Locale.setDefault(Locale.ENGLISH);
144 initLnF();
145 new BridgeDemo().start("Bridge Demo");
146 }
147 });
148 }
149 }
150