Search in sources :

Example 1 with PreferenceContainer

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

the class FragmentationService method reloadFragmenterSettings.

/**
 * Reloads settings of the available fragmenters. If something goes wrong, it is logged.
 */
public void reloadFragmenterSettings() {
    String tmpDirectoryPath = FileUtil.getSettingsDirPath() + FragmentationService.FRAGMENTER_SETTINGS_SUBFOLDER_NAME + File.separator;
    for (IMoleculeFragmenter tmpFragmenter : this.fragmenters) {
        String tmpClassName = tmpFragmenter.getClass().getSimpleName();
        File tmpFragmenterSettingsFile = new File(tmpDirectoryPath + tmpClassName + BasicDefinitions.PREFERENCE_CONTAINER_FILE_EXTENSION);
        if (tmpFragmenterSettingsFile.exists() && tmpFragmenterSettingsFile.isFile() && tmpFragmenterSettingsFile.canRead()) {
            PreferenceContainer tmpContainer;
            try {
                tmpContainer = new PreferenceContainer(tmpFragmenterSettingsFile);
            } catch (IllegalArgumentException | IOException anException) {
                FragmentationService.LOGGER.log(Level.WARNING, "Unable to reload settings of fragmenter " + tmpClassName + " : " + anException.toString(), anException);
                continue;
            }
            this.updatePropertiesFromPreferences(tmpFragmenter.settingsProperties(), tmpContainer);
        } else {
            // settings will remain in default
            FragmentationService.LOGGER.log(Level.WARNING, "No persisted settings for " + tmpClassName + " available.");
        }
    }
}
Also used : PreferenceContainer(de.unijena.cheminf.mortar.preference.PreferenceContainer) IOException(java.io.IOException) IMoleculeFragmenter(de.unijena.cheminf.mortar.model.fragmentation.algorithm.IMoleculeFragmenter) File(java.io.File)

Example 2 with PreferenceContainer

use of de.unijena.cheminf.mortar.preference.PreferenceContainer 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 PreferenceContainer

use of de.unijena.cheminf.mortar.preference.PreferenceContainer 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)

Example 4 with PreferenceContainer

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

the class SettingsContainer method preserveSettings.

// </editor-fold>
// 
// <editor-fold desc="public methods">
/**
 * Triggers the preservation of the current settings values in a file for re-import at the next application session.
 */
public void preserveSettings() {
    String tmpSettingsDirectoryPathName = FileUtil.getSettingsDirPath();
    File tmpSettingsDirectoryFile = new File(tmpSettingsDirectoryPathName);
    if (!tmpSettingsDirectoryFile.exists()) {
        tmpSettingsDirectoryFile.mkdirs();
    }
    if (!tmpSettingsDirectoryFile.canWrite()) {
        SettingsContainer.LOGGER.log(Level.WARNING, "Global settings persistence went wrong, cannot write to settings directory.");
        GuiUtil.guiMessageAlert(Alert.AlertType.ERROR, Message.get("Error.ExceptionAlert.Title"), Message.get("Error.ExceptionAlert.Header"), Message.get("SettingsContainer.Error.settingsPersistence"));
        return;
    }
    String tmpPreferenceContainerFilePathName = tmpSettingsDirectoryPathName + BasicDefinitions.SETTINGS_CONTAINER_FILE_NAME + BasicDefinitions.PREFERENCE_CONTAINER_FILE_EXTENSION;
    List<Property> tmpSettings = new ArrayList<>(6);
    tmpSettings.addAll(this.settings);
    tmpSettings.add(this.recentDirectoryPathSetting);
    try {
        PreferenceContainer tmpPrefContainer = PreferenceUtil.translateJavaFxPropertiesToPreferences(tmpSettings, tmpPreferenceContainerFilePathName);
        tmpPrefContainer.writeRepresentation();
    } catch (NullPointerException | IllegalArgumentException | IOException | SecurityException anException) {
        SettingsContainer.LOGGER.log(Level.WARNING, "Global settings persistence went wrong, exception: " + anException.toString(), anException);
        GuiUtil.guiExceptionAlert(Message.get("Error.ExceptionAlert.Title"), Message.get("Error.ExceptionAlert.Header"), Message.get("SettingsContainer.Error.settingsPersistence"), anException);
        return;
    }
}
Also used : PreferenceContainer(de.unijena.cheminf.mortar.preference.PreferenceContainer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) 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)

Example 5 with PreferenceContainer

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

the class FragmentationService method reloadActiveFragmenterAndPipeline.

/**
 * Reloads fragmentation service settings like the selected fragmenter and the pipeline configurations from the previous session.
 * If anything goes wrong, the errors are logged and in some cases, a warning is given to the user.
 */
