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