1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  package demo.view.viewmode;
29  
30  import demo.view.DemoBase;
31  import y.base.Edge;
32  import y.view.CreateEdgeMode;
33  import y.view.EditMode;
34  import y.view.LineType;
35  import y.view.ViewMode;
36  import y.util.DataProviderAdapter;
37  
38  import javax.swing.AbstractAction;
39  import javax.swing.AbstractButton;
40  import javax.swing.JToggleButton;
41  import javax.swing.JToolBar;
42  import java.awt.Color;
43  import java.awt.EventQueue;
44  import java.awt.event.ActionEvent;
45  import java.util.Locale;
46  
47  /**
48   * Demonstrates how to customize {@link EditMode} to create and edit orthogonal edges.
49   * <br>
50   * This demo supports switching between the creation of orthogonal and polygonal edges.
51   * Additionally, the demo shows the usage of the snapping feature of the various {@link ViewMode}s.
52   * Toggling the buttons in the toolbar switches the type of newly created edges or toggle snapping on and off.
53   * This affects the behavior of {@link CreateEdgeMode} and {@link EditMode}, as well as implicitly the minor modes
54   * of {@link EditMode}.
55   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/mvc_controller#orthogonal_edge_paths" target="_blank">Section User Interaction</a> in the yFiles for Java Developer's Guide
56   */
57  public class OrthogonalEdgeViewModeDemo extends DemoBase {
58  
59    private boolean orthogonalRouting;
60    private boolean usingSnapping;
61    private EditMode editMode;
62    private JToggleButton orthogonalButton;
63    private JToggleButton snapLineButton;
64    private SnappingConfiguration snappingConfiguration;
65  
66    public OrthogonalEdgeViewModeDemo() {
67      snappingConfiguration = DemoBase.createDefaultSnappingConfiguration();
68      snappingConfiguration.setSnappingEnabled(true);
69      snappingConfiguration.setRemovingInnerBends(true);
70      snappingConfiguration.configureView(view);
71      snappingConfiguration.configureEditMode(editMode);
72  
73      setOrthogonalRouting(true);
74      setUsingSnapping(true);
75      loadGraph("resource/orthogonalEdge.graphml");
76    }
77  
78    protected JToolBar createToolBar() {
79      orthogonalButton = new JToggleButton(new AbstractAction("Orthogonal") {
80        public void actionPerformed(ActionEvent e) {
81          setOrthogonalRouting(((AbstractButton) e.getSource()).isSelected());
82        }
83      });
84      orthogonalButton.setIcon(getIconResource("resource/mode_orthogonal.png"));
85  
86      snapLineButton = new JToggleButton(new AbstractAction("Snapping") {
87        public void actionPerformed(ActionEvent e) {
88          setUsingSnapping(((AbstractButton) e.getSource()).isSelected());
89        }
90      });
91      snapLineButton.setIcon(getIconResource("resource/mode_snapping.png"));
92  
93      JToolBar toolBar = super.createToolBar();
94      toolBar.addSeparator();
95      toolBar.add(orthogonalButton);
96      toolBar.addSeparator(TOOLBAR_SMALL_SEPARATOR);
97      toolBar.add(snapLineButton);
98      return toolBar;
99    }
100 
101   protected EditMode createEditMode() {
102     editMode = super.createEditMode();
103 
104     // Route all red edges orthogonally.
105     view.getGraph2D().addDataProvider(EditMode.ORTHOGONAL_ROUTING_DPKEY, new DataProviderAdapter() {
106       public boolean getBool(Object dataHolder) {
107         return view.getGraph2D().getRealizer((Edge) dataHolder).getLineColor() == Color.RED;
108       }
109     });
110 
111     return editMode;
112   }
113 
114   public boolean isOrthogonalRouting() {
115     return orthogonalRouting;
116   }
117 
118   public void setOrthogonalRouting(boolean orthogonalRouting) {
119     this.orthogonalRouting = orthogonalRouting;
120     this.orthogonalButton.setSelected(orthogonalRouting);
121     ((CreateEdgeMode) editMode.getCreateEdgeMode()).setOrthogonalEdgeCreation(orthogonalRouting);
122     view.getGraph2D().getDefaultEdgeRealizer().setLineColor(orthogonalRouting ? Color.RED : Color.BLACK);
123     view.getGraph2D().getDefaultEdgeRealizer().setLineType(orthogonalRouting ? LineType.LINE_2 : LineType.LINE_1);
124   }
125 
126   public boolean isUsingSnapping() {
127     return usingSnapping;
128   }
129 
130   public void setUsingSnapping(boolean usingSnapping) {
131     this.usingSnapping = usingSnapping;
132     this.snapLineButton.setSelected(usingSnapping);
133     snappingConfiguration.setSnappingEnabled(usingSnapping);
134     snappingConfiguration.setRemovingInnerBends(usingSnapping);
135     snappingConfiguration.configureView(view);
136     snappingConfiguration.configureEditMode(editMode);
137   }
138 
139   public static void main(String[] args) {
140     EventQueue.invokeLater(new Runnable() {
141       public void run() {
142         Locale.setDefault(Locale.ENGLISH);
143         initLnF();
144         (new OrthogonalEdgeViewModeDemo()).start("Orthogonal Edge ViewMode Demo");
145       }
146     });
147   }
148 }
149