1010import static org .phoebus .applications .alarm .AlarmSystem .logger ;
1111
1212import java .time .Instant ;
13- import java .util .ArrayList ;
14- import java .util .Arrays ;
15- import java .util .List ;
16- import java .util .Optional ;
13+ import java .util .*;
1714import java .util .logging .Level ;
1815import java .util .regex .Pattern ;
1916import java .util .stream .Collectors ;
@@ -657,8 +654,20 @@ public void update(final List<AlarmInfoRow> active,
657654 {
658655 limitAlarmCount (active , active_count , "Active Alarms: " );
659656 limitAlarmCount (acknowledged , acknowledged_count , "Acknowledged Alarms: " );
657+
658+ /* Previously selected items that are still valid with the new alarm lists */
659+ List <String > active_selection = extractOldSelection (this .active , active );
660+ List <String > acknowledged_selection = extractOldSelection (this .acknowledged , acknowledged );
661+
660662 update (active_rows , active );
661663 update (acknowledged_rows , acknowledged );
664+
665+ /* Move this up here out of update(), doesn't need to be ran twice */
666+ selectRows ();
667+
668+ /* Now that the table has been appropriately updated, select any still valid pvs */
669+ selectPvs (this .active , active_selection );
670+ selectPvs (this .acknowledged , acknowledged_selection );
662671 }
663672
664673 /** Limit the number of alarms
@@ -704,29 +713,29 @@ private void update(final ObservableList<AlarmInfoRow> items, final List<AlarmIn
704713 else // Trim items, input has fewer elements
705714 items .remove (N , items .size ());
706715
707- selectRows ();
708716 }
709717
710718 /** Select all rows that match the current 'search' pattern */
711719 private void selectRows ()
712720 {
721+ active .getSelectionModel ().clearSelection ();
722+ acknowledged .getSelectionModel ().clearSelection ();
723+
713724 final String glob = search .getText ().trim ();
714- if (glob .isEmpty ())
725+ if (glob .isEmpty ())
715726 {
716- active .getSelectionModel ().clearSelection ();
717- acknowledged .getSelectionModel ().clearSelection ();
718727 return ;
719728 }
720729
721730 final Pattern pattern = Pattern .compile (RegExHelper .fullRegexFromGlob (glob ),
722- Pattern .CASE_INSENSITIVE );
731+ Pattern .CASE_INSENSITIVE );
732+
723733 selectRows (active , pattern );
724734 selectRows (acknowledged , pattern );
725735 }
726736
727737 private void selectRows (final TableView <AlarmInfoRow > table , final Pattern pattern )
728738 {
729- table .getSelectionModel ().clearSelection ();
730739
731740 int i = 0 ;
732741 for (AlarmInfoRow row : table .getItems ())
@@ -737,4 +746,44 @@ private void selectRows(final TableView<AlarmInfoRow> table, final Pattern patte
737746 ++i ;
738747 }
739748 }
749+
750+ /** adds a list of pvs to the selection **/
751+ private void selectPvs (final TableView <AlarmInfoRow > table , final List <String > pvs ) {
752+ HashSet <String > pv_set = new HashSet <>(pvs );
753+
754+ int i = 0 ;
755+ for (AlarmInfoRow row : table .getItems ())
756+ {
757+ if (pv_set .contains (row .pv .get ()))
758+ {
759+ table .getSelectionModel ().select (i );
760+ }
761+ ++i ;
762+ }
763+ }
764+
765+ /** Find all selected items that are still valid in this new state **/
766+ private List <String > extractOldSelection (final TableView <AlarmInfoRow > table , final List <AlarmInfoRow > input )
767+ {
768+ /* input objects are probably not equal, compare them by their pv names */
769+ final HashSet <String > pvs = new HashSet <>();
770+ for (AlarmInfoRow row : input )
771+ {
772+ pvs .add (row .pv .get ());
773+ }
774+
775+ final List <String > validSelection = new ArrayList <>();
776+ for (AlarmInfoRow row : table .getSelectionModel ().getSelectedItems ())
777+ {
778+ String pv = row .pv .get ();
779+ if (pvs .contains (pv ))
780+ {
781+ validSelection .add (pv );
782+ }
783+ }
784+
785+ table .getSelectionModel ().clearSelection ();
786+
787+ return validSelection ;
788+ }
740789}
0 commit comments