Skip to content

Commit

Permalink
Merge pull request #195 from trixon/unregisterable-sessions
Browse files Browse the repository at this point in the history
Make SessionManager able to unregister
  • Loading branch information
dlemmermann authored Aug 14, 2024
2 parents 13039a3 + c522abe commit e8fdb5f
Showing 1 changed file with 81 additions and 17 deletions.
98 changes: 81 additions & 17 deletions gemsfx/src/main/java/com/dlsc/gemsfx/util/SessionManager.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.dlsc.gemsfx.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javafx.beans.property.*;

import java.util.Objects;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javafx.beans.value.ChangeListener;

/**
* A manager for storing observable values in the user preferences.
Expand All @@ -13,6 +16,7 @@ public class SessionManager {

private static final Logger LOG = Logger.getLogger(SessionManager.class.getSimpleName());
private final Preferences preferences;
private final HashMap<Property, ArrayList<ChangeListener>> propertyToListeners = new HashMap<>();

/**
* Constructs a new session manager that will use the passed in preferences.
Expand Down Expand Up @@ -43,13 +47,15 @@ public final Preferences getPreferences() {
public void register(String path, DoubleProperty property) {
LOG.fine("registering double property at path " + path);
property.set(preferences.getDouble(path, property.get()));
property.addListener((it, oldValue, newValue) -> {
putPropertyValueIntoPreferences(path, property);
ChangeListener<Double> listener = (it, oldValue, newValue) -> {
if (newValue != null) {
preferences.putDouble(path, newValue.doubleValue());
preferences.putDouble(path, newValue);
} else {
preferences.remove(path);
}
});
};
addListener(property, listener);
}

/**
Expand All @@ -63,13 +69,15 @@ public void register(String path, DoubleProperty property) {
public void register(String path, IntegerProperty property) {
LOG.fine("registering integer property at path " + path);
property.set(preferences.getInt(path, property.get()));
property.addListener((it, oldValue, newValue) -> {
putPropertyValueIntoPreferences(path, property);
ChangeListener<Integer> listener = (it, oldValue, newValue) -> {
if (newValue != null) {
preferences.putInt(path, newValue.intValue());
preferences.putInt(path, newValue);
} else {
preferences.remove(path);
}
});
};
addListener(property, listener);
}

/**
Expand All @@ -83,13 +91,15 @@ public void register(String path, IntegerProperty property) {
public void register(String path, FloatProperty property) {
LOG.fine("registering float property at path " + path);
property.set(preferences.getFloat(path, property.get()));
property.addListener((it, oldValue, newValue) -> {
putPropertyValueIntoPreferences(path, property);
ChangeListener<Float> listener = (it, oldValue, newValue) -> {
if (newValue != null) {
preferences.putFloat(path, newValue.floatValue());
preferences.putFloat(path, newValue);
} else {
preferences.remove(path);
}
});
};
addListener(property, listener);
}

/**
Expand All @@ -103,13 +113,15 @@ public void register(String path, FloatProperty property) {
public void register(String path, LongProperty property) {
LOG.fine("registering long property at path " + path);
property.set(preferences.getLong(path, property.get()));
property.addListener((it, oldValue, newValue) -> {
putPropertyValueIntoPreferences(path, property);
ChangeListener<Long> listener = (it, oldValue, newValue) -> {
if (newValue != null) {
preferences.putLong(path, newValue.longValue());
preferences.putLong(path, newValue);
} else {
preferences.remove(path);
}
});
};
addListener(property, listener);
}

/**
Expand All @@ -123,13 +135,15 @@ public void register(String path, LongProperty property) {
public void register(String path, BooleanProperty property) {
LOG.fine("registering boolean property at path " + path);
property.set(preferences.getBoolean(path, property.get()));
property.addListener((it, oldValue, newValue) -> {
putPropertyValueIntoPreferences(path, property);
ChangeListener<Boolean> listener = (it, oldValue, newValue) -> {
if (newValue != null) {
preferences.putBoolean(path, newValue);
} else {
preferences.remove(path);
}
});
};
addListener(property, listener);
}

/**
Expand All @@ -143,12 +157,62 @@ public void register(String path, BooleanProperty property) {
public void register(String path, StringProperty property) {
LOG.fine("registering string property at path " + path);
property.set(preferences.get(path, property.get()));
property.addListener((it, oldValue, newValue) -> {
putPropertyValueIntoPreferences(path, property);
ChangeListener<String> listener = (it, oldValue, newValue) -> {
if (newValue != null) {
preferences.put(path, newValue);
} else {
preferences.remove(path);
}
});
};
addListener(property, listener);
}

/**
* Unregisters all listeners created by SessionManager for the specified property.
*
* @param property the property to unregister
*/
public void unregister(Property property) {
ArrayList<ChangeListener> listeners = propertyToListeners.get(property);
if (listeners != null) {
listeners.forEach(listener -> property.removeListener(listener));
listeners.clear();
propertyToListeners.remove(property);
}
}

/**
* Unregisters all listeners created by SessionManager.
*
*/
public void unregisterAll() {
for (Map.Entry<Property, ArrayList<ChangeListener>> entry : propertyToListeners.entrySet()) {
entry.getValue().forEach(listener -> entry.getKey().removeListener(listener));
entry.getValue().clear();
}
propertyToListeners.clear();
}

private void addListener(Property property, ChangeListener listener) {
property.addListener(listener);
propertyToListeners.computeIfAbsent(property, k -> new ArrayList<>()).add(listener);
}

private void putPropertyValueIntoPreferences(String path, Property property) {
Object value = property.getValue();
if (value instanceof Boolean b) {
preferences.putBoolean(path, b);
} else if (value instanceof Double d) {
preferences.putDouble(path, d);
} else if (value instanceof Float f) {
preferences.putFloat(path, f);
} else if (value instanceof Integer i) {
preferences.putInt(path, i);
} else if (value instanceof Long l) {
preferences.putLong(path, l);
} else if (value instanceof String s) {
preferences.put(path, s);
}
}
}

0 comments on commit e8fdb5f

Please sign in to comment.