forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxCell.java
More file actions
641 lines (555 loc) · 12.6 KB
/
mxCell.java
File metadata and controls
641 lines (555 loc) · 12.6 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/**
* Copyright (c) 2007, Gaudenz Alder
*/
package com.mxgraph.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* Cells are the elements of the graph model. They represent the state
* of the groups, vertices and edges in a graph.
*
* <h4>Edge Labels</h4>
*
* Using the x- and y-coordinates of a cell's geometry it is
* possible to position the label on edges on a specific location
* on the actual edge shape as it appears on the screen. The
* x-coordinate of an edge's geometry is used to describe the
* distance from the center of the edge from -1 to 1 with 0
* being the center of the edge and the default value. The
* y-coordinate of an edge's geometry is used to describe
* the absolute, orthogonal distance in pixels from that
* point. In addition, the mxGeometry.offset is used
* as a absolute offset vector from the resulting point.
*
* The width and height of an edge geometry are ignored.
*
* To add more than one edge label, add a child vertex with
* a relative geometry. The x- and y-coordinates of that
* geometry will have the same semantiv as the above for
* edge labels.
*/
public class mxCell implements mxICell, Cloneable, Serializable
{
/**
*
*/
private static final long serialVersionUID = 910211337632342672L;
/**
* Holds the Id. Default is null.
*/
protected String id;
/**
* Holds the user object. Default is null.
*/
protected Object value;
/**
* Holds the geometry. Default is null.
*/
protected mxGeometry geometry;
/**
* Holds the style as a string of the form
* stylename[;key=value]. Default is null.
*/
protected String style;
/**
* Specifies whether the cell is a vertex or edge and whether it is
* connectable, visible and collapsed. Default values are false, false,
* true, true and false respectively.
*/
protected boolean vertex = false, edge = false, connectable = true,
visible = true, collapsed = false;
/**
* Reference to the parent cell and source and target terminals for edges.
*/
protected mxICell parent, source, target;
/**
* Holds the child cells and connected edges.
*/
protected List<Object> children, edges;
/**
* Constructs a new cell with an empty user object.
*/
public mxCell()
{
this(null);
}
/**
* Constructs a new cell for the given user object.
*
* @param value
* Object that represents the value of the cell.
*/
public mxCell(Object value)
{
this(value, null, null);
}
/**
* Constructs a new cell for the given parameters.
*
* @param value Object that represents the value of the cell.
* @param geometry Specifies the geometry of the cell.
* @param style Specifies the style as a formatted string.
*/
public mxCell(Object value, mxGeometry geometry, String style)
{
setValue(value);
setGeometry(geometry);
setStyle(style);
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getId()
*/
public String getId()
{
return id;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setId(String)
*/
public void setId(String id)
{
this.id = id;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getValue()
*/
public Object getValue()
{
return value;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setValue(Object)
*/
public void setValue(Object value)
{
this.value = value;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getGeometry()
*/
public mxGeometry getGeometry()
{
return geometry;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setGeometry(com.mxgraph.model.mxGeometry)
*/
public void setGeometry(mxGeometry geometry)
{
this.geometry = geometry;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getStyle()
*/
public String getStyle()
{
return style;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setStyle(String)
*/
public void setStyle(String style)
{
this.style = style;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#isVertex()
*/
public boolean isVertex()
{
return vertex;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setVertex(boolean)
*/
public void setVertex(boolean vertex)
{
this.vertex = vertex;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#isEdge()
*/
public boolean isEdge()
{
return edge;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setEdge(boolean)
*/
public void setEdge(boolean edge)
{
this.edge = edge;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#isConnectable()
*/
public boolean isConnectable()
{
return connectable;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setConnectable(boolean)
*/
public void setConnectable(boolean connectable)
{
this.connectable = connectable;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#isVisible()
*/
public boolean isVisible()
{
return visible;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setVisible(boolean)
*/
public void setVisible(boolean visible)
{
this.visible = visible;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#isCollapsed()
*/
public boolean isCollapsed()
{
return collapsed;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setCollapsed(boolean)
*/
public void setCollapsed(boolean collapsed)
{
this.collapsed = collapsed;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getParent()
*/
public mxICell getParent()
{
return parent;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setParent(com.mxgraph.model.mxICell)
*/
public void setParent(mxICell parent)
{
this.parent = parent;
}
/**
* Returns the source terminal.
*/
public mxICell getSource()
{
return source;
}
/**
* Sets the source terminal.
*
* @param source Cell that represents the new source terminal.
*/
public void setSource(mxICell source)
{
this.source = source;
}
/**
* Returns the target terminal.
*/
public mxICell getTarget()
{
return target;
}
/**
* Sets the target terminal.
*
* @param target Cell that represents the new target terminal.
*/
public void setTarget(mxICell target)
{
this.target = target;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getTerminal(boolean)
*/
public mxICell getTerminal(boolean source)
{
return (source) ? getSource() : getTarget();
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#setTerminal(com.mxgraph.model.mxICell, boolean)
*/
public mxICell setTerminal(mxICell terminal, boolean isSource)
{
if (isSource)
{
setSource(terminal);
}
else
{
setTarget(terminal);
}
return terminal;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getChildCount()
*/
public int getChildCount()
{
return (children != null) ? children.size() : 0;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getIndex(com.mxgraph.model.mxICell)
*/
public int getIndex(mxICell child)
{
return (children != null) ? children.indexOf(child) : -1;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getChildAt(int)
*/
public mxICell getChildAt(int index)
{
return (children != null) ? (mxICell) children.get(index) : null;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#insert(com.mxgraph.model.mxICell)
*/
public mxICell insert(mxICell child)
{
int index = getChildCount();
if (child.getParent() == this)
{
index--;
}
return insert(child, index);
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#insert(com.mxgraph.model.mxICell, int)
*/
public mxICell insert(mxICell child, int index)
{
if (child != null)
{
child.removeFromParent();
child.setParent(this);
if (children == null)
{
children = new ArrayList<Object>();
children.add(child);
}
else
{
children.add(index, child);
}
}
return child;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#remove(int)
*/
public mxICell remove(int index)
{
mxICell child = null;
if (children != null && index >= 0)
{
child = getChildAt(index);
remove(child);
}
return child;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#remove(com.mxgraph.model.mxICell)
*/
public mxICell remove(mxICell child)
{
if (child != null && children != null)
{
children.remove(child);
child.setParent(null);
}
return child;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#removeFromParent()
*/
public void removeFromParent()
{
if (parent != null)
{
parent.remove(this);
}
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getEdgeCount()
*/
public int getEdgeCount()
{
return (edges != null) ? edges.size() : 0;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getEdgeIndex(com.mxgraph.model.mxICell)
*/
public int getEdgeIndex(mxICell edge)
{
return (edges != null) ? edges.indexOf(edge) : -1;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#getEdgeAt(int)
*/
public mxICell getEdgeAt(int index)
{
return (edges != null) ? (mxICell) edges.get(index) : null;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#insertEdge(com.mxgraph.model.mxICell, boolean)
*/
public mxICell insertEdge(mxICell edge, boolean isOutgoing)
{
if (edge != null)
{
edge.removeFromTerminal(isOutgoing);
edge.setTerminal(this, isOutgoing);
if (edges == null || edge.getTerminal(!isOutgoing) != this
|| !edges.contains(edge))
{
if (edges == null)
{
edges = new ArrayList<Object>();
}
edges.add(edge);
}
}
return edge;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#removeEdge(com.mxgraph.model.mxICell, boolean)
*/
public mxICell removeEdge(mxICell edge, boolean isOutgoing)
{
if (edge != null)
{
if (edge.getTerminal(!isOutgoing) != this && edges != null)
{
edges.remove(edge);
}
edge.setTerminal(null, isOutgoing);
}
return edge;
}
/* (non-Javadoc)
* @see com.mxgraph.model.mxICell#removeFromTerminal(boolean)
*/
public void removeFromTerminal(boolean isSource)
{
mxICell terminal = getTerminal(isSource);
if (terminal != null)
{
terminal.removeEdge(this, isSource);
}
}
/**
* Returns the specified attribute from the user object if it is an XML
* node.
*
* @param name Name of the attribute whose value should be returned.
* @return Returns the value of the given attribute or null.
*/
public String getAttribute(String name)
{
return getAttribute(name, null);
}
/**
* Returns the specified attribute from the user object if it is an XML
* node.
*
* @param name Name of the attribute whose value should be returned.
* @param defaultValue Default value to use if the attribute has no value.
* @return Returns the value of the given attribute or defaultValue.
*/
public String getAttribute(String name, String defaultValue)
{
Object userObject = getValue();
String val = null;
if (userObject instanceof Element)
{
Element element = (Element) userObject;
val = element.getAttribute(name);
}
if (val == null)
{
val = defaultValue;
}
return val;
}
/**
* Sets the specified attribute on the user object if it is an XML node.
*
* @param name Name of the attribute whose value should be set.
* @param value New value of the attribute.
*/
public void setAttribute(String name, String value)
{
Object userObject = getValue();
if (userObject instanceof Element)
{
Element element = (Element) userObject;
element.setAttribute(name, value);
}
}
/**
* Returns a clone of the cell.
*/
public Object clone() throws CloneNotSupportedException
{
mxCell clone = (mxCell) super.clone();
clone.setValue(cloneValue());
clone.setStyle(getStyle());
clone.setCollapsed(isCollapsed());
clone.setConnectable(isConnectable());
clone.setEdge(isEdge());
clone.setVertex(isVertex());
clone.setVisible(isVisible());
clone.setParent(null);
clone.setSource(null);
clone.setTarget(null);
clone.children = null;
clone.edges = null;
mxGeometry geometry = getGeometry();
if (geometry != null)
{
clone.setGeometry((mxGeometry) geometry.clone());
}
return clone;
}
/**
* Returns a clone of the user object. This implementation clones any XML
* nodes or otherwise returns the same user object instance.
*/
protected Object cloneValue()
{
Object value = getValue();
if (value instanceof Node)
{
value = ((Node) value).cloneNode(true);
}
return value;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder(64);
builder.append(getClass().getSimpleName());
builder.append(" [");
builder.append("id=");
builder.append(id);
builder.append(", value=");
builder.append(value);
builder.append(", geometry=");
builder.append(geometry);
builder.append("]");
return builder.toString();
}
}