Skip to content

Instantly share code, notes, and snippets.

@ennerf
Created May 28, 2022 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ennerf/9963268dd947d017381b311a2e7cb3cf to your computer and use it in GitHub Desktop.
Save ennerf/9963268dd947d017381b311a2e7cb3cf to your computer and use it in GitHub Desktop.
Class to help persist JavaFX properties between runs
/*-
* %%
* Copyright (C) 2018-2022 HEBI Robotics
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package us.hebi.gui.utils;
import javafx.beans.property.*;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
/**
* Stores properties via Preferences API. Previous values are loaded
* at initialization, and stored on close.
* <p>
* Preferences are stored by user and by package name. Classes in the
* same package must have unique strings.
*
* @author Florian Enner
* @since 02 Aug 2020
*/
public class PersistentProperties {
public BooleanProperty getBoolean(String name, boolean defValue) {
var prop = new SimpleBooleanProperty(prefs.getBoolean(name, defValue));
onSave.add(() -> prefs.putBoolean(name, prop.getValue()));
onReset.add(() -> prop.set(defValue));
return prop;
}
public FloatProperty getFloat(String name, float defValue) {
var prop = new SimpleFloatProperty(prefs.getFloat(name, defValue));
onReset.add(() -> prop.set(defValue));
onSave.add(() -> prefs.putFloat(name, prop.getValue()));
return prop;
}
public DoubleProperty getDouble(String name, double defValue) {
var prop = new SimpleDoubleProperty(prefs.getDouble(name, defValue));
onReset.add(() -> prop.set(defValue));
onSave.add(() -> prefs.putDouble(name, prop.getValue()));
return prop;
}
public IntegerProperty getInt(String name, int defValue) {
var prop = new SimpleIntegerProperty(prefs.getInt(name, defValue));
onReset.add(() -> prop.set(defValue));
onSave.add(() -> prefs.putInt(name, prop.getValue()));
return prop;
}
public LongProperty getLong(String name, long defValue) {
var prop = new SimpleLongProperty(prefs.getLong(name, defValue));
onReset.add(() -> prop.set(defValue));
onSave.add(() -> prefs.putLong(name, prop.getValue()));
return prop;
}
public StringProperty getString(String name) {
return getString(name, "");
}
public StringProperty getString(String name, String defValue) {
var prop = new SimpleStringProperty(prefs.get(name, defValue));
onReset.add(() -> prop.set(defValue));
onSave.add(() -> prefs.put(name, prop.getValue()));
return prop;
}
@SuppressWarnings("unchecked")
public <E extends Enum<E>> ObjectProperty<E> getEnum(String name, E defValue) {
E value;
try {
value = (E) Enum.valueOf(defValue.getClass(), prefs.get(name, defValue.name()));
} catch (NullPointerException | IllegalArgumentException notFound) {
value = defValue;
}
var prop = new SimpleObjectProperty<E>(value);
onReset.add(() -> prop.set(defValue));
onSave.add(() -> prefs.put(name, Optional.ofNullable(prop.get())
.map(Enum::name)
.orElse("")));
return prop;
}
/**
* resets all values to their defaults
*/
public void reset() {
onReset.forEach(Runnable::run);
}
/**
* Clears existing user preferences from the store
*/
public void clear() throws BackingStoreException {
prefs.clear();
}
@PreDestroy
public void save() {
onSave.forEach(Runnable::run);
}
public PersistentProperties() {
prefs = Preferences.userNodeForPackage(this.getClass());
}
public PersistentProperties(Class<?> clazz) {
prefs = Preferences.userNodeForPackage(Objects.requireNonNull(clazz));
}
public PersistentProperties(Preferences prefs) {
this.prefs = Objects.requireNonNull(prefs);
}
final List<Runnable> onReset = new ArrayList<>(4);
final List<Runnable> onSave = new ArrayList<>(4);
final Preferences prefs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment