田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
 * $Id: mxGdCodec.java,v 1.1 2010-08-25 08:36:59 gaudenz Exp $
 * Copyright (c) 2010, Gaudenz Alder, David Benson
 */
package com.vci.rmip.workflow.client.editor.code;
 
import com.mxgraph.io.gd.mxGdDocument;
import com.mxgraph.io.gd.mxGdEdge;
import com.mxgraph.io.gd.mxGdNode;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.view.mxGraph;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
/**
 * Parses a GD .txt file and imports it in the given graph.<br/>
 * This class depends from the classes contained in
 * com.mxgraph.io.gd.
 */
public class GraphGdCodec
{
    /**
     * Map with the vertex cells added in the addNode method.
     */
    private static HashMap<String, Object> cellsMap = new HashMap<String, Object>();
 
    /**
     * Returns the coordinates of the left top corner of the node.
     * @param node Node
     * @return mxPoint that represent the coordinates.
     */
    private static mxPoint getOriginPoint(mxGdNode node)
    {
        mxPoint coord = node.getCoordinates();
        mxPoint dim = node.getDimentions();
 
        double x = coord.getX() - dim.getX() / 2;
        double y = coord.getY() - dim.getY() / 2;
 
        return new mxPoint(x, y);
    }
 
    /**
     * Adds a new vertex to the graph.
     * @param graph Graph where the vertex is added.
     * @param parent Parent of the vertex to add.
     * @param node Node
     * @return Returns the vertex added.
     */
    private static mxCell addNode(mxGraph graph, Object parent, mxGdNode node)
    {
 
        mxPoint cordenates = getOriginPoint(node);
        mxPoint dimentions = node.getDimentions();
 
        //Set the node name as label.
        String label = node.getName();
 
        //Set the node name as ID.
        String id = node.getName();
 
        //Insert a new vertex in the graph
        mxCell v1 = (mxCell) graph.insertVertex(parent, id, label,
                cordenates.getX(), cordenates.getY(), dimentions.getX(),
                dimentions.getY());
 
        cellsMap.put(node.getName(), v1);
 
        return v1;
    }
 
    /**
     * Returns the string that represents the content of a given style map.
     * @param styleMap Map with the styles values
     * @return string that represents the style.
     */
    private static String getStyleString(Map<String, Object> styleMap,
            String asig)
    {
        String style = "";
        Iterator<Object> it = styleMap.values().iterator();
        Iterator<String> kit = styleMap.keySet().iterator();
 
        while (kit.hasNext())
        {
            String key = kit.next();
            Object value = it.next();
            style = style + key + asig + value + ";";
        }
        return style;
    }
 
    /**
     * Analizes a edge shape and returns a string with the style.
     * @return style read from the edge shape.
     */
    private static String getEdgeStyle()
    {
        Hashtable<String, Object> styleMap = new Hashtable<String, Object>();
 
        //Defines Edge Style
        //Defines if line is rounding
        styleMap.put(mxConstants.STYLE_ROUNDED, false);
 
        return getStyleString(styleMap, "=");
    }
 
    /**
     * Adds a new edge to the graph.
     * @param graph Graph where the edge is added.
     * @param parent Parent of the edge to add.
     * @param node Node
     * @return Returns the edge added.
     */
    private static mxCell addEdge(mxGraph graph, Object parent, mxGdEdge edge)
    {
 
        //Get source and target vertex
        Object source = cellsMap.get(edge.getSourceName());
        Object target = cellsMap.get(edge.getTargetName());
 
        //Defines style of the edge.
        String style = getEdgeStyle();
 
        //Insert new edge and set constraints.
        mxCell e = (mxCell) graph.insertEdge(parent, null, "", source, target,
                style);
 
        return e;
    }
 
    /**
     * Recieves a mxGDDocument document and parses it generating a new graph that is inserted in graph.
     * @param document GD to be parsed
     * @param graph Graph where the parsed graph is included.
     */
    public static void decode(mxGdDocument document, mxGraph graph)
    {
 
        Object parent = graph.getDefaultParent();
 
        graph.getModel().beginUpdate();
 
        //Add nodes.
        List<mxGdNode> nodes = document.getNodes();
 
        for (mxGdNode node : nodes)
        {
            addNode(graph, parent, node);
        }
 
        //Add Edges.
        List<mxGdEdge> edges = document.getEdges();
 
        for (mxGdEdge edge : edges)
        {
            addEdge(graph, parent, edge);
        }
 
        graph.getModel().endUpdate();
 
    }
 
    /**
     * Returns a GD document with the data of the vertexes and edges in the graph.
     * @param document GD document where the elements are put.
     * @param parent Parent cell of the vertexes and edges to be added.
     * @param graph Graph that contains the vertexes and edges.
     * @return Returns the document with the elements added.
     */
    private static mxGdDocument encodeNodesAndEdges(mxGdDocument document,
            Object parent, mxGraph graph, mxPoint parentCoord)
    {
        Object[] vertexes = graph.getChildVertices(parent);
 
        List<mxGdEdge> GDedges = document.getEdges();
        GDedges = encodeEdges(GDedges, parent, graph);
        document.setEdges(GDedges);
 
        for (Object vertex : vertexes)
        {
            List<mxGdNode> GDnodes = document.getNodes();
 
            mxCell v = (mxCell) vertex;
            mxGeometry geom = v.getGeometry();
 
            String id = v.getId();
 
            mxPoint coord = new mxPoint(parentCoord.getX() + geom.getCenterX(),
                    parentCoord.getY() + geom.getCenterY());
            mxPoint dim = new mxPoint(geom.getWidth(), geom.getHeight());
 
            mxPoint cornerCoord = new mxPoint(parentCoord.getX() + geom.getX(),
                    parentCoord.getY() + geom.getY());
 
            mxGdNode GDnode = new mxGdNode(id, coord, dim);
            GDnodes.add(GDnode);
            document.setNodes(GDnodes);
 
            document = encodeNodesAndEdges(document, vertex, graph, cornerCoord);
        }
        return document;
    }
 
    /**
     * Returns a list of mxGDEdge with the data of the edges in the graph.
     * @param GDedges List where the elements are put.
     * @param parent Parent cell of the edges to be added.
     * @param graph Graph that contains the edges.
     * @return Returns the list GDedges with the elements added.
     */
    private static List<mxGdEdge> encodeEdges(List<mxGdEdge> GDedges,
            Object parent, mxGraph graph)
    {
        Object[] edges = graph.getChildEdges(parent);
        for (Object edge : edges)
        {
            mxCell e = (mxCell) edge;
            mxCell source = (mxCell) e.getSource();
            mxCell target = (mxCell) e.getTarget();
 
            String sourceName = "";
            String targetName = "";
 
            sourceName = source.getId();
 
            targetName = target.getId();
 
            mxGdEdge GDedge = new mxGdEdge(sourceName, targetName);
 
            GDedges.add(GDedge);
        }
        return GDedges;
    }
 
    /**
     * Generates a GD document with the cells in the graph.
     * The actual implementation only uses the cells located in the first level.
     * @param graph Graph with the cells.
     * @return The GD document generated.
     */
    public static mxGdDocument encode(mxGraph graph)
    {
        Object parent = graph.getDefaultParent();
        mxGdDocument document = new mxGdDocument();
 
        //Adds Nodes and Edges.
        document = encodeNodesAndEdges(document, parent, graph, new mxPoint(0,
                0));
 
        return document;
    }
}