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.viewmode;
15  
16  import demo.view.DemoBase;
17  import y.base.Edge;
18  import y.base.EdgeMap;
19  import y.base.GraphEvent;
20  import y.base.GraphListener;
21  import y.view.CreateEdgeMode;
22  import y.view.EditMode;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.JToggleButton;
26  import javax.swing.JToolBar;
27  import java.awt.Color;
28  import java.awt.event.ActionEvent;
29  
30  /**
31   * Demonstrates how to customize EditMode in order to simulate orthogonal edges. <br> This demo only allows
32   * to switch between the creation of orthogonal and polyline edges.
33   * Toggling the button in the toolbar switches the type of newly created edges.
34   * This affects the behavior of {@link CreateEdgeMode} and {@link y.view.EditMode}, as well as implicitly the minor modes
35   * of EditMode.
36   */
37  public class OrthogonalEdgeViewModeDemo extends DemoBase {
38  
39    private boolean orthogonalRouting;
40    private EditMode editMode;
41    private EdgeMap orthogonalEdgeMap;
42    private JToggleButton orthogonalButton;
43  
44  
45    protected void initialize() {
46      super.initialize();
47      orthogonalEdgeMap = view.getGraph2D().createEdgeMap();
48      view.getGraph2D().addGraphListener(new GraphListener() {
49        public void onGraphEvent(GraphEvent e) {
50          if (e.getType() == GraphEvent.EDGE_CREATION) {
51            orthogonalEdgeMap.setBool(e.getData(), orthogonalRouting);
52          }
53        }
54      });
55    }
56  
57  
58    protected JToolBar createToolBar() {
59      JToolBar toolBar = super.createToolBar();
60      orthogonalButton = new JToggleButton(new AbstractAction("Orthogonal") {
61        public void actionPerformed(ActionEvent e) {
62          setOrthogonalRouting(((JToggleButton) e.getSource()).isSelected());
63        }
64      });
65      toolBar.add(orthogonalButton);
66      setOrthogonalRouting(true);
67      return toolBar;
68    }
69  
70    protected void registerViewModes() {
71      editMode = new EditMode() {
72        protected boolean isOrthogonalRouting(Edge edge) {
73          return orthogonalEdgeMap != null && orthogonalEdgeMap.getBool(edge);
74        }
75      };
76      view.addViewMode(editMode);
77      //set a custom CreateEdgeMode for the edge mode
78    }
79  
80  
81    public boolean isOrthogonalRouting() {
82      return orthogonalRouting;
83    }
84  
85    public void setOrthogonalRouting(boolean orthogonalRouting) {
86      this.orthogonalRouting = orthogonalRouting;
87      this.orthogonalButton.setSelected(orthogonalRouting);
88      ((CreateEdgeMode) editMode.getCreateEdgeMode()).setOrthogonalEdgeCreation(orthogonalRouting);
89      view.getGraph2D().getDefaultEdgeRealizer().setLineColor(orthogonalRouting ? Color.RED : Color.BLACK);
90    }
91  
92    public static void main(String args[]) {
93      OrthogonalEdgeViewModeDemo demo = new OrthogonalEdgeViewModeDemo();
94      demo.start("Orthogonal Edge ViewMode Demo");
95    }
96  }
97