use of de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty in project MORTAR by FelixBaensch.
the class SettingsView method addPropertyItems.
//
/**
* Adds a row for each {@link Property} of given List which contains of properties name and a control to change properties value
* @param aGridPane GridPane to add row
* @param aPropertiesList List of properties to show in created tab
* @param aTooltipTextsMap Map containing setting names as keys and tooltip text as values
* @param aRecentPropertiesMap Map to hold recent properties to restore them if necessary
*/
private void addPropertyItems(GridPane aGridPane, List<Property> aPropertiesList, Map<String, String> aTooltipTextsMap, Map<String, Object> aRecentPropertiesMap) {
int tmpRowIndex = 0;
for (Property tmpProperty : aPropertiesList) {
RowConstraints tmpRow = new RowConstraints();
tmpRow.setVgrow(Priority.ALWAYS);
tmpRow.setPrefHeight(50);
tmpRow.setMaxHeight(50);
tmpRow.setMinHeight(50);
aGridPane.getRowConstraints().add(tmpRow);
String tmpPropName = tmpProperty.getName();
Label tmpNameLabel = new Label(tmpPropName);
Tooltip tmpTooltip = new Tooltip(aTooltipTextsMap.get(tmpProperty.getName()));
tmpTooltip.setMaxWidth(GuiDefinitions.GUI_TOOLTIP_MAX_WIDTH);
tmpTooltip.setWrapText(true);
tmpNameLabel.setTooltip(tmpTooltip);
aGridPane.add(tmpNameLabel, 0, tmpRowIndex);
GridPane.setMargin(tmpNameLabel, new Insets(GuiDefinitions.GUI_INSETS_VALUE));
Object tmpRecentValue = tmpProperty.getValue();
aRecentPropertiesMap.put(tmpPropName, tmpRecentValue);
if (tmpProperty instanceof SimpleBooleanProperty) {
ComboBox<Boolean> tmpBooleanComboBox = new ComboBox<>();
tmpBooleanComboBox.getItems().addAll(Boolean.FALSE, Boolean.TRUE);
tmpBooleanComboBox.valueProperty().bindBidirectional(tmpProperty);
tmpBooleanComboBox.setTooltip(tmpTooltip);
// add to gridpane
aGridPane.add(tmpBooleanComboBox, 1, tmpRowIndex++);
GridPane.setMargin(tmpBooleanComboBox, new Insets(GuiDefinitions.GUI_INSETS_VALUE));
} else if (tmpProperty instanceof SimpleIntegerProperty) {
TextField tmpIntegerTextField = new TextField();
tmpIntegerTextField.setPrefWidth(GuiDefinitions.GUI_TEXT_FIELD_PREF_WIDTH_VALUE);
tmpIntegerTextField.setMaxWidth(GuiDefinitions.GUI_SETTINGS_TEXT_FIELD_MAX_WIDTH_VALUE);
tmpIntegerTextField.setAlignment(Pos.CENTER_RIGHT);
TextFormatter<Integer> tmpFormatter = new TextFormatter<>(GuiUtil.getStringToIntegerConverter(), 0, GuiUtil.getIntegerFilter());
tmpIntegerTextField.setTextFormatter(tmpFormatter);
tmpFormatter.valueProperty().bindBidirectional(tmpProperty);
tmpIntegerTextField.setTooltip(tmpTooltip);
// add to gridpane
aGridPane.add(tmpIntegerTextField, 1, tmpRowIndex++);
GridPane.setMargin(tmpIntegerTextField, new Insets(GuiDefinitions.GUI_INSETS_VALUE));
} else if (tmpProperty instanceof SimpleDoubleProperty) {
TextField tmpDoubleTextField = new TextField();
tmpDoubleTextField.setPrefWidth(GuiDefinitions.GUI_TEXT_FIELD_PREF_WIDTH_VALUE);
tmpDoubleTextField.setMaxWidth(GuiDefinitions.GUI_SETTINGS_TEXT_FIELD_MAX_WIDTH_VALUE);
tmpDoubleTextField.setAlignment(Pos.CENTER_RIGHT);
TextFormatter<Double> tmpFormatter = new TextFormatter<>(GuiUtil.getStringToDoubleConverter(), 0.0, GuiUtil.getDoubleFilter());
tmpDoubleTextField.setTextFormatter(tmpFormatter);
tmpFormatter.valueProperty().bindBidirectional(tmpProperty);
tmpDoubleTextField.setTooltip(tmpTooltip);
// add to gridpane
aGridPane.add(tmpDoubleTextField, 1, tmpRowIndex++);
GridPane.setMargin(tmpDoubleTextField, new Insets(GuiDefinitions.GUI_INSETS_VALUE));
} else if (tmpProperty instanceof SimpleEnumConstantNameProperty) {
ComboBox<String> tmpEnumComboBox = new ComboBox();
tmpEnumComboBox.getItems().addAll(((SimpleEnumConstantNameProperty) tmpProperty).getAssociatedEnumConstantNames());
tmpEnumComboBox.valueProperty().bindBidirectional(tmpProperty);
tmpEnumComboBox.setTooltip(tmpTooltip);
// add to gridpane
aGridPane.add(tmpEnumComboBox, 1, tmpRowIndex++);
GridPane.setMargin(tmpEnumComboBox, new Insets(GuiDefinitions.GUI_INSETS_VALUE));
} else if (tmpProperty instanceof SimpleStringProperty) {
TextField tmpStringTextField = new TextField();
tmpStringTextField.setPrefWidth(GuiDefinitions.GUI_TEXT_FIELD_PREF_WIDTH_VALUE);
tmpStringTextField.setMaxWidth(GuiDefinitions.GUI_SETTINGS_TEXT_FIELD_MAX_WIDTH_VALUE);
tmpStringTextField.setAlignment(Pos.CENTER_RIGHT);
tmpStringTextField.textProperty().bindBidirectional(tmpProperty);
tmpStringTextField.setTooltip(tmpTooltip);
// add to gridpane
aGridPane.add(tmpStringTextField, 1, tmpRowIndex++);
GridPane.setMargin(tmpStringTextField, new Insets(GuiDefinitions.GUI_INSETS_VALUE));
}
}
}
use of de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty in project MORTAR by FelixBaensch.
the class FragmentationService method updatePropertiesFromPreferences.
/**
* Sets the values of the given properties according to the preferences in the given container with the same name.
* If no matching preference for a given property is found, the value will remain in its default setting.
*/
private void updatePropertiesFromPreferences(List<Property> aPropertiesList, PreferenceContainer aPreferenceContainer) {
for (Property tmpSettingProperty : aPropertiesList) {
String tmpPropertyName = tmpSettingProperty.getName();
if (aPreferenceContainer.containsPreferenceName(tmpPropertyName)) {
IPreference[] tmpPreferences = aPreferenceContainer.getPreferences(tmpPropertyName);
try {
if (tmpSettingProperty instanceof SimpleBooleanProperty) {
BooleanPreference tmpBooleanPreference = (BooleanPreference) tmpPreferences[0];
tmpSettingProperty.setValue(tmpBooleanPreference.getContent());
} else if (tmpSettingProperty instanceof SimpleIntegerProperty) {
SingleIntegerPreference tmpIntPreference = (SingleIntegerPreference) tmpPreferences[0];
tmpSettingProperty.setValue(tmpIntPreference.getContent());
} else if (tmpSettingProperty instanceof SimpleDoubleProperty) {
SingleNumberPreference tmpDoublePreference = (SingleNumberPreference) tmpPreferences[0];
tmpSettingProperty.setValue(tmpDoublePreference.getContent());
} else if (tmpSettingProperty instanceof SimpleEnumConstantNameProperty || tmpSettingProperty instanceof SimpleStringProperty) {
SingleTermPreference tmpStringPreference = (SingleTermPreference) tmpPreferences[0];
tmpSettingProperty.setValue(tmpStringPreference.getContent());
} else {
// setting will remain in default
FragmentationService.LOGGER.log(Level.WARNING, "Setting " + tmpPropertyName + " is of unknown type.");
}
} catch (ClassCastException | IllegalArgumentException anException) {
// setting will remain in default
FragmentationService.LOGGER.log(Level.WARNING, anException.toString(), anException);
}
} else {
// setting will remain in default
FragmentationService.LOGGER.log(Level.WARNING, "No persisted settings for " + tmpPropertyName + " available.");
}
}
}
use of de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty in project MORTAR by FelixBaensch.
the class SettingsContainer method reloadGlobalSettings.
/**
* Reloads the setting values from a previous MORTAR session via the persisted preference container.
*/
public void reloadGlobalSettings() {
String tmpSettingsDirectoryPathName = FileUtil.getSettingsDirPath();
File tmpSettingsDirectoryFile = new File(tmpSettingsDirectoryPathName);
String tmpPreferenceContainerFilePathName = tmpSettingsDirectoryPathName + BasicDefinitions.SETTINGS_CONTAINER_FILE_NAME + BasicDefinitions.PREFERENCE_CONTAINER_FILE_EXTENSION;
File tmpPreferenceContainerFile = new File(tmpPreferenceContainerFilePathName);
if (!tmpSettingsDirectoryFile.exists()) {
FileUtil.createDirectory(tmpSettingsDirectoryFile.getAbsolutePath());
SettingsContainer.LOGGER.info("No persisted global settings could be found, all set to default.");
return;
} else {
boolean tmpExists = tmpPreferenceContainerFile.exists();
boolean tmpIsFile = tmpPreferenceContainerFile.isFile();
boolean tmpCanRead = tmpPreferenceContainerFile.canRead();
if (!tmpExists || !tmpIsFile || !tmpCanRead) {
SettingsContainer.LOGGER.warning("Preference container file does not exist or cannot be read. " + "A new one is initialised.");
return;
} else {
PreferenceContainer tmpContainer;
try {
tmpContainer = new PreferenceContainer(tmpPreferenceContainerFile);
} catch (IOException | SecurityException anException) {
SettingsContainer.LOGGER.log(Level.SEVERE, "Unable to reload global settings: " + anException.toString(), anException);
return;
}
List<Property> tmpSettings = new ArrayList<>(6);
tmpSettings.addAll(this.settings);
tmpSettings.add(this.recentDirectoryPathSetting);
for (Property tmpSettingProperty : tmpSettings) {
String tmpPropertyName = tmpSettingProperty.getName();
if (tmpContainer.containsPreferenceName(tmpPropertyName)) {
IPreference[] tmpPreferences = tmpContainer.getPreferences(tmpPropertyName);
try {
if (tmpSettingProperty instanceof SimpleBooleanProperty) {
BooleanPreference tmpBooleanPreference = (BooleanPreference) tmpPreferences[0];
tmpSettingProperty.setValue(tmpBooleanPreference.getContent());
} else if (tmpSettingProperty instanceof SimpleIntegerProperty) {
SingleIntegerPreference tmpIntPreference = (SingleIntegerPreference) tmpPreferences[0];
tmpSettingProperty.setValue(tmpIntPreference.getContent());
} else if (tmpSettingProperty instanceof SimpleDoubleProperty) {
SingleNumberPreference tmpDoublePreference = (SingleNumberPreference) tmpPreferences[0];
tmpSettingProperty.setValue(tmpDoublePreference.getContent());
} else if (tmpSettingProperty instanceof SimpleEnumConstantNameProperty || tmpSettingProperty instanceof SimpleStringProperty) {
SingleTermPreference tmpStringPreference = (SingleTermPreference) tmpPreferences[0];
tmpSettingProperty.setValue(tmpStringPreference.getContent());
} else {
// setting will remain in default
SettingsContainer.LOGGER.log(Level.WARNING, "Setting " + tmpPropertyName + " is of unknown type.");
}
} catch (ClassCastException | IllegalArgumentException anException) {
// setting will remain in default
SettingsContainer.LOGGER.log(Level.WARNING, anException.toString(), anException);
}
} else {
// setting will remain in default
SettingsContainer.LOGGER.log(Level.WARNING, "No persisted settings for " + tmpPropertyName + " available.");
}
}
}
}
}
use of de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty in project MORTAR by FelixBaensch.
the class PreferenceUtil method translateJavaFxPropertiesToPreferences.
/**
* Translates a list of JavaFX property objects into their respective preference counterparts, conserving their names
* and values. A preference container object containing the created preferences is returned. If a property cannot
* be translated because its name or content are no valid arguments for the respective preference, a warning is logged.
*
* @param aPropertiesList list of properties to translate
* @param aContainerFilePathname the PreferenceContainer object created needs a valid file pathname where it can write
* its persisted form to
* @return preference container containing all translated preferences
* @throws NullPointerException if a given argument is null
* @throws IllegalArgumentException if the given file path name is invalid
*/
public static PreferenceContainer translateJavaFxPropertiesToPreferences(List<Property> aPropertiesList, String aContainerFilePathname) throws NullPointerException, IllegalArgumentException {
Objects.requireNonNull(aPropertiesList);
Objects.requireNonNull(aContainerFilePathname);
if (!PreferenceContainer.isValidContainerFilePathname(aContainerFilePathname)) {
throw new IllegalArgumentException("File pathname " + aContainerFilePathname + " is no valid path.");
}
if (aPropertiesList.isEmpty()) {
return new PreferenceContainer(aContainerFilePathname);
}
PreferenceContainer tmpContainer = new PreferenceContainer(aContainerFilePathname);
for (Property tmpProperty : aPropertiesList) {
try {
if (Objects.isNull(tmpProperty)) {
continue;
}
if (tmpProperty instanceof SimpleBooleanProperty) {
BooleanPreference tmpBooleanPreference = new BooleanPreference(tmpProperty.getName(), ((SimpleBooleanProperty) tmpProperty).get());
tmpContainer.add(tmpBooleanPreference);
} else if (tmpProperty instanceof SimpleIntegerProperty) {
SingleIntegerPreference tmpIntPreference = new SingleIntegerPreference(tmpProperty.getName(), ((SimpleIntegerProperty) tmpProperty).get());
tmpContainer.add(tmpIntPreference);
} else if (tmpProperty instanceof SimpleDoubleProperty) {
SingleNumberPreference tmpDoublePreference = new SingleNumberPreference(tmpProperty.getName(), ((SimpleDoubleProperty) tmpProperty).get());
tmpContainer.add(tmpDoublePreference);
} else if (tmpProperty instanceof SimpleEnumConstantNameProperty || tmpProperty instanceof SimpleStringProperty) {
SingleTermPreference tmpStringPreference = new SingleTermPreference(tmpProperty.getName(), ((SimpleStringProperty) tmpProperty).get());
tmpContainer.add(tmpStringPreference);
} else {
PreferenceUtil.LOGGER.log(Level.WARNING, "Unknown property type " + tmpProperty.getClass().getSimpleName() + " was given.");
}
} catch (IllegalArgumentException anException) {
PreferenceUtil.LOGGER.log(Level.WARNING, "Setting translation to property went wrong, exception: " + anException.toString(), anException);
continue;
}
}
return tmpContainer;
}
use of de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty in project MORTAR by FelixBaensch.
the class FragmentationService method checkFragmenters.
/**
* Checks the available fragmenters and their settings for restrictions imposed by persistence. Throws an exception if
* anything does not meet the requirements.
*/
private void checkFragmenters() throws Exception {
HashSet<String> tmpAlgorithmNames = new HashSet<>(this.fragmenters.length + 6, 1.0f);
for (IMoleculeFragmenter tmpFragmenter : this.fragmenters) {
// algorithm name should be singleton and must be persistable
String tmpAlgName = tmpFragmenter.getFragmentationAlgorithmName();
if (!PreferenceUtil.isValidName(tmpAlgName) || !SingleTermPreference.isValidContent(tmpAlgName)) {
throw new Exception("Algorithm name " + tmpAlgName + " is invalid.");
}
if (tmpAlgorithmNames.contains(tmpAlgName)) {
throw new Exception("Algorithm name " + tmpAlgName + " is used multiple times.");
} else {
tmpAlgorithmNames.add(tmpAlgName);
}
// setting names must be singletons within the respective class
// setting names and values must adhere to the preference input restrictions
// setting values are only tested for their current state, not the entire possible input space! It is tested again at persistence
List<Property> tmpSettingsList = tmpFragmenter.settingsProperties();
HashSet<String> tmpSettingNames = new HashSet<>(tmpSettingsList.size() + 6, 1.0f);
for (Property tmpSetting : tmpSettingsList) {
if (!PreferenceUtil.isValidName(tmpSetting.getName())) {
throw new Exception("Setting " + tmpSetting.getName() + " has an invalid name.");
}
if (tmpSettingNames.contains(tmpSetting.getName())) {
throw new Exception("Setting name " + tmpSetting.getName() + " is used multiple times.");
} else {
tmpSettingNames.add(tmpSetting.getName());
}
if (tmpSetting instanceof SimpleBooleanProperty) {
// nothing to do here, booleans cannot have invalid values
} else if (tmpSetting instanceof SimpleIntegerProperty) {
if (!SingleIntegerPreference.isValidContent(Integer.toString(((SimpleIntegerProperty) tmpSetting).get()))) {
throw new Exception("Setting value " + ((SimpleIntegerProperty) tmpSetting).get() + " of setting name " + tmpSetting.getName() + " is invalid.");
}
} else if (tmpSetting instanceof SimpleDoubleProperty) {
if (!SingleNumberPreference.isValidContent(((SimpleDoubleProperty) tmpSetting).get())) {
throw new Exception("Setting value " + ((SimpleDoubleProperty) tmpSetting).get() + " of setting name " + tmpSetting.getName() + " is invalid.");
}
} else if (tmpSetting instanceof SimpleEnumConstantNameProperty || tmpSetting instanceof SimpleStringProperty) {
if (!SingleTermPreference.isValidContent(((SimpleStringProperty) tmpSetting).get())) {
throw new Exception("Setting value " + ((SimpleStringProperty) tmpSetting).get() + " of setting name " + tmpSetting.getName() + " is invalid.");
}
} else {
throw new Exception("Setting " + tmpSetting.getName() + " is of an invalid type.");
}
}
}
}
Aggregations