Search in sources :

Example 1 with SingleTermPreference

use of de.unijena.cheminf.mortar.preference.SingleTermPreference 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.");
        }
    }
}
Also used : SingleNumberPreference(de.unijena.cheminf.mortar.preference.SingleNumberPreference) IPreference(de.unijena.cheminf.mortar.preference.IPreference) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) SingleIntegerPreference(de.unijena.cheminf.mortar.preference.SingleIntegerPreference) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) BooleanPreference(de.unijena.cheminf.mortar.preference.BooleanPreference) SingleTermPreference(de.unijena.cheminf.mortar.preference.SingleTermPreference) SimpleEnumConstantNameProperty(de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) SimpleEnumConstantNameProperty(de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty) Property(javafx.beans.property.Property) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty)

Example 2 with SingleTermPreference

use of de.unijena.cheminf.mortar.preference.SingleTermPreference in project MORTAR by FelixBaensch.

the class FragmentationService method persistSelectedFragmenterAndPipeline.

/**
 * Persists the fragmentation service settings (the selected fragmenter) and the currently configured pipeline.
 * All settings are saved as matching preference objects in preference container files in a respective subfolder in
 * the settings directory. If anything fails, a warning is given to the user.
 */
public void persistSelectedFragmenterAndPipeline() {
    String tmpFragmentationServiceSettingsPath = FileUtil.getSettingsDirPath() + FragmentationService.FRAGMENTATION_SERVICE_SETTINGS_SUBFOLDER_NAME + File.separator;
    File tmpFragmentationServiceSettingsDir = new File(tmpFragmentationServiceSettingsPath);
    if (!tmpFragmentationServiceSettingsDir.exists()) {
        tmpFragmentationServiceSettingsDir.mkdirs();
    } else {
        FileUtil.deleteAllFilesInDirectory(tmpFragmentationServiceSettingsPath);
    }
    if (!tmpFragmentationServiceSettingsDir.canWrite()) {
        GuiUtil.guiMessageAlert(Alert.AlertType.ERROR, Message.get("Error.ExceptionAlert.Title"), Message.get("Error.ExceptionAlert.Header"), Message.get("FragmentationService.Error.settingsPersistence"));
        return;
    }
    PreferenceContainer tmpFragmentationServiceSettingsContainer = new PreferenceContainer(tmpFragmentationServiceSettingsPath + FragmentationService.FRAGMENTATION_SERVICE_SETTINGS_FILE_NAME + BasicDefinitions.PREFERENCE_CONTAINER_FILE_EXTENSION);
    if (!SingleTermPreference.isValidContent(this.selectedFragmenter.getFragmentationAlgorithmName())) {
        FragmentationService.LOGGER.log(Level.WARNING, "Selected fragmenter could not be persisted");
        GuiUtil.guiMessageAlert(Alert.AlertType.WARNING, Message.get("Error.ExceptionAlert.Title"), Message.get("Error.ExceptionAlert.Header"), Message.get("FragmentationService.Error.settingsPersistence"));
        for (IMoleculeFragmenter tmpFragmenter : this.fragmenters) {
            if (tmpFragmenter.getFragmentationAlgorithmName().equals(FragmentationService.DEFAULT_SELECTED_FRAGMENTER_ALGORITHM_NAME)) {
                this.selectedFragmenter = tmpFragmenter;
            }
        }
    }
    SingleTermPreference tmpSelectedFragmenterPreference = new SingleTermPreference(FragmentationService.SELECTED_FRAGMENTER_SETTING_NAME, this.selectedFragmenter.getFragmentationAlgorithmName());
    tmpFragmentationServiceSettingsContainer.add(tmpSelectedFragmenterPreference);
    if (Objects.isNull(this.pipeliningFragmentationName) || this.pipeliningFragmentationName.isEmpty()) {
        this.pipeliningFragmentationName = FragmentationService.DEFAULT_PIPELINE_NAME;
    }
    if (!SingleTermPreference.isValidContent(this.pipeliningFragmentationName)) {
        FragmentationService.LOGGER.log(Level.WARNING, "Given pipeline name " + this.pipeliningFragmentationName + " is invalid, will be reset to default.");
        this.pipeliningFragmentationName = FragmentationService.DEFAULT_PIPELINE_NAME;
    }
    SingleTermPreference tmpPipelineNamePreference = new SingleTermPreference(FragmentationService.PIPELINE_SETTING_NAME, this.pipeliningFragmentationName);
    tmpFragmentationServiceSettingsContainer.add(tmpPipelineNamePreference);
    tmpFragmentationServiceSettingsContainer.add(new SingleIntegerPreference(FragmentationService.PIPELINE_SIZE_SETTING_NAME, this.pipelineFragmenter.length));
    try {
        tmpFragmentationServiceSettingsContainer.writeRepresentation();
    } catch (IOException | SecurityException anException) {
        FragmentationService.LOGGER.log(Level.WARNING, "Fragmentation service settings persistence went wrong, exception: " + anException.toString(), anException);
        GuiUtil.guiExceptionAlert(Message.get("Error.ExceptionAlert.Title"), Message.get("Error.ExceptionAlert.Header"), Message.get("FragmentationService.Error.settingsPersistence"), anException);
    }
    for (int i = 0; i < this.pipelineFragmenter.length; i++) {
        IMoleculeFragmenter tmpFragmenter = this.pipelineFragmenter[i];
        List<Property> tmpSettings = tmpFragmenter.settingsProperties();
        String tmpFilePath = tmpFragmentationServiceSettingsPath + FragmentationService.PIPELINE_FRAGMENTER_FILE_NAME_PREFIX + i + BasicDefinitions.PREFERENCE_CONTAINER_FILE_EXTENSION;
        PreferenceContainer tmpPrefContainer = PreferenceUtil.translateJavaFxPropertiesToPreferences(tmpSettings, tmpFilePath);
        tmpPrefContainer.add(new SingleTermPreference(FragmentationService.PIPELINE_FRAGMENTER_ALGORITHM_NAME_SETTING_NAME, tmpFragmenter.getFragmentationAlgorithmName()));
        try {
            tmpPrefContainer.writeRepresentation();
        } catch (IOException | SecurityException anException) {
            FragmentationService.LOGGER.log(Level.WARNING, "Pipeline fragmenter settings persistence went wrong, exception: " + anException.toString(), anException);
            GuiUtil.guiExceptionAlert(Message.get("Error.ExceptionAlert.Title"), Message.get("Error.ExceptionAlert.Header"), Message.get("FragmentationService.Error.settingsPersistence"), anException);
            continue;
        }
    }
}
Also used : SingleTermPreference(de.unijena.cheminf.mortar.preference.SingleTermPreference) SingleIntegerPreference(de.unijena.cheminf.mortar.preference.SingleIntegerPreference) PreferenceContainer(de.unijena.cheminf.mortar.preference.PreferenceContainer) IOException(java.io.IOException) File(java.io.File) IMoleculeFragmenter(de.unijena.cheminf.mortar.model.fragmentation.algorithm.IMoleculeFragmenter) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) SimpleEnumConstantNameProperty(de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty) Property(javafx.beans.property.Property) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty)

