forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxStyleUtils.java
More file actions
388 lines (349 loc) · 8.18 KB
/
mxStyleUtils.java
File metadata and controls
388 lines (349 loc) · 8.18 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* Copyright (c) 2007-2012, JGraph Ltd
*/
package com.mxgraph.util;
import java.util.ArrayList;
import java.util.List;
import com.mxgraph.model.mxIGraphModel;
/**
* Contains various style helper methods for use with mxGraph.
*/
public class mxStyleUtils
{
/**
* Returns the stylename in a style of the form stylename[;key=value] or an
* empty string if the given style does not contain a stylename.
*
* @param style
* String of the form stylename[;key=value].
* @return Returns the stylename from the given formatted string.
*/
public static String getStylename(String style)
{
if (style != null)
{
String[] pairs = style.split(";");
String stylename = pairs[0];
if (stylename.indexOf("=") < 0)
{
return stylename;
}
}
return "";
}
/**
* Returns the stylenames in a style of the form stylename[;key=value] or an
* empty array if the given style does not contain any stylenames.
*
* @param style
* String of the form stylename[;stylename][;key=value].
* @return Returns the stylename from the given formatted string.
*/
public static String[] getStylenames(String style)
{
List<String> result = new ArrayList<String>();
if (style != null)
{
String[] pairs = style.split(";");
for (int i = 0; i < pairs.length; i++)
{
if (pairs[i].indexOf("=") < 0)
{
result.add(pairs[i]);
}
}
}
return result.toArray(new String[result.size()]);
}
/**
* Returns the index of the given stylename in the given style. This returns
* -1 if the given stylename does not occur (as a stylename) in the given
* style, otherwise it returns the index of the first character.
*/
public static int indexOfStylename(String style, String stylename)
{
if (style != null && stylename != null)
{
String[] tokens = style.split(";");
int pos = 0;
for (int i = 0; i < tokens.length; i++)
{
if (tokens[i].equals(stylename))
{
return pos;
}
pos += tokens[i].length() + 1;
}
}
return -1;
}
/**
* Adds the specified stylename to the given style if it does not already
* contain the stylename.
*/
public static String addStylename(String style, String stylename)
{
if (indexOfStylename(style, stylename) < 0)
{
if (style == null)
{
style = "";
}
else if (style.length() > 0
&& style.charAt(style.length() - 1) != ';')
{
style += ';';
}
style += stylename;
}
return style;
}
/**
* Removes all occurrences of the specified stylename in the given style and
* returns the updated style. Trailing semicolons are preserved.
*/
public static String removeStylename(String style, String stylename)
{
StringBuffer buffer = new StringBuffer();
if (style != null)
{
String[] tokens = style.split(";");
for (int i = 0; i < tokens.length; i++)
{
if (!tokens[i].equals(stylename))
{
buffer.append(tokens[i] + ";");
}
}
}
return (buffer.length() > 1) ? buffer.substring(0, buffer.length() - 1)
: buffer.toString();
}
/**
* Removes all stylenames from the given style and returns the updated
* style.
*/
public static String removeAllStylenames(String style)
{
StringBuffer buffer = new StringBuffer();
if (style != null)
{
String[] tokens = style.split(";");
for (int i = 0; i < tokens.length; i++)
{
if (tokens[i].indexOf('=') >= 0)
{
buffer.append(tokens[i] + ";");
}
}
}
return (buffer.length() > 1) ? buffer.substring(0, buffer.length() - 1)
: buffer.toString();
}
/**
* Assigns the value for the given key in the styles of the given cells, or
* removes the key from the styles if the value is null.
*
* @param model
* Model to execute the transaction in.
* @param cells
* Array of cells to be updated.
* @param key
* Key of the style to be changed.
* @param value
* New value for the given key.
*/
public static void setCellStyles(mxIGraphModel model, Object[] cells,
String key, String value)
{
if (cells != null && cells.length > 0)
{
model.beginUpdate();
try
{
for (int i = 0; i < cells.length; i++)
{
if (cells[i] != null)
{
String style = setStyle(model.getStyle(cells[i]), key,
value);
model.setStyle(cells[i], style);
}
}
}
finally
{
model.endUpdate();
}
}
}
/**
* Adds or removes the given key, value pair to the style and returns the
* new style. If value is null or zero length then the key is removed from
* the style.
*
* @param style
* String of the form <code>stylename[;key=value]</code>.
* @param key
* Key of the style to be changed.
* @param value
* New value for the given key.
* @return Returns the new style.
*/
public static String setStyle(String style, String key, String value)
{
boolean isValue = value != null && value.length() > 0;
if (style == null || style.length() == 0)
{
if (isValue)
{
style = key + "=" + value;
}
}
else
{
int index = style.indexOf(key + "=");
if (index < 0)
{
if (isValue)
{
String sep = (style.endsWith(";")) ? "" : ";";
style = style + sep + key + '=' + value;
}
}
else
{
String tmp = (isValue) ? key + "=" + value : "";
int cont = style.indexOf(";", index);
if (!isValue)
{
cont++;
}
style = style.substring(0, index) + tmp
+ ((cont > index) ? style.substring(cont) : "");
}
}
return style;
}
/**
* Sets or toggles the flag bit for the given key in the cell's styles. If
* value is null then the flag is toggled.
*
* <code>
* mxUtils.setCellStyleFlags(graph.getModel(),
* cells,
* mxConstants.STYLE_FONTSTYLE,
* mxConstants.FONT_BOLD, null);
* </code>
*
* Toggles the bold font style.
*
* @param model
* Model that contains the cells.
* @param cells
* Array of cells to change the style for.
* @param key
* Key of the style to be changed.
* @param flag
* Integer for the bit to be changed.
* @param value
* Optional boolean value for the flag.
*/
public static void setCellStyleFlags(mxIGraphModel model, Object[] cells,
String key, int flag, Boolean value)
{
if (cells != null && cells.length > 0)
{
model.beginUpdate();
try
{
for (int i = 0; i < cells.length; i++)
{
if (cells[i] != null)
{
String style = setStyleFlag(model.getStyle(cells[i]),
key, flag, value);
model.setStyle(cells[i], style);
}
}
}
finally
{
model.endUpdate();
}
}
}
/**
* Sets or removes the given key from the specified style and returns the
* new style. If value is null then the flag is toggled.
*
* @param style
* String of the form stylename[;key=value].
* @param key
* Key of the style to be changed.
* @param flag
* Integer for the bit to be changed.
* @param value
* Optional boolean value for the given flag.
*/
public static String setStyleFlag(String style, String key, int flag,
Boolean value)
{
if (style == null || style.length() == 0)
{
if (value == null || value.booleanValue())
{
style = key + "=" + flag;
}
else
{
style = key + "=0";
}
}
else
{
int index = style.indexOf(key + "=");
if (index < 0)
{
String sep = (style.endsWith(";")) ? "" : ";";
if (value == null || value.booleanValue())
{
style = style + sep + key + "=" + flag;
}
else
{
style = style + sep + key + "=0";
}
}
else
{
int cont = style.indexOf(";", index);
String tmp = "";
int result = 0;
if (cont < 0)
{
tmp = style.substring(index + key.length() + 1);
}
else
{
tmp = style.substring(index + key.length() + 1, cont);
}
if (value == null)
{
result = Integer.parseInt(tmp) ^ flag;
}
else if (value.booleanValue())
{
result = Integer.parseInt(tmp) | flag;
}
else
{
result = Integer.parseInt(tmp) & ~flag;
}
style = style.substring(0, index) + key + "=" + result
+ ((cont >= 0) ? style.substring(cont) : "");
}
}
return style;
}
}