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