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