forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxGraphView.java
More file actions
1989 lines (1723 loc) · 48.1 KB
/
mxGraphView.java
File metadata and controls
1989 lines (1723 loc) · 48.1 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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2007-2010, Gaudenz Alder, David Benson
*/
package com.mxgraph.view;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxUndoableEdit;
import com.mxgraph.util.mxUndoableEdit.mxUndoableChange;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxEdgeStyle.mxEdgeStyleFunction;
import com.mxgraph.view.mxPerimeter.mxPerimeterFunction;
/**
* Implements a view for the graph. This class is in charge of computing the
* absolute coordinates for the relative child geometries, the points for
* perimeters and edge styles and keeping them cached in cell states for faster
* retrieval. The states are updated whenever the model or the view state
* (translate, scale) changes. The scale and translate are honoured in the
* bounds.
*
* This class fires the following events:
*
* mxEvent.UNDO fires after the root was changed in setCurrentRoot. The
* <code>edit</code> property contains the mxUndoableEdit which contains the
* mxCurrentRootChange.
*
* mxEvent.SCALE_AND_TRANSLATE fires after the scale and transle have been
* changed in scaleAndTranslate. The <code>scale</code>,
* <code>previousScale</code>, <code>translate</code> and
* <code>previousTranslate</code> properties contain the new and previous scale
* and translate, respectively.
*
* mxEvent.SCALE fires after the scale was changed in setScale. The
* <code>scale</code> and <code>previousScale</code> properties contain the new
* and previous scale.
*
* mxEvent.TRANSLATE fires after the translate was changed in setTranslate. The
* <code>translate</code> and <code>previousTranslate</code> properties contain
* the new and previous value for translate.
*
* mxEvent.UP and mxEvent.DOWN fire if the current root is changed by executing
* a mxCurrentRootChange. The event name depends on the location of the root in
* the cell hierarchy with respect to the current root. The <code>root</code>
* and <code>previous</code> properties contain the new and previous root,
* respectively.
*/
public class mxGraphView extends mxEventSource
{
/**
*
*/
private static mxPoint EMPTY_POINT = new mxPoint();
/**
* Reference to the enclosing graph.
*/
protected mxGraph graph;
/**
* mxCell that acts as the root of the displayed cell hierarchy.
*/
protected Object currentRoot = null;
/**
* Caches the current bounds of the graph.
*/
protected mxRectangle graphBounds = new mxRectangle();
/**
* Specifies the scale. Default is 1 (100%).
*/
protected double scale = 1;
/**
* Point that specifies the current translation. Default is a new empty
* point.
*/
protected mxPoint translate = new mxPoint(0, 0);
/**
* Maps from cells to cell states.
*/
protected Hashtable<Object, mxCellState> states = new Hashtable<Object, mxCellState>();
/**
* Constructs a new view for the given graph.
*
* @param graph
* Reference to the enclosing graph.
*/
public mxGraphView(mxGraph graph)
{
this.graph = graph;
}
/**
* Returns the enclosing graph.
*
* @return Returns the enclosing graph.
*/
public mxGraph getGraph()
{
return graph;
}
/**
* Returns the dictionary that maps from cells to states.
*/
public Hashtable<Object, mxCellState> getStates()
{
return states;
}
/**
* Returns the dictionary that maps from cells to states.
*/
public void setStates(Hashtable<Object, mxCellState> states)
{
this.states = states;
}
/**
* Returns the cached diagram bounds.
*
* @return Returns the diagram bounds.
*/
public mxRectangle getGraphBounds()
{
return graphBounds;
}
/**
* Sets the graph bounds.
*/
public void setGraphBounds(mxRectangle value)
{
graphBounds = value;
}
/**
* Returns the current root.
*/
public Object getCurrentRoot()
{
return currentRoot;
}
/**
* Sets and returns the current root and fires an undo event.
*
* @param root
* mxCell that specifies the root of the displayed cell
* hierarchy.
* @return Returns the object that represents the current root.
*/
public Object setCurrentRoot(Object root)
{
if (currentRoot != root)
{
mxCurrentRootChange change = new mxCurrentRootChange(this, root);
change.execute();
mxUndoableEdit edit = new mxUndoableEdit(this, false);
edit.add(change);
fireEvent(new mxEventObject(mxEvent.UNDO, "edit", edit));
}
return root;
}
/**
* Sets the scale and translation. Fires a "scaleAndTranslate" event after
* calling revalidate. Revalidate is only called if isEventsEnabled.
*
* @param scale
* Decimal value that specifies the new scale (1 is 100%).
* @param dx
* X-coordinate of the translation.
* @param dy
* Y-coordinate of the translation.
*/
public void scaleAndTranslate(double scale, double dx, double dy)
{
double previousScale = this.scale;
Object previousTranslate = translate.clone();
if (scale != this.scale || dx != translate.getX()
|| dy != translate.getY())
{
this.scale = scale;
translate = new mxPoint(dx, dy);
if (isEventsEnabled())
{
revalidate();
}
}
fireEvent(new mxEventObject(mxEvent.SCALE_AND_TRANSLATE, "scale",
scale, "previousScale", previousScale, "translate", translate,
"previousTranslate", previousTranslate));
}
/**
* Returns the current scale.
*
* @return Returns the scale.
*/
public double getScale()
{
return scale;
}
/**
* Sets the current scale and revalidates the view. Fires a "scale" event
* after calling revalidate. Revalidate is only called if isEventsEnabled.
*
* @param value
* New scale to be used.
*/
public void setScale(double value)
{
double previousScale = scale;
if (scale != value)
{
scale = value;
if (isEventsEnabled())
{
revalidate();
}
}
fireEvent(new mxEventObject(mxEvent.SCALE, "scale", scale,
"previousScale", previousScale));
}
/**
* Returns the current translation.
*
* @return Returns the translation.
*/
public mxPoint getTranslate()
{
return translate;
}
/**
* Sets the current translation and invalidates the view. Fires a property
* change event for "translate" after calling revalidate. Revalidate is only
* called if isEventsEnabled.
*
* @param value
* New translation to be used.
*/
public void setTranslate(mxPoint value)
{
Object previousTranslate = translate.clone();
if (value != null
&& (value.getX() != translate.getX() || value.getY() != translate
.getY()))
{
translate = value;
if (isEventsEnabled())
{
revalidate();
}
}
fireEvent(new mxEventObject(mxEvent.TRANSLATE, "translate", translate,
"previousTranslate", previousTranslate));
}
/**
* Returns the bounding box for an array of cells or null, if no cells are
* specified.
*
* @param cells
* @return Returns the bounding box for the given cells.
*/
public mxRectangle getBounds(Object[] cells)
{
return getBounds(cells, false);
}
/**
* Returns the bounding box for an array of cells or null, if no cells are
* specified.
*
* @param cells
* @return Returns the bounding box for the given cells.
*/
public mxRectangle getBoundingBox(Object[] cells)
{
return getBounds(cells, true);
}
/**
* Returns the bounding box for an array of cells or null, if no cells are
* specified.
*
* @param cells
* @return Returns the bounding box for the given cells.
*/
public mxRectangle getBounds(Object[] cells, boolean boundingBox)
{
mxRectangle result = null;
if (cells != null && cells.length > 0)
{
mxIGraphModel model = graph.getModel();
for (int i = 0; i < cells.length; i++)
{
if (model.isVertex(cells[i]) || model.isEdge(cells[i]))
{
mxCellState state = getState(cells[i]);
if (state != null)
{
mxRectangle tmp = (boundingBox) ? state
.getBoundingBox() : state;
if (tmp != null)
{
if (result == null)
{
result = new mxRectangle(tmp);
}
else
{
result.add(tmp);
}
}
}
}
}
}
return result;
}
/**
* Removes all existing cell states and invokes validate.
*/
public void reload()
{
states.clear();
validate();
}
/**
*
*/
public void revalidate()
{
invalidate();
validate();
}
/**
* Invalidates all cell states.
*/
public void invalidate()
{
invalidate(null);
}
/**
* Removes the state of the given cell and all descendants if the given cell
* is not the current root.
*
* @param cell
* @param force
* @param recurse
*/
public void clear(Object cell, boolean force, boolean recurse)
{
removeState(cell);
if (recurse && (force || cell != currentRoot))
{
mxIGraphModel model = graph.getModel();
int childCount = model.getChildCount(cell);
for (int i = 0; i < childCount; i++)
{
clear(model.getChildAt(cell, i), force, recurse);
}
}
else
{
invalidate(cell);
}
}
/**
* Invalidates the state of the given cell, all its descendants and
* connected edges.
*/
public void invalidate(Object cell)
{
mxIGraphModel model = graph.getModel();
cell = (cell != null) ? cell : model.getRoot();
mxCellState state = getState(cell);
if (state == null || !state.isInvalid())
{
if (state != null)
{
state.setInvalid(true);
}
// Recursively invalidates all descendants
int childCount = model.getChildCount(cell);
for (int i = 0; i < childCount; i++)
{
Object child = model.getChildAt(cell, i);
invalidate(child);
}
// Propagates invalidation to all connected edges
int edgeCount = model.getEdgeCount(cell);
for (int i = 0; i < edgeCount; i++)
{
invalidate(model.getEdgeAt(cell, i));
}
}
}
/**
* First validates all bounds and then validates all points recursively on
* all visible cells.
*/
public void validate()
{
mxRectangle graphBounds = getBoundingBox(validateCellState(validateCell((currentRoot != null) ? currentRoot
: graph.getModel().getRoot())));
setGraphBounds((graphBounds != null) ? graphBounds : new mxRectangle());
}
/**
* Shortcut to validateCell with visible set to true.
*/
public mxRectangle getBoundingBox(mxCellState state)
{
return getBoundingBox(state, true);
}
/**
* Returns the bounding box of the shape and the label for the given cell
* state and its children if recurse is true.
*
* @param state
* Cell state whose bounding box should be returned.
* @param recurse
* Boolean indicating if the children should be included.
*/
public mxRectangle getBoundingBox(mxCellState state, boolean recurse)
{
mxRectangle bbox = null;
if (state != null)
{
if (state.getBoundingBox() != null)
{
bbox = (mxRectangle) state.getBoundingBox().clone();
}
if (recurse)
{
mxIGraphModel model = graph.getModel();
int childCount = model.getChildCount(state.getCell());
for (int i = 0; i < childCount; i++)
{
mxRectangle bounds = getBoundingBox(
getState(model.getChildAt(state.getCell(), i)), true);
if (bounds != null)
{
if (bbox == null)
{
bbox = bounds;
}
else
{
bbox.add(bounds);
}
}
}
}
}
return bbox;
}
/**
* Shortcut to validateCell with visible set to true.
*/
public Object validateCell(Object cell)
{
return validateCell(cell, true);
}
/**
* Recursively creates the cell state for the given cell if visible is true
* and the given cell is visible. If the cell is not visible but the state
* exists then it is removed using removeState.
*
* @param cell
* Cell whose cell state should be created.
* @param visible
* Boolean indicating if the cell should be visible.
*/
public Object validateCell(Object cell, boolean visible)
{
if (cell != null)
{
visible = visible && graph.isCellVisible(cell);
mxCellState state = getState(cell, visible);
if (state != null && !visible)
{
removeState(cell);
}
else
{
mxIGraphModel model = graph.getModel();
int childCount = model.getChildCount(cell);
for (int i = 0; i < childCount; i++)
{
validateCell(
model.getChildAt(cell, i),
visible
&& (!graph.isCellCollapsed(cell) || cell == currentRoot));
}
}
}
return cell;
}
/**
* Shortcut to validateCellState with recurse set to true.
*/
public mxCellState validateCellState(Object cell)
{
return validateCellState(cell, true);
}
/**
* Validates the cell state for the given cell.
*
* @param cell
* Cell whose cell state should be validated.
* @param recurse
* Boolean indicating if the children of the cell should be
* validated.
*/
public mxCellState validateCellState(Object cell, boolean recurse)
{
mxCellState state = null;
if (cell != null)
{
state = getState(cell);
if (state != null)
{
mxIGraphModel model = graph.getModel();
if (state.isInvalid())
{
state.setInvalid(false);
if (cell != currentRoot)
{
validateCellState(model.getParent(cell), false);
}
state.setVisibleTerminalState(
validateCellState(getVisibleTerminal(cell, true),
false), true);
state.setVisibleTerminalState(
validateCellState(getVisibleTerminal(cell, false),
false), false);
updateCellState(state);
if (model.isEdge(cell) || model.isVertex(cell))
{
updateLabelBounds(state);
updateBoundingBox(state);
}
}
if (recurse)
{
int childCount = model.getChildCount(cell);
for (int i = 0; i < childCount; i++)
{
validateCellState(model.getChildAt(cell, i));
}
}
}
}
return state;
}
/**
* Updates the given cell state.
*
* @param state
* Cell state to be updated.
*/
public void updateCellState(mxCellState state)
{
state.getAbsoluteOffset().setX(0);
state.getAbsoluteOffset().setY(0);
state.getOrigin().setX(0);
state.getOrigin().setY(0);
state.setLength(0);
if (state.getCell() != currentRoot)
{
mxIGraphModel model = graph.getModel();
mxCellState pState = getState(model.getParent(state.getCell()));
if (pState != null && pState.getCell() != currentRoot)
{
state.getOrigin().setX(
state.getOrigin().getX() + pState.getOrigin().getX());
state.getOrigin().setY(
state.getOrigin().getY() + pState.getOrigin().getY());
}
mxPoint offset = graph.getChildOffsetForCell(state.getCell());
if (offset != null)
{
state.getOrigin()
.setX(state.getOrigin().getX() + offset.getX());
state.getOrigin()
.setY(state.getOrigin().getY() + offset.getY());
}
mxGeometry geo = graph.getCellGeometry(state.getCell());
if (geo != null)
{
if (!model.isEdge(state.getCell()))
{
mxPoint origin = state.getOrigin();
offset = geo.getOffset();
if (offset == null)
{
offset = EMPTY_POINT;
}
if (geo.isRelative() && pState != null)
{
if (model.isEdge(pState.getCell()))
{
mxPoint orig = getPoint(pState, geo);
if (orig != null)
{
origin.setX(origin.getX()
+ (orig.getX() / scale) - pState.getOrigin().getX()
- translate.getX());
origin.setY(origin.getY()
+ (orig.getY() / scale) - pState.getOrigin().getY()
- translate.getY());
}
}
else
{
origin.setX(origin.getX() + geo.getX()
* pState.getWidth() / scale + offset.getX());
origin.setY(origin.getY() + geo.getY()
* pState.getHeight() / scale
+ offset.getY());
}
}
else
{
state.setAbsoluteOffset(new mxPoint(scale
* offset.getX(), scale * offset.getY()));
origin.setX(origin.getX() + geo.getX());
origin.setY(origin.getY() + geo.getY());
}
}
state.setX(scale
* (translate.getX() + state.getOrigin().getX()));
state.setY(scale
* (translate.getY() + state.getOrigin().getY()));
state.setWidth(scale * geo.getWidth());
state.setHeight(scale * geo.getHeight());
if (model.isVertex(state.getCell()))
{
updateVertexState(state, geo);
}
if (model.isEdge(state.getCell()))
{
updateEdgeState(state, geo);
}
// Updates the cached label
updateLabel(state);
}
}
}
/**
* Validates the given cell state.
*/
public void updateVertexState(mxCellState state, mxGeometry geo)
{
// LATER: Add support for rotation
updateVertexLabelOffset(state);
}
/**
* Validates the given cell state.
*/
public void updateEdgeState(mxCellState state, mxGeometry geo)
{
mxCellState source = state.getVisibleTerminalState(true);
mxCellState target = state.getVisibleTerminalState(false);
// This will remove edges with no terminals and no terminal points
// as such edges are invalid and produce NPEs in the edge styles.
// Also removes connected edges that have no visible terminals.
if ((graph.getModel().getTerminal(state.getCell(), true) != null && source == null)
|| (source == null && geo.getTerminalPoint(true) == null)
|| (graph.getModel().getTerminal(state.getCell(), false) != null && target == null)
|| (target == null && geo.getTerminalPoint(false) == null))
{
clear(state.getCell(), true, true);
}
else
{
updateFixedTerminalPoints(state, source, target);
updatePoints(state, geo.getPoints(), source, target);
updateFloatingTerminalPoints(state, source, target);
if (state.getCell() != getCurrentRoot()
&& (state.getAbsolutePointCount() < 2
|| state.getAbsolutePoint(0) == null || state
.getAbsolutePoint(state.getAbsolutePointCount() - 1) == null))
{
// This will remove edges with invalid points from the list of
// states in the view.
// Happens if the one of the terminals and the corresponding
// terminal point is null.
clear(state.getCell(), true, true);
}
else
{
updateEdgeBounds(state);
state.setAbsoluteOffset(getPoint(state, geo));
}
}
}
/**
* Updates the absoluteOffset of the given vertex cell state. This takes
* into account the label position styles.
*
* @param state
* Cell state whose absolute offset should be updated.
*/
public void updateVertexLabelOffset(mxCellState state)
{
String horizontal = mxUtils.getString(state.getStyle(),
mxConstants.STYLE_LABEL_POSITION, mxConstants.ALIGN_CENTER);
if (horizontal.equals(mxConstants.ALIGN_LEFT))
{
state.absoluteOffset.setX(state.absoluteOffset.getX()
- state.getWidth());
}
else if (horizontal.equals(mxConstants.ALIGN_RIGHT))
{
state.absoluteOffset.setX(state.absoluteOffset.getX()
+ state.getWidth());
}
String vertical = mxUtils.getString(state.getStyle(),
mxConstants.STYLE_VERTICAL_LABEL_POSITION,
mxConstants.ALIGN_MIDDLE);
if (vertical.equals(mxConstants.ALIGN_TOP))
{
state.absoluteOffset.setY(state.absoluteOffset.getY()
- state.getHeight());
}
else if (vertical.equals(mxConstants.ALIGN_BOTTOM))
{
state.absoluteOffset.setY(state.absoluteOffset.getY()
+ state.getHeight());
}
}
/**
* Updates the label of the given state.
*/
public void updateLabel(mxCellState state)
{
String label = graph.getLabel(state.getCell());
Map<String, Object> style = state.getStyle();
// Applies word wrapping to non-HTML labels and stores the result in the
// state
if (label != null
&& label.length() > 0
&& !graph.isHtmlLabel(state.getCell())
&& !graph.getModel().isEdge(state.getCell())
&& mxUtils.getString(style, mxConstants.STYLE_WHITE_SPACE,
"nowrap").equals("wrap"))
{
double w = getWordWrapWidth(state);
// The lines for wrapping within the given width are calculated for
// no
// scale. The reason for this is the granularity of actual displayed
// font can cause the displayed lines to change based on scale. A
// factor
// is used to allow for different overalls widths, it ensures the
// largest
// font size/scale factor still stays within the bounds. All this
// ensures
// the wrapped lines are constant overing scaling, at the expense
// the
// label bounds will vary.
String[] lines = mxUtils.wordWrap(label,
mxUtils.getFontMetrics(mxUtils.getFont(state.getStyle())),
w * mxConstants.LABEL_SCALE_BUFFER);
if (lines.length > 0)
{
StringBuffer buffer = new StringBuffer();
for (String line : lines)
{
buffer.append(line + '\n');
}
label = buffer.substring(0, buffer.length() - 1);
}
}
state.setLabel(label);
}
/**
* Returns the width for wrapping the label of the given state at scale 1.
*/
public double getWordWrapWidth(mxCellState state)
{
Map<String, Object> style = state.getStyle();
boolean horizontal = mxUtils.isTrue(style,
mxConstants.STYLE_HORIZONTAL, true);
double w = 0;
// Computes the available width for the wrapped label
if (horizontal)
{
w = (state.getWidth() / scale) - 2 * mxConstants.LABEL_INSET - 2
* mxUtils.getDouble(style, mxConstants.STYLE_SPACING)
- mxUtils.getDouble(style, mxConstants.STYLE_SPACING_LEFT)
- mxUtils.getDouble(style, mxConstants.STYLE_SPACING_RIGHT);
}
else
{
w = (state.getHeight() / scale)
- 2
* mxConstants.LABEL_INSET
- 2
* mxUtils.getDouble(style, mxConstants.STYLE_SPACING)
- mxUtils.getDouble(style, mxConstants.STYLE_SPACING_TOP)
+ mxUtils
.getDouble(style, mxConstants.STYLE_SPACING_BOTTOM);
}
return w;
}
/**
* Updates the label bounds in the given state.
*/
public void updateLabelBounds(mxCellState state)
{
Object cell = state.getCell();
Map<String, Object> style = state.getStyle();
String overflow = mxUtils.getString(style, mxConstants.STYLE_OVERFLOW,
"");
if (overflow.equals("fill"))
{
state.setLabelBounds(new mxRectangle(state));
}
else if (state.getLabel() != null)
{
// For edges, the width of the geometry is used for wrapping HTML
// labels or no wrapping is applied if the width is set to 0
mxRectangle vertexBounds = state;
if (graph.getModel().isEdge(cell))
{
mxGeometry geo = graph.getCellGeometry(cell);
if (geo != null && geo.getWidth() > 0)
{
vertexBounds = new mxRectangle(0, 0, geo.getWidth()
* this.getScale(), 0);
}
else
{
vertexBounds = null;
}
}
state.setLabelBounds(mxUtils.getLabelPaintBounds(state.getLabel(),
style, graph.isHtmlLabel(cell), state.getAbsoluteOffset(),
vertexBounds, scale, graph.getModel().isEdge(cell)));
if (overflow.equals("width"))
{
state.getLabelBounds().setX(state.getX());
state.getLabelBounds().setWidth(state.getWidth());
}
}
}
/**
* Updates the bounding box in the given cell state.
*
* @param state
* Cell state whose bounding box should be updated.
*/
public mxRectangle updateBoundingBox(mxCellState state)
{
// Gets the cell bounds and adds shadows and markers
mxRectangle rect = new mxRectangle(state);
Map<String, Object> style = state.getStyle();
// Adds extra pixels for the marker and stroke assuming
// that the border stroke is centered around the bounds
// and the first pixel is drawn inside the bounds
double strokeWidth = Math.max(
1,
Math.round(mxUtils.getInt(style, mxConstants.STYLE_STROKEWIDTH,
1) * scale));
strokeWidth -= Math.max(1, strokeWidth / 2);
if (graph.getModel().isEdge(state.getCell()))
{
int ms = 0;
if (style.containsKey(mxConstants.STYLE_ENDARROW)
|| style.containsKey(mxConstants.STYLE_STARTARROW))
{
ms = (int) Math.round(mxConstants.DEFAULT_MARKERSIZE * scale);
}
// Adds the strokewidth
rect.grow(ms + strokeWidth);
// Adds worst case border for an arrow shape
if (mxUtils.getString(style, mxConstants.STYLE_SHAPE, "").equals(
mxConstants.SHAPE_ARROW))
{
rect.grow(mxConstants.ARROW_WIDTH / 2);