001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.flow;
042:
043: import org.netbeans.api.visual.widget.Widget;
044: import org.netbeans.api.visual.widget.ConnectionWidget;
045: import org.netbeans.modules.vmd.api.flow.visual.FlowDescriptor;
046: import org.netbeans.modules.vmd.api.flow.visual.FlowNodeDescriptor;
047: import org.netbeans.modules.vmd.api.flow.visual.FlowScene;
048: import org.netbeans.modules.vmd.api.flow.visual.FlowEdgeDescriptor;
049: import org.netbeans.modules.vmd.api.flow.FlowSupport;
050: import org.netbeans.modules.vmd.api.io.DataObjectContext;
051: import org.netbeans.modules.vmd.api.io.DataSerializer;
052: import org.netbeans.modules.vmd.api.io.ProjectTypeInfo;
053: import org.netbeans.modules.vmd.api.model.DesignComponent;
054: import org.netbeans.modules.vmd.api.model.DesignDocument;
055: import org.netbeans.modules.vmd.api.model.Debug;
056: import org.w3c.dom.*;
057:
058: import javax.swing.*;
059: import java.awt.*;
060: import java.util.*;
061:
062: /**
063: * @author David Kaspar
064: */
065: // TODO - polish serialized data - save dpi value for proper deserialization
066: public class FlowDataSerializer implements DataSerializer {
067:
068: private static final String FLOW_DOCUMENT_NODE = "FlowScene"; // NOI18N
069: private static final String VERSION_ATTR = "version"; // NOI18N
070: private static final String NODE_NODE = "Node"; // NOI18N
071: private static final String EDGE_NODE = "Edge"; // NOI18N
072: private static final String COMPONENTID_ATTR = "componentID"; // NOI18N
073: private static final String DESCRIPTORID_ATTR = "descriptorID"; // NOI18N
074: private static final String X_NODE_ATTR = "x"; // NOI18N
075: private static final String Y_NODE_ATTR = "y"; // NOI18N
076: private static final String X_EDGE_ATTR = "x"; // NOI18N
077: private static final String Y_EDGE_ATTR = "y"; // NOI18N
078:
079: private static final String VERSION_VALUE_1 = "1"; // NOI18N
080:
081: public Node serializeData(DataObjectContext context,
082: DesignDocument document, Document file) {
083: ProjectTypeInfo info = ProjectTypeInfo
084: .getProjectTypeInfoFor(context.getProjectType());
085: if (!info.getTags().contains(FlowSupport.PROJECT_TYPE_TAG_FLOW))
086: return null;
087:
088: FlowAccessController accessController = document
089: .getListenerManager().getAccessController(
090: FlowAccessController.class);
091: FlowScene scene = accessController.getScene();
092: Node node = file.createElement(FLOW_DOCUMENT_NODE);
093: setAttribute(file, node, VERSION_ATTR, VERSION_VALUE_1); // NOI18N
094:
095: Set<?> objects = scene.getObjects();
096: for (Object o : objects) {
097: if (scene.isNode(o)) {
098: FlowDescriptor descriptor = (FlowDescriptor) o;
099: Widget widget = scene.findWidget(descriptor);
100: if (widget != null) {
101: Point location = widget.getPreferredLocation();
102: if (location != null) {
103: Node data = file.createElement(NODE_NODE);
104: setAttribute(file, data, COMPONENTID_ATTR, Long
105: .toString(descriptor
106: .getRepresentedComponent()
107: .getComponentID()));
108: setAttribute(file, data, DESCRIPTORID_ATTR,
109: descriptor.getDescriptorID());
110: setAttribute(file, data, X_NODE_ATTR, Integer
111: .toString(location.x));
112: setAttribute(file, data, Y_NODE_ATTR, Integer
113: .toString(location.y));
114: node.appendChild(data);
115: }
116: }
117: } else if (scene.isEdge(o)) {
118: FlowDescriptor descriptor = (FlowDescriptor) o;
119: Widget widget = scene.findWidget(descriptor);
120: if (widget instanceof ConnectionWidget) {
121: ConnectionWidget conn = (ConnectionWidget) widget;
122: java.util.List<Point> controlPoints = conn
123: .getControlPoints();
124: if (conn.getRoutingPolicy() == ConnectionWidget.RoutingPolicy.DISABLE_ROUTING_UNTIL_END_POINT_IS_MOVED
125: && !controlPoints.isEmpty()) {
126: Node data = file.createElement(EDGE_NODE);
127: setAttribute(file, data, COMPONENTID_ATTR, Long
128: .toString(descriptor
129: .getRepresentedComponent()
130: .getComponentID()));
131: setAttribute(file, data, DESCRIPTORID_ATTR,
132: descriptor.getDescriptorID());
133: for (int i = 0; i < controlPoints.size(); i++) {
134: Point point = controlPoints.get(i);
135: setAttribute(file, data, X_EDGE_ATTR + i,
136: Integer.toString(point.x));
137: setAttribute(file, data, Y_EDGE_ATTR + i,
138: Integer.toString(point.y));
139: }
140: node.appendChild(data);
141: }
142: }
143: }
144: }
145:
146: return node;
147: }
148:
149: public boolean deserializeData(final DataObjectContext context,
150: final DesignDocument document, final Node data) {
151: ProjectTypeInfo info = ProjectTypeInfo
152: .getProjectTypeInfoFor(context.getProjectType());
153: if (!info.getTags().contains(FlowSupport.PROJECT_TYPE_TAG_FLOW))
154: return false;
155:
156: if (!FLOW_DOCUMENT_NODE.equals(data.getNodeName()))
157: return false;
158:
159: if (!VERSION_VALUE_1.equals(getAttributeValue(data,
160: VERSION_ATTR)))
161: return false;
162:
163: SwingUtilities.invokeLater(new Runnable() {
164: public void run() {
165: final FlowScene scene = document
166: .getListenerManager()
167: .getAccessController(FlowAccessController.class)
168: .getScene();
169:
170: document.getTransactionManager().readAccess(
171: new Runnable() {
172: public void run() {
173: deserializeDataVersion1(document, data,
174: scene);
175: }
176: });
177:
178: scene.validate();
179: }
180: });
181:
182: return true;
183: }
184:
185: private void deserializeDataVersion1(DesignDocument document,
186: Node data, FlowScene scene) {
187: boolean isAnythingLoaded = false;
188: for (Node node : getChildNode(data)) {
189: if (NODE_NODE.equals(node.getNodeName())) {
190: long componentID = Long.parseLong(getAttributeValue(
191: node, COMPONENTID_ATTR));
192: String descriptorid = getAttributeValue(node,
193: DESCRIPTORID_ATTR);
194: int x, y;
195: try {
196: x = Integer.parseInt(getAttributeValue(node,
197: X_NODE_ATTR));
198: y = Integer.parseInt(getAttributeValue(node,
199: Y_NODE_ATTR));
200: } catch (NumberFormatException e) {
201: continue;
202: }
203: DesignComponent representedComponent = document
204: .getComponentByUID(componentID);
205: if (representedComponent == null
206: || descriptorid == null)
207: continue;
208: FlowNodeDescriptor descriptor = new FlowNodeDescriptor(
209: representedComponent, descriptorid);
210: Widget widget = scene.findWidget(descriptor);
211: if (widget != null) {
212: widget.setPreferredLocation(new Point(x, y));
213: isAnythingLoaded = true;
214: }
215: } else if (EDGE_NODE.equals(node.getNodeName())) {
216: long componentID = Long.parseLong(getAttributeValue(
217: node, COMPONENTID_ATTR));
218: String descriptorid = getAttributeValue(node,
219: DESCRIPTORID_ATTR);
220: DesignComponent representedComponent = document
221: .getComponentByUID(componentID);
222: if (representedComponent == null
223: || descriptorid == null)
224: continue;
225: FlowEdgeDescriptor descriptor = new FlowEdgeDescriptor(
226: representedComponent, descriptorid, null,
227: false, null, false);
228: Widget widget = scene.findWidget(descriptor);
229: if (widget instanceof ConnectionWidget) {
230: ConnectionWidget conn = (ConnectionWidget) widget;
231: conn
232: .setRoutingPolicy(ConnectionWidget.RoutingPolicy.DISABLE_ROUTING_UNTIL_END_POINT_IS_MOVED);
233: ArrayList<Point> controlPoints = new ArrayList<Point>();
234: for (int i = 0;; i++) {
235: int x, y;
236: try {
237: x = Integer.parseInt(getAttributeValue(
238: node, X_EDGE_ATTR + i));
239: y = Integer.parseInt(getAttributeValue(
240: node, X_EDGE_ATTR + i));
241: } catch (NumberFormatException e) {
242: break;
243: }
244: controlPoints.add(new Point(x, y));
245: }
246: if (!controlPoints.isEmpty()) {
247: conn
248: .setRoutingPolicy(ConnectionWidget.RoutingPolicy.DISABLE_ROUTING_UNTIL_END_POINT_IS_MOVED);
249: conn.setControlPoints(controlPoints, false);
250: }
251: }
252: }
253: }
254: if (!isAnythingLoaded)
255: scene.layoutScene();
256: }
257:
258: private static String getAttributeValue(Node node, String attr) {
259: try {
260: if (node != null) {
261: NamedNodeMap map = node.getAttributes();
262: if (map != null) {
263: node = map.getNamedItem(attr);
264: if (node != null)
265: return node.getNodeValue();
266: }
267: }
268: } catch (DOMException e) {
269: Debug.warning(e);
270: }
271: return null;
272: }
273:
274: private static void setAttribute(Document xml, Node node,
275: String name, String value) {
276: NamedNodeMap map = node.getAttributes();
277: Attr attribute = xml.createAttribute(name);
278: attribute.setValue(value);
279: map.setNamedItem(attribute);
280: }
281:
282: private static Node[] getChildNode(Node node) {
283: NodeList childNodes = node.getChildNodes();
284: Node[] nodes = new Node[childNodes != null ? childNodes
285: .getLength() : 0];
286: for (int i = 0; i < nodes.length; i++)
287: nodes[i] = childNodes.item(i);
288: return nodes;
289: }
290:
291: }
|