1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
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.Arrow;
21  import y.view.BridgeCalculator;
22  import y.view.DefaultGraph2DRenderer;
23  import y.view.Graph2D;
24  import y.view.Graph2DRenderer;
25  
26  import javax.swing.JComboBox;
27  import javax.swing.JToolBar;
28  import java.awt.event.ActionEvent;
29  import java.awt.event.ActionListener;
30  
31  /**
32   * This class demonstrates how to utilize {@link BridgeCalculator} to draw bridges/gaps for crossing edges.
33   * It demonstrates how {@link y.view.PolyLineEdgeRealizer} automatically makes use of
34   * the {@link BridgeCalculator} registered with the default {@link Graph2DRenderer} instance to
35   * incorporate the calculation of bridges.
36   *
37   */
38  public class BridgeDemo extends DemoBase {
39    BridgeCalculator bridgeCalculator;
40  
41    public BridgeDemo() {
42      // create the BridgeCalculator
43      bridgeCalculator = new BridgeCalculator();
44  
45      // register it with the DefaultGraph2DRenderer
46      ((DefaultGraph2DRenderer) view.getGraph2DRenderer()).setBridgeCalculator(bridgeCalculator);
47  
48      // create a nice graph that shows all possible configurations...
49      final Graph2D graph = view.getGraph2D();
50      graph.getDefaultEdgeRealizer().setTargetArrow(Arrow.SHORT);
51      Node[] nodes = new Node[16];
52      int count = 0;
53      for (int i = 1; i < 5; i++) {
54        nodes[count++] = graph.createNode(50 + 40 * i, 260);
55        nodes[count++] = graph.createNode(50 + 40 * i, 40);
56        nodes[count++] = graph.createNode(40, 50 + 40 * i);
57        nodes[count++] = graph.createNode(260, 50 + 40 * i);
58      }
59  
60      graph.createEdge(nodes[0], nodes[1]);
61  
62      Edge e = graph.createEdge(nodes[0], nodes[1]);
63      graph.setSourcePointRel(e, new YPoint(5, 0));
64      graph.setTargetPointRel(e, new YPoint(5, 0));
65  
66      graph.createEdge(nodes[5], nodes[4]);
67  
68      graph.createEdge(nodes[2], nodes[3]);
69      e = graph.createEdge(nodes[2], nodes[3]);
70      graph.setSourcePointRel(e, new YPoint(0, 5));
71      graph.setTargetPointRel(e, new YPoint(0, 5));
72  
73      graph.createEdge(nodes[7], nodes[6]);
74  
75      graph.createEdge(nodes[2 + 8], nodes[3 + 8]);
76      graph.createEdge(nodes[7 + 8], nodes[6 + 8]);
77  
78      graph.createEdge(nodes[0 + 8], nodes[1 + 8]);
79      graph.createEdge(nodes[5 + 8], nodes[4 + 8]);
80    }
81  
82    protected JToolBar createToolBar() {
83      final JToolBar toolBar = super.createToolBar();
84      final JComboBox crossingModeCB = new JComboBox(new Object[]{"Order Induced", "Horizontal Crosses Vertical", "Vertical Crosses Horizontal"});
85      crossingModeCB.setSelectedIndex(0);
86      crossingModeCB.addActionListener(new ActionListener() {
87        public void actionPerformed(ActionEvent e) {
88          bridgeCalculator.setCrossingMode((short) crossingModeCB.getSelectedIndex());
89          view.getGraph2D().updateViews();
90        }
91      });
92      toolBar.add(crossingModeCB);
93      final JComboBox crossingStyleCB = new JComboBox(new Object[]{"Gap", "Arc", "Square", "Two Sides", "Scaled Arc", "Scaled Square", "Scaled Two Sides"});
94      crossingStyleCB.setSelectedIndex(1);
95      crossingStyleCB.addActionListener(new ActionListener() {
96        public void actionPerformed(ActionEvent e) {
97          bridgeCalculator.setCrossingStyle((short) crossingStyleCB.getSelectedIndex());
98          view.getGraph2D().updateViews();
99        }
100     });
101     toolBar.add(crossingStyleCB);
102     final JComboBox orientationStyleCB = new JComboBox(
103         new Object[]{"Up", "Down", "Left", "Right", "Positive", "Negative", "Flow Left", "Flow Right"});
104     orientationStyleCB.setSelectedIndex(4);
105     orientationStyleCB.addActionListener(new ActionListener() {
106       public void actionPerformed(ActionEvent e) {
107         bridgeCalculator.setOrientationStyle((short) (orientationStyleCB.getSelectedIndex() + 1));
108         view.getGraph2D().updateViews();
109       }
110     });
111     toolBar.add(orientationStyleCB);
112     return toolBar;
113   }
114 
115   public static void main(String[] args) {
116     initLnF();
117     new BridgeDemo().start("Bridge Demo");
118   }
119 }
120