-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLargeGraphsDemo.java
1764 lines (1570 loc) · 65.2 KB
/
LargeGraphsDemo.java
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
/****************************************************************************
**
** This demo file is part of yFiles for Java (Swing) 3.6.
**
** Copyright (c) 2000-2023 by yWorks GmbH, Vor dem Kreuzberg 28,
** 72070 Tuebingen, Germany. All rights reserved.
**
** yFiles demo files exhibit yFiles for Java (Swing) functionalities. Any redistribution
** of demo files in source code or binary form, with or without
** modification, is not permitted.
**
** Owners of a valid software license for a yFiles for Java (Swing) version that this
** demo is shipped with are allowed to use the demo source code as basis
** for their own yFiles for Java (Swing) powered applications. Use of such programs is
** governed by the rights and conditions as set out in the yFiles for Java (Swing)
** license agreement.
**
** THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
** NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
***************************************************************************/
package viewer.largegraphs;
import com.yworks.yfiles.geometry.IOrientedRectangle;
import com.yworks.yfiles.geometry.InsetsD;
import com.yworks.yfiles.geometry.PointD;
import com.yworks.yfiles.graph.DefaultGraph;
import com.yworks.yfiles.graph.GraphDecorator;
import com.yworks.yfiles.graph.GraphItemTypes;
import com.yworks.yfiles.graph.IEdge;
import com.yworks.yfiles.graph.IEdgeDefaults;
import com.yworks.yfiles.graph.IGraph;
import com.yworks.yfiles.graph.ILabel;
import com.yworks.yfiles.graph.ILabelOwner;
import com.yworks.yfiles.graph.IModelItem;
import com.yworks.yfiles.graph.INode;
import com.yworks.yfiles.graph.INodeDefaults;
import com.yworks.yfiles.graph.IPort;
import com.yworks.yfiles.graph.IPortOwner;
import com.yworks.yfiles.graph.labelmodels.FreeEdgeLabelModel;
import com.yworks.yfiles.graph.labelmodels.FreeLabelModel;
import com.yworks.yfiles.graph.labelmodels.FreeNodeLabelModel;
import com.yworks.yfiles.graph.labelmodels.ILabelModelParameter;
import com.yworks.yfiles.graph.styles.DefaultLabelStyle;
import com.yworks.yfiles.graph.styles.PolylineEdgeStyle;
import com.yworks.yfiles.graph.styles.RectangleNodeStyle;
import com.yworks.yfiles.graph.styles.ShapeNodeShape;
import com.yworks.yfiles.graph.styles.ShapeNodeStyle;
import com.yworks.yfiles.graph.styles.VoidLabelStyle;
import com.yworks.yfiles.graphml.GraphMLIOHandler;
import com.yworks.yfiles.graphml.SerializationProperties;
import com.yworks.yfiles.utils.IEnumerator;
import com.yworks.yfiles.utils.IListEnumerable;
import com.yworks.yfiles.view.Animator;
import com.yworks.yfiles.view.CanvasComponent;
import com.yworks.yfiles.view.Colors;
import com.yworks.yfiles.view.EdgeStyleDecorationInstaller;
import com.yworks.yfiles.view.GraphComponent;
import com.yworks.yfiles.view.GraphOverviewComponent;
import com.yworks.yfiles.view.GraphSelection;
import com.yworks.yfiles.view.IAnimation;
import com.yworks.yfiles.view.IGraphSelection;
import com.yworks.yfiles.view.LabelStyleDecorationInstaller;
import com.yworks.yfiles.view.NodeStyleDecorationInstaller;
import com.yworks.yfiles.view.Pen;
import com.yworks.yfiles.view.StyleDecorationZoomPolicy;
import com.yworks.yfiles.view.VisualCachingPolicy;
import com.yworks.yfiles.view.input.GraphEditorInputMode;
import com.yworks.yfiles.view.input.ICommand;
import com.yworks.yfiles.view.input.IInputMode;
import com.yworks.yfiles.view.input.WaitInputMode;
import toolkit.AbstractDemo;
import viewer.largegraphs.animations.CircleNodeAnimation;
import viewer.largegraphs.animations.CirclePanAnimation;
import viewer.largegraphs.animations.ZoomInAndBackAnimation;
import viewer.largegraphs.styles.WrapperEdgeStyle;
import viewer.largegraphs.styles.WrapperLabelStyle;
import viewer.largegraphs.styles.WrapperNodeStyle;
import viewer.largegraphs.styles.fast.FastEdgeStyle;
import viewer.largegraphs.styles.fast.FastLabelStyle;
import viewer.largegraphs.styles.levelofdetail.LevelOfDetailLabelStyle;
import viewer.largegraphs.styles.levelofdetail.LevelOfDetailNodeStyle;
import viewer.largegraphs.styles.selection.FastEdgeSelectionStyle;
import viewer.largegraphs.styles.selection.FastLabelSelectionStyle;
import viewer.largegraphs.styles.selection.FastNodeSelectionStyle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.NumberFormat;
import java.text.ParseException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Random;
import java.util.function.Consumer;
import java.util.zip.GZIPInputStream;
/**
* This demo illustrates improvements in rendering performance for very large graphs in yFiles for Java (Swing).
*/
public class LargeGraphsDemo extends AbstractDemo {
// region Wrapper styles so that styles are easier to change.
// Wrapper style for edge labels.
private WrapperLabelStyle edgeLabelStyle = new WrapperLabelStyle(null);
// Wrapper style for edges.
private WrapperEdgeStyle edgeStyle = new WrapperEdgeStyle(null);
// Wrapper style for node labels
private WrapperLabelStyle nodeLabelStyle = new WrapperLabelStyle(null);
// Wrapper style for nodes
private WrapperNodeStyle nodeStyle = new WrapperNodeStyle(null);
// region Fields for GUI items
private GraphOverviewComponent overview;
private ShowGraph previousBtn;
private ShowGraph nextBtn;
private JCheckBox disableOverviewCB;
private JCheckBox enableFastStylesCB;
private JPanel fastStylesPane;
private DoubleTextField hideEdgesTF;
private DoubleTextField hideBendsTF;
private DoubleTextField hideEdgeLabelsTF;
private DoubleTextField sketchEdgeLabelsTF;
private DoubleTextField nodeStyleTF;
private DoubleTextField hideNodeLabelsTF;
private DoubleTextField sketchNodeLabelsTF;
private JCheckBox disableSelectionHandlesCB;
private JCheckBox customSelectionDecorationCB;
private JCheckBox fixLabelPositionCB;
private JCheckBox visualCachingCB;
private JLabel zoomLbl;
private JLabel selectedItemsLbl;
private JLabel fpsLbl;
private JLabel frameCountLbl;
private FPSMeter fpsMeter;
// endregion
// region Configure GUI Components
protected void configure(JRootPane rootPane) {
Container contentPane = rootPane.getContentPane();
contentPane.add(graphComponent, BorderLayout.CENTER);
JToolBar toolBar = createToolBar();
if (toolBar != null) {
configureToolBar(toolBar);
contentPane.add(toolBar, BorderLayout.NORTH);
}
JPanel rightBox = new JPanel();
rightBox.setLayout(new BoxLayout(rightBox, BoxLayout.Y_AXIS));
overview = new GraphOverviewComponent();
overview.setSize(100, 165);
overview.setVisible(false);
overview.setBorder(BorderFactory.createTitledBorder("Overview"));
rightBox.add(overview);
JComponent helpPane = createHelpPane();
if (helpPane != null) {
rightBox.add(helpPane);
}
contentPane.add(rightBox, BorderLayout.EAST);
JPanel leftBox = new JPanel(new BorderLayout());
JComponent settingsPane = createSettingsPane();
JScrollPane settingScrollPane = new JScrollPane(settingsPane);
leftBox.add(settingScrollPane, BorderLayout.CENTER);
JComponent testControlsPane = createTestControlsPane();
leftBox.add(testControlsPane, BorderLayout.SOUTH);
contentPane.add(leftBox, BorderLayout.WEST);
// Adds a menu bar to the JRootPane of the application frame in addition to the default graph component, toolbar, and help pane.
JMenuBar menuBar = new JMenuBar();
configureMenu(menuBar);
rootPane.setJMenuBar(menuBar);
}
// region Create Performance Settings
private JComponent createSettingsPane() {
JPanel settingsPane = new JPanel(new GridBagLayout());
TitledBorder performanceBorder = BorderFactory.createTitledBorder("Performance optimizations");
settingsPane.setBorder(performanceBorder);
GridBagConstraints gbc = new GridBagConstraints();
int settingsGridY = 0;
{
disableOverviewCB = new JCheckBox("Disable overview", true);
disableOverviewCB.setToolTipText("Disables the overview component, which can make drawing the main graph control slower");
disableOverviewCB.addActionListener(
e -> getPerformanceSettings().setOverviewDisabled(disableOverviewCB.isSelected()));
gbc.anchor = GridBagConstraints.LINE_START;
settingsPane.add(disableOverviewCB, gbc);
}
{
enableFastStylesCB = new JCheckBox("Enable fast styles", true);
enableFastStylesCB.setToolTipText("Enables level-of-detail styles and low-fidelity styles for low zoom levels");
enableFastStylesCB.addActionListener(e -> {
boolean enabled = enableFastStylesCB.isSelected();
getPerformanceSettings().setFastStylesEnabled(enabled);
fastStylesPane.setEnabled(enabled);
for (Component component : fastStylesPane.getComponents()) {
component.setEnabled(enabled);
}
});
gbc.gridy = ++settingsGridY;
settingsPane.add(enableFastStylesCB, gbc);
}
{
fastStylesPane = new JPanel(new GridBagLayout());
TitledBorder fastStyleBorder = BorderFactory.createTitledBorder("Fast Styles");
fastStylesPane.setBorder(fastStyleBorder);
int fastStyleGridY = 0;
{
gbc.gridy = fastStyleGridY;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.LINE_START;
fastStylesPane.add(new JLabel("Edges"), gbc);
JPanel spacerCol3 = new JPanel();
Dimension col3Size = new Dimension(40, 20);
spacerCol3.setMinimumSize(col3Size);
spacerCol3.setMaximumSize(col3Size);
spacerCol3.setPreferredSize(col3Size);
gbc.gridx = 2;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.BOTH;
fastStylesPane.add(spacerCol3, gbc);
JPanel spacerCol4 = new JPanel();
Dimension col4Size = new Dimension(20, 20);
spacerCol4.setMinimumSize(col4Size);
spacerCol4.setMaximumSize(col4Size);
spacerCol4.setPreferredSize(col4Size);
gbc.gridx = 3;
fastStylesPane.add(spacerCol4, gbc);
}
{
JPanel spacerCol1 = new JPanel();
Dimension col1Size = new Dimension(20, 20);
spacerCol1.setMinimumSize(col1Size);
spacerCol1.setMaximumSize(col1Size);
spacerCol1.setPreferredSize(col1Size);
gbc.gridx = 0;
gbc.gridy = ++fastStyleGridY;
fastStylesPane.add(spacerCol1, gbc);
gbc.gridx = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
JLabel hideEdgesLbl = new JLabel("Hide edges shorter than");
hideEdgesLbl.setToolTipText("Edges shorter than this many pixels are not drawn; this doesn't affect the visual result much");
fastStylesPane.add(hideEdgesLbl, gbc);
hideEdgesTF = new DoubleTextField(0, value -> getPerformanceSettings().setMinimumEdgeLength(value));
gbc.gridx = 2;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_END;
fastStylesPane.add(hideEdgesTF, gbc);
gbc.gridx = 3;
gbc.fill = GridBagConstraints.NONE;
fastStylesPane.add(new JLabel("px"), gbc);
}
{
gbc.gridx = 1;
gbc.gridy = ++fastStyleGridY;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel hideBendsLbl = new JLabel("Don't show bends below");
hideBendsLbl.setToolTipText("Bends will not be shown below this zoom level");
fastStylesPane.add(hideBendsLbl, gbc);
hideBendsTF = new DoubleTextField(0, value -> getPerformanceSettings().setEdgeBendThreshold(value));
gbc.gridx = 2;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_END;
fastStylesPane.add(hideBendsTF, gbc);
gbc.gridx = 3;
gbc.fill = GridBagConstraints.NONE;
fastStylesPane.add(new JLabel("%"), gbc);
}
gbc.gridx = 0;
gbc.gridy = ++fastStyleGridY;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.LINE_START;
fastStylesPane.add(new JLabel("Edge labels"), gbc);
{
gbc.gridx = 1;
gbc.gridy = ++fastStyleGridY;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel hideEdgeLabelsBelowLbl = new JLabel("Hide below");
hideEdgeLabelsBelowLbl.setToolTipText("Hide edge labels below this zoom level");
fastStylesPane.add(hideEdgeLabelsBelowLbl, gbc);
hideEdgeLabelsTF = new DoubleTextField(50, value -> getPerformanceSettings().setEdgeLabelVisibilityThreshold(value));
gbc.gridx = 2;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_END;
fastStylesPane.add(hideEdgeLabelsTF, gbc);
gbc.gridx = 3;
gbc.fill = GridBagConstraints.NONE;
fastStylesPane.add(new JLabel("%"), gbc);
}
{
gbc.gridx = 1;
gbc.gridy = ++fastStyleGridY;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel sketchEdgeLabelsLbl = new JLabel("Sketch below");
sketchEdgeLabelsLbl.setToolTipText("Render edge labels as sketch below this zoom level");
fastStylesPane.add(sketchEdgeLabelsLbl, gbc);
sketchEdgeLabelsTF = new DoubleTextField(50,
value -> getPerformanceSettings().setEdgeLabelTextThreshold(value));
gbc.gridx = 2;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_END;
fastStylesPane.add(sketchEdgeLabelsTF, gbc);
gbc.gridx = 3;
gbc.fill = GridBagConstraints.NONE;
fastStylesPane.add(new JLabel("%"), gbc);
}
gbc.gridx = 0;
gbc.gridy = ++fastStyleGridY;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.LINE_START;
fastStylesPane.add(new JLabel("Nodes"), gbc);
{
gbc.gridx = 1;
gbc.gridy = ++fastStyleGridY;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel complexNodeStyleLbl = new JLabel("Prettier node style above");
complexNodeStyleLbl.setToolTipText("Render nodes in a more complex style above this zoom level");
fastStylesPane.add(complexNodeStyleLbl, gbc);
nodeStyleTF = new DoubleTextField(60, value -> getPerformanceSettings().setComplexNodeStyleThreshold(value));
gbc.gridx = 2;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_END;
fastStylesPane.add(nodeStyleTF, gbc);
gbc.gridx = 3;
gbc.fill = GridBagConstraints.NONE;
fastStylesPane.add(new JLabel("%"), gbc);
}
gbc.gridx = 0;
gbc.gridy = ++fastStyleGridY;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.LINE_START;
fastStylesPane.add(new JLabel("Node labels"), gbc);
{
gbc.gridx = 1;
gbc.gridy = ++fastStyleGridY;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel hideNodeLabelsLbl = new JLabel("Hide below");
hideNodeLabelsLbl.setToolTipText("Hide node labels below this zoom level");
fastStylesPane.add(hideNodeLabelsLbl, gbc);
hideNodeLabelsTF = new DoubleTextField(20,
value -> getPerformanceSettings().setNodeLabelVisibilityThreshold(value));
gbc.gridx = 2;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_END;
fastStylesPane.add(hideNodeLabelsTF, gbc);
gbc.gridx = 3;
gbc.fill = GridBagConstraints.NONE;
fastStylesPane.add(new JLabel("%"), gbc);
}
{
gbc.gridx = 1;
gbc.gridy = ++fastStyleGridY;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel sketchNodeLabelsLbl = new JLabel("Sketch below");
sketchNodeLabelsLbl.setToolTipText("Render node labels as sketch below this zoom level");
fastStylesPane.add(sketchNodeLabelsLbl, gbc);
sketchNodeLabelsTF = new DoubleTextField(40,
value -> getPerformanceSettings().setNodeLabelTextThreshold(value));
gbc.gridx = 2;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.LINE_END;
fastStylesPane.add(sketchNodeLabelsTF, gbc);
gbc.gridx = 3;
gbc.fill = GridBagConstraints.NONE;
fastStylesPane.add(new JLabel("%"), gbc);
}
{
JPanel dummy = new JPanel();
gbc.gridx = 0;
gbc.gridy = ++fastStyleGridY;
gbc.gridwidth = 4;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.BOTH;
fastStylesPane.add(dummy, gbc);
}
gbc.gridx = 0;
gbc.gridy = ++settingsGridY;
gbc.gridwidth = 1;
settingsPane.add(fastStylesPane, gbc);
}
{
disableSelectionHandlesCB = new JCheckBox("Disable selection handles", true);
disableSelectionHandlesCB.addActionListener(e -> getPerformanceSettings().setSelectionHandlesDisabled(
disableSelectionHandlesCB.isSelected()));
disableSelectionHandlesCB.setToolTipText("Disables selection handles, which can slow down things considerably if there are many of them");
gbc.gridy = ++settingsGridY;
gbc.anchor = GridBagConstraints.LINE_START;
settingsPane.add(disableSelectionHandlesCB, gbc);
}
{
customSelectionDecorationCB = new JCheckBox("Enable custom selection decoration", true);
customSelectionDecorationCB.addActionListener(e -> getPerformanceSettings().setCustomSelectionDecoratorEnabled(
customSelectionDecorationCB.isSelected()));
customSelectionDecorationCB.setToolTipText("Uses faster implementations for the selection decoration");
gbc.gridy = ++settingsGridY;
gbc.anchor = GridBagConstraints.LINE_START;
settingsPane.add(customSelectionDecorationCB, gbc);
}
{
fixLabelPositionCB = new JCheckBox("Fix label position", false);
fixLabelPositionCB.addActionListener(e -> getPerformanceSettings().setFixedLabelPositionsEnabled(fixLabelPositionCB.isSelected()));
fixLabelPositionCB.setToolTipText("Fixes the position of labels on the canvas which makes calculating their position much cheaper");
gbc.gridy = ++settingsGridY;
gbc.anchor = GridBagConstraints.LINE_START;
settingsPane.add(fixLabelPositionCB, gbc);
}
{
visualCachingCB = new JCheckBox("Enable visual caching", false);
visualCachingCB.addActionListener(e -> getPerformanceSettings().setVisualCachingEnabled(visualCachingCB.isSelected()));
visualCachingCB.setToolTipText("Caches some visuals while they are outside the view port so they don't have to be recreated so often.");
gbc.gridy = ++settingsGridY;
gbc.anchor = GridBagConstraints.LINE_START;
settingsPane.add(visualCachingCB, gbc);
}
gbc.gridx = 0;
gbc.gridy = ++settingsGridY;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
JPanel bottomPane = new JPanel();
settingsPane.add(bottomPane, gbc);
return settingsPane;
}
// endregion
// region Create Test controls
private JComponent createTestControlsPane() {
JPanel testControlPane = new JPanel(new GridBagLayout());
testControlPane.setBorder(BorderFactory.createTitledBorder("Test controls"));
GridBagConstraints gbc = new GridBagConstraints();
int testControlGridY = 0;
// Information
JPanel informationPane = new JPanel(new GridBagLayout());
TitledBorder virtualizationPaneBorder = BorderFactory.createTitledBorder("Information");
informationPane.setBorder(virtualizationPaneBorder);
int informationGridY = 0;
{
gbc.gridx = 0;
gbc.gridy = informationGridY;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel zoomLevelLbl = new JLabel("Zoom level");
zoomLevelLbl.setToolTipText("The current zoom level of the graph component");
informationPane.add(zoomLevelLbl, gbc);
JPanel spacer = new JPanel();
gbc.gridx = 1;
gbc.weightx = 1;
informationPane.add(spacer, gbc);
gbc.gridx = 2;
gbc.weightx = 0;
gbc.anchor = GridBagConstraints.LINE_END;
zoomLbl = new JLabel();
informationPane.add(zoomLbl, gbc);
}
{
gbc.gridx = 0;
gbc.gridy = ++informationGridY;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel selectedItemsLbl = new JLabel("Selected items");
selectedItemsLbl.setToolTipText("The number of currently selected elements");
informationPane.add(selectedItemsLbl, gbc);
JPanel spacer = new JPanel();
gbc.gridx = 1;
gbc.weightx = 1;
informationPane.add(spacer, gbc);
gbc.gridx = 2;
gbc.weightx = 0;
gbc.anchor = GridBagConstraints.LINE_END;
this.selectedItemsLbl = new JLabel("0");
informationPane.add(this.selectedItemsLbl, gbc);
}
{
gbc.gridx = 0;
gbc.gridy = ++informationGridY;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel animationFPSLbl = new JLabel("Animation FPS");
animationFPSLbl.setToolTipText("The current number of frames drawn per second");
informationPane.add(animationFPSLbl, gbc);
JPanel spacer = new JPanel();
gbc.gridx = 1;
gbc.weightx = 1;
informationPane.add(spacer, gbc);
gbc.gridx = 2;
gbc.weightx = 0;
gbc.anchor = GridBagConstraints.LINE_END;
fpsLbl = new JLabel("0");
informationPane.add(fpsLbl, gbc);
}
{
gbc.gridx = 0;
gbc.gridy = ++informationGridY;
gbc.anchor = GridBagConstraints.LINE_START;
JLabel fpaLbl = new JLabel("Frames in Animation");
fpaLbl.setToolTipText("The total number of frames rendered for the current animation");
informationPane.add(fpaLbl, gbc);
JPanel spacer = new JPanel();
gbc.gridx = 1;
gbc.weightx = 1;
informationPane.add(spacer, gbc);
gbc.gridx = 2;
gbc.weightx = 0;
gbc.anchor = GridBagConstraints.LINE_END;
frameCountLbl = new JLabel("0");
informationPane.add(frameCountLbl, gbc);
}
gbc.gridx = 0;
gbc.gridy = testControlGridY;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
testControlPane.add(informationPane, gbc);
// Animations
JPanel animationPane = new JPanel(new GridBagLayout());
animationPane.setBorder(BorderFactory.createTitledBorder("Animations"));
{
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
animationPane.add(new IconActionLabel("zoom.PNG", "Zooms to a random node and back", this::onZoomAnimationClicked), gbc);
gbc.gridx = 1;
animationPane.add(new IconActionLabel("panInCircle.PNG","Pans the viewport in a circular motion" , this::onPanAnimationClicked), gbc);
gbc.gridx = 2;
animationPane.add(new IconActionLabel("spiralZoom.PNG", "Combines zooming and panning at the same time", this::onSpiralZoomAnimationClicked), gbc);
gbc.gridx = 3;
animationPane.add(new IconActionLabel("moveNodes.PNG","Moves selected nodes randomly" , this::onNodeAnimationClicked), gbc);
}
gbc.gridx = 0;
gbc.gridy = ++testControlGridY;
testControlPane.add(animationPane, gbc);
// Selections
JPanel selectionPane = new JPanel(new GridBagLayout());
selectionPane.setBorder(BorderFactory.createTitledBorder("Selection"));
{
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.CENTER;
selectionPane.add(new JLabel("Nodes"), gbc);
gbc.gridx = 2;
selectionPane.add(new JLabel("Edges"), gbc);
gbc.gridx = 3;
selectionPane.add(new JLabel("Labels"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.BOTH;
JButton selNothingBtn = new JButton("Nothing");
selNothingBtn.setToolTipText("Deselect everything");
selNothingBtn.addActionListener(this::onSelectNothing);
selectionPane.add(selNothingBtn, gbc);
JButton sel1000NodesBtn = new JButton("1000");
sel1000NodesBtn.setToolTipText("Select 1000 random nodes");
sel1000NodesBtn.addActionListener(this::onSelect1000Nodes);
gbc.gridx = 1;
selectionPane.add(sel1000NodesBtn, gbc);
JButton sel1000EdgesBtn = new JButton("1000");
sel1000EdgesBtn.setToolTipText("Select 1000 random edges");
sel1000EdgesBtn.addActionListener(this::onSelect1000Edges);
gbc.gridx = 2;
selectionPane.add(sel1000EdgesBtn, gbc);
JButton sel1000LabelsBtn = new JButton("1000");
sel1000LabelsBtn.setToolTipText("Select 1000 random labels");
sel1000LabelsBtn.addActionListener(this::onSelect1000Labels);
gbc.gridx = 3;
selectionPane.add(sel1000LabelsBtn, gbc);
JButton selEveryThingBtn = new JButton("Everything");
selEveryThingBtn.setToolTipText("Select all nodes, edges and labels in the graph");
selEveryThingBtn.addActionListener(this::onSelectAll);
gbc.gridy = 2;
gbc.gridx = 0;
selectionPane.add(selEveryThingBtn, gbc);
JButton selAllNodesBtn = new JButton("All");
selAllNodesBtn.setToolTipText("Select all nodes");
selAllNodesBtn.addActionListener(this::onSelectAllNodes);
gbc.gridx = 1;
selectionPane.add(selAllNodesBtn, gbc);
JButton selAllEdgesBtn = new JButton("All");
selAllEdgesBtn.setToolTipText("Select all edges");
selAllEdgesBtn.addActionListener(this::onSelectAllEdges);
gbc.gridx = 2;
selectionPane.add(selAllEdgesBtn, gbc);
JButton selAllLabelsBtn = new JButton("All");
selAllLabelsBtn.setToolTipText("Select all labels");
selAllLabelsBtn.addActionListener(this::onSelectAllLabels);
gbc.gridx = 3;
selectionPane.add(selAllLabelsBtn, gbc);
}
gbc.gridx = 0;
gbc.gridy = ++testControlGridY;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
testControlPane.add(selectionPane, gbc);
return testControlPane;
}
// endregion
// region Configure Menu
/**
* Configures the given {@link javax.swing.JMenuBar}.
*
* @param menuBar the {@link javax.swing.JMenuBar} to configure
*/
private void configureMenu(JMenuBar menuBar) {
JMenu fileMenu = new JMenu("File");
fileMenu.add(createExitAction());
menuBar.add(fileMenu);
JMenu editMenu = new JMenu("Edit");
editMenu.add(createCommandMenuItemAction("Cut", ICommand.CUT, null, graphComponent));
editMenu.add(createCommandMenuItemAction("Copy", ICommand.COPY, null, graphComponent));
editMenu.add(createCommandMenuItemAction("Paste", ICommand.PASTE, null, graphComponent));
editMenu.add(createCommandMenuItemAction("Duplicate", ICommand.DUPLICATE, null, graphComponent));
editMenu.add(createCommandMenuItemAction("Delete", ICommand.DELETE, null, graphComponent));
editMenu.addSeparator();
editMenu.add(createCommandMenuItemAction("Undo", ICommand.UNDO, null, graphComponent));
editMenu.add(createCommandMenuItemAction("Redo", ICommand.REDO, null, graphComponent));
menuBar.add(editMenu);
JMenu viewMenu = new JMenu("View");
viewMenu.add(createCommandMenuItemAction("Increase zoom", ICommand.INCREASE_ZOOM, null, graphComponent));
viewMenu.add(createCommandMenuItemAction("Zoom 1:1", ICommand.ZOOM, 1, graphComponent));
viewMenu.add(createCommandMenuItemAction("Decrease zoom", ICommand.DECREASE_ZOOM, null, graphComponent));
viewMenu.add(createCommandMenuItemAction("Fit Graph to Bounds", ICommand.FIT_GRAPH_BOUNDS, null, graphComponent));
menuBar.add(viewMenu);
}
/**
* Creates an {@link javax.swing.Action} to exit the demo.
*/
private Action createExitAction() {
AbstractAction action = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
action.putValue(Action.NAME, "Exit");
return action;
}
// endregion
// region Configure ToolBar
private JComboBox<GraphEntry> graphChooserBox;
@Override
protected void configureToolBar(JToolBar toolBar) {
toolBar.add(createCommandButtonAction("Undo", "undo-16.png", ICommand.UNDO, null, graphComponent));
toolBar.add(createCommandButtonAction("Redo", "redo-16.png", ICommand.REDO, null, graphComponent));
toolBar.addSeparator();
toolBar.add(createCommandButtonAction("Cut", "cut-16.png", ICommand.CUT, null, graphComponent));
toolBar.add(createCommandButtonAction("Copy", "copy-16.png", ICommand.COPY, null, graphComponent));
toolBar.add(createCommandButtonAction("Paste", "paste-16.png", ICommand.PASTE, null, graphComponent));
toolBar.add(createCommandButtonAction("Delete", "delete3-16.png", ICommand.DELETE, null, graphComponent));
toolBar.addSeparator();
toolBar.add(createCommandButtonAction("Zoom in", "plus2-16.png", ICommand.INCREASE_ZOOM, null, graphComponent));
toolBar.add(createCommandButtonAction("Zoom 1:1", "zoom-original2-16.png", ICommand.ZOOM, 1, graphComponent));
toolBar.add(createCommandButtonAction("Zoom out", "minus2-16.png", ICommand.DECREASE_ZOOM, null, graphComponent));
toolBar.add(createCommandButtonAction("Fit the graph content", "fit2-16.png", ICommand.FIT_CONTENT, null, graphComponent));
toolBar.addSeparator();
Component comboBox = createComboBox();
previousBtn = new ShowGraph(false);
toolBar.add(previousBtn);
toolBar.add(comboBox);
nextBtn = new ShowGraph(true);
toolBar.add(nextBtn);
}
/**
* Creates the JComboBox where the various graphs are selectable.
*/
private Component createComboBox() {
graphChooserBox = new JComboBox<>(new GraphEntry[]{
new GraphEntry("hierarchic_2000_2100.graphmlz", "Hierarchic: 2000 nodes, 2100 edges"),
new GraphEntry("hierarchic_5000_5100.graphmlz", "Hierarchic: 5000 nodes, 5100 edges"),
new GraphEntry("hierarchic_10000_11000.graphmlz", "Hierarchic: 10000 nodes, 11000 edges"),
new GraphEntry("hierarchic_15000_16000.graphmlz", "Hierarchic: 15000 nodes, 16000 edges"),
new GraphEntry("balloon_2000_1999.graphmlz", "Tree: 2000 nodes, 1999 edges"),
new GraphEntry("balloon_5000_4999.graphmlz", "Tree: 5000 nodes, 4999 edges"),
new GraphEntry("balloon_10000_9999.graphmlz", "Tree: 10000 nodes, 9999 edges"),
new GraphEntry("balloon_15000_14999.graphmlz", "Tree: 15000 nodes, 14999 edges")
});
graphChooserBox.setMaximumSize(graphChooserBox.getPreferredSize());
graphChooserBox.addActionListener(e -> readSampleGraph());
return graphChooserBox;
}
/**
* Reads the currently selected GraphML from the graphChooserBox
*/
private void readSampleGraph() {
GraphEntry graphEntry = (GraphEntry) this.graphChooserBox.getSelectedItem();
loadGraphAsync(graphEntry);
}
/**
* Loads a graph asynchronously and places it in the {@link GraphComponent}.
*
* @param graphEntry The graph information
*/
private void loadGraphAsync(GraphEntry graphEntry) {
graphChooserBox.setEnabled(false);
nextBtn.setEnabled(false);
previousBtn.setEnabled(false);
graphComponent.lookup(WaitInputMode.class).setWaiting(true);
updatePerformanceSettings(bestSettings[graphChooserBox.getSelectedIndex()]);
final DefaultGraph g = new DefaultGraph();
g.setUndoEngineEnabled(true);
setDefaultStyles(g);
updateStyles();
setSelectionDecorators(g);
updateSelectionHandlesSetting();
updateOverviewDisabledSetting();
// first derive the file name
URL graphML = getClass().getResource("resources/" + graphEntry.fileName);
new Thread(() -> {
// then load the graph asynchronously
try {
GraphMLIOHandler handler = graphComponent.getGraphMLIOHandler();
FileInputStream inputStream = new FileInputStream(new File(graphML.toURI()));
GZIPInputStream zipInputStream = new GZIPInputStream(inputStream);
handler.getDeserializationPropertyOverrides().set(SerializationProperties.PARSE_LABEL_SIZE, Boolean.FALSE);
handler.read(g, zipInputStream);
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
EventQueue.invokeLater(() ->
{
// update and set the graph in the AWT Event thread
updateFixedLabelPositionsSetting(g);
updateVisualCaching();
graphChooserBox.setEnabled(true);
updateButtons();
graphComponent.lookup(WaitInputMode.class).setWaiting(false);
graphComponent.setGraph(g);
graphComponent.fitGraphBounds();
// the commands CanExecute state might have changed - suggest a re-query. mainly to update the enabled status of the previous / next buttons.
ICommand.invalidateRequerySuggested();
}
);
}).start();
}
/**
* Disables the 'Previous/Next graph' buttons in the UI according to whether there is a previous/next graph to switch
* to.
*/
private void updateButtons() {
nextBtn.setEnabled(graphChooserBox.getSelectedIndex() < graphChooserBox.getItemCount() - 1);
previousBtn.setEnabled(graphChooserBox.getSelectedIndex() > 0);
}
private class ShowGraph extends AbstractAction {
final boolean next;
ShowGraph(boolean next) {
super(next ? "Next" : "Previous");
this.next = next;
putValue(SHORT_DESCRIPTION, next ? "Show next graph" : "Show previous graph");
putValue(SMALL_ICON, createIcon(next ? "arrow-right-16.png" : "arrow-left-16.png"));
graphChooserBox.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
updateEnabledState();
}
});
updateEnabledState();
}
@Override
public void actionPerformed(ActionEvent e) {
final JComboBox jcb = LargeGraphsDemo.this.graphChooserBox;
if (next) {
jcb.setSelectedIndex(jcb.getSelectedIndex() + 1);
} else {
jcb.setSelectedIndex(jcb.getSelectedIndex() - 1);
}
}
private void updateEnabledState() {
final JComboBox jcb = LargeGraphsDemo.this.graphChooserBox;
if (next) {
setEnabled(jcb.getSelectedIndex() < jcb.getItemCount() - 1);
} else {
setEnabled(jcb.getSelectedIndex() > 0);
}
}
}
/**
* Entry of the {@link #graphChooserBox} containing the file name and the display name of a sample graph.
*/
private static class GraphEntry {
private String fileName;
private String displayName;
public GraphEntry(String fileName, String displayName) {
this.fileName = fileName;
this.displayName = displayName;
}
@Override
public String toString() {
return displayName;
}
}
// endregion
// endregion
@Override
public void initialize() {
initializeInformationPane();
initializeInputMode();
initializePerformanceSettings();
}
@Override
public void onVisible() {
graphChooserBox.setSelectedIndex(0);
}
/**
* Initializes the listener to update the information on the Information pane.
*/
private void initializeInformationPane() {
graphComponent.addZoomChangedListener(
(source, args) -> zoomLbl.setText(((int) (graphComponent.getZoom() * 10000)) / 100.0 + " %"));
graphComponent.getSelection().addItemSelectionChangedListener((source, args) -> {
IGraphSelection s = graphComponent.getSelection();
int selectedItemCount = s.getSelectedBends().size() + s.getSelectedEdges().size() + s.getSelectedLabels().size() + s.getSelectedNodes().size() + s.getSelectedPorts().size();
selectedItemsLbl.setText(Integer.toString(selectedItemCount));
});
fpsMeter = new FPSMeter(graphComponent, () -> {
fpsLbl.setText(fpsMeter.getFps());
frameCountLbl.setText(Integer.toString(fpsMeter.getFrameCount()));
});
}
/**
* Initializes the input mode for the {@link GraphComponent}.
*/
private void initializeInputMode() {
GraphEditorInputMode geim = new GraphEditorInputMode();
{