public void reloadActiveFragmenterAndPipeline() {
    String tmpFragmentationServiceSettingsPath = FileUtil.getSettingsDirPath() + FragmentationService.FRAGMENTATION_SERVICE_SETTINGS_SUBFOLDER_NAME + File.separator;
    String tmpServiceSettingsFilePath = tmpFragmentationServiceSettingsPath + FragmentationService.FRAGMENTATION_SERVICE_SETTINGS_FILE_NAME + BasicDefinitions.PREFERENCE_CONTAINER_FILE_EXTENSION;
    File tmpServiceSettingsFile = new File(tmpServiceSettingsFilePath);
    if (tmpServiceSettingsFile.exists() && tmpServiceSettingsFile.isFile() && tmpServiceSettingsFile.canRead()) {
        PreferenceContainer tmpFragmentationServiceSettingsContainer;
        int tmpPipelineSize = 0;
        try {
            tmpFragmentationServiceSettingsContainer = new PreferenceContainer(tmpServiceSettingsFile);
            tmpPipelineSize = ((SingleIntegerPreference) tmpFragmentationServiceSettingsContainer.getPreferences(FragmentationService.PIPELINE_SIZE_SETTING_NAME)[0]).getContent();
            String tmpPipelineName = tmpFragmentationServiceSettingsContainer.getPreferences(FragmentationService.PIPELINE_SETTING_NAME)[0].getContentRepresentative();
            String tmpSelectedFragmenterAlgorithmName = tmpFragmentationServiceSettingsContainer.getPreferences(FragmentationService.SELECTED_FRAGMENTER_SETTING_NAME)[0].getContentRepresentative();
            this.pipeliningFragmentationName = tmpPipelineName;
            for (IMoleculeFragmenter tmpFragmenter : this.fragmenters) {
                if (tmpFragmenter.getFragmentationAlgorithmName().equals(tmpSelectedFragmenterAlgorithmName)) {
                    this.selectedFragmenter = tmpFragmenter;
                    this.selectedFragmenterNameProperty.set(this.selectedFragmenter.getFragmentationAlgorithmName());
                    break;
                }
            }
        } catch (IllegalArgumentException | IOException anException) {
            FragmentationService.LOGGER.log(Level.WARNING, "FragmentationService settings reload failed: " + anException.toString(), anException);
            GuiUtil.guiExceptionAlert(Message.get("Error.ExceptionAlert.Title"), Message.get("Error.ExceptionAlert.Header"), Message.get("FragmentationService.Error.settingsReload"), anException);
            return;
        }
        IMoleculeFragmenter[] tmpFragmenterArray = new IMoleculeFragmenter[tmpPipelineSize];
        for (int i = 0; i < tmpPipelineSize; i++) {
            String tmpPath = tmpFragmentationServiceSettingsPath + FragmentationService.PIPELINE_FRAGMENTER_FILE_NAME_PREFIX + i + BasicDefinitions.PREFERENCE_CONTAINER_FILE_EXTENSION;
            File tmpFragmenterFile = new File(tmpPath);
            if (tmpFragmenterFile.exists() && tmpFragmenterFile.isFile() && tmpFragmenterFile.canRead()) {
                try {
                    PreferenceContainer tmpFragmenterSettingsContainer = new PreferenceContainer(tmpFragmenterFile);
                    String tmpFragmenterClassName = tmpFragmenterSettingsContainer.getPreferences(FragmentationService.PIPELINE_FRAGMENTER_ALGORITHM_NAME_SETTING_NAME)[0].getContentRepresentative();
                    IMoleculeFragmenter tmpFragmenter = this.createNewFragmenterObjectByAlgorithmName(tmpFragmenterClassName);
                    this.updatePropertiesFromPreferences(tmpFragmenter.settingsProperties(), tmpFragmenterSettingsContainer);
                    tmpFragmenterArray[i] = tmpFragmenter;
                } catch (Exception anException) {
                    FragmentationService.LOGGER.log(Level.WARNING, "FragmentationService settings reload failed: " + anException.toString(), anException);
                    GuiUtil.guiExceptionAlert(Message.get("Error.ExceptionAlert.Title"), Message.get("Error.ExceptionAlert.Header"), Message.get("FragmentationService.Error.settingsReload"), anException);
                    continue;
                }
            } else {
                FragmentationService.LOGGER.log(Level.WARNING, "Unable to reload pipeline fragmenter " + i + " : No respective file available.");
                continue;
            }
        }
        this.setPipelineFragmenter(tmpFragmenterArray);
    } else {
        FragmentationService.LOGGER.log(Level.WARNING, "File containing persisted FragmentationService settings not found.");
    }
}
Also used : PreferenceContainer(de.unijena.cheminf.mortar.preference.PreferenceContainer) IOException(java.io.IOException) File(java.io.File) IMoleculeFragmenter(de.unijena.cheminf.mortar.model.fragmentation.algorithm.IMoleculeFragmenter) CDKException(org.openscience.cdk.exception.CDKException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

PreferenceContainer (de.unijena.cheminf.mortar.preference.PreferenceContainer)6 File (java.io.File)6 IOException (java.io.IOException)6 IMoleculeFragmenter (de.unijena.cheminf.mortar.model.fragmentation.algorithm.IMoleculeFragmenter)4 SimpleEnumConstantNameProperty (de.unijena.cheminf.mortar.model.util.SimpleEnumConstantNameProperty)4 Property (javafx.beans.property.Property)4 SimpleBooleanProperty (javafx.beans.property.SimpleBooleanProperty)4 SimpleDoubleProperty (javafx.beans.property.SimpleDoubleProperty)4 SimpleIntegerProperty (javafx.beans.property.SimpleIntegerProperty)4 SimpleStringProperty (javafx.beans.property.SimpleStringProperty)4 SingleIntegerPreference (de.unijena.cheminf.mortar.preference.SingleIntegerPreference)2 SingleTermPreference (de.unijena.cheminf.mortar.preference.SingleTermPreference)2 ArrayList (java.util.ArrayList)2 BooleanPreference (de.unijena.cheminf.mortar.preference.BooleanPreference)1 IPreference (de.unijena.cheminf.mortar.preference.IPreference)1 SingleNumberPreference (de.unijena.cheminf.mortar.preference.SingleNumberPreference)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CancellationException (java.util.concurrent.CancellationException)1 CDKException (org.openscience.cdk.exception.CDKException)1