|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 European Spallation Source ERIC. |
| 3 | + */ |
| 4 | + |
| 5 | +package org.phoebus.applications.saveandrestore.ui.configuration; |
| 6 | + |
| 7 | +import javafx.beans.property.BooleanProperty; |
| 8 | +import javafx.beans.property.DoubleProperty; |
| 9 | +import javafx.beans.property.ObjectProperty; |
| 10 | +import javafx.beans.property.SimpleBooleanProperty; |
| 11 | +import javafx.beans.property.SimpleDoubleProperty; |
| 12 | +import javafx.beans.property.SimpleObjectProperty; |
| 13 | +import javafx.beans.property.SimpleStringProperty; |
| 14 | +import javafx.beans.property.StringProperty; |
| 15 | +import org.phoebus.applications.saveandrestore.model.ConfigPv; |
| 16 | +import org.phoebus.applications.saveandrestore.model.PvCompareMode; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +/** |
| 21 | + * Wrapper around a {@link ConfigPv} instance for the purpose of facilitating |
| 22 | + * configuration and data binding in (for instance) a {@link javafx.scene.control.TableView}. |
| 23 | + */ |
| 24 | +public class ConfigPvEntry implements Comparable<ConfigPvEntry> { |
| 25 | + |
| 26 | + private final StringProperty pvNameProperty; |
| 27 | + private final StringProperty readBackPvNameProperty; |
| 28 | + private final BooleanProperty readOnlyProperty; |
| 29 | + private final ObjectProperty<PvCompareMode> pvCompareModeProperty; |
| 30 | + private ObjectProperty<Double> toleranceProperty; |
| 31 | + |
| 32 | + public ConfigPvEntry(ConfigPv configPv) { |
| 33 | + this.pvNameProperty = new SimpleStringProperty(this, "pvNameProperty", configPv.getPvName()); |
| 34 | + this.readBackPvNameProperty = new SimpleStringProperty(configPv.getReadbackPvName()); |
| 35 | + this.readOnlyProperty = new SimpleBooleanProperty(configPv.isReadOnly()); |
| 36 | + this.pvCompareModeProperty = new SimpleObjectProperty(configPv.getPvCompareMode()); |
| 37 | + this.toleranceProperty = configPv.getTolerance() != null ? |
| 38 | + new SimpleObjectProperty<>(configPv.getTolerance()) : new SimpleObjectProperty<>(null); |
| 39 | + } |
| 40 | + |
| 41 | + public StringProperty getPvNameProperty() { |
| 42 | + return pvNameProperty; |
| 43 | + } |
| 44 | + |
| 45 | + public StringProperty getReadBackPvNameProperty() { |
| 46 | + return readBackPvNameProperty; |
| 47 | + } |
| 48 | + |
| 49 | + public BooleanProperty getReadOnlyProperty() { |
| 50 | + return readOnlyProperty; |
| 51 | + } |
| 52 | + |
| 53 | + public ObjectProperty<PvCompareMode> getPvCompareModeProperty() { |
| 54 | + return pvCompareModeProperty; |
| 55 | + } |
| 56 | + |
| 57 | + public ObjectProperty<Double> getToleranceProperty() { |
| 58 | + return toleranceProperty; |
| 59 | + } |
| 60 | + |
| 61 | + public void setPvNameProperty(String pvNameProperty) { |
| 62 | + this.pvNameProperty.set(pvNameProperty); |
| 63 | + } |
| 64 | + |
| 65 | + public void setReadBackPvNameProperty(String readBackPvNameProperty) { |
| 66 | + this.readBackPvNameProperty.set(readBackPvNameProperty); |
| 67 | + } |
| 68 | + |
| 69 | + public void setReadOnlyProperty(boolean readOnlyProperty) { |
| 70 | + this.readOnlyProperty.set(readOnlyProperty); |
| 71 | + } |
| 72 | + |
| 73 | + public void setPvCompareModeProperty(PvCompareMode pvCompareModeProperty) { |
| 74 | + this.pvCompareModeProperty.set(pvCompareModeProperty); |
| 75 | + } |
| 76 | + |
| 77 | + public void setToleranceProperty(Double toleranceProperty) { |
| 78 | + this.toleranceProperty.set(toleranceProperty ); |
| 79 | + } |
| 80 | + |
| 81 | + public ConfigPv toConfigPv() { |
| 82 | + ConfigPv configPv = ConfigPv.builder() |
| 83 | + .pvName(pvNameProperty.get()) |
| 84 | + .readbackPvName(readBackPvNameProperty.get()) |
| 85 | + .readOnly(readOnlyProperty.get()) |
| 86 | + .build(); |
| 87 | + if (!readBackPvNameProperty.isEmpty().get() && |
| 88 | + pvCompareModeProperty.get() != null) { |
| 89 | + configPv.setPvCompareMode(pvCompareModeProperty.get()); |
| 90 | + configPv.setTolerance(toleranceProperty.get()); |
| 91 | + } |
| 92 | + return configPv; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean equals(Object other) { |
| 97 | + if (other instanceof ConfigPvEntry otherConfigPv) { |
| 98 | + return Objects.equals(pvNameProperty, otherConfigPv.getPvNameProperty()) && |
| 99 | + Objects.equals(readBackPvNameProperty, otherConfigPv.getReadBackPvNameProperty()) && |
| 100 | + Objects.equals(readOnlyProperty.get(), otherConfigPv.getReadOnlyProperty().get()); |
| 101 | + } |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public int hashCode() { |
| 107 | + return Objects.hash(pvNameProperty, readBackPvNameProperty, readOnlyProperty); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public int compareTo(ConfigPvEntry other) { |
| 112 | + return pvNameProperty.get().compareTo(other.getPvNameProperty().get()); |
| 113 | + } |
| 114 | +} |
0 commit comments