From 436310fcd573bb5725d628dbeab95b664a65ab30 Mon Sep 17 00:00:00 2001 From: Percy Baker Date: Tue, 10 Nov 2026 10:57:34 +0000 Subject: [PATCH 1/2] Overhaul disable alarms dialog --- .../applications/alarm/AlarmSystem.java | 42 +++--- .../alarm/DisableDurationPreset.java | 130 ++++++++++++++++ .../alarm/model/EnabledState.java | 2 +- .../resources/alarm_preferences.properties | 11 +- .../alarm/DisableDurationPresetTest.java | 65 ++++++++ .../applications/alarm/ui/Messages.java | 6 +- .../alarm/ui/config/DateTimePicker.java | 13 +- .../ui/config/DisableDialogController.java | 117 +++++++++++++++ .../config/DisableUntilDialogController.java | 142 ------------------ .../alarm/ui/tree/AlarmTreeHelper.java | 74 ++++++--- .../alarm/ui/tree/AlarmTreeView.java | 2 +- .../alarm/ui/tree/AlarmTreeViewCell.java | 2 +- .../alarm/ui/tree/ComponentActionHelper.java | 112 +++++++------- .../alarm/ui/tree/DisableAction.java | 105 ++++--------- .../alarm/ui/tree/DisableComponentAction.java | 36 ----- ...ComponentAction.java => EnableAction.java} | 26 ++-- .../alarm/ui/tree/TreeNodeInfo.java | 7 +- .../alarm/ui/config/DisableDialog.fxml | 20 +++ .../alarm/ui/config/DisableUntilDialog.fxml | 35 ----- .../applications/alarm/ui/messages.properties | 24 ++- .../alarm/ui/tree/AlarmTreeHelperTest.java | 19 +-- .../ui/tree/ComponentActionHelperTest.java | 74 --------- 22 files changed, 543 insertions(+), 521 deletions(-) create mode 100644 app/alarm/model/src/main/java/org/phoebus/applications/alarm/DisableDurationPreset.java create mode 100644 app/alarm/model/src/test/java/org/phoebus/applications/alarm/DisableDurationPresetTest.java create mode 100644 app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DisableDialogController.java delete mode 100644 app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DisableUntilDialogController.java delete mode 100644 app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DisableComponentAction.java rename app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/{EnableComponentAction.java => EnableAction.java} (62%) create mode 100644 app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/config/DisableDialog.fxml delete mode 100644 app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/config/DisableUntilDialog.fxml delete mode 100644 app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/ComponentActionHelperTest.java diff --git a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/AlarmSystem.java b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/AlarmSystem.java index 63b0126b17..5bc2d109d8 100644 --- a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/AlarmSystem.java +++ b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/AlarmSystem.java @@ -8,8 +8,8 @@ package org.phoebus.applications.alarm; import java.io.File; -import java.time.LocalDateTime; -import java.time.temporal.TemporalAmount; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.logging.Level; import org.phoebus.applications.alarm.client.IdentificationHelper; @@ -20,7 +20,7 @@ import org.phoebus.framework.preferences.Preference; import org.phoebus.framework.preferences.PreferencesReader; import org.phoebus.util.time.SecondsParser; -import org.phoebus.util.time.TimeParser; + /** Common alarm system code * @author Kay Kasemir @@ -121,11 +121,11 @@ public class AlarmSystem extends AlarmSystemConstants /** Enable context menu for adding PVs to alarm configuration */ @Preference public static boolean enable_add_to_alarm_context_menu; - /** "Disable until.." shortcuts */ - @Preference public static String[] shelving_options; - @Preference public static int max_block_ms; + /** Presets for the re-enable time when disabling an alarm */ + public static final Map disable_duration_presets; + /** Macros used in UI display/command/web links */ public static MacroValueProvider macros; @@ -146,23 +146,6 @@ public class AlarmSystem extends AlarmSystemConstants } nag_period_ms = Math.round(Math.max(0, secs) * 1000.0); - // Check if the provided options can be parsed to avoid later runtime errors - final LocalDateTime now = LocalDateTime.now(); - for (String option : shelving_options) - { - try - { - final TemporalAmount amount = TimeParser.parseTemporalAmount(option); - final LocalDateTime end = now.plus(amount); - if (! end.isAfter(now)) - throw new Exception("Invalid 'shelving_options' value '" + option + "'"); - } - catch (Throwable ex) - { - logger.log(Level.WARNING, "Error in 'shelving_options'", ex); - } - } - try { macros = new MacroOrSystemProvider(Macros.fromSimpleSpec(prefs.get("macros"))); @@ -173,6 +156,19 @@ public class AlarmSystem extends AlarmSystemConstants macros = new MacroOrSystemProvider(new Macros()); } + disable_duration_presets = new LinkedHashMap<>(); + for (final String preset : prefs.get("disable_duration_presets").split(",")) + { + try + { + disable_duration_presets.put(preset.strip(), DisableDurationPreset.parse(preset)); + } + catch (Exception ex) + { + logger.log(Level.WARNING, "Invalid disable duration preset '" + preset + "'", ex); + } + } + IdentificationHelper.initialize(); } } diff --git a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/DisableDurationPreset.java b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/DisableDurationPreset.java new file mode 100644 index 0000000000..690f1b8690 --- /dev/null +++ b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/DisableDurationPreset.java @@ -0,0 +1,130 @@ +package org.phoebus.applications.alarm; + +import org.phoebus.util.time.TimeParser; + +import java.time.DayOfWeek; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAdjusters; +import java.time.temporal.TemporalAmount; +import java.util.Map; + + +/** + * Represents a disable duration that the user can select when disabling an alarm. + * + * Presets can be relative (e.g. 5 hours, 10 days) or occurrences (e.g. 17:00, Monday 3:50). + * Occurrences simply select the next occurrence of the given time or day. + * + * Occurrences can be minutes, hours:minutes, or weekday hours:minutes. + */ +public sealed interface DisableDurationPreset permits DisableDurationPreset.Relative, DisableDurationPreset.Occurrence { + + record Relative(TemporalAmount amount) implements DisableDurationPreset { + + public Relative(final TemporalAmount amount) { + this.amount = amount; + } + + @Override + public LocalDateTime getNextDisableDateTime(final LocalDateTime now) { + return now.plus(amount); + } + } + + record Occurrence(DayOfWeek day, Integer hour, int minute) implements DisableDurationPreset { + + public Occurrence(final DayOfWeek day, final Integer hour, final int minute) { + this.day = day; + this.hour = hour; + this.minute = minute; + + if (hour == null && day != null) { + throw new IllegalArgumentException("A day and minute was provided, but no hour"); + } + + if (hour != null && hour > 23) { + throw new IllegalArgumentException("Invalid hour " + hour); + } + + if (minute > 59) { + throw new IllegalArgumentException("Invalid minute " + minute); + } + } + + @Override + public LocalDateTime getNextDisableDateTime(final LocalDateTime now) { + LocalDateTime nextDisableTime = now.withMinute(minute).truncatedTo(ChronoUnit.MINUTES); + + if (hour == null) { + // only minute + if (nextDisableTime.isBefore(now)) { + nextDisableTime = nextDisableTime.plusHours(1); + } + return nextDisableTime; + } + + nextDisableTime = nextDisableTime.withHour(hour); + if (day == null) { + // hour and minute + if (nextDisableTime.isBefore(now)) { + nextDisableTime = nextDisableTime.plusDays(1); + } + return nextDisableTime; + } + + // day, hour, and minute + nextDisableTime = nextDisableTime.with(TemporalAdjusters.nextOrSame(day)); + if (nextDisableTime.isBefore(now)) { + nextDisableTime = nextDisableTime.plusWeeks(1); + } + return nextDisableTime; + } + } + + LocalDateTime getNextDisableDateTime(final LocalDateTime now); + + static DisableDurationPreset parse(final String string) { + final String stripped = string.strip(); + + final Map weekdaysMap = Map.of( + "Mon", DayOfWeek.MONDAY, + "Tue", DayOfWeek.TUESDAY, + "Wed", DayOfWeek.WEDNESDAY, + "Thu", DayOfWeek.THURSDAY, + "Fri", DayOfWeek.FRIDAY, + "Sat", DayOfWeek.SATURDAY, + "Sun", DayOfWeek.SUNDAY + ); + final String weekdaysRegex = "(" + String.join("|", weekdaysMap.keySet()) + ")"; + + if (stripped.matches("xx:\\d\\d")) + { + return new DisableDurationPreset.Occurrence( + null, + null, + Integer.parseInt(stripped.substring(3)) + ); + } else if (stripped.matches("\\d\\d:\\d\\d")) + { + return new Occurrence( + null, + Integer.parseInt(stripped.substring(0, 2)), + Integer.parseInt(stripped.substring(3)) + ); + } else if (stripped.matches(weekdaysRegex + " \\d\\d:\\d\\d")) { + return new Occurrence( + weekdaysMap.get(stripped.substring(0, 3)), + Integer.parseInt(stripped.substring(4, 6)), + Integer.parseInt(stripped.substring(7)) + ); + } + + final TemporalAmount amount = TimeParser.parseTemporalAmount(stripped); + final LocalDateTime now = LocalDateTime.now(); + final LocalDateTime end = now.plus(amount); + if (!end.isAfter(now)) + throw new RuntimeException("Invalid 'disable_duration_presets' value '" + string + "'"); + return new DisableDurationPreset.Relative(amount); + } +} diff --git a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/model/EnabledState.java b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/model/EnabledState.java index eea91f440a..057174299d 100644 --- a/app/alarm/model/src/main/java/org/phoebus/applications/alarm/model/EnabledState.java +++ b/app/alarm/model/src/main/java/org/phoebus/applications/alarm/model/EnabledState.java @@ -20,7 +20,7 @@ public class EnabledState /** Time to (re-)enable */ public final LocalDateTime enabled_date; - /** Plain enablement */ + /** Plain enablement - always false if enabled_date is not null */ public final boolean enabled; private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; diff --git a/app/alarm/model/src/main/resources/alarm_preferences.properties b/app/alarm/model/src/main/resources/alarm_preferences.properties index e724d9c00e..601cfc102f 100644 --- a/app/alarm/model/src/main/resources/alarm_preferences.properties +++ b/app/alarm/model/src/main/resources/alarm_preferences.properties @@ -172,13 +172,16 @@ disable_notify_visible=false # PVs to the alarm tree. enable_add_to_alarm_context_menu=true -# Options for the "Disable until.." shortcuts in the PV config dialog. +# Duration presets for the "Disable" shortcut in the alarm context menu. # # :format: -# comma separated, each option needs to comply with `TimeParser.parseTemporalAmount()`: +# Comma separated list of presets. Presets can be relative (5 minutes, +# 6 hours, 1 day, etc) or occurrences. There are three formats for +# occurrences: xx:15 (the next time it's quarter past any hour), +# 17:43, and Tue 14:30 (this or the next Tuesday at 14:30). # -# 30 seconds, 5 minutes, 1 hour, 6 hours, 1 day, 30 days, ... -shelving_options=1 hour, 6 hours, 12 hours, 1 day, 7 days, 30 days +# Weekdays can be Mon, Tue, Wed, Thu, Fri, Sat, Sun +disable_duration_presets=1 hour, 6 hours, 12 hours, 1 day, 7 days, 30 days # Macros for UI display, command or web links # diff --git a/app/alarm/model/src/test/java/org/phoebus/applications/alarm/DisableDurationPresetTest.java b/app/alarm/model/src/test/java/org/phoebus/applications/alarm/DisableDurationPresetTest.java new file mode 100644 index 0000000000..aeaee5e152 --- /dev/null +++ b/app/alarm/model/src/test/java/org/phoebus/applications/alarm/DisableDurationPresetTest.java @@ -0,0 +1,65 @@ +package org.phoebus.applications.alarm; + +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.phoebus.util.time.TimeDuration; + +import java.time.DayOfWeek; +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertThrows; + + +public class DisableDurationPresetTest { + + @Test + public void testParse() { + assertThat(DisableDurationPreset.parse("5 min"), equalTo(new DisableDurationPreset.Relative(TimeDuration.ofMinutes(5)))); + assertThat(DisableDurationPreset.parse("xx:16"), equalTo(new DisableDurationPreset.Occurrence(null, null, 16))); + assertThat(DisableDurationPreset.parse("12:16"), equalTo(new DisableDurationPreset.Occurrence(null, 12, 16))); + assertThat(DisableDurationPreset.parse("Mon 18:16"), equalTo(new DisableDurationPreset.Occurrence(DayOfWeek.MONDAY, 18, 16))); + assertThrows(Exception.class, () -> DisableDurationPreset.parse("25:00")); + assertThrows(Exception.class, () -> DisableDurationPreset.parse("xx:70")); + } + + @Test + public void testGetNextDisableDateTime() { + final LocalDateTime now = LocalDateTime.of(2050, 12, 25, 6, 9); + + assertThat( + new DisableDurationPreset.Relative(Duration.of(5, ChronoUnit.DAYS)).getNextDisableDateTime(now), + equalTo(now.plusDays(5)) + ); + + assertThat( + new DisableDurationPreset.Occurrence(null, null, 50).getNextDisableDateTime(now), + equalTo(now.withMinute(50)) + ); + assertThat( + new DisableDurationPreset.Occurrence(null, null, 5).getNextDisableDateTime(now), + equalTo(now.withMinute(5).plusHours(1)) + ); + + assertThat( + new DisableDurationPreset.Occurrence(null, 12, 5).getNextDisableDateTime(now), + equalTo(now.withHour(12).withMinute(5)) + ); + assertThat( + new DisableDurationPreset.Occurrence(null, 3, 5).getNextDisableDateTime(now), + equalTo(now.withHour(3).withMinute(5).plusDays(1)) + ); + + assertThat( + new DisableDurationPreset.Occurrence(now.getDayOfWeek(), 12, 5).getNextDisableDateTime(now), + equalTo(now.withHour(12).withMinute(5)) + ); + assertThat( + new DisableDurationPreset.Occurrence(now.getDayOfWeek(), 3, 5).getNextDisableDateTime(now), + equalTo(now.withHour(3).withMinute(5).plusWeeks(1)) + ); + } +} diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/Messages.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/Messages.java index 1a8d67bb5e..962f87364a 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/Messages.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/Messages.java @@ -22,6 +22,7 @@ public class Messages public static String Annunciator; public static String commands; public static String configure; + public static String custom; public static String delayTooltip0; public static String delayTooltip1; public static String delayTooltip2; @@ -45,12 +46,13 @@ public class Messages public static String indefinitely; public static String MenuPath; public static String moveItemFailed; - public static String promptTitle; public static String removeComponentFailed; public static String renameItemFailed; + public static String selectedDateInThePast; + public static String today; + public static String tomorrow; public static String totalPVs; public static String unacknowledgeFailed; - public static String withEnableDate; static { diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DateTimePicker.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DateTimePicker.java index c8665f2a08..dcfc367807 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DateTimePicker.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DateTimePicker.java @@ -9,8 +9,10 @@ import java.time.LocalTime; import java.time.format.DateTimeFormatter; +import javafx.beans.NamedArg; import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleObjectProperty; +import javafx.scene.control.DateCell; import javafx.scene.control.DatePicker; import javafx.scene.control.TextFormatter; import javafx.util.StringConverter; @@ -39,7 +41,7 @@ private void alignColumnCountWithFormat() { } /** Constructor */ - public DateTimePicker() { + public DateTimePicker(@NamedArg(value = "allowPastDateTimes", defaultValue = "false") final boolean allowPastDateTimes) { getStyleClass().add("datetime-picker"); setFormat(DefaultFormat); setConverter(new InternalConverter()); @@ -80,6 +82,15 @@ public DateTimePicker() { simulateEnterPressed(); }); + if (!allowPastDateTimes) { + setDayCellFactory(_ -> new DateCell() { + @Override + public void updateItem(final LocalDate date, final boolean empty) { + super.updateItem(date, empty); + setDisable(empty || LocalDate.now().isAfter(date)); + } + }); + } } private void simulateEnterPressed() { diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DisableDialogController.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DisableDialogController.java new file mode 100644 index 0000000000..0fce22f905 --- /dev/null +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DisableDialogController.java @@ -0,0 +1,117 @@ +package org.phoebus.applications.alarm.ui.config; + +import javafx.beans.property.SimpleObjectProperty; +import javafx.fxml.FXML; +import javafx.scene.Node; +import javafx.scene.control.*; +import javafx.scene.layout.GridPane; +import org.phoebus.applications.alarm.AlarmSystem; +import org.phoebus.applications.alarm.DisableDurationPreset; +import org.phoebus.applications.alarm.model.EnabledState; +import org.phoebus.applications.alarm.ui.Messages; +import org.phoebus.applications.alarm.ui.tree.AlarmTreeHelper; +import org.phoebus.util.time.TimestampFormats; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Map; + + +public class DisableDialogController { + + @FXML + private GridPane disableDurationsBox; + @FXML + private DateTimePicker enabledDatePicker; + + /** The new enabled state which the user is requesting. + * + * Set to `new EnabledState(true)` to indicate an invalid selection. + */ + private final SimpleObjectProperty enabledStateProperty = new SimpleObjectProperty<>(); + private final Node okButton; + + public DisableDialogController(final EnabledState initialDateTime, final Node okButton) + { + enabledStateProperty.set(initialDateTime); + this.okButton = okButton; + } + + @FXML + public void initialize() + { + // Disable OK button when in invalid state + okButton.disableProperty().bind(enabledStateProperty.map(enabledState -> enabledState.enabled)); + + // Create radio buttons from presets + final ToggleGroup group = new ToggleGroup(); + int row = 0; + for (final Map.Entry preset : AlarmSystem.disable_duration_presets.entrySet()) { + final LocalDateTime enableDateTime = preset.getValue().getNextDisableDateTime(LocalDateTime.now()); + + final RadioButton radioButton = new RadioButton(preset.getKey()); + disableDurationsBox.add(radioButton, 0, row); + radioButton.setToggleGroup(group); + radioButton.selectedProperty().addListener(_ -> { + if (radioButton.isSelected()) { + enabledStateProperty.set(new EnabledState(enableDateTime)); + } + }); + + final Label timeLabel = new Label(AlarmTreeHelper.formatEnableDateTime(enableDateTime)); + disableDurationsBox.add(timeLabel, 1, row); + timeLabel.setStyle("-fx-text-fill: #888888"); + + row++; + } + + // 'indefinitely' radio button + final RadioButton indefinitelyRadioButton = new RadioButton(Messages.indefinitely); + disableDurationsBox.add(indefinitelyRadioButton, 0, row); + indefinitelyRadioButton.setToggleGroup(group); + indefinitelyRadioButton.selectedProperty().addListener(_ -> { + if (indefinitelyRadioButton.isSelected()) { + enabledStateProperty.set(new EnabledState(false)); + } + }); + row++; + + // 'custom' radio button + final RadioButton customRadioButton = new RadioButton(Messages.custom); + disableDurationsBox.add(customRadioButton, 0, row); + customRadioButton.setToggleGroup(group); + customRadioButton.selectedProperty().addListener(_ -> { + // If the 'custom' option has just been selected and the previous state was indefinite, + // invalidate the selection because the user needs to select a date + if (customRadioButton.isSelected() && enabledStateProperty.get().enabled_date == null) { + enabledStateProperty.set(new EnabledState(true)); + } + }); + + // Custom enabled date picker + enabledDatePicker.disableProperty().bind(customRadioButton.selectedProperty().not()); + enabledDatePicker.dateTimeValueProperty().addListener(_ -> { + // Only update enabled state from enabled date picker if 'custom' is selected + if (customRadioButton.isSelected()) { + enabledStateProperty.set(new EnabledState(enabledDatePicker.getDateTimeValue())); + } + }); + enabledStateProperty.addListener(_ -> { + enabledDatePicker.setDateTimeValue(enabledStateProperty.get().enabled_date); + }); + + // Select initial datetime + final EnabledState enabledState = enabledStateProperty.get(); + if (enabledState.enabled_date != null) { + customRadioButton.setSelected(true); + enabledDatePicker.setDateTimeValue(enabledState.enabled_date); + } else if (!enabledState.enabled) { + indefinitelyRadioButton.setSelected(true); + } + } + + public EnabledState getEnabledState() { + return enabledStateProperty.get(); + } +} diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DisableUntilDialogController.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DisableUntilDialogController.java deleted file mode 100644 index 5cb37b53d3..0000000000 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/config/DisableUntilDialogController.java +++ /dev/null @@ -1,142 +0,0 @@ -package org.phoebus.applications.alarm.ui.config; - -import javafx.beans.property.BooleanProperty; -import javafx.beans.property.SimpleBooleanProperty; -import javafx.beans.property.SimpleObjectProperty; -import javafx.beans.property.SimpleStringProperty; -import javafx.fxml.FXML; -import javafx.scene.control.*; -import javafx.scene.input.KeyCode; -import javafx.scene.input.KeyEvent; -import javafx.scene.paint.Color; -import javafx.util.StringConverter; -import org.phoebus.applications.alarm.AlarmSystem; -import org.phoebus.util.time.TimeParser; - -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.format.DateTimeParseException; -import java.time.temporal.TemporalAmount; - -public class DisableUntilDialogController { - - @FXML - private DateTimePicker enabledDatePicker; - - @FXML - private ComboBox relativeDate; - - @FXML - private Label invalidDate; - - - protected final SimpleStringProperty relativeDateProperty = new SimpleStringProperty(null); - protected final SimpleObjectProperty enableDateProperty = new SimpleObjectProperty<>(); - protected final BooleanProperty invalidDateProperty = new SimpleBooleanProperty(); - - public void initialize(){ - invalidDate.setTextFill(Color.RED); - relativeDate.valueProperty().bindBidirectional(relativeDateProperty); - enabledDatePicker.dateTimeValueProperty().bindBidirectional(enableDateProperty); - invalidDate.visibleProperty().bind(invalidDateProperty); - enableDateProperty.addListener((observable, oldValue, newValue) -> { - if (newValue != null) { - relativeDateProperty.setValue(null); - } - }); - - - enabledDatePicker.focusedProperty().addListener((observable, oldValue, newValue) -> { - if (newValue && invalidDateProperty.get()){ - invalidDateProperty.set(false); - } - }); - - relativeDate.focusedProperty().addListener((observable, oldValue, newValue) -> { - if (newValue && invalidDateProperty.get()){ - invalidDateProperty.set(false); - } - }); - - relativeDateProperty.addListener((observable, oldValue, newValue) -> { - if (newValue != null) { - enableDateProperty.setValue(null); - } - }); - - enabledDatePicker.setDayCellFactory(picker -> new DateCell() { - @Override - public void updateItem(LocalDate date, boolean empty) { - super.updateItem(date, empty); - setDisable(empty || date.isBefore(LocalDate.now())); - } - }); - - enabledDatePicker.addEventHandler(KeyEvent.KEY_PRESSED, keyEvent -> { - if (keyEvent.getCode() == KeyCode.ENTER) { - try { - TextFormatter tf = enabledDatePicker.getEditor().getTextFormatter(); - @SuppressWarnings("unchecked") - StringConverter conv = - (StringConverter) tf.getValueConverter(); - conv.fromString(enabledDatePicker.getEditor().getText()); - enableDateProperty.set(conv.fromString(enabledDatePicker.getEditor().getText())); - enabledDatePicker.getEditor().commitValue(); - } catch (DateTimeParseException ex) { - keyEvent.consume(); - } - } - }); - - - String[] shelvingOptions = new String[AlarmSystem.shelving_options.length + 1]; - System.arraycopy(AlarmSystem.shelving_options, 0, shelvingOptions, 1, AlarmSystem.shelving_options.length); - relativeDate.getItems().addAll(shelvingOptions); - - - } - - public void setDefaultDate(LocalDateTime date){ - enableDateProperty.set(date); - } - - - /** - * @param enableDate A non-null {@link LocalDateTime} - * @return true if the specified date/time is considered valid, e.g. in the future. - */ - private boolean isEnableDateValid(LocalDateTime enableDate) { - return !enableDate.isBefore(LocalDateTime.now()) && !enableDate.isEqual(LocalDateTime.now()); - } - - - /** - * Attempts to determine a {@link LocalDateTime} based on the user input. - * - * @return A non-null {@link LocalDateTime} if user has specified a valid date/time, or null if - * there is no user input from which to determine a date/time. - * @throws IllegalArgumentException if user has entered an invalid date/time. - */ - public LocalDateTime determineEnableDate() { - - if (enableDateProperty.isNotNull().get()) { - if (isEnableDateValid(enableDateProperty.get())) { - return enableDateProperty.get(); - } else { - invalidDateProperty.set(true); - throw new IllegalArgumentException("Enable date invalid"); - } - } else if (relativeDateProperty.isNotNull().get()) { - final TemporalAmount amount = - TimeParser.parseTemporalAmount(relativeDateProperty.get()); - final LocalDateTime updateDate = LocalDateTime.now().plus(amount); - if (isEnableDateValid(updateDate)) { - return updateDate; - } else { - invalidDateProperty.set(true); - throw new IllegalArgumentException("Enable date invalid"); - } - } - return null; - } -} diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelper.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelper.java index 25e77114a6..3b6cbcd121 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelper.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelper.java @@ -8,7 +8,11 @@ package org.phoebus.applications.alarm.ui.tree; import java.text.MessageFormat; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAmount; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -17,6 +21,7 @@ import org.phoebus.applications.alarm.client.AlarmClientLeaf; import org.phoebus.applications.alarm.model.AlarmTreeItem; import org.phoebus.applications.alarm.model.AlarmTreePath; +import org.phoebus.applications.alarm.model.EnabledState; import org.phoebus.applications.alarm.ui.Messages; import org.phoebus.ui.dialog.DialogHelper; @@ -159,30 +164,37 @@ private static Stream streamLeafItems(final AlarmTreeItem al * selection in the alarm tree view. * @return A {@link TreeNodeInfo} object. */ - public static TreeNodeInfo getTreeNodeInfo(List> items){ + public static TreeNodeInfo getTreeNodeInfo(List> items) { Set leaves = getLeafItems(items); + int disabledIndefinitely = 0; int disabledWithEnableDate = 0; - Optional localDateTime = Optional.empty(); - - for(AlarmClientLeaf leaf : leaves){ - if(!leaf.isEnabled()){ - LocalDateTime enableDate = leaf.getEnabledDate(); - if(enableDate != null){ - if(localDateTime.isPresent() && !localDateTime.get().equals(enableDate)){ - localDateTime = Optional.empty(); - } - else{ - localDateTime = Optional.of(enableDate); - } - disabledWithEnableDate++; - } - else{ - disabledIndefinitely++; - } + for (AlarmClientLeaf leaf : leaves) { + if (leaf.getEnabled().enabled_date != null) { + disabledWithEnableDate++; + } else if (!leaf.getEnabled().enabled){ + disabledIndefinitely++; } } - return new TreeNodeInfo(leaves, disabledIndefinitely, disabledWithEnableDate, localDateTime); + + List uniqueDisabledStates = leaves.stream() + .map(AlarmClientLeaf::getEnabled) + .filter(state -> !state.enabled) + .map(state -> { + // For most intents and purposes, we can consider disable times to the minute + if (state.enabled_date != null) { + return new EnabledState(state.enabled_date.truncatedTo(ChronoUnit.MINUTES)); + } + return state; + }) + .distinct() + .toList(); + Optional commonDisableState = Optional.empty(); + if (uniqueDisabledStates.size() == 1) { + commonDisableState = Optional.of(uniqueDisabledStates.getFirst()); + } + + return new TreeNodeInfo(leaves, disabledIndefinitely, disabledWithEnableDate, commonDisableState); } /** @@ -202,19 +214,35 @@ public static TreeNodeInfo getTreeNodeInfo(AlarmTreeItem item){ public static String treeNodeInfoToString(TreeNodeInfo treeNodeInfo){ StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(MessageFormat.format(Messages.totalPVs, treeNodeInfo.leaves().size())); - if(treeNodeInfo.disabled() > 0){ + if(treeNodeInfo.disabled() > 0 && treeNodeInfo.disabledWithEnableDate() == 0){ stringBuilder.append(", ").append(MessageFormat.format(Messages.disabledIndefinitely, treeNodeInfo.disabled())); } int disabledWithEnableDate = treeNodeInfo.disabledWithEnableDate(); if(disabledWithEnableDate > 0){ stringBuilder.append(", "); - if(treeNodeInfo.commonEnableDate().isPresent()){ - stringBuilder.append(MessageFormat.format(Messages.disabledCommonEnableDate, TimestampFormats.SECONDS_FORMAT.format(treeNodeInfo.commonEnableDate().get()), disabledWithEnableDate)); + if(treeNodeInfo.commonDisableState().isPresent()){ + stringBuilder.append(MessageFormat.format( + Messages.disabledCommonEnableDate, + formatEnableDateTime(treeNodeInfo.commonDisableState().get().enabled_date), disabledWithEnableDate + )); } else{ - stringBuilder.append(MessageFormat.format(Messages.disabledVaryingEnableDate, disabledWithEnableDate)); + stringBuilder.append(MessageFormat.format(Messages.disabledVaryingEnableDate, disabledWithEnableDate + treeNodeInfo.disabled())); } } return stringBuilder.toString(); } + + public static String formatEnableDateTime(final LocalDateTime dateTime) { + final LocalDate date = dateTime.toLocalDate(); + final LocalDate today = LocalDate.now(); + final LocalDate tomorrow = today.plusDays(1); + if (date.equals(today)) { + return Messages.today + " " + dateTime.format(DateTimeFormatter.ofPattern("HH:mm")); + } + if (date.equals(tomorrow)) { + return Messages.tomorrow + " " + dateTime.format(DateTimeFormatter.ofPattern("HH:mm")); + } + return dateTime.format(TimestampFormats.DATETIME_FORMAT); + } } diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeView.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeView.java index f3d65c9801..2afddf22a0 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeView.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeView.java @@ -616,7 +616,7 @@ else if (selection.size() == 1) } if (selection.size() >= 1) { - menu_items.add(new EnableComponentAction(tree_view, model, selection)); + menu_items.add(new EnableAction(tree_view, model, selection)); menu_items.add(new DisableAction(tree_view, model, selection)); menu_items.add(new RemoveComponentAction(tree_view, model, selection)); diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeViewCell.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeViewCell.java index 6879c30802..28e77aca5a 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeViewCell.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeViewCell.java @@ -106,7 +106,7 @@ protected void updateItem(final AlarmTreeItem item, final boolean empty) } else { if (leaf.getEnabled().enabled_date != null) { LocalDateTime enabledDate = leaf.getEnabled().enabled_date; - String enabledDateString = TimestampFormats.SECONDS_FORMAT.format(enabledDate); + String enabledDateString = AlarmTreeHelper.formatEnableDateTime(enabledDate); disabledTimerIndicator.setText("(" + Messages.disabledUntil + " " + enabledDateString + ")"); } else { disabledTimerIndicator.setText("(" + Messages.disabled + ")"); diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/ComponentActionHelper.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/ComponentActionHelper.java index 5256328d3d..6580419859 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/ComponentActionHelper.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/ComponentActionHelper.java @@ -9,92 +9,92 @@ import javafx.scene.control.ButtonType; import org.phoebus.applications.alarm.client.AlarmClient; import org.phoebus.applications.alarm.client.AlarmClientLeaf; -import org.phoebus.applications.alarm.model.AlarmTreeItem; +import org.phoebus.applications.alarm.model.EnabledState; import org.phoebus.applications.alarm.ui.Messages; import org.phoebus.framework.jobs.JobManager; import org.phoebus.ui.dialog.DialogHelper; import org.phoebus.ui.dialog.ExceptionDetailsErrorDialog; +import org.phoebus.util.time.TimestampFormats; import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; +import java.time.LocalDateTime; +import java.util.Set; + /** - * Code externalized from {@link EnableComponentAction}. + * Code externalized from {@link EnableAction}. */ -public class ComponentActionHelper { +public final class ComponentActionHelper { + + private ComponentActionHelper() {} /** - * Updates a component or PV node to enable or disable alarms + * Updates a hierarchy of PVs to a new {@link EnabledState}. * - * @param node The visual component relative to which a confirmation dialog is positioned. - * @param model {@link AlarmClient} dispatching producer messages. - * @param items {@link List} of items subject for update, e.g. selected by user. - * @param enable If true, enable alarms on selected nodes, otherwise disable. + * @param newEnabledState The {@link EnabledState} to set on all leaf nodes specified in items . */ - public static void updateEnablement(final Node node, final AlarmClient model, final List> items, boolean enable) { - final List pvs = new ArrayList<>(); - for (AlarmTreeItem item : items) { - findAffectedPVs(item, pvs, enable); + public static void updateEnabledState( + final Node node, + final AlarmClient model, + final EnabledState newEnabledState, + final Set totalLeafItems + ) { + if (totalLeafItems.stream().allMatch(leaf -> leaf.getEnabled().equals(newEnabledState))) { + final Alert dialog = new Alert(Alert.AlertType.INFORMATION); + DialogHelper.positionDialog(dialog, node, 0, 0); + dialog.setHeaderText(newEnabledState.enabled ? Messages.headerAlreadyEnabled : Messages.headerAlreadyDisabled); + dialog.show(); + return; + } + + // check the enable datetime is in the future right before updating - we could guard against more in the + // disable dialog, but the user could still for example select the next minute and then let two minutes pass + // So if we didn't check here we'd need to constantly be checking in the dialog box, not really worth it + if (newEnabledState.enabled_date != null && newEnabledState.enabled_date.isBefore(LocalDateTime.now())) { + final Alert dialog = new Alert(Alert.AlertType.ERROR); + DialogHelper.positionDialog(dialog, node, 0, 0); + dialog.setHeaderText(MessageFormat.format(Messages.selectedDateInThePast, TimestampFormats.SECONDS_FORMAT.format(newEnabledState.enabled_date))); + dialog.show(); + return; } - // If this affects exactly one PV, just do it. - // Otherwise ask for confirmation - if (pvs.size() != 1) { + // If more than one alarm affected, show a dialog box + if (totalLeafItems.size() > 1) { final Alert dialog = new Alert(Alert.AlertType.CONFIRMATION); - dialog.setTitle(enable ? Messages.enableAlarms : Messages.disableAlarms); - if (pvs.isEmpty()) { - dialog.setHeaderText( - enable - ? Messages.headerAlreadyEnabled - : Messages.headerAlreadyDisabled); - } else { + DialogHelper.positionDialog(dialog, node, 0, 0); + dialog.setTitle(newEnabledState.enabled ? Messages.enableAlarms : Messages.disableAlarms); + if (newEnabledState.enabled_date != null) { dialog.setHeaderText(MessageFormat.format( - enable - ? Messages.headerConfirmEnable - : Messages.headerConfirmDisable, - pvs.size())); + Messages.headerConfirmDisableWithEnableDate, + totalLeafItems.size(), + TimestampFormats.DATETIME_FORMAT.format(newEnabledState.enabled_date) + )); + } else if (!newEnabledState.enabled) { + dialog.setHeaderText(MessageFormat.format(Messages.headerConfirmDisable, totalLeafItems.size())); + } else { + dialog.setHeaderText(MessageFormat.format(Messages.headerConfirmEnable, totalLeafItems.size())); } - DialogHelper.positionDialog(dialog, node, -100, -50); + if (dialog.showAndWait().get() != ButtonType.OK) { return; } } - JobManager.schedule(enable ? Messages.enableAlarms : Messages.disableAlarms, monitor -> + JobManager.schedule(newEnabledState.enabled ? Messages.enableAlarms : Messages.disableAlarms, monitor -> { - for (AlarmClientLeaf pv : pvs) { + for (final AlarmClientLeaf pv : totalLeafItems) { final AlarmClientLeaf copy = pv.createDetachedCopy(); - if (copy.setEnabled(enable)) + if (copy.setEnabled(newEnabledState)) { try { model.sendItemConfigurationUpdate(pv.getPathName(), copy); - } catch (Exception e) { + } catch (final Exception ex) { ExceptionDetailsErrorDialog.openError(Messages.error, - copy.isEnabled() ? Messages.enableAlarmFailed : Messages.disableAlarmFailed, - e); - throw e; + newEnabledState.enabled ? Messages.enableAlarmFailed : Messages.disableAlarmFailed, + ex); + throw ex; } + } } }); } - - - /** - * @param item Node where to start recursing for PVs that would be affected - * @param pvs Array to update with PVs that would be affected - */ - public static void findAffectedPVs(final AlarmTreeItem item, final List pvs, boolean enable) { - if (item instanceof AlarmClientLeaf) { - final AlarmClientLeaf pv = (AlarmClientLeaf) item; - // If pv has different enablement, and wasn't already added - // because selection contains its parent as well as the PV itself... - if ((pv.isEnabled() != enable || pv.getEnabledDate() != null) && !pvs.contains(pv)) { - pvs.add(pv); - } - } else { - for (AlarmTreeItem sub : item.getChildren()) { - findAffectedPVs(sub, pvs, enable); - } - } - } } diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DisableAction.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DisableAction.java index 6bcc61d255..673c442ba2 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DisableAction.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DisableAction.java @@ -1,15 +1,13 @@ package org.phoebus.applications.alarm.ui.tree; -import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.control.*; -import javafx.scene.layout.GridPane; -import org.phoebus.applications.alarm.client.AlarmClientLeaf; +import javafx.scene.layout.VBox; +import org.phoebus.applications.alarm.model.EnabledState; import org.phoebus.applications.alarm.ui.AlarmUI; -import org.phoebus.applications.alarm.ui.config.DisableUntilDialogController; -import org.phoebus.framework.jobs.JobManager; +import org.phoebus.applications.alarm.ui.config.DisableDialogController; import org.phoebus.framework.nls.NLS; -import org.phoebus.ui.dialog.ExceptionDetailsErrorDialog; +import org.phoebus.ui.dialog.DialogHelper; import org.phoebus.ui.javafx.ImageCache; import javafx.scene.Node; @@ -20,99 +18,48 @@ import org.phoebus.applications.alarm.ui.Messages; import java.io.IOException; -import java.text.MessageFormat; -import java.time.LocalDateTime; import java.util.*; -import java.util.concurrent.atomic.AtomicReference; -public class DisableAction extends Menu { - - private final AlarmClient alarmClient; +public class DisableAction extends MenuItem { public DisableAction(final Node node, final AlarmClient model, final List> items) { - this.alarmClient = model; + super(); + setText(Messages.disableMenu); setGraphic(ImageCache.getImageView(AlarmUI.class, "/icons/disabled.png")); - MenuItem disable = new DisableComponentAction(node, model, items); - disable.setText(Messages.indefinitely); - MenuItem disableUntil = new MenuItem(Messages.withEnableDate); - disableUntil.setDisable(true); - AtomicReference treeNodeInfo = new AtomicReference<>(); - setOnShowing(e -> JobManager.schedule("Get Tree Node Info", monitor -> { - treeNodeInfo.set(AlarmTreeHelper.getTreeNodeInfo(items)); - if(treeNodeInfo.get().disabledWithEnableDate() == 0 || treeNodeInfo.get().commonEnableDate().isPresent()) { - Platform.runLater(() -> disableUntil.setDisable(false)); - } - })); - disableUntil.setOnAction(e -> { + setOnAction(e -> { + final TreeNodeInfo treeNodeInfo = AlarmTreeHelper.getTreeNodeInfo(items); + + final Dialog dlg = new Dialog<>(); + dlg.setTitle(Messages.disableAlarms); + dlg.getDialogPane().getButtonTypes().addAll(ButtonType.CANCEL, ButtonType.OK); + DialogHelper.positionDialog(dlg, node, 0, 0); + final FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setResources(NLS.getMessages(Messages.class)); - fxmlLoader.setLocation(DisableUntilDialogController.class.getResource("DisableUntilDialog.fxml")); + fxmlLoader.setLocation(DisableDialogController.class.getResource("DisableDialog.fxml")); + fxmlLoader.setControllerFactory(_ -> new DisableDialogController( + treeNodeInfo.commonDisableState().orElseGet(() -> new EnabledState(true)), + dlg.getDialogPane().lookupButton(ButtonType.OK) + )); - final GridPane root; + final VBox root; try { root = fxmlLoader.load(); - } catch (IOException ex) { + } catch (final IOException ex) { throw new RuntimeException(ex); } - DisableUntilDialogController dialogController = fxmlLoader.getController(); - - if (treeNodeInfo.get().commonEnableDate().isPresent()) { - dialogController.setDefaultDate(treeNodeInfo.get().commonEnableDate().get()); - } - - final Dialog dlg = new Dialog<>(); - dlg.setTitle("Disable until"); dlg.getDialogPane().setContent(root); - dlg.getDialogPane().getButtonTypes().addAll(ButtonType.CANCEL, ButtonType.OK); dlg.setResultConverter(button -> { - if (button.equals(ButtonType.OK)) { - return dialogController.determineEnableDate(); + if (button == ButtonType.OK) { + return fxmlLoader.getController().getEnabledState(); } return null; }); - Optional localDateTime = dlg.showAndWait(); - localDateTime.ifPresent(dateTime -> updateEnablement(dateTime, treeNodeInfo.get().leaves())); - }); - - getItems().addAll(disable, disableUntil); - } - - /** - * Updates a component to disable a hierarchy of PVs with an enable date. - * - * @param enableDate The {@link LocalDateTime} to set on all leaf nodes specified in items . - */ - private void updateEnablement(LocalDateTime enableDate, Set totalLeafItems) { - if (totalLeafItems.isEmpty()) { - return; - } - if (totalLeafItems.size() > 1) { - final Alert dialog = new Alert(Alert.AlertType.CONFIRMATION); - dialog.setTitle(Messages.disableAlarms); - dialog.setHeaderText(MessageFormat.format(Messages.headerConfirmDisableWithEnableDate, enableDate, totalLeafItems.size())); - if (dialog.showAndWait().get() != ButtonType.OK) { - return; - } - } - - JobManager.schedule(Messages.disableAlarms, monitor -> - { - for (AlarmClientLeaf pv : totalLeafItems) { - final AlarmClientLeaf copy = pv.createDetachedCopy(); - if (copy.setEnabledDate(enableDate)) { - try { - alarmClient.sendItemConfigurationUpdate(pv.getPathName(), copy); - } catch (Exception e) { - ExceptionDetailsErrorDialog.openError(Messages.error, - Messages.disableAlarmFailed, - e); - throw e; - } - } - } + final Optional newEnabledState = dlg.showAndWait(); + newEnabledState.ifPresent(enabledState -> ComponentActionHelper.updateEnabledState(node, model, enabledState, treeNodeInfo.leaves())); }); } } diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DisableComponentAction.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DisableComponentAction.java deleted file mode 100644 index c10105f441..0000000000 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/DisableComponentAction.java +++ /dev/null @@ -1,36 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 Oak Ridge National Laboratory. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - *******************************************************************************/ -package org.phoebus.applications.alarm.ui.tree; - -import java.util.List; - -import org.phoebus.applications.alarm.client.AlarmClient; -import org.phoebus.applications.alarm.model.AlarmTreeItem; - -import javafx.scene.Node; - -/** Action that disables items in the alarm tree configuration - * @author Kay Kasemir - */ -class DisableComponentAction extends EnableComponentAction -{ - /** @param node Node to position dialog - * @param model {@link AlarmClient} - * @param items Items to disable - */ - public DisableComponentAction(final Node node, final AlarmClient model, final List> items) - { - super(node, model, items); - } - - @Override - protected boolean doEnable() - { - return false; - } -} diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/EnableComponentAction.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/EnableAction.java similarity index 62% rename from app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/EnableComponentAction.java rename to app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/EnableAction.java index e5a8d0b96a..c0a3d7d5f2 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/EnableComponentAction.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/EnableAction.java @@ -8,10 +8,10 @@ package org.phoebus.applications.alarm.ui.tree; import javafx.scene.Node; -import javafx.scene.control.Menu; import javafx.scene.control.MenuItem; import org.phoebus.applications.alarm.client.AlarmClient; import org.phoebus.applications.alarm.model.AlarmTreeItem; +import org.phoebus.applications.alarm.model.EnabledState; import org.phoebus.applications.alarm.ui.AlarmUI; import org.phoebus.applications.alarm.ui.Messages; import org.phoebus.ui.javafx.ImageCache; @@ -24,26 +24,18 @@ * @author Kay Kasemir */ @SuppressWarnings("nls") -class EnableComponentAction extends MenuItem { +class EnableAction extends MenuItem { /** * @param node Node to position dialog * @param model {@link AlarmClient} * @param items Items to enable */ - public EnableComponentAction(final Node node, final AlarmClient model, final List> items) { - - if (doEnable()) { - setText(Messages.enableAlarms); - setGraphic(ImageCache.getImageView(AlarmUI.class, "/icons/enabled.png")); - } else { - setText(Messages.disableAlarms); - } - - setOnAction(event -> ComponentActionHelper.updateEnablement(node, model, items, doEnable())); - } - - // Implementation can actually disable or enable. Which one is it going to be? - protected boolean doEnable() { - return true; + public EnableAction(final Node node, final AlarmClient model, final List> items) { + setText(Messages.enableAlarms); + setGraphic(ImageCache.getImageView(AlarmUI.class, "/icons/enabled.png")); + setOnAction(event -> { + final TreeNodeInfo treeNodeInfo = AlarmTreeHelper.getTreeNodeInfo(items); + ComponentActionHelper.updateEnabledState(node, model, new EnabledState(true), treeNodeInfo.leaves()); + }); } } diff --git a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/TreeNodeInfo.java b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/TreeNodeInfo.java index 5ce8a2cd13..1cad52884b 100644 --- a/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/TreeNodeInfo.java +++ b/app/alarm/ui/src/main/java/org/phoebus/applications/alarm/ui/tree/TreeNodeInfo.java @@ -1,8 +1,8 @@ package org.phoebus.applications.alarm.ui.tree; import org.phoebus.applications.alarm.client.AlarmClientLeaf; +import org.phoebus.applications.alarm.model.EnabledState; -import java.time.LocalDateTime; import java.util.Optional; import java.util.Set; @@ -11,8 +11,7 @@ * @param leaves A {@link Set} of {@link AlarmClientLeaf}s. Note that the elements do not necessarily share the same parent node. * @param disabled Number of {@link AlarmClientLeaf}s in {@link #leaves} disabled indefinitely, i.e. disabled with no enable date set. * @param disabledWithEnableDate Number of {@link AlarmClientLeaf}s disabled with an enable date set. - * @param commonEnableDate Non-empty {@link Optional} if all the {@link AlarmClientLeaf}s counted by {@link #disabledWithEnableDate} - * have the same enable date. + * @param commonDisableState Non-empty {@link Optional} if all the {@link AlarmClientLeaf}s which are disabled have the same {@link EnabledState} */ -public record TreeNodeInfo(Set leaves, int disabled, int disabledWithEnableDate, Optional commonEnableDate) { +public record TreeNodeInfo(Set leaves, int disabled, int disabledWithEnableDate, Optional commonDisableState) { } diff --git a/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/config/DisableDialog.fxml b/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/config/DisableDialog.fxml new file mode 100644 index 0000000000..56bb1b7066 --- /dev/null +++ b/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/config/DisableDialog.fxml @@ -0,0 +1,20 @@ + + + + + + + + diff --git a/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/config/DisableUntilDialog.fxml b/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/config/DisableUntilDialog.fxml deleted file mode 100644 index 006a7f083b..0000000000 --- a/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/config/DisableUntilDialog.fxml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - diff --git a/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/messages.properties b/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/messages.properties index 2d6d863eff..93acffc81f 100644 --- a/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/messages.properties +++ b/app/alarm/ui/src/main/resources/org/phoebus/applications/alarm/ui/messages.properties @@ -17,7 +17,6 @@ # # acknowledgeFailed=Failed to acknowledge alarm(s) -absoluteDateLabel=Absolute Date: addComponentFailed=Failed to add component AlarmAreaPanel=Alarm Area Panel AlarmTable=Alarm Table @@ -33,6 +32,7 @@ behavior=Behavior: behaviorTooltip=Enable alarms? See also 'Enabling Filter' commands=Commands: configure=Configure +custom=Custom dateTimePickerTooltip=Select a date until which the alarm should be disabled delay=Delay delayTooltip0=Alarms are indicated when they persist for at least this long.\n{0} @@ -45,9 +45,8 @@ disabled=Disabled disableAlarmFailed=Failed to disable alarm disableAlarms=Disable Alarms disabledIndefinitely=Disabled: {0} -disableMenu=Disable... +disableMenu=Disable Alarms disabledUntil=Disabled until -disableUntil=Disable until: disabledCommonEnableDate=Disabled until {0}: {1} disabledVaryingEnableDate=Disabled (varying enable date): {0} displays=Displays: @@ -58,14 +57,13 @@ error=Error enableAlarmFailed=Failed to enable alarm enableAlarms=Enable Alarms guidance=Guidance: -headerAlreadyDisabled=All PVs in the selected section are already disabled -headerAlreadyEnabled=All PVs in the selected section are already enabled -headerConfirmDisable=Disable all PVs in the selected section of the alarm hierarchy?\nThis would disable {0} PVs. -headerConfirmDisableWithEnableDate=Disable all PVs in the selected section of the alarm hierarchy until {0}?\nThis would affect {1} PVs. -headerConfirmEnable=Enable all PVs in the selected section of the alarm hierarchy?\nThis would enable {0} PVs. +headerAlreadyDisabled=All the selected PVs are already disabled +headerAlreadyEnabled=All the selected PVs are already enabled +headerConfirmDisable=Disable {0} PVs indefinitely? +headerConfirmDisableWithEnableDate=Disable {0} PVs until {1}? +headerConfirmEnable=Enable {0} PVs? indefinitely=Indefinitely info=Info -invalidDate=You have selected a date that is either invalid or in the past latch=Latch latchTooltip=Latch alarm until acknowledged? MenuPath=Alarm @@ -73,18 +71,18 @@ moveItemFailed=Failed to move item option=Option partlyDisabled2=(Partly disabled) path=Path: -promptTitle=Absolute Date is set to a point in time in the past -relativeDateLabel=Relative Date: -relativeDateTooltip=Select a predefined duration for disabling the alarm +reenableDateTime=Re-enable date/time removeComponentFailed=Failed to remove component renameItemFailed=Failed to rename item +selectedDateInThePast=The selected date ({0}) is in the past timer=Timer title=Title +today=today tooltipAddTableItem=Add a new table item tooltipDeleteSelectedItems=Delete selected table items tooltipMoveEditDetail=Edit the detail field of table item tooltipMoveTableItemDown=Move table item down tooltipMoveTableItemUp=Move table item up +tomorrow=tomorrow totalPVs=PVs: {0} unacknowledgeFailed=Failed to unacknowledge alarm(s) -withEnableDate=With Enable Date diff --git a/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelperTest.java b/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelperTest.java index eb6e3f3fcf..f0d4e87e7e 100644 --- a/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelperTest.java +++ b/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelperTest.java @@ -5,9 +5,11 @@ import org.phoebus.applications.alarm.client.AlarmClientNode; import org.phoebus.applications.alarm.model.AlarmTreeItem; import org.phoebus.applications.alarm.model.BasicState; +import org.phoebus.applications.alarm.model.EnabledState; import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; import java.util.Set; import static org.junit.jupiter.api.Assertions.*; @@ -82,7 +84,7 @@ public void testGetAlarmNodeInfo1() { assertEquals(3, treeNodeInfo.leaves().size()); assertEquals(0, treeNodeInfo.disabled()); assertEquals(0, treeNodeInfo.disabledWithEnableDate()); - assertTrue(treeNodeInfo.commonEnableDate().isEmpty()); + assertEquals(treeNodeInfo.commonDisableState(), Optional.of(new EnabledState(true))); } @Test @@ -101,8 +103,7 @@ public void testGetAlarmNodeContent2() { assertEquals(3, treeNodeInfo.leaves().size()); assertEquals(1, treeNodeInfo.disabled()); assertEquals(0, treeNodeInfo.disabledWithEnableDate()); - assertTrue(treeNodeInfo.commonEnableDate().isEmpty()); - + assertTrue(treeNodeInfo.commonDisableState().isEmpty()); } @Test @@ -124,7 +125,7 @@ public void testGetAlarmNodeContent3() { assertEquals(3, treeNodeInfo.leaves().size()); assertEquals(0, treeNodeInfo.disabled()); assertEquals(2, treeNodeInfo.disabledWithEnableDate()); - assertTrue(treeNodeInfo.commonEnableDate().isEmpty()); + assertTrue(treeNodeInfo.commonDisableState().isEmpty()); } @Test @@ -138,15 +139,15 @@ public void testGetAlarmNodeContent4() { leaf1.setEnabledDate(localDateTime1); leaf1.addToParent(parent1); AlarmClientLeaf leaf2 = new AlarmClientLeaf("test/path/2", "testName2"); - leaf2.setEnabled(false); + leaf2.setEnabledDate(localDateTime1); leaf2.addToParent(parent1); TreeNodeInfo treeNodeInfo = AlarmTreeHelper.getTreeNodeInfo(parent1); assertEquals(3, treeNodeInfo.leaves().size()); - assertEquals(1, treeNodeInfo.disabled()); - assertEquals(2, treeNodeInfo.disabledWithEnableDate()); - assertTrue(treeNodeInfo.commonEnableDate().isPresent()); - assertEquals(localDateTime1, treeNodeInfo.commonEnableDate().get()); + assertEquals(0, treeNodeInfo.disabled()); + assertEquals(3, treeNodeInfo.disabledWithEnableDate()); + assertTrue(treeNodeInfo.commonDisableState().isPresent()); + assertEquals(new EnabledState(localDateTime1), treeNodeInfo.commonDisableState().get()); } } diff --git a/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/ComponentActionHelperTest.java b/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/ComponentActionHelperTest.java deleted file mode 100644 index 91ad5c79c1..0000000000 --- a/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/ComponentActionHelperTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2025 European Spallation Source ERIC. - */ - -package org.phoebus.applications.alarm.ui.tree; - -import org.junit.jupiter.api.Test; -import org.phoebus.applications.alarm.client.AlarmClientLeaf; -import org.phoebus.applications.alarm.client.AlarmClientNode; -import org.phoebus.applications.alarm.model.AlarmTreeItem; -import org.phoebus.applications.alarm.model.BasicState; -import org.phoebus.applications.alarm.model.EnabledState; - -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class ComponentActionHelperTest { - - @Test - public void testFindAffectedPvs_1() { - AlarmTreeItem parent1 = new AlarmClientNode("/root", "parent1"); - AlarmTreeItem parent2 = new AlarmClientNode("/parent", "parent2"); - AlarmClientLeaf child1 = new AlarmClientLeaf("/paren1", "child1"); - parent2.addToParent(parent1); - child1.addToParent(parent1); - AlarmClientLeaf child2 = new AlarmClientLeaf("/parent2", "child2"); - child2.addToParent(parent2); - - List disabledPvs = new ArrayList<>(); - ComponentActionHelper.findAffectedPVs(parent1, disabledPvs, true); - - assertTrue(disabledPvs.isEmpty()); - } - - @Test - public void testFindAffectedPvs_2() { - AlarmTreeItem parent1 = new AlarmClientNode("/root", "parent1"); - AlarmTreeItem parent2 = new AlarmClientNode("/parent", "parent2"); - AlarmClientLeaf child1 = new AlarmClientLeaf("/paren1", "child1"); - parent2.addToParent(parent1); - child1.addToParent(parent1); - AlarmClientLeaf child2 = new AlarmClientLeaf("/parent2", "child2"); - child2.setEnabled(new EnabledState(LocalDateTime.now())); - child2.addToParent(parent2); - - List disabledPvs = new ArrayList<>(); - ComponentActionHelper.findAffectedPVs(parent1, disabledPvs, true); - - assertEquals(1, disabledPvs.size()); - } - - @Test - public void testFindAffectedPvs_3() { - AlarmTreeItem parent1 = new AlarmClientNode("/root", "parent1"); - AlarmTreeItem parent2 = new AlarmClientNode("/parent", "parent2"); - AlarmClientLeaf child1 = new AlarmClientLeaf("/paren1", "child1"); - parent2.addToParent(parent1); - child1.addToParent(parent1); - AlarmClientLeaf child2 = new AlarmClientLeaf("/parent2", "child2"); - child2.addToParent(parent2); - AlarmClientLeaf child3 = new AlarmClientLeaf("/parent2", "child3"); - child3.setEnabled(false); - child3.addToParent(parent2); - - List disabledPvs = new ArrayList<>(); - ComponentActionHelper.findAffectedPVs(parent1, disabledPvs, true); - - assertEquals(1, disabledPvs.size()); - } -} From b973cd435ed5d0540c4f86c8b442b180889aa507 Mon Sep 17 00:00:00 2001 From: Percy Baker Date: Tue, 10 Nov 2026 11:59:32 +0000 Subject: [PATCH 2/2] fix tests --- .../applications/alarm/ui/tree/AlarmTreeHelperTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelperTest.java b/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelperTest.java index f0d4e87e7e..a8bf940795 100644 --- a/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelperTest.java +++ b/app/alarm/ui/src/test/java/org/phoebus/applications/alarm/ui/tree/AlarmTreeHelperTest.java @@ -8,6 +8,7 @@ import org.phoebus.applications.alarm.model.EnabledState; import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Optional; import java.util.Set; @@ -84,7 +85,7 @@ public void testGetAlarmNodeInfo1() { assertEquals(3, treeNodeInfo.leaves().size()); assertEquals(0, treeNodeInfo.disabled()); assertEquals(0, treeNodeInfo.disabledWithEnableDate()); - assertEquals(treeNodeInfo.commonDisableState(), Optional.of(new EnabledState(true))); + assertEquals(treeNodeInfo.commonDisableState(), Optional.empty()); } @Test @@ -103,7 +104,7 @@ public void testGetAlarmNodeContent2() { assertEquals(3, treeNodeInfo.leaves().size()); assertEquals(1, treeNodeInfo.disabled()); assertEquals(0, treeNodeInfo.disabledWithEnableDate()); - assertTrue(treeNodeInfo.commonDisableState().isEmpty()); + assertTrue(treeNodeInfo.commonDisableState().equals(Optional.of(new EnabledState(false)))); } @Test @@ -125,7 +126,6 @@ public void testGetAlarmNodeContent3() { assertEquals(3, treeNodeInfo.leaves().size()); assertEquals(0, treeNodeInfo.disabled()); assertEquals(2, treeNodeInfo.disabledWithEnableDate()); - assertTrue(treeNodeInfo.commonDisableState().isEmpty()); } @Test @@ -148,6 +148,6 @@ public void testGetAlarmNodeContent4() { assertEquals(0, treeNodeInfo.disabled()); assertEquals(3, treeNodeInfo.disabledWithEnableDate()); assertTrue(treeNodeInfo.commonDisableState().isPresent()); - assertEquals(new EnabledState(localDateTime1), treeNodeInfo.commonDisableState().get()); + assertEquals(new EnabledState(localDateTime1.truncatedTo(ChronoUnit.MINUTES)), treeNodeInfo.commonDisableState().get()); } }