1package com.mxgraph.examples.swing; 2 3import java.util.EventObject; 4 5import javax.swing.JFrame; 6 7import org.w3c.dom.Document; 8import org.w3c.dom.Element; 9import org.w3c.dom.Node; 10 11import com.mxgraph.model.mxCell; 12import com.mxgraph.swing.mxGraphComponent; 13import com.mxgraph.util.mxUtils; 14import com.mxgraph.view.mxGraph; 15 16public class UserObject extends JFrame 17{ 18 19 /** 20 * 21 */ 22 private static final long serialVersionUID = -708317745824467773L; 23 24 public UserObject() 25 { 26 super("Hello, World!"); 27 28 // Defines the user objects, which are preferrably XML nodes that allow 29 // storage of complex values as child nodes and string, number or 30 // boolean properties as attributes. 31 // 32 // When using Java objects as user objects, make sure to add the 33 // package name containg the class and register a codec for the user 34 // object class as follows: 35 // 36 // mxCodecRegistry.addPackage("com.example"); 37 // mxCodecRegistry.register(new mxObjectCodec( 38 // new com.example.CustomUserObject())); 39 // 40 // Note that the object must have an empty constructor and a setter and 41 // getter for each property to be persisted. 42 // 43 Document doc = mxUtils.createDocument(); 44 Element person1 = doc.createElement("Person"); 45 person1.setAttribute("firstName", "Daffy"); 46 person1.setAttribute("lastName", "Duck"); 47 48 Element person2 = doc.createElement("Person"); 49 person2.setAttribute("firstName", "Bugs"); 50 person2.setAttribute("lastName", "Bunny"); 51 52 Element relation = doc.createElement("Knows"); 53 relation.setAttribute("since", "1985"); 54 55 mxGraph graph = new mxGraph() 56 { 57 // Overrides method to disallow edge label editing 58 public boolean isCellEditable(Object cell) 59 { 60 return !getModel().isEdge(cell); 61 } 62 63 // Overrides method to provide a cell label in the display 64 public String convertValueToString(Object cell) 65 { 66 if (cell instanceof mxCell) 67 { 68 Object value = ((mxCell) cell).getValue(); 69 70 if (value instanceof Element) 71 { 72 Element elt = (Element) value; 73 74 if (elt.getTagName().equalsIgnoreCase("person")) 75 { 76 String firstName = elt.getAttribute("firstName"); 77 String lastName = elt.getAttribute("lastName"); 78 79 if (lastName != null && lastName.length() > 0) 80 { 81 return lastName + ", " + firstName; 82 } 83 84 return firstName; 85 } 86 else if (elt.getTagName().equalsIgnoreCase("knows")) 87 { 88 return elt.getTagName() + " (Since " 89 + elt.getAttribute("since") + ")"; 90 } 91 92 } 93 } 94 95 return super.convertValueToString(cell); 96 } 97 98 // Overrides method to store a cell label in the model 99 public void cellLabelChanged(Object cell, Object newValue, 100 boolean autoSize) 101 { 102 if (cell instanceof mxCell && newValue != null) 103 { 104 Object value = ((mxCell) cell).getValue(); 105 106 if (value instanceof Node) 107 { 108 String label = newValue.toString(); 109 Element elt = (Element) value; 110 111 if (elt.getTagName().equalsIgnoreCase("person")) 112 { 113 int pos = label.indexOf(' '); 114 115 String firstName = (pos > 0) ? label.substring(0, 116 pos).trim() : label; 117 String lastName = (pos > 0) ? label.substring( 118 pos + 1, label.length()).trim() : ""; 119 120 // Clones the value for correct undo/redo 121 elt = (Element) elt.cloneNode(true); 122 123 elt.setAttribute("firstName", firstName); 124 elt.setAttribute("lastName", lastName); 125 126 newValue = elt; 127 } 128 } 129 } 130 131 super.cellLabelChanged(cell, newValue, autoSize); 132 } 133 }; 134 135 Object parent = graph.getDefaultParent(); 136 137 graph.getModel().beginUpdate(); 138 try 139 { 140 Object v1 = graph.insertVertex(parent, null, person1, 20, 20, 80, 141 30); 142 Object v2 = graph.insertVertex(parent, null, person2, 240, 150, 80, 143 30); 144 graph.insertEdge(parent, null, relation, v1, v2); 145 } 146 finally 147 { 148 graph.getModel().endUpdate(); 149 } 150 151 // Overrides method to create the editing value 152 mxGraphComponent graphComponent = new mxGraphComponent(graph) 153 { 154 /** 155 * 156 */ 157 private static final long serialVersionUID = 6824440535661529806L; 158 159 public String getEditingValue(Object cell, EventObject trigger) 160 { 161 if (cell instanceof mxCell) 162 { 163 Object value = ((mxCell) cell).getValue(); 164 165 if (value instanceof Element) 166 { 167 Element elt = (Element) value; 168 169 if (elt.getTagName().equalsIgnoreCase("person")) 170 { 171 String firstName = elt.getAttribute("firstName"); 172 String lastName = elt.getAttribute("lastName"); 173 174 return firstName + " " + lastName; 175 } 176 } 177 } 178 179 return super.getEditingValue(cell, trigger); 180 }; 181 182 }; 183 184 getContentPane().add(graphComponent); 185 186 // Stops editing after enter has been pressed instead 187 // of adding a newline to the current editing value 188 graphComponent.setEnterStopsCellEditing(true); 189 } 190 191 public static void main(String[] args) 192 { 193 UserObject frame = new UserObject(); 194 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 195 frame.setSize(400, 320); 196 frame.setVisible(true); 197 } 198 199} 200