田源
2025-01-15 78fa1f005a9ec2581611e53d7eba8efeacb4df6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
 * $Id: mxModelCodec.java,v 1.8 2010-11-23 08:46:41 gaudenz Exp $
 * Copyright (c) 2006-2010, Gaudenz Alder, David Benson
 */
package com.vci.rmip.workflow.client.editor.code;
 
import java.util.Map;
 
import org.w3c.dom.Element;
import org.w3c.dom.Node;
 
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxICell;
 
/**
 * Codec for mxGraphModels. This class is created and registered
 * dynamically at load time and used implicitly via mxCodec
 * and the mxCodecRegistry.
 */
public class GraphModelCodec extends GraphObjectCodec
{
 
    /**
     * Constructs a new model codec.
     */
    public GraphModelCodec()
    {
        this(new mxGraphModel());
    }
 
    /**
     * Constructs a new model codec for the given template.
     */
    public GraphModelCodec(Object template)
    {
        this(template, null, null, null);
    }
 
    /**
     * Constructs a new model codec for the given arguments.
     */
    public GraphModelCodec(Object template, String[] exclude, String[] idrefs,
            Map<String, String> mapping)
    {
        super(template, exclude, idrefs, mapping);
    }
 
    /**
     * Encodes the given mxGraphModel by writing a (flat) XML sequence
     * of cell nodes as produced by the mxCellCodec. The sequence is
     * wrapped-up in a node with the name root.
     */
    protected void encodeObject(GraphCodec enc, Object obj, Node node)
    {
        if (obj instanceof mxGraphModel)
        {
            Node rootNode = enc.document.createElement("root");
            mxGraphModel model = (mxGraphModel) obj;
            enc.encodeCell((mxICell) model.getRoot(), rootNode, true);
            node.appendChild(rootNode);
        }
    }
 
    /**
     * Reads the cells into the graph model. All cells are children of the root
     * element in the node.
     */
    public Node beforeDecode(GraphCodec dec, Node node, Object into)
    {
        if (node instanceof Element)
        {
            Element elt = (Element) node;
            mxGraphModel model = null;
 
            if (into instanceof mxGraphModel)
            {
                model = (mxGraphModel) into;
            }
            else
            {
                model = new mxGraphModel();
            }
 
            // Reads the cells into the graph model. All cells
            // are children of the root element in the node.
            Node root = elt.getElementsByTagName("root").item(0);
            mxICell rootCell = null;
 
            if (root != null)
            {
                Node tmp = root.getFirstChild();
 
                while (tmp != null)
                {
                    mxICell cell = dec.decodeCell(tmp, true);
 
                    if (cell != null && cell.getParent() == null)
                    {
                        rootCell = cell;
                    }
 
                    tmp = tmp.getNextSibling();
                }
 
                root.getParentNode().removeChild(root);
            }
 
            // Sets the root on the model if one has been decoded
            if (rootCell != null)
            {
                model.setRoot(rootCell);
            }
        }
 
        return node;
    }
 
}