diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/table/AlarmTableUI.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/table/AlarmTableUI.java index 70c2c2a8da..04c268910d 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/table/AlarmTableUI.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/table/AlarmTableUI.java @@ -10,10 +10,7 @@ import static org.phoebus.applications.alarm.AlarmSystem.logger; import java.time.Instant; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.logging.Level; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -657,8 +654,20 @@ public void update(final List active, { limitAlarmCount(active, active_count, "Active Alarms: "); limitAlarmCount(acknowledged, acknowledged_count, "Acknowledged Alarms: "); + + /* Previously selected items that are still valid with the new alarm lists */ + List active_selection = extractOldSelection(this.active, active); + List acknowledged_selection = extractOldSelection(this.acknowledged, acknowledged); + update(active_rows, active); update(acknowledged_rows, acknowledged); + + /* Move this up here out of update(), doesn't need to be ran twice */ + selectRows(); + + /* Now that the table has been appropriately updated, select any still valid pvs */ + selectPvs(this.active, active_selection); + selectPvs(this.acknowledged, acknowledged_selection); } /** Limit the number of alarms @@ -704,29 +713,29 @@ private void update(final ObservableList items, final List table, final Pattern pattern) { - table.getSelectionModel().clearSelection(); int i = 0; for (AlarmInfoRow row : table.getItems()) @@ -737,4 +746,44 @@ private void selectRows(final TableView table, final Pattern patte ++i; } } + + /** adds a list of pvs to the selection **/ + private void selectPvs(final TableView table, final List pvs) { + HashSet pv_set = new HashSet<>(pvs); + + int i = 0; + for (AlarmInfoRow row : table.getItems()) + { + if(pv_set.contains(row.pv.get())) + { + table.getSelectionModel().select(i); + } + ++i; + } + } + + /** Find all selected items that are still valid in this new state **/ + private List extractOldSelection(final TableView table, final List input) + { + /* input objects are probably not equal, compare them by their pv names */ + final HashSet pvs = new HashSet<>(); + for(AlarmInfoRow row : input) + { + pvs.add(row.pv.get()); + } + + final List validSelection = new ArrayList<>(); + for(AlarmInfoRow row : table.getSelectionModel().getSelectedItems()) + { + String pv = row.pv.get(); + if(pvs.contains(pv)) + { + validSelection.add(pv); + } + } + + table.getSelectionModel().clearSelection(); + + return validSelection; + } }