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.viewmode;
15  
16  import demo.view.DemoBase;
17  import y.base.Edge;
18  import y.view.CreateEdgeMode;
19  import y.view.EditMode;
20  import y.view.LineType;
21  import y.view.ViewMode;
22  import y.util.DataProviderAdapter;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.AbstractButton;
26  import javax.swing.JToggleButton;
27  import javax.swing.JToolBar;
28  import java.awt.Color;
29  import java.awt.EventQueue;
30  import java.awt.event.ActionEvent;
31  import java.util.Locale;
32  
33  /**
34   * Demonstrates how to customize {@link EditMode} to create and edit orthogonal edges.
35   * <br>
36   * This demo supports switching between the creation of orthogonal and polygonal edges.
37   * Additionally, the demo shows the usage of the snapping feature of the various {@link ViewMode}s.
38   * Toggling the buttons in the toolbar switches the type of newly created edges or toggle snapping on and off.
39   * This affects the behavior of {@link CreateEdgeMode} and {@link EditMode}, as well as implicitly the minor modes
40   * of {@link EditMode}.
41   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/mvc_controller.html#orthogonal_edge_paths">Section User Interaction</a> in the yFiles for Java Developer's Guide
42   */
43  public class OrthogonalEdgeViewModeDemo extends DemoBase {
44  
45    private boolean orthogonalRouting;
46    private boolean usingSnapping;
47    private EditMode editMode;
48    private JToggleButton orthogonalButton;
49    private JToggleButton snapLineButton;
50    private SnappingConfiguration snappingConfiguration;
51  
52    public OrthogonalEdgeViewModeDemo() {
53      snappingConfiguration = DemoBase.createDefaultSnappingConfiguration();
54      snappingConfiguration.setSnappingEnabled(true);
55      snappingConfiguration.setRemovingInnerBends(true);
56      snappingConfiguration.configureView(view);
57      snappingConfiguration.configureEditMode(editMode);
58  
59      setOrthogonalRouting(true);
60      setUsingSnapping(true);
61      loadGraph("resource/orthogonalEdge.graphml");
62    }
63  
64    protected JToolBar createToolBar() {
65      orthogonalButton = new JToggleButton(new AbstractAction("Orthogonal") {
66        public void actionPerformed(ActionEvent e) {
67          setOrthogonalRouting(((AbstractButton) e.getSource()).isSelected());
68        }
69      });
70      orthogonalButton.setIcon(getIconResource("resource/mode_orthogonal.png"));
71  
72      snapLineButton = new JToggleButton(new AbstractAction("Snapping") {
73        public void actionPerformed(ActionEvent e) {
74          setUsingSnapping(((AbstractButton) e.getSource()).isSelected());
75        }
76      });
77      snapLineButton.setIcon(getIconResource("resource/mode_snapping.png"));
78  
79      JToolBar toolBar = super.createToolBar();
80      toolBar.addSeparator();
81      toolBar.add(orthogonalButton);
82      toolBar.addSeparator(TOOLBAR_SMALL_SEPARATOR);
83      toolBar.add(snapLineButton);
84      return toolBar;
85    }
86  
87    protected EditMode createEditMode() {
88      editMode = super.createEditMode();
89  
90      // Route all red edges orthogonally.
91      view.getGraph2D().addDataProvider(EditMode.ORTHOGONAL_ROUTING_DPKEY, new DataProviderAdapter() {
92        public boolean getBool(Object dataHolder) {
93          return view.getGraph2D().getRealizer((Edge) dataHolder).getLineColor() == Color.RED;
94        }
95      });
96  
97      return editMode;
98    }
99  
100   public boolean isOrthogonalRouting() {
101     return orthogonalRouting;
102   }
103 
104   public void setOrthogonalRouting(boolean orthogonalRouting) {
105     this.orthogonalRouting = orthogonalRouting;
106     this.orthogonalButton.setSelected(orthogonalRouting);
107     ((CreateEdgeMode) editMode.getCreateEdgeMode()).setOrthogonalEdgeCreation(orthogonalRouting);
108     view.getGraph2D().getDefaultEdgeRealizer().setLineColor(orthogonalRouting ? Color.RED : Color.BLACK);
109     view.getGraph2D().getDefaultEdgeRealizer().setLineType(orthogonalRouting ? LineType.LINE_2 : LineType.LINE_1);
110   }
111 
112   public boolean isUsingSnapping() {
113     return usingSnapping;
114   }
115 
116   public void setUsingSnapping(boolean usingSnapping) {
117     this.usingSnapping = usingSnapping;
118     this.snapLineButton.setSelected(usingSnapping);
119     snappingConfiguration.setSnappingEnabled(usingSnapping);
120     snappingConfiguration.setRemovingInnerBends(usingSnapping);
121     snappingConfiguration.configureView(view);
122     snappingConfiguration.configureEditMode(editMode);
123   }
124 
125   public static void main(String[] args) {
126     EventQueue.invokeLater(new Runnable() {
127       public void run() {
128         Locale.setDefault(Locale.ENGLISH);
129         initLnF();
130         (new OrthogonalEdgeViewModeDemo()).start("Orthogonal Edge ViewMode Demo");
131       }
132     });
133   }
134 }
135