1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
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-2011 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.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  /**
33   * This class demonstrates how to utilize {@link BridgeCalculator} to draw bridges/gaps for crossing edges.
34   * It demonstrates how {@link y.view.PolyLineEdgeRealizer} automatically makes use of
35   * the {@link BridgeCalculator} registered with the default {@link y.view.Graph2DRenderer} instance to
36   * incorporate the calculation of bridges.
37   *
38   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/mvc_view.html#renderer">Section View Implementations</a> in the yFiles for Java Developer's Guide
39   */
40  public class BridgeDemo extends DemoBase {
41    BridgeCalculator bridgeCalculator;
42  
43    public BridgeDemo() {
44      // create the BridgeCalculator
45      bridgeCalculator = new BridgeCalculator();
46  
47      // register it with the DefaultGraph2DRenderer
48      ((DefaultGraph2DRenderer) view.getGraph2DRenderer()).setBridgeCalculator(bridgeCalculator);
49  
50      // create a nice graph that shows all possible configurations...
51      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