forked from kptran/mct
/
PlotView.java
1267 lines (1079 loc) · 40 KB
/
PlotView.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
/*******************************************************************************
* Mission Control Technologies, Copyright (c) 2009-2012, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* The MCT platform is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* MCT includes source code licensed under additional open source licenses. See
* the MCT Open Source Licenses file included with this distribution or the About
* MCT Licenses dialog available at runtime from the MCT Help menu for additional
* information.
*******************************************************************************/
package gov.nasa.arc.mct.fastplot.bridge;
import gov.nasa.arc.mct.components.FeedProvider;
import gov.nasa.arc.mct.fastplot.bridge.PlotConstants.AxisOrientationSetting;
import gov.nasa.arc.mct.fastplot.bridge.PlotConstants.LimitAlarmState;
import gov.nasa.arc.mct.fastplot.bridge.PlotConstants.TimeAxisSubsequentBoundsSetting;
import gov.nasa.arc.mct.fastplot.bridge.PlotConstants.XAxisMaximumLocationSetting;
import gov.nasa.arc.mct.fastplot.bridge.PlotConstants.YAxisMaximumLocationSetting;
import gov.nasa.arc.mct.fastplot.bridge.controls.CornerResetButton;
import gov.nasa.arc.mct.fastplot.bridge.controls.LocalControlKeyEventDispatcher;
import gov.nasa.arc.mct.fastplot.bridge.controls.ControllableAxis;
import gov.nasa.arc.mct.fastplot.bridge.controls.PanControls;
import gov.nasa.arc.mct.fastplot.bridge.controls.ZoomControls;
import gov.nasa.arc.mct.fastplot.settings.LineSettings;
import gov.nasa.arc.mct.fastplot.settings.PlotConfiguration;
import gov.nasa.arc.mct.fastplot.settings.PlotConfigurationDelegator;
import gov.nasa.arc.mct.fastplot.settings.PlotSettings;
import gov.nasa.arc.mct.fastplot.utils.AbbreviatingPlotLabelingAlgorithm;
import gov.nasa.arc.mct.fastplot.view.Axis;
import gov.nasa.arc.mct.fastplot.view.LegendEntryPopupMenuFactory;
import gov.nasa.arc.mct.fastplot.view.PinSupport;
import gov.nasa.arc.mct.fastplot.view.Pinnable;
import gov.nasa.arc.mct.fastplot.view.PlotViewManifestation;
import gov.nasa.arc.mct.fastplot.view.legend.AbstractLegendEntry;
import gov.nasa.arc.mct.gui.FeedView.SynchronizationControl;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
import java.util.Map.Entry;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of the general plot interface.
*
* Construct using the builder pattern.
*
* Example:
* <code>Plot.Builder(new <GeneralPlottingPackageInterface).plotName("Your Plot Name").build();</code>
*
* RefinedAbstraction in bridge pattern.
*/
public class PlotView extends PlotConfigurationDelegator implements PlotAbstraction {
private final static Logger logger = LoggerFactory.getLogger(PlotView.class);
private static final Timer timer = new Timer();
// Manifestation holding this plot
private PlotViewManifestation plotUser;
// Used in time synchronization line mode.
private SynchronizationControl synControl;
private Class <? extends AbstractPlottingPackage> plotPackage;
private String plotName;
/* Appearance Constants */
// - Fonts
private Font timeAxisFont;
// Thickness of the plotted lines.
private int plotLineThickness;
private Color plotBackgroundFrameColor;
private Color plotAreaBackgroundColor;
// - Axis
// -- x-axis
// Point where x-axis intercepts y axis
private int timeAxisIntercept;
//color for drawing x-axis
private Color timeAxisColor;
// x-axis labels
private Color timeAxisLabelColor;
private Color timeAxisLabelTextColor;
// format of the date when shown on the x-axis
private String timeAxisDataFormat;
// -- y-axis
private Color nonTimeAxisColor;
// - Gridlines
private Color gridLineColor;
/* Scrolling and scaling behaviors */
// Number of sample to accumulate before autoscaling the y-axis. This
// prevents rapid changing of the y axis.
private int minSamplesForAutoScale;
private boolean compressionEnabled;
private boolean localControlsEnabled;
private int numberOfSubPlots;
/** The list of sub plots. */
public List<AbstractPlottingPackage> subPlots;
/** The plot panel. */
JPanel plotPanel;
/** Map for containing the data set name to sub-group. */
public Map<String, Set<AbstractPlottingPackage>> dataSetNameToSubGroupMap = new HashMap<String, Set<AbstractPlottingPackage>>();
/** Map for containing the data set name to display map. */
public Map<String, String> dataSetNameToDisplayMap = new HashMap<String, String>();
private AbbreviatingPlotLabelingAlgorithm plotLabelingAlgorithm = new AbbreviatingPlotLabelingAlgorithm();;
/** List of plot subjects. */
List<PlotSubject> subPlotsToIgnoreNextUpdateFrom = new ArrayList<PlotSubject>();
/** Time axis at start of update cycle. */
GregorianCalendar timeAxisMaxAtStartOfDataUpdateCycle = new GregorianCalendar();
/** Lock updates flag. */
boolean lockUpdates = false;
private PinSupport pinSupport = new PinSupport() {
protected void informPinned(boolean pinned) {
if(pinned) {
pause();
} else {
unpause();
}
}
};
private Pinnable timeSyncLinePin = createPin();
private Axis timeAxis = new Axis();
private Pinnable timeAxisUserPin = timeAxis.createPin();
private TimerTask updateTimeBoundsTask;
private AbstractAxis plotTimeAxis;
/**
* Sets the plot view manifestation.
* @param theManifestation - the plot view manifestation.
*/
public void setManifestation(PlotViewManifestation theManifestation) {
if (theManifestation == null ) {
throw new IllegalArgumentException("Plot must not have a null user");
}
plotUser = theManifestation;
for (AbstractPlottingPackage p: subPlots) {
p.setPlotAbstraction(this);
}
}
@Override
public JPanel getPlotPanel() {
return plotPanel;
}
@Override
public void addDataSet(String dataSetName) {
addDataSet(dataSetName.toLowerCase(), getNextColor(subPlots.size()-1));
}
@Override
public void addDataSet(String dataSetName, Color plottingColor) {
throwIllegalArgumentExcpetionIfWeHaveNoPlots();
getLastPlot().addDataSet(dataSetName.toLowerCase(), plottingColor);
String name = dataSetName.toLowerCase();
Set<AbstractPlottingPackage> set = dataSetNameToSubGroupMap.get(name);
if(set == null) {
set = new HashSet<AbstractPlottingPackage>();
dataSetNameToSubGroupMap.put(name, set);
}
set.add(getLastPlot());
dataSetNameToDisplayMap.put(dataSetName.toLowerCase(), dataSetName);
}
@Override
public void addDataSet(String dataSetName, String displayName) {
throwIllegalArgumentExcpetionIfWeHaveNoPlots();
getLastPlot().addDataSet(dataSetName.toLowerCase(), getNextColor(subPlots.size()-1), displayName);
String name = dataSetName.toLowerCase();
Set<AbstractPlottingPackage> set = dataSetNameToSubGroupMap.get(name);
if(set == null) {
set = new HashSet<AbstractPlottingPackage>();
dataSetNameToSubGroupMap.put(name, set);
}
set.add(getLastPlot());
dataSetNameToDisplayMap.put(dataSetName.toLowerCase(), displayName);
}
/**
* Adds the data set per subgroup index, data set name and display name.
* @param subGroupIndex - the subgroup index.
* @param dataSetName - data set name.
* @param displayName - base display name.
*/
public void addDataSet(int subGroupIndex, String dataSetName, String displayName) {
throwIllegalArgumentExcpetionIfIndexIsNotInSubPlots(subGroupIndex);
String lowerCaseDataSetName = dataSetName.toLowerCase();
int actualIndex = subGroupIndex;
subPlots.get(actualIndex).addDataSet(lowerCaseDataSetName,
getNextColor(subGroupIndex), displayName);
Set<AbstractPlottingPackage> set = dataSetNameToSubGroupMap
.get(lowerCaseDataSetName);
if (set == null) {
set = new HashSet<AbstractPlottingPackage>();
dataSetNameToSubGroupMap.put(lowerCaseDataSetName, set);
}
set.add(subPlots.get(actualIndex));
dataSetNameToDisplayMap.put(lowerCaseDataSetName, displayName);
}
/**
* Adds the data set per subgroup index, data set name and display name.
* @param subGroupIndex - the subgroup index.
* @param dataSetName - data set name.
* @param displayName - base display name.
*/
public void addDataSet(int subGroupIndex, String dataSetName, AbstractLegendEntry legendEntry) {
throwIllegalArgumentExcpetionIfIndexIsNotInSubPlots(subGroupIndex);
String lowerCaseDataSetName = dataSetName.toLowerCase();
int actualIndex = subGroupIndex;
subPlots.get(actualIndex).getLegendManager().addLegendEntry(legendEntry);
subPlots.get(actualIndex).addDataSet(lowerCaseDataSetName,
getNextColor(subGroupIndex), legendEntry);
Set<AbstractPlottingPackage> set = dataSetNameToSubGroupMap
.get(lowerCaseDataSetName);
if (set == null) {
set = new HashSet<AbstractPlottingPackage>();
dataSetNameToSubGroupMap.put(lowerCaseDataSetName, set);
}
set.add(subPlots.get(actualIndex));
dataSetNameToDisplayMap.put(lowerCaseDataSetName, legendEntry.getDisplayedName());
}
/**
* Adds the popup menus to plot legend entry.
*/
public void addPopupMenus() {
LegendEntryPopupMenuFactory popupManager = new LegendEntryPopupMenuFactory(plotUser);
for (int index = 0; index < subPlots.size(); index++) {
AbstractPlottingPackage plot = (AbstractPlottingPackage) subPlots.get(index);
for (LegendEntry entry : plot.getLegendManager().getLegendEntryList()) {
entry.setPopup(popupManager);
}
}
}
/**
* Get per-line settings currently in use for this stack of plots.
* Each element of the returned list corresponds,
* in order, to the sub-plots displayed, and maps subscription ID to a
* LineSettings object describing how its plot line should be drawn.
* @return a list of subscription->setting mappings for this plot
*/
public List<Map<String, LineSettings>> getLineSettings() {
List<Map<String,LineSettings>> settingsAssignments = new ArrayList<Map<String,LineSettings>>();
for (int subPlotIndex = 0; subPlotIndex < subPlots.size(); subPlotIndex++) {
Map<String, LineSettings> settingsMap = new HashMap<String, LineSettings>();
settingsAssignments.add(settingsMap);
AbstractPlottingPackage plot = (AbstractPlottingPackage) subPlots.get(subPlotIndex);
for (LegendEntry entry : plot.getLegendManager().getLegendEntryList()) {
settingsMap.put(entry.getDataSetName(), entry.getLineSettings());
}
}
return settingsAssignments;
}
/**
* Set line settings for use in this stack of plots.
* Each element corresponds, in order, to the sub-plots displayed, and maps
* subscription ID to the line settings described by a LineSettings object
* @param lineSettings a list of subscription->line setting mappings for this plot
*/
public void setLineSettings(
List<Map<String, LineSettings>> lineSettings) {
if (lineSettings != null) {
for (int subPlotIndex = 0; subPlotIndex < lineSettings.size() && subPlotIndex < subPlots.size(); subPlotIndex++) {
AbstractPlottingPackage plot = (AbstractPlottingPackage) subPlots.get(subPlotIndex);
for (Entry<String, LineSettings> entry : lineSettings.get(subPlotIndex).entrySet()) {
AbstractPlotDataSeries series = plot.getPlotDataManager().getNamedDataSeries(entry.getKey());
if (series != null) {
AbstractLegendEntry legendEntry = series.getLegendEntry();
if (legendEntry instanceof LegendEntry) {
((LegendEntry) legendEntry).setLineSettings(entry.getValue());
}
}
}
}
}
}
@Override
public boolean isKnownDataSet(String setName) {
assert setName != null : "data set is null";
for (AbstractPlottingPackage p : subPlots) {
if (p.isKnownDataSet(setName.toLowerCase())) return true;
}
return false;
//return dataSetNameToSubGroupMap.containsKey(setName.toLowerCase());
}
@Override
public void refreshDisplay() {
for (AbstractPlottingPackage p: subPlots) {
p.refreshDisplay();
}
}
public void initialDataRequest() {
plotUser.requestDataRefresh(getLastPlot().getCurrentTimeAxisMin(), getLastPlot().getCurrentTimeAxisMax());
}
@Override
public void updateLegend(String dataSetName, FeedProvider.RenderingInfo info) {
String dataSetNameLower = dataSetName.toLowerCase();
if (dataSetNameToSubGroupMap.containsKey(dataSetNameLower)) {
for(AbstractPlottingPackage plot : dataSetNameToSubGroupMap.get(dataSetNameLower)) {
plot.updateLegend(dataSetNameLower, info);
}
}
}
@Override
public LimitAlarmState getNonTimeMaxAlarmState(int subGroupIndex) {
throwIllegalArgumentExcpetionIfIndexIsNotInSubPlots(subGroupIndex);
return subPlots.get(subGroupIndex).getDependentMaxAlarmState();
}
@Override
public LimitAlarmState getNonTimeMinAlarmState(int subGroupIndex) {
throwIllegalArgumentExcpetionIfIndexIsNotInSubPlots(subGroupIndex);
return subPlots.get(subGroupIndex).getDependentMinAlarmState();
}
@Override
public void showTimeSyncLine(GregorianCalendar time) {
assert time != null;
timeSyncLinePin.setPinned(true);
for (AbstractPlottingPackage p: subPlots) {
p.showTimeSyncLine(time);
}
}
@Override
public void removeTimeSyncLine() {
timeSyncLinePin.setPinned(false);
for (AbstractPlottingPackage p: subPlots) {
p.removeTimeSyncLine();
}
}
@Override
public boolean isTimeSyncLineVisible() {
return getLastPlot().isTimeSyncLineVisible();
}
@Override
public void initiateGlobalTimeSync(GregorianCalendar time) {
synControl = plotUser.synchronizeTime(time.getTimeInMillis());
}
@Override
public void updateGlobalTimeSync(GregorianCalendar time) {
if(synControl == null) {
synControl = plotUser.synchronizeTime(time.getTimeInMillis());
} else {
synControl.update(time.getTimeInMillis());
}
}
@Override
public void notifyGlobalTimeSyncFinished() {
if (synControl!=null) {
synControl.synchronizationDone();
}
removeTimeSyncLine();
}
@Override
public boolean inTimeSyncMode() {
return getLastPlot().inTimeSyncMode();
}
private Color getNextColor(int subGroupIndex) {
throwIllegalArgumentExcpetionIfIndexIsNotInSubPlots(subGroupIndex);
if (subPlots.get(subGroupIndex).getDataSetSize() < PlotLineColorPalette.getColorCount()) {
return PlotLineColorPalette.getColor(subPlots.get(subGroupIndex).getDataSetSize());
} else {
// Exceeded the number of colors in the pallet.
return PlotConstants.ROLL_OVER_PLOT_LINE_COLOR;
}
}
@Override
public double getNonTimeMaxCurrentlyDisplayed() {
return getLastPlot().getNonTimeMaxDataValueCurrentlyDisplayed();
}
@Override
public double getNonTimeMinCurrentlyDisplayed() {
return getLastPlot().getNonTimeMinDataValueCurrentlyDisplayed();
}
@Override
public String toString() {
assert plotPackage != null : "Plot package not initalized";
return "Plot: + " + plotName + "\n" + plotPackage.toString();
}
/**
* Construct plots using the builder pattern.
*
*/
public static class Builder {
// Required parameters
private Class<? extends AbstractPlottingPackage> plotPackage;
//Optional parameters
// default values give a "traditional" chart with time on the x-axis etc.
private String plotName = "Plot Name Undefined";
private PlotConfiguration settings = new PlotSettings();
// initial settings
private Font timeAxisFont = PlotConstants.DEFAULT_TIME_AXIS_FONT;
private int plotLineThickness = PlotConstants.DEFAULT_PLOTLINE_THICKNESS ;
private Color plotBackgroundFrameColor = PlotConstants.DEFAULT_PLOT_FRAME_BACKGROUND_COLOR;
private Color plotAreaBackgroundColor = PlotConstants.DEFAULT_PLOT_AREA_BACKGROUND_COLOR;
private int timeAxisIntercept = PlotConstants.DEFAULT_TIME_AXIS_INTERCEPT;
private Color timeAxisColor = PlotConstants.DEFAULT_TIME_AXIS_COLOR;
private Color timeAxisLabelColor = PlotConstants.DEFAULT_TIME_AXIS_LABEL_COLOR;
private String timeAxisDateFormat = PlotConstants.DEFAULT_TIME_AXIS_DATA_FORMAT;
private Color nonTimeAxisColor = PlotConstants.DEFAULT_NON_TIME_AXIS_COLOR;
private Color gridLineColor = PlotConstants.DEFAULT_GRID_LINE_COLOR;
private int minSamplesForAutoScale = PlotConstants.DEFAULT_MIN_SAMPLES_FOR_AUTO_SCALE;
private boolean compressionEnabled = PlotConstants.COMPRESSION_ENABLED_BY_DEFAULT;
private int numberOfSubPlots = PlotConstants.DEFAULT_NUMBER_OF_SUBPLOTS;
private boolean localControlsEnabled = PlotConstants.LOCAL_CONTROLS_ENABLED_BY_DEFAULT;
private AbbreviatingPlotLabelingAlgorithm plotLabelingAlgorithm = new AbbreviatingPlotLabelingAlgorithm();
/**
* Specifies the required parameters for constructing a plot.
* @param selectedPlotPackage plotting package to render the plot
*/
public Builder(Class<? extends AbstractPlottingPackage>selectedPlotPackage) {
this.plotPackage = selectedPlotPackage;
}
/**
* Specify the plot's user readable name.
* @param initPlotName the initial plot name.
* @return builder the plot view.
*/
public Builder plotName(String initPlotName) {
plotName = initPlotName;
return this;
}
/**
* Specify the grouped plot settings for this chart
* @param plotSettings
* @return
*/
public Builder plotSettings(PlotConfiguration plotSettings) {
settings = plotSettings;
return this;
}
/**
* Specify the size of the font of the labels on the time axis.
* @param theTimeAxisFontSize font size.
* @return the builder the plot view.
*/
public Builder timeAxisFontSize(int theTimeAxisFontSize) {
timeAxisFont = new Font(timeAxisFont.getFontName(), Font.PLAIN, theTimeAxisFontSize);
return this;
}
/**
* Specify the font that will be used to draw the labels on the axis axis.
* This parameter overrides the time axis font size parameter when specified.
* @param theTimeAxisFont the font size.
* @return the builder the plot view.
*/
public Builder timeAxisFont(Font theTimeAxisFont) {
timeAxisFont = theTimeAxisFont;
return this;
}
/**
* Specify the thickness of the line used to plot data on the plot.
* @param theThickness the thickness.
* @return the builder the plot view.
*/
public Builder plotLineThickness(int theThickness) {
plotLineThickness = theThickness;
return this;
}
/**
* Specify the color of the frame surrounding the plot area.
* @param theBackgroundColor the color.
* @return the builder the plot view.
*/
public Builder plotBackgroundFrameColor(Color theBackgroundColor) {
plotBackgroundFrameColor = theBackgroundColor;
return this;
}
/**
* Specify the background color of the plot area.
* @param thePlotAreaColor the color.
* @return the builder the plot view.
*/
public Builder plotAreaBackgroundColor (Color thePlotAreaColor) {
plotAreaBackgroundColor = thePlotAreaColor;
return this;
}
/**
* Specify the point at which the time axis intercepts the non time axis.
* @param theIntercept the intercept point.
* @return the builder the plot view.
*/
public Builder timeAxisIntercept(int theIntercept) {
timeAxisIntercept = theIntercept;
return this;
}
/**
* Specify the color of the time axis.
* @param theTimeAxisColor the color.
* @return the builder the plot view.
*/
public Builder timeAxisColor(Color theTimeAxisColor) {
timeAxisColor = theTimeAxisColor;
return this;
}
/**
* Specify color of text on the time axis.
* @param theTimeAxisTextColor the color.
* @return the builder the plot view.
*/
public Builder timeAxisTextColor(Color theTimeAxisTextColor) {
timeAxisLabelColor = theTimeAxisTextColor;
return this;
}
/**
* Set the format of how time information is printed on time axis labels.
* @param theTimeAxisDateFormat the format.
* @return the builder the plot view.
*/
public Builder timeAxisDateFormat(String theTimeAxisDateFormat) {
timeAxisDateFormat = theTimeAxisDateFormat;
return this;
}
/**
* Set the color of the non time axis.
* @param theNonTimeAxisColor the color.
* @return the builder the plot view.
*/
public Builder nonTimeAxisColor(Color theNonTimeAxisColor) {
nonTimeAxisColor = theNonTimeAxisColor;
return this;
}
/**
* Set the color of the plot gridlines.
* @param theGridLineColor the color.
* @return the builder the plot view.
*/
public Builder gridLineColor(Color theGridLineColor) {
gridLineColor = theGridLineColor;
return this;
}
/**
* The minimum number of samples to accumulate out of range before an autoscale occurs. This
* prevents rapid autoscaling on every plot action.
* @param theMinSamplesForAutoScale the number of samples.
* @return the plot view.
*/
public Builder minSamplesForAutoScale(int theMinSamplesForAutoScale) {
minSamplesForAutoScale = theMinSamplesForAutoScale;
return this;
}
/**
* Specify if the plot is to compress its data to match the screen resolution.
* @param state true to compress, false otherwise.
* @return the builder the plot view.
*/
public Builder isCompressionEnabled(boolean state) {
compressionEnabled = state;
return this;
}
/**
* Specify the number of subplots in this plotview.
* @param theNumberOfSubPlots the number of sub-plots.
* @return the builder the plot view.
*/
public Builder numberOfSubPlots(int theNumberOfSubPlots) {
numberOfSubPlots = theNumberOfSubPlots;
return this;
}
/**
* Turn the plot local controls on and off.
* @param theIsEnabled true enabled; otherwise false.
* @return builder the plot view.
*/
public Builder localControlsEnabled(boolean theIsEnabled) {
localControlsEnabled = theIsEnabled;
return this;
}
/**
* Specify the plot abbreviation labeling algorithm.
* @param thePlotLabelingAlgorithm the plot labeling algorithm.
* @return builder the plot view.
*/
public Builder plotLabelingAlgorithm(AbbreviatingPlotLabelingAlgorithm thePlotLabelingAlgorithm) {
plotLabelingAlgorithm = thePlotLabelingAlgorithm;
assert plotLabelingAlgorithm != null : "Plot labeling algorithm should NOT be NULL at this point.";
return this;
}
/**
* Build a new plot instance and return it.
* @return the new plot instance.
*/
public PlotView build() {
return new PlotView(this);
}
}
// Private constructor. Construct using builder pattern.
private PlotView(Builder builder) {
super(builder.settings);
plotPackage = builder.plotPackage;
plotName = builder.plotName;
timeAxisFont = builder.timeAxisFont;
plotLineThickness = builder.plotLineThickness;
plotBackgroundFrameColor = builder.plotBackgroundFrameColor;
plotAreaBackgroundColor = builder.plotAreaBackgroundColor;
timeAxisIntercept = builder.timeAxisIntercept;
timeAxisColor = builder.timeAxisColor;
timeAxisLabelTextColor = builder.timeAxisLabelColor;
timeAxisDataFormat = builder.timeAxisDateFormat;
nonTimeAxisColor = builder.nonTimeAxisColor;
gridLineColor = builder.gridLineColor;
minSamplesForAutoScale = builder.minSamplesForAutoScale;
compressionEnabled = builder.compressionEnabled;
numberOfSubPlots = builder.numberOfSubPlots;
localControlsEnabled = builder.localControlsEnabled;
plotLabelingAlgorithm = builder.plotLabelingAlgorithm;
plotPanel = new JPanel();
plotPanel.addAncestorListener(new AncestorListener() {
@Override
public synchronized void ancestorRemoved(AncestorEvent event) {
if(updateTimeBoundsTask != null) {
updateTimeBoundsTask.cancel();
updateTimeBoundsTask = null;
}
}
@Override
public void ancestorMoved(AncestorEvent event) {
}
@Override
public synchronized void ancestorAdded(AncestorEvent event) {
for(AbstractPlottingPackage p : subPlots) {
p.updateCompressionRatio();
}
updateTimeBoundsTask = new TimerTask() {
@Override
public void run() {
try {
timeReachedEnd();
} catch(Exception e) {
// We need to catch exceptions because they can kill the timer.
logger.error(e.toString(), e);
}
}
};
timer.schedule(updateTimeBoundsTask, 0, 1000);
}
});
/* Make sure key events get dispatched to local controls (which enable for Alt/Ctrl/etc...) */
plotPanel.addAncestorListener(new LocalControlKeyEventDispatcher(this));
GridBagLayout layout = new StackPlotLayout(this);
plotPanel.setLayout(layout);
subPlots = new ArrayList<AbstractPlottingPackage>(numberOfSubPlots);
// create the specified number of subplots
for (int i=0; i< numberOfSubPlots; i++) {
AbstractPlottingPackage newPlot;
try {
newPlot = plotPackage.newInstance();
boolean isTimeLabelEnabled = i == (numberOfSubPlots -1);
newPlot.createChart(timeAxisFont,
plotLineThickness,
plotBackgroundFrameColor,
plotAreaBackgroundColor,
timeAxisIntercept,
timeAxisColor,
timeAxisLabelColor,
timeAxisLabelTextColor,
timeAxisDataFormat,
nonTimeAxisColor,
gridLineColor,
minSamplesForAutoScale,
compressionEnabled,
isTimeLabelEnabled,
localControlsEnabled,
this, plotLabelingAlgorithm);
// TODO: Move control attachment elsewhere
List<ControllableAxis> observableAxes = new ArrayList<ControllableAxis>();
for (AbstractAxis axis : newPlot.getAxes()) {
if (axis != null && axis.getVisibleOrientation() != null) {
ControllableAxis a = new ControllableAxis(newPlot, axis);
observableAxes.add(a);
newPlot.attachLocalControl(new PanControls(a));
newPlot.attachLocalControl(new ZoomControls(a));
newPlot.attachLocalControl(new CornerResetButton(a));
}
}
newPlot.attachLocalControl(new CornerResetButton(observableAxes.toArray(new ControllableAxis[observableAxes.size()])));
newPlot.setPlotLabelingAlgorithm(plotLabelingAlgorithm);
subPlots.add(newPlot);
newPlot.registerObservor(this);
logger.debug("plotLabelingAlgorithm.getPanelContextTitleList().size()="
+ plotLabelingAlgorithm.getPanelContextTitleList().size()
+ ", plotLabelingAlgorithm.getCanvasContextTitleList().size()="
+ plotLabelingAlgorithm.getCanvasContextTitleList().size());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
if (getAxisOrientationSetting() == AxisOrientationSetting.Y_AXIS_AS_TIME) {
Collections.reverse(subPlots);
}
for (AbstractPlottingPackage subPlot: subPlots) {
JComponent subPanel = subPlot.getPlotComponent();
plotPanel.add(subPanel);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
if(getAxisOrientationSetting() == AxisOrientationSetting.X_AXIS_AS_TIME) {
c.gridwidth = GridBagConstraints.REMAINDER;
}
layout.setConstraints(subPanel, c);
}
// Note that using InputMap does not work for our situation.
// See http://stackoverflow.com/questions/4880704/listening-to-key-events-for-a-component-hierarchy
addRecursiveListeners(plotPanel);
if (builder.settings.getPinTimeAxis()) {
timeAxisUserPin.setPinned(true);
// update the corner reset buttons after the plot is visible
SwingUtilities.invokeLater(new Runnable() {
public void run() {
for(AbstractPlottingPackage subPlot : subPlots) {
subPlot.updateResetButtons();
}
}
});
}
}
/**
* Gets the pinnable time axis by user.
* @return pinnable time axis.
*/
public Pinnable getTimeAxisUserPin() {
return timeAxisUserPin;
}
private void addRecursiveListeners(Component c) {
// c.addKeyListener(keyListener);
// if(c instanceof Container) {
// Container cont = (Container) c;
// cont.addContainerListener(containerListener);
// for(Component child : cont.getComponents()) {
// addRecursiveListeners(child);
// }
// }
}
private void removeRecursiveListeners(Component c) {
// c.removeKeyListener(keyListener);
// if(c instanceof Container) {
// Container cont = (Container) c;
// cont.removeContainerListener(containerListener);
// for(Component child : cont.getComponents()) {
// removeRecursiveListeners(child);
// }
// }
}
/**
* Gets the plot labeling algorithm.
* @return abbreviating plot labeling algorithm.
*/
public AbbreviatingPlotLabelingAlgorithm getPlotLabelingAlgorithm() {
return plotLabelingAlgorithm;
}
/**
* Sets the plot labeling algorithm.
* @param thePlotLabelingAlgorithm the plot labeling algorithm.
*/
public void setPlotLabelingAlgorithm(AbbreviatingPlotLabelingAlgorithm thePlotLabelingAlgorithm) {
plotLabelingAlgorithm = thePlotLabelingAlgorithm;
}
@Override
public AbstractPlottingPackage returnPlottingPackage() {
return getLastPlot();
}
@Override
public void setCompressionEnabled(boolean compression) {
for (AbstractPlottingPackage p: subPlots) {
p.setCompressionEnabled(compression);
}
}
@Override
public boolean isCompressionEnabled() {
return getLastPlot().isCompressionEnabled();
}
@Override
public void requestPlotData(GregorianCalendar startTime, GregorianCalendar endTime) {
plotUser.requestDataRefresh(startTime, endTime);
}
private void requestPredictivePlotData(GregorianCalendar startTime, GregorianCalendar endTime) {
plotUser.requestPredictiveData(startTime, endTime);
}
@Override
public void informUpdateDataEventStarted() {
timeAxisMaxAtStartOfDataUpdateCycle.setTimeInMillis(this.getLastPlot().getCurrentTimeAxisMax().getTimeInMillis());
for (AbstractPlottingPackage p: subPlots) {
p.informUpdateCachedDataStreamStarted();
}
}
@Override
public void informUpdateFromFeedEventStarted() {
timeAxisMaxAtStartOfDataUpdateCycle.setTimeInMillis(this.getLastPlot().getCurrentTimeAxisMax().getTimeInMillis());
for (AbstractPlottingPackage p: subPlots) {
p.informUpdateFromLiveDataStreamStarted();
}
}
@Override
public void informUpdateDataEventCompleted() {
for (AbstractPlottingPackage p: subPlots) {
p.informUpdateCacheDataStreamCompleted();
}
syncTimeAxisAcrossPlots();
}
@Override
public void informUpdateFromFeedEventCompleted() {
for (AbstractPlottingPackage p: subPlots) {
p.informUpdateFromLiveDataStreamCompleted();
}
syncTimeAxisAcrossPlots();
}
/**
* Synchronizes the time axis across all plots.
*/
void syncTimeAxisAcrossPlots() {
long maxAtStart = timeAxisMaxAtStartOfDataUpdateCycle.getTimeInMillis();
long currentMaxTime = maxAtStart;
long currentMinTime = maxAtStart;
for (AbstractPlottingPackage p: subPlots) {
long max = p.getMaxTime();
if (max > currentMaxTime) {
currentMaxTime = max;
currentMinTime = p.getMinTime();
}
}
if (currentMaxTime > maxAtStart) {
boolean inverted;
if(getAxisOrientationSetting() == AxisOrientationSetting.X_AXIS_AS_TIME) {
inverted = getXAxisMaximumLocation() == XAxisMaximumLocationSetting.MAXIMUM_AT_LEFT;
} else {
inverted = getYAxisMaximumLocation() == YAxisMaximumLocationSetting.MAXIMUM_AT_BOTTOM;
}
long start;
long end;