田源
2025-01-16 404966637eda6881a0f17683c5aacc7c1c34aed8
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
 * $Id: mxChildChangeCodec.java,v 1.1 2012/11/15 13:26:47 gaudenz Exp $
 * Copyright (c) 2006, Gaudenz Alder
 */
package com.mxgraph.io;
 
import java.util.Map;
 
import org.w3c.dom.Element;
import org.w3c.dom.Node;
 
import com.mxgraph.model.mxGraphModel.mxChildChange;
import com.mxgraph.model.mxICell;
 
/**
 * Codec for mxChildChanges. This class is created and registered
 * dynamically at load time and used implicitely via mxCodec
 * and the mxCodecRegistry.
 */
public class mxChildChangeCodec extends mxObjectCodec
{
 
    /**
     * Constructs a new model codec.
     */
    public mxChildChangeCodec()
    {
        this(new mxChildChange(), new String[] { "model", "child",
                "previousIndex" }, new String[] { "parent", "previous" }, null);
    }
 
    /**
     * Constructs a new model codec for the given arguments.
     */
    public mxChildChangeCodec(Object template, String[] exclude,
            String[] idrefs, Map<String, String> mapping)
    {
        super(template, exclude, idrefs, mapping);
    }
 
    /* (non-Javadoc)
     * @see com.mxgraph.io.mxObjectCodec#isReference(java.lang.Object, java.lang.String, java.lang.Object, boolean)
     */
    @Override
    public boolean isReference(Object obj, String attr, Object value,
            boolean isWrite)
    {
        if (attr.equals("child") && obj instanceof mxChildChange
                && (((mxChildChange) obj).getPrevious() != null || !isWrite))
        {
            return true;
        }
 
        return idrefs.contains(attr);
    }
 
    /* (non-Javadoc)
     * @see com.mxgraph.io.mxObjectCodec#afterEncode(com.mxgraph.io.mxCodec, java.lang.Object, org.w3c.dom.Node)
     */
    @Override
    public Node afterEncode(mxCodec enc, Object obj, Node node)
    {
        if (obj instanceof mxChildChange)
        {
            mxChildChange change = (mxChildChange) obj;
            Object child = change.getChild();
 
            if (isReference(obj, "child", child, true))
            {
                // Encodes as reference (id)
                mxCodec.setAttribute(node, "child", enc.getId(child));
            }
            else
            {
                // At this point, the encoder is no longer able to know which cells
                // are new, so we have to encode the complete cell hierarchy and
                // ignore the ones that are already there at decoding time. Note:
                // This can only be resolved by moving the notify event into the
                // execute of the edit.
                enc.encodeCell((mxICell) child, node, true);
            }
        }
 
        return node;
    }
 
    /**
     * Reads the cells into the graph model. All cells are children of the root
     * element in the node.
     */
    public Node beforeDecode(mxCodec dec, Node node, Object into)
    {
        if (into instanceof mxChildChange)
        {
            mxChildChange change = (mxChildChange) into;
 
            if (node.getFirstChild() != null
                    && node.getFirstChild().getNodeType() == Node.ELEMENT_NODE)
            {
                // Makes sure the original node isn't modified
                node = node.cloneNode(true);
 
                Node tmp = node.getFirstChild();
                change.setChild(dec.decodeCell(tmp, false));
 
                Node tmp2 = tmp.getNextSibling();
                tmp.getParentNode().removeChild(tmp);
                tmp = tmp2;
 
                while (tmp != null)
                {
                    tmp2 = tmp.getNextSibling();
 
                    if (tmp.getNodeType() == Node.ELEMENT_NODE)
                    {
                        // Ignores all existing cells because those do not need
                        // to be re-inserted into the model. Since the encoded
                        // version of these cells contains the new parent, this
                        // would leave to an inconsistent state on the model
                        // (ie. a parent change without a call to
                        // parentForCellChanged).
                        String id = ((Element) tmp).getAttribute("id");
 
                        if (dec.lookup(id) == null)
                        {
                            dec.decodeCell(tmp, true);
                        }
                    }
 
                    tmp.getParentNode().removeChild(tmp);
                    tmp = tmp2;
                }
            }
            else
            {
                String childRef = ((Element) node).getAttribute("child");
                change.setChild((mxICell) dec.getObject(childRef));
            }
        }
 
        return node;
    }
 
    /* (non-Javadoc)
     * @see com.mxgraph.io.mxObjectCodec#afterDecode(com.mxgraph.io.mxCodec, org.w3c.dom.Node, java.lang.Object)
     */
    @Override
    public Object afterDecode(mxCodec dec, Node node, Object obj)
    {
        if (obj instanceof mxChildChange)
        {
            mxChildChange change = (mxChildChange) obj;
 
            // Cells are encoded here after a complete transaction so the previous
            // parent must be restored on the cell for the case where the cell was
            // added. This is needed for the local model to identify the cell as a
            // new cell and register the ID.
            ((mxICell) change.getChild()).setParent((mxICell) change
                    .getPrevious());
            change.setPrevious(change.getParent());
            change.setPreviousIndex(change.getIndex());
        }
 
        return obj;
    }
 
}