| BuildGraphDemo.java |
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;
29
30 import y.base.Node;
31 import y.view.Arrow;
32 import y.view.EdgeLabel;
33 import y.view.EditMode;
34 import y.view.EdgeRealizer;
35 import y.view.Graph2D;
36 import y.view.Graph2DView;
37 import y.view.ImageNodeRealizer;
38 import y.view.NodeLabel;
39 import y.view.NodeRealizer;
40 import y.view.ShapeNodeRealizer;
41 import y.view.SmartEdgeLabelModel;
42 import y.view.SmartNodeLabelModel;
43
44 import java.awt.BorderLayout;
45 import java.awt.EventQueue;
46 import javax.swing.JFrame;
47 import javax.swing.JPanel;
48 import javax.swing.JRootPane;
49
50 /**
51 * <p>
52 * Demonstrates simple usage of {@link Graph2DView}, {@link Graph2D}
53 * and {@link EditMode}.
54 * </p>
55 * <p>
56 * This demo creates an initial graph by adding nodes and edges
57 * to the {@link Graph2D} displayed by the main {@link Graph2DView}
58 * view using API calls. It further shows how some graphical node
59 * and edge properties can be set (see {@link #buildGraph()}).
60 * </p>
61 * <p>
62 * Additionally it is shown how the appearance of the default nodes
63 * and edges can be set (see {@link #configureDefaultRealizers()}).
64 * This applies to new nodes and edges added to the initial graph.
65 * Editing the initial graph is possible due to the {@link EditMode}
66 * added to the view.
67 * </p>
68 *
69 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/base#Creating%20Graphs%20and%20Graph%20Elements" target="_blank">Section Creating Graphs and Graph Elements</a> in the yFiles for Java Developer's Guide
70 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/mvc_view" target="_blank">Section View Implementations</a> in the yFiles for Java Developer's Guide
71 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/realizer_related" target="_blank">Section Realizer-Related Features</a> in the yFiles for Java Developer's Guide
72 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/realizers" target="_blank">Section Bringing Graph Elements to Life: The Realizer Concept</a> in the yFiles for Java Developer's Guide
73 */
74 public class BuildGraphDemo extends JPanel
75 {
76 Graph2DView view;
77
78 public BuildGraphDemo()
79 {
80 setLayout(new BorderLayout());
81 view = new Graph2DView();
82 EditMode mode = new EditMode();
83 view.addViewMode(mode);
84 add(view);
85
86 configureDefaultRealizers();
87 buildGraph();
88 }
89
90 protected void configureDefaultRealizers()
91 {
92 Graph2D graph = view.getGraph2D();
93
94 //change the looks of the default edge
95 EdgeRealizer er = graph.getDefaultEdgeRealizer();
96 //a standard (target) arrow
97 er.setArrow(Arrow.STANDARD);
98
99 //change the looks (and type) of the default node
100 ShapeNodeRealizer snr = new ShapeNodeRealizer(ShapeNodeRealizer.ROUND_RECT);
101 snr.setSize(80, 30);
102 snr.setFillColor(DemoDefaults.DEFAULT_NODE_COLOR);
103 NodeLabel label = snr.getLabel();
104 SmartNodeLabelModel model = new SmartNodeLabelModel();
105 label.setLabelModel(model, model.getDefaultParameter());
106
107 //use it as default node realizer
108 graph.setDefaultNodeRealizer(snr);
109 }
110
111 void buildGraph()
112 {
113 Graph2D graph = view.getGraph2D();
114
115 //register an image with ImageNodeRealizer.
116 //must be a path name relative to your java CLASSPATH.
117 ImageNodeRealizer inr = new ImageNodeRealizer();
118 //set the image
119 inr.setImageURL(DemoBase.getResource(getClass(), "resource/yicon.png"));
120 //set node size equals to half of original image size
121 inr.setToImageSize();
122 inr.setSize(inr.getWidth()/2, inr.getHeight()/2);
123 inr.setLocation(60, 200);
124 //set a label text
125 inr.setLabelText("yFiles");
126
127 // set the label model to be a SmartNodeLabelModel
128 // (free label placement with snapping to significant positions)
129 // with an initial position south of the node
130 SmartNodeLabelModel labelModel = new SmartNodeLabelModel();
131 NodeLabel nodeLabel = inr.getLabel();
132 nodeLabel.setLabelModel(labelModel,
133 labelModel.createDiscreteModelParameter(SmartNodeLabelModel.POSITION_SOUTH));
134
135 //create a node that displays the image
136 Node v = graph.createNode(inr);
137
138
139
140 //create some edges and new nodes
141 for (int i = 0; i < 5; i++) {
142 Node w = graph.createNode();
143
144 //customize position and label of the created node
145 NodeRealizer nr = graph.getRealizer(w);
146 nr.setLocation(300, 100 + i*50);
147 nr.setLabelText("Node " + (i+1));
148
149 graph.createEdge(v, w);
150
151 //decorations for the created edge
152 EdgeRealizer er = graph.getRealizer(graph.lastEdge());
153 if (i % 2 == 0) {
154 //set diamond source arrow
155 er.setSourceArrow(Arrow.WHITE_DIAMOND);
156 } else {
157 //a label for the edge
158 EdgeLabel edgeLabel = er.getLabel();
159 edgeLabel.setText("Edge " + (i + 1));
160 SmartEdgeLabelModel model = new SmartEdgeLabelModel();
161 model.setDefaultDistance(2);
162 model.setAutoRotationEnabled(true);
163 int pos = ((i+1) % 4) == 0
164 ? SmartEdgeLabelModel.POSITION_RIGHT
165 : SmartEdgeLabelModel.POSITION_LEFT;
166 edgeLabel.setLabelModel(model, model.createDiscreteModelParameter(pos));
167 }
168 }
169 }
170
171 public void start()
172 {
173 JFrame frame = new JFrame(getClass().getName());
174 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
175 addContentTo(frame.getRootPane());
176 frame.pack();
177 frame.setLocationRelativeTo(null);
178 frame.setVisible(true);
179 }
180
181 public final void addContentTo( final JRootPane rootPane )
182 {
183 rootPane.setContentPane(this);
184 }
185
186 public static void main(String[] args) {
187 EventQueue.invokeLater(new Runnable() {
188 public void run() {
189 (new BuildGraphDemo()).start();
190 }
191 });
192 }
193 }
194