forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxGraph.java
More file actions
8223 lines (7237 loc) · 204 KB
/
mxGraph.java
File metadata and controls
8223 lines (7237 loc) · 204 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, Gaudenz Alder
*/
package com.mxgraph.view;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Element;
import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.canvas.mxICanvas;
import com.mxgraph.canvas.mxImageCanvas;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxGraphModel.Filter;
import com.mxgraph.model.mxGraphModel.mxChildChange;
import com.mxgraph.model.mxGraphModel.mxCollapseChange;
import com.mxgraph.model.mxGraphModel.mxGeometryChange;
import com.mxgraph.model.mxGraphModel.mxRootChange;
import com.mxgraph.model.mxGraphModel.mxStyleChange;
import com.mxgraph.model.mxGraphModel.mxTerminalChange;
import com.mxgraph.model.mxGraphModel.mxValueChange;
import com.mxgraph.model.mxGraphModel.mxVisibleChange;
import com.mxgraph.model.mxICell;
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.mxImageBundle;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxResources;
import com.mxgraph.util.mxStyleUtils;
import com.mxgraph.util.mxUndoableEdit;
import com.mxgraph.util.mxUndoableEdit.mxUndoableChange;
import com.mxgraph.util.mxUtils;
/**
* Implements a graph object that allows to create diagrams from a graph model
* and stylesheet.
*
* <h3>Images</h3>
* To create an image from a graph, use the following code for a given
* XML document (doc) and File (file):
*
* <code>
* Image img = mxCellRenderer.createBufferedImage(
* graph, null, 1, Color.WHITE, false, null);
* ImageIO.write(img, "png", file);
* </code>
*
* If the XML is given as a string rather than a document, the document can
* be obtained using mxUtils.parse.
*
* This class fires the following events:
*
* mxEvent.ROOT fires if the root in the model has changed. This event has no
* properties.
*
* mxEvent.ALIGN_CELLS fires between begin- and endUpdate in alignCells. The
* <code>cells</code> and <code>align</code> properties contain the respective
* arguments that were passed to alignCells.
*
* mxEvent.FLIP_EDGE fires between begin- and endUpdate in flipEdge. The
* <code>edge</code> property contains the edge passed to flipEdge.
*
* mxEvent.ORDER_CELLS fires between begin- and endUpdate in orderCells. The
* <code>cells</code> and <code>back</code> properties contain the respective
* arguments that were passed to orderCells.
*
* mxEvent.CELLS_ORDERED fires between begin- and endUpdate in cellsOrdered.
* The <code>cells</code> and <code>back</code> arguments contain the
* respective arguments that were passed to cellsOrdered.
*
* mxEvent.GROUP_CELLS fires between begin- and endUpdate in groupCells. The
* <code>group</code>, <code>cells</code> and <code>border</code> arguments
* contain the respective arguments that were passed to groupCells.
*
* mxEvent.UNGROUP_CELLS fires between begin- and endUpdate in ungroupCells.
* The <code>cells</code> property contains the array of cells that was passed
* to ungroupCells.
*
* mxEvent.REMOVE_CELLS_FROM_PARENT fires between begin- and endUpdate in
* removeCellsFromParent. The <code>cells</code> property contains the array of
* cells that was passed to removeCellsFromParent.
*
* mxEvent.ADD_CELLS fires between begin- and endUpdate in addCells. The
* <code>cells</code>, <code>parent</code>, <code>index</code>,
* <code>source</code> and <code>target</code> properties contain the
* respective arguments that were passed to addCells.
*
* mxEvent.CELLS_ADDED fires between begin- and endUpdate in cellsAdded. The
* <code>cells</code>, <code>parent</code>, <code>index</code>,
* <code>source</code>, <code>target</code> and <code>absolute</code>
* properties contain the respective arguments that were passed to cellsAdded.
*
* mxEvent.REMOVE_CELLS fires between begin- and endUpdate in removeCells. The
* <code>cells</code> and <code>includeEdges</code> arguments contain the
* respective arguments that were passed to removeCells.
*
* mxEvent.CELLS_REMOVED fires between begin- and endUpdate in cellsRemoved.
* The <code>cells</code> argument contains the array of cells that was
* removed.
*
* mxEvent.SPLIT_EDGE fires between begin- and endUpdate in splitEdge. The
* <code>edge</code> property contains the edge to be splitted, the
* <code>cells</code>, <code>newEdge</code>, <code>dx</code> and
* <code>dy</code> properties contain the respective arguments that were passed
* to splitEdge.
*
* mxEvent.TOGGLE_CELLS fires between begin- and endUpdate in toggleCells. The
* <code>show</code>, <code>cells</code> and <code>includeEdges</code>
* properties contain the respective arguments that were passed to toggleCells.
*
* mxEvent.FOLD_CELLS fires between begin- and endUpdate in foldCells. The
* <code>collapse</code>, <code>cells</code> and <code>recurse</code>
* properties contain the respective arguments that were passed to foldCells.
*
* mxEvent.CELLS_FOLDED fires between begin- and endUpdate in cellsFolded. The
* <code>collapse</code>, <code>cells</code> and <code>recurse</code>
* properties contain the respective arguments that were passed to cellsFolded.
*
* mxEvent.UPDATE_CELL_SIZE fires between begin- and endUpdate in
* updateCellSize. The <code>cell</code> and <code>ignoreChildren</code>
* properties contain the respective arguments that were passed to
* updateCellSize.
*
* mxEvent.RESIZE_CELLS fires between begin- and endUpdate in resizeCells. The
* <code>cells</code> and <code>bounds</code> properties contain the respective
* arguments that were passed to resizeCells.
*
* mxEvent.CELLS_RESIZED fires between begin- and endUpdate in cellsResized.
* The <code>cells</code> and <code>bounds</code> properties contain the
* respective arguments that were passed to cellsResized.
*
* mxEvent.MOVE_CELLS fires between begin- and endUpdate in moveCells. The
* <code>cells</code>, <code>dx</code>, <code>dy</code>, <code>clone</code>,
* <code>target</code> and <code>location</code> properties contain the
* respective arguments that were passed to moveCells.
*
* mxEvent.CELLS_MOVED fires between begin- and endUpdate in cellsMoved. The
* <code>cells</code>, <code>dx</code>, <code>dy</code> and
* <code>disconnect</code> properties contain the respective arguments that
* were passed to cellsMoved.
*
* mxEvent.CONNECT_CELL fires between begin- and endUpdate in connectCell. The
* <code>edge</code>, <code>terminal</code> and <code>source</code> properties
* contain the respective arguments that were passed to connectCell.
*
* mxEvent.CELL_CONNECTED fires between begin- and endUpdate in cellConnected.
* The <code>edge</code>, <code>terminal</code> and <code>source</code>
* properties contain the respective arguments that were passed to
* cellConnected.
*
* mxEvent.REPAINT fires if a repaint was requested by calling repaint. The
* <code>region</code> property contains the optional mxRectangle that was
* passed to repaint to define the dirty region.
*/
public class mxGraph extends mxEventSource
{
private static final Logger log = Logger.getLogger(mxGraph.class.getName());
/**
* Adds required resources.
*/
static
{
try
{
mxResources.add("com.mxgraph.resources.graph");
}
catch (Exception e)
{
log.log(Level.SEVERE, "Failed to add the resource bundle", e);
}
}
/**
* Holds the version number of this release. Current version
* is 3.9.12.
*/
public static final String VERSION = "3.9.12";
/**
*
*/
public interface mxICellVisitor
{
/**
*
* @param vertex
* @param edge
*/
boolean visit(Object vertex, Object edge);
}
/**
* Property change event handling.
*/
protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
this);
/**
* Holds the model that contains the cells to be displayed.
*/
protected mxIGraphModel model;
/**
* Holds the view that caches the cell states.
*/
protected mxGraphView view;
/**
* Holds the stylesheet that defines the appearance of the cells.
*/
protected mxStylesheet stylesheet;
/**
* Holds the <mxGraphSelection> that models the current selection.
*/
protected mxGraphSelectionModel selectionModel;
/**
* Specifies the grid size. Default is 10.
*/
protected int gridSize = 10;
/**
* Specifies if the grid is enabled. Default is true.
*/
protected boolean gridEnabled = true;
/**
* Specifies if ports are enabled. This is used in <cellConnected> to update
* the respective style. Default is true.
*/
protected boolean portsEnabled = true;
/**
* Value returned by getOverlap if isAllowOverlapParent returns
* true for the given cell. getOverlap is used in keepInside if
* isKeepInsideParentOnMove returns true. The value specifies the
* portion of the child which is allowed to overlap the parent.
*/
protected double defaultOverlap = 0.5;
/**
* Specifies the default parent to be used to insert new cells.
* This is used in getDefaultParent. Default is null.
*/
protected Object defaultParent;
/**
* Specifies the alternate edge style to be used if the main control point
* on an edge is being doubleclicked. Default is null.
*/
protected String alternateEdgeStyle;
/**
* Specifies the return value for isEnabled. Default is true.
*/
protected boolean enabled = true;
/**
* Specifies the return value for isCell(s)Locked. Default is false.
*/
protected boolean cellsLocked = false;
/**
* Specifies the return value for isCell(s)Editable. Default is true.
*/
protected boolean cellsEditable = true;
/**
* Specifies the return value for isCell(s)Sizable. Default is true.
*/
protected boolean cellsResizable = true;
/**
* Specifies the return value for isCell(s)Movable. Default is true.
*/
protected boolean cellsMovable = true;
/**
* Specifies the return value for isCell(s)Bendable. Default is true.
*/
protected boolean cellsBendable = true;
/**
* Specifies the return value for isCell(s)Selectable. Default is true.
*/
protected boolean cellsSelectable = true;
/**
* Specifies the return value for isCell(s)Deletable. Default is true.
*/
protected boolean cellsDeletable = true;
/**
* Specifies the return value for isCell(s)Cloneable. Default is true.
*/
protected boolean cellsCloneable = true;
/**
* Specifies the return value for isCellDisconntableFromTerminal. Default
* is true.
*/
protected boolean cellsDisconnectable = true;
/**
* Specifies the return value for isLabel(s)Clipped. Default is false.
*/
protected boolean labelsClipped = false;
/**
* Specifies the return value for edges in isLabelMovable. Default is true.
*/
protected boolean edgeLabelsMovable = true;
/**
* Specifies the return value for vertices in isLabelMovable. Default is false.
*/
protected boolean vertexLabelsMovable = false;
/**
* Specifies the return value for isDropEnabled. Default is true.
*/
protected boolean dropEnabled = true;
/**
* Specifies if dropping onto edges should be enabled. Default is true.
*/
protected boolean splitEnabled = true;
/**
* Specifies if the graph should automatically update the cell size
* after an edit. This is used in isAutoSizeCell. Default is false.
*/
protected boolean autoSizeCells = false;
/**
* <mxRectangle> that specifies the area in which all cells in the
* diagram should be placed. Uses in getMaximumGraphBounds. Use a width
* or height of 0 if you only want to give a upper, left corner.
*/
protected mxRectangle maximumGraphBounds = null;
/**
* mxRectangle that specifies the minimum size of the graph canvas inside
* the scrollpane.
*/
protected mxRectangle minimumGraphSize = null;
/**
* Border to be added to the bottom and right side when the container is
* being resized after the graph has been changed. Default is 0.
*/
protected int border = 0;
/**
* Specifies if edges should appear in the foreground regardless of their
* order in the model. This has precendence over keepEdgeInBackground
* Default is false.
*/
protected boolean keepEdgesInForeground = false;
/**
* Specifies if edges should appear in the background regardless of their
* order in the model. Default is false.
*/
protected boolean keepEdgesInBackground = false;
/**
* Specifies if the cell size should be changed to the preferred size when
* a cell is first collapsed. Default is true.
*/
protected boolean collapseToPreferredSize = true;
/**
* Specifies if negative coordinates for vertices are allowed. Default is true.
*/
protected boolean allowNegativeCoordinates = true;
/**
* Specifies the return value for isConstrainChildren. Default is true.
*/
protected boolean constrainChildren = true;
/**
* Specifies if a parent should contain the child bounds after a resize of
* the child. Default is true.
*/
protected boolean extendParents = true;
/**
* Specifies if parents should be extended according to the <extendParents>
* switch if cells are added. Default is true.
*/
protected boolean extendParentsOnAdd = true;
/**
* Specifies if the scale and translate should be reset if
* the root changes in the model. Default is true.
*/
protected boolean resetViewOnRootChange = true;
/**
* Specifies if loops (aka self-references) are allowed.
* Default is false.
*/
protected boolean resetEdgesOnResize = false;
/**
* Specifies if edge control points should be reset after
* the move of a connected cell. Default is false.
*/
protected boolean resetEdgesOnMove = false;
/**
* Specifies if edge control points should be reset after
* the the edge has been reconnected. Default is true.
*/
protected boolean resetEdgesOnConnect = true;
/**
* Specifies if loops (aka self-references) are allowed.
* Default is false.
*/
protected boolean allowLoops = false;
/**
* Specifies the multiplicities to be used for validation of the graph.
*/
protected mxMultiplicity[] multiplicities;
/**
* Specifies the default style for loops.
*/
protected mxEdgeStyle.mxEdgeStyleFunction defaultLoopStyle = mxEdgeStyle.Loop;
/**
* Specifies if multiple edges in the same direction between
* the same pair of vertices are allowed. Default is true.
*/
protected boolean multigraph = true;
/**
* Specifies if edges are connectable. Default is false.
* This overrides the connectable field in edges.
*/
protected boolean connectableEdges = false;
/**
* Specifies if edges with disconnected terminals are
* allowed in the graph. Default is false.
*/
protected boolean allowDanglingEdges = true;
/**
* Specifies if edges that are cloned should be validated and only inserted
* if they are valid. Default is true.
*/
protected boolean cloneInvalidEdges = false;
/**
* Specifies if edges should be disconnected from their terminals when they
* are moved. Default is true.
*/
protected boolean disconnectOnMove = true;
/**
* Specifies if labels should be visible. This is used in
* getLabel. Default is true.
*/
protected boolean labelsVisible = true;
/**
* Specifies the return value for isHtmlLabel. Default is false.
*/
protected boolean htmlLabels = false;
/**
* Specifies if nesting of swimlanes is allowed. Default is true.
*/
protected boolean swimlaneNesting = true;
/**
* Specifies the maximum number of changes that should be processed to find
* the dirty region. If the number of changes is larger, then the complete
* grah is repainted. A value of zero will always compute the dirty region
* for any number of changes. Default is 1000.
*/
protected int changesRepaintThreshold = 1000;
/**
* Specifies if the origin should be automatically updated.
*/
protected boolean autoOrigin = false;
/**
* Holds the current automatic origin.
*/
protected mxPoint origin = new mxPoint();
/**
* Holds the list of bundles.
*/
protected static List<mxImageBundle> imageBundles = new LinkedList<mxImageBundle>();
/**
* Fires repaint events for full repaints.
*/
protected mxIEventListener fullRepaintHandler = new mxIEventListener()
{
public void invoke(Object sender, mxEventObject evt)
{
repaint();
}
};
/**
* Fires repaint events for full repaints.
*/
protected mxIEventListener updateOriginHandler = new mxIEventListener()
{
public void invoke(Object sender, mxEventObject evt)
{
if (isAutoOrigin())
{
updateOrigin();
}
}
};
/**
* Fires repaint events for model changes.
*/
protected mxIEventListener graphModelChangeHandler = new mxIEventListener()
{
public void invoke(Object sender, mxEventObject evt)
{
mxRectangle dirty = graphModelChanged((mxIGraphModel) sender,
(List<mxUndoableChange>) ((mxUndoableEdit) evt
.getProperty("edit")).getChanges());
repaint(dirty);
}
};
/**
* Constructs a new graph with an empty
* {@link com.mxgraph.model.mxGraphModel}.
*/
public mxGraph()
{
this(null, null);
}
/**
* Constructs a new graph for the specified model. If no model is
* specified, then a new, empty {@link com.mxgraph.model.mxGraphModel} is
* used.
*
* @param model Model that contains the graph data
*/
public mxGraph(mxIGraphModel model)
{
this(model, null);
}
/**
* Constructs a new graph for the specified model. If no model is
* specified, then a new, empty {@link com.mxgraph.model.mxGraphModel} is
* used.
*
* @param stylesheet The stylesheet to use for the graph.
*/
public mxGraph(mxStylesheet stylesheet)
{
this(null, stylesheet);
}
/**
* Constructs a new graph for the specified model. If no model is
* specified, then a new, empty {@link com.mxgraph.model.mxGraphModel} is
* used.
*
* @param model Model that contains the graph data
*/
public mxGraph(mxIGraphModel model, mxStylesheet stylesheet)
{
selectionModel = createSelectionModel();
setModel((model != null) ? model : new mxGraphModel());
setStylesheet((stylesheet != null) ? stylesheet : createStylesheet());
setView(createGraphView());
}
/**
* Constructs a new selection model to be used in this graph.
*/
protected mxGraphSelectionModel createSelectionModel()
{
return new mxGraphSelectionModel(this);
}
/**
* Constructs a new stylesheet to be used in this graph.
*/
protected mxStylesheet createStylesheet()
{
return new mxStylesheet();
}
/**
* Constructs a new view to be used in this graph.
*/
protected mxGraphView createGraphView()
{
return new mxGraphView(this);
}
/**
* Returns the graph model that contains the graph data.
*
* @return Returns the model that contains the graph data
*/
public mxIGraphModel getModel()
{
return model;
}
/**
* Sets the graph model that contains the data, and fires an
* mxEvent.CHANGE followed by an mxEvent.REPAINT event.
*
* @param value Model that contains the graph data
*/
public void setModel(mxIGraphModel value)
{
if (model != null)
{
model.removeListener(graphModelChangeHandler);
}
Object oldModel = model;
model = value;
if (view != null)
{
view.revalidate();
}
model.addListener(mxEvent.CHANGE, graphModelChangeHandler);
changeSupport.firePropertyChange("model", oldModel, model);
repaint();
}
/**
* Returns the view that contains the cell states.
*
* @return Returns the view that contains the cell states
*/
public mxGraphView getView()
{
return view;
}
/**
* Sets the view that contains the cell states.
*
* @param value View that contains the cell states
*/
public void setView(mxGraphView value)
{
if (view != null)
{
view.removeListener(fullRepaintHandler);
view.removeListener(updateOriginHandler);
}
Object oldView = view;
view = value;
if (view != null)
{
view.revalidate();
}
// Listens to changes in the view
view.addListener(mxEvent.SCALE, fullRepaintHandler);
view.addListener(mxEvent.SCALE, updateOriginHandler);
view.addListener(mxEvent.TRANSLATE, fullRepaintHandler);
view.addListener(mxEvent.SCALE_AND_TRANSLATE, fullRepaintHandler);
view.addListener(mxEvent.SCALE_AND_TRANSLATE, updateOriginHandler);
view.addListener(mxEvent.UP, fullRepaintHandler);
view.addListener(mxEvent.DOWN, fullRepaintHandler);
changeSupport.firePropertyChange("view", oldView, view);
}
/**
* Returns the stylesheet that provides the style.
*
* @return Returns the stylesheet that provides the style.
*/
public mxStylesheet getStylesheet()
{
return stylesheet;
}
/**
* Sets the stylesheet that provides the style.
*
* @param value Stylesheet that provides the style.
*/
public void setStylesheet(mxStylesheet value)
{
mxStylesheet oldValue = stylesheet;
stylesheet = value;
changeSupport.firePropertyChange("stylesheet", oldValue, stylesheet);
}
/**
* Returns the cells to be selected for the given list of changes.
*/
public void addTopmostVerticesAndEdges(Object cell, List<Object> cells)
{
if (!cells.contains(cell) && model.contains(cell))
{
if (model.isVertex(cell) || model.isEdge(cell))
{
cells.add(cell);
}
else
{
int childCount = model.getChildCount(cell);
for (int i = 0; i < childCount; i++)
{
addTopmostVerticesAndEdges(model.getChildAt(cells, i), cells);
}
}
}
};
/**
* Returns the cells to be selected for the given list of changes.
*/
public Object[] getSelectionCellsForChanges(List<mxUndoableChange> changes)
{
List<Object> cells = new ArrayList<Object>();
Iterator<mxUndoableChange> it = changes.iterator();
while (it.hasNext())
{
Object change = it.next();
if (change instanceof mxChildChange)
{
addTopmostVerticesAndEdges(((mxChildChange) change).getChild(), cells);
}
else if (change instanceof mxTerminalChange)
{
addTopmostVerticesAndEdges(((mxTerminalChange) change).getCell(), cells);
}
else if (change instanceof mxValueChange)
{
addTopmostVerticesAndEdges(((mxValueChange) change).getCell(), cells);
}
else if (change instanceof mxStyleChange)
{
addTopmostVerticesAndEdges(((mxStyleChange) change).getCell(), cells);
}
else if (change instanceof mxGeometryChange)
{
addTopmostVerticesAndEdges(((mxGeometryChange) change).getCell(), cells);
}
else if (change instanceof mxCollapseChange)
{
addTopmostVerticesAndEdges(((mxCollapseChange) change).getCell(), cells);
}
else if (change instanceof mxVisibleChange)
{
mxVisibleChange vc = (mxVisibleChange) change;
if (vc.isVisible())
{
addTopmostVerticesAndEdges(((mxVisibleChange) change).getCell(), cells);
}
}
}
return cells.toArray();
}
/**
* Called when the graph model changes. Invokes processChange on each
* item of the given array to update the view accordingly.
*/
public mxRectangle graphModelChanged(mxIGraphModel sender,
List<mxUndoableChange> changes)
{
int thresh = getChangesRepaintThreshold();
boolean ignoreDirty = thresh > 0 && changes.size() > thresh;
// Ignores dirty rectangle if there was a root change
if (!ignoreDirty)
{
Iterator<mxUndoableChange> it = changes.iterator();
while (it.hasNext())
{
if (it.next() instanceof mxRootChange)
{
ignoreDirty = true;
break;
}
}
}
mxRectangle dirty = processChanges(changes, true, ignoreDirty);
view.validate();
if (isAutoOrigin())
{
updateOrigin();
}
if (!ignoreDirty)
{
mxRectangle tmp = processChanges(changes, false, ignoreDirty);
if (tmp != null)
{
if (dirty == null)
{
dirty = tmp;
}
else
{
dirty.add(tmp);
}
}
}
removeSelectionCells(getRemovedCellsForChanges(changes));
return dirty;
}
/**
* Extends the canvas by doing another validation with a shifted
* global translation if the bounds of the graph are below (0,0).
*
* The first validation is required to compute the bounds of the graph
* while the second validation is required to apply the new translate.
*/
protected void updateOrigin()
{
mxRectangle bounds = getGraphBounds();
if (bounds != null)
{
double scale = getView().getScale();
double x = bounds.getX() / scale - getBorder();
double y = bounds.getY() / scale - getBorder();
if (x < 0 || y < 0)
{
double x0 = Math.min(0, x);
double y0 = Math.min(0, y);
origin.setX(origin.getX() + x0);
origin.setY(origin.getY() + y0);
mxPoint t = getView().getTranslate();
getView().setTranslate(
new mxPoint(t.getX() - x0, t.getY() - y0));
}
else if ((x > 0 || y > 0)
&& (origin.getX() < 0 || origin.getY() < 0))
{
double dx = Math.min(-origin.getX(), x);
double dy = Math.min(-origin.getY(), y);
origin.setX(origin.getX() + dx);
origin.setY(origin.getY() + dy);
mxPoint t = getView().getTranslate();
getView().setTranslate(
new mxPoint(t.getX() - dx, t.getY() - dy));
}
}
}
/**
* Returns the cells that have been removed from the model.
*/
public Object[] getRemovedCellsForChanges(List<mxUndoableChange> changes)
{
List<Object> result = new ArrayList<Object>();
Iterator<mxUndoableChange> it = changes.iterator();
while (it.hasNext())
{
Object change = it.next();
if (change instanceof mxRootChange)
{
break;
}
else if (change instanceof mxChildChange)
{
mxChildChange cc = (mxChildChange) change;
if (model.contains(cc.getPrevious())
&& !model.contains(cc.getParent()))
{
result.addAll(
mxGraphModel.getDescendants(model, cc.getChild()));
}
}
else if (change instanceof mxVisibleChange)
{
Object cell = ((mxVisibleChange) change).getCell();
result.addAll(mxGraphModel.getDescendants(model, cell));
}
}
return result.toArray();
}
/**
* Processes the changes and returns the minimal rectangle to be
* repainted in the buffer. A return value of null means no repaint
* is required.
*/
public mxRectangle processChanges(List<mxUndoableChange> changes,
boolean invalidate, boolean ignoreDirty)
{
mxRectangle bounds = null;
Iterator<mxUndoableChange> it = changes.iterator();
while (it.hasNext())
{
mxRectangle rect = processChange(it.next(), invalidate,
ignoreDirty);
if (bounds == null)
{
bounds = rect;
}
else
{
bounds.add(rect);
}
}
return bounds;
}
/**
* Processes the given change and invalidates the respective cached data
* in <view>. This fires a <root> event if the root has changed in the
* model.
*/
public mxRectangle processChange(mxUndoableChange change,
boolean invalidate, boolean ignoreDirty)
{
mxRectangle result = null;
if (change instanceof mxRootChange)
{
result = (ignoreDirty) ? null : getGraphBounds();
if (invalidate)
{