forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxCellPath.java
More file actions
144 lines (122 loc) · 2.98 KB
/
mxCellPath.java
File metadata and controls
144 lines (122 loc) · 2.98 KB
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
/**
* Copyright (c) 2007, Gaudenz Alder
*/
package com.mxgraph.model;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
/**
* Implements a mechanism for temporary cell Ids.
*/
public class mxCellPath
{
/**
* Defines the separator between the path components. Default is
* <code>.</code>.
*/
public static String PATH_SEPARATOR = ".";
/**
* Creates the cell path for the given cell. The cell path is a
* concatenation of the indices of all cells on the (finite) path to
* the root, eg. "0.0.0.1".
*
* @param cell Cell whose path should be returned.
* @return Returns the string that represents the path.
*/
public static String create(mxICell cell)
{
String result = "";
if (cell != null)
{
mxICell parent = cell.getParent();
while (parent != null)
{
int index = parent.getIndex(cell);
result = index + mxCellPath.PATH_SEPARATOR + result;
cell = parent;
parent = cell.getParent();
}
}
return (result.length() > 1) ? result.substring(0, result.length() - 1)
: "";
}
/**
* Returns the path for the parent of the cell represented by the given
* path. Returns null if the given path has no parent.
*
* @param path Path whose parent path should be returned.
*/
public static String getParentPath(String path)
{
if (path != null)
{
int index = path.lastIndexOf(mxCellPath.PATH_SEPARATOR);
if (index >= 0)
{
return path.substring(0, index);
}
else if (path.length() > 0)
{
return "";
}
}
return null;
}
/**
* Returns the cell for the specified cell path using the given root as the
* root of the path.
*
* @param root Root cell of the path to be resolved.
* @param path String that defines the path.
* @return Returns the cell that is defined by the path.
*/
public static mxICell resolve(mxICell root, String path)
{
mxICell parent = root;
String[] tokens = path.split(Pattern.quote(PATH_SEPARATOR));
for (int i = 0; i < tokens.length; i++)
{
parent = parent.getChildAt(Integer.parseInt(tokens[i]));
}
return parent;
}
/**
* Compares the given cell paths and returns -1 if cp1 is smaller, 0 if
* cp1 is equal and 1 if cp1 is greater than cp2.
*/
public static int compare(String cp1, String cp2)
{
StringTokenizer p1 = new StringTokenizer(cp1, mxCellPath.PATH_SEPARATOR);
StringTokenizer p2 = new StringTokenizer(cp2, mxCellPath.PATH_SEPARATOR);
int comp = 0;
while (p1.hasMoreTokens() &&
p2.hasMoreTokens())
{
String t1 = p1.nextToken();
String t2 = p2.nextToken();
if (!t1.equals(t2))
{
if (t1.length() == 0 ||
t2.length() == 0)
{
comp = t1.compareTo(t2);
}
else
{
comp = Integer.valueOf(t1).compareTo(Integer.valueOf(t2));
}
break;
}
}
// Compares path length if both paths are equal to this point
if (comp == 0)
{
int t1 = p1.countTokens();
int t2 = p2.countTokens();
if (t1 != t2)
{
comp = (t1 > t2) ? 1 : -1;
}
}
return comp;
}
}