1package com.mxgraph.examples.swing;
2
3import java.util.Arrays;
4
5import javax.swing.JFrame;
6
7import org.w3c.dom.Document;
8import org.w3c.dom.Element;
9
10import com.mxgraph.swing.mxGraphComponent;
11import com.mxgraph.swing.handler.mxKeyboardHandler;
12import com.mxgraph.swing.handler.mxRubberband;
13import com.mxgraph.util.mxEvent;
14import com.mxgraph.util.mxEventObject;
15import com.mxgraph.util.mxUtils;
16import com.mxgraph.util.mxEventSource.mxIEventListener;
17import com.mxgraph.view.mxGraph;
18import com.mxgraph.view.mxMultiplicity;
19
20public class Validation extends JFrame
21{
22
23	/**
24	 *
25	 */
26	private static final long serialVersionUID = -8928982366041695471L;
27
28	public Validation()
29	{
30		super("Hello, World!");
31
32		Document xmlDocument = mxUtils.createDocument();
33		Element sourceNode = xmlDocument.createElement("Source");
34		Element targetNode = xmlDocument.createElement("Target");
35		Element subtargetNode = xmlDocument.createElement("Subtarget");
36
37		mxGraph graph = new mxGraph();
38		Object parent = graph.getDefaultParent();
39
40		graph.getModel().beginUpdate();
41		try
42		{
43			Object v1 = graph.insertVertex(parent, null, sourceNode, 20, 20,
44					80, 30);
45			Object v2 = graph.insertVertex(parent, null, targetNode, 200, 20,
46					80, 30);
47			Object v3 = graph.insertVertex(parent, null, targetNode
48					.cloneNode(true), 200, 80, 80, 30);
49			Object v4 = graph.insertVertex(parent, null, targetNode
50					.cloneNode(true), 200, 140, 80, 30);
51			graph.insertVertex(parent, null, subtargetNode, 200,
52					200, 80, 30);
53			Object v6 = graph.insertVertex(parent, null, sourceNode
54					.cloneNode(true), 20, 140, 80, 30);
55			graph.insertEdge(parent, null, "", v1, v2);
56			graph.insertEdge(parent, null, "", v1, v3);
57			graph.insertEdge(parent, null, "", v6, v4);
58			//Object e4 = graph.insertEdge(parent, null, "", v1, v4);
59		}
60		finally
61		{
62			graph.getModel().endUpdate();
63		}
64
65		mxMultiplicity[] multiplicities = new mxMultiplicity[3];
66
67		// Source nodes needs 1..2 connected Targets
68		multiplicities[0] = new mxMultiplicity(true, "Source", null, null, 1,
69				"2", Arrays.asList(new String[] { "Target" }),
70				"Source Must Have 1 or 2 Targets",
71				"Source Must Connect to Target", true);
72
73		// Source node does not want any incoming connections
74		multiplicities[1] = new mxMultiplicity(false, "Source", null, null, 0,
75				"0", null, "Source Must Have No Incoming Edge", null, true); // Type does not matter
76
77		// Target needs exactly one incoming connection from Source
78		multiplicities[2] = new mxMultiplicity(false, "Target", null, null, 1,
79				"1", Arrays.asList(new String[] { "Source" }),
80				"Target Must Have 1 Source", "Target Must Connect From Source",
81				true);
82
83		graph.setMultiplicities(multiplicities);
84
85		final mxGraphComponent graphComponent = new mxGraphComponent(graph);
86		graph.setMultigraph(false);
87		graph.setAllowDanglingEdges(false);
88		graphComponent.setConnectable(true);
89		graphComponent.setToolTips(true);
90
91		// Enables rubberband selection
92		new mxRubberband(graphComponent);
93		new mxKeyboardHandler(graphComponent);
94
95		// Installs automatic validation (use editor.validation = true
96		// if you are using an mxEditor instance)
97		graph.getModel().addListener(mxEvent.CHANGE, new mxIEventListener()
98		{
99			public void invoke(Object sender, mxEventObject evt)
100			{
101				graphComponent.validateGraph();
102			}
103		});
104
105		// Initial validation
106		graphComponent.validateGraph();
107
108		getContentPane().add(graphComponent);
109	}
110
111	public static void main(String[] args)
112	{
113		Validation frame = new Validation();
114		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
115		frame.setSize(400, 320);
116		frame.setVisible(true);
117	}
118
119}
120