Example 3 with SingleTermPreference

use of de.unijena.cheminf.mortar.preference.SingleTermPreference 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.");
                }
            }
        }
    }
}
Also used : SingleNumberPreference(de.unijena.cheminf.mortar.preference.SingleNumberPreference) IPreference(de.unijena.cheminf.mortar.preference.IPreference) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) SingleIntegerPreference(de.unijena.cheminf.mortar.preference.SingleIntegerPreference) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) BooleanPreference(de.unijena.cheminf.mortar.preference.BooleanPreference) SingleTermPreference(de.unijena.cheminf.mortar.preference.SingleTermPreference) SimpleEnumConstantNameProperty(de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty) PreferenceContainer(de.unijena.cheminf.mortar.preference.PreferenceContainer) File(java.io.File) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) SimpleEnumConstantNameProperty(de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty) Property(javafx.beans.property.Property) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty)

Aggregations

SimpleEnumConstantNameProperty (de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty)3 SingleIntegerPreference (de.unijena.cheminf.mortar.preference.SingleIntegerPreference)3 SingleTermPreference (de.unijena.cheminf.mortar.preference.SingleTermPreference)3 Property (javafx.beans.property.Property)3 SimpleBooleanProperty (javafx.beans.property.SimpleBooleanProperty)3 SimpleDoubleProperty (javafx.beans.property.SimpleDoubleProperty)3 SimpleIntegerProperty (javafx.beans.property.SimpleIntegerProperty)3 SimpleStringProperty (javafx.beans.property.SimpleStringProperty)3 BooleanPreference (de.unijena.cheminf.mortar.preference.BooleanPreference)2 IPreference (de.unijena.cheminf.mortar.preference.IPreference)2 PreferenceContainer (de.unijena.cheminf.mortar.preference.PreferenceContainer)2 SingleNumberPreference (de.unijena.cheminf.mortar.preference.SingleNumberPreference)2 File (java.io.File)2 IOException (java.io.IOException)2 IMoleculeFragmenter (de.unijena.cheminf.mortar.model.fragmentation.algorithm.IMoleculeFragmenter)1 ArrayList (java.util.ArrayList)1