diff --git a/src/main/java/viewtify/model/NamedPreferences.java b/src/main/java/viewtify/model/NamedPreferences.java deleted file mode 100644 index b37311516..000000000 --- a/src/main/java/viewtify/model/NamedPreferences.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2023 Nameless Production Committee - * - * Licensed under the MIT License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://opensource.org/licenses/mit-license.php - */ -package viewtify.model; - -import kiss.Managed; -import kiss.Storable; - -@Managed -public class NamedPreferences extends Preferences { - - /** The user defined name. */ - public final Preference name = initialize(""); - - Storable container; - - /** - * {@inheritDoc} - */ - @Override - public Preferences store() { - container.store(); - System.out.println("Store " + this); - return this; - } -} diff --git a/src/main/java/viewtify/model/Preferences.java b/src/main/java/viewtify/model/Preferences.java index 5d32ad978..563c82312 100644 --- a/src/main/java/viewtify/model/Preferences.java +++ b/src/main/java/viewtify/model/Preferences.java @@ -27,15 +27,17 @@ import kiss.Storable; import kiss.Variable; import kiss.WiseFunction; -import viewtify.Viewtify; public abstract class Preferences implements Storable, Extensible { /** The cache for preferences. */ - private static final Map cache = new ConcurrentHashMap(); + private static final Map CACHE = new ConcurrentHashMap(); + + /** The user defined name. */ + public final Preference name = initialize(""); - /** The preference id. */ - private String id; + /** The grouping container. */ + Storable container; /** * Hide constructor. @@ -108,31 +110,12 @@ protected final Preference initialize(Variable defaultValue) { } /** - * Synchronize data from/to source. - */ - void sync() { - Viewtify.UserPreference.observing().to(x -> { - // Not all property values are preserved in the restore source, so they must always be - // reset before restoring. If not reset, some properties may continue to apply the - // previous user's values to the new user. - reset(); - restore(); - }); - auto(); - } - - /** - * Synchronize data from/to source. + * {@inheritDoc} */ - void sync2() { - Viewtify.UserPreference.observing().to(x -> { - // Not all property values are preserved in the restore source, so they must always be - // reset before restoring. If not reset, some properties may continue to apply the - // previous user's values to the new user. - reset(); - restore(); - }); - auto(); + @Override + public Preferences store() { + container.store(); + return this; } /** @@ -177,15 +160,7 @@ private Variable> findBy(Property property) { } /** - * {@inheritDoc} - */ - @Override - public String locate() { - return Viewtify.UserPreference.exact().file(id + ".json").path(); - } - - /** - * Get the user preferences by type. + * Search the user preference of the specified type. * * @param

* @param type @@ -196,46 +171,40 @@ public static

P of(Class

type) { } /** - * Get the user preferences by type and key. + * Search the user preference of the specified type and name. * * @param

* @param type - * @param key * @return */ - public static

P of(Class

type, String key) { - String id = type.getName(); - if (key != null) { - id = id + "@" + key; - } - - return (P) cache.computeIfAbsent(id, x -> { - Preferences prefs = I.make(type); - prefs.id = x; - prefs.sync(); - return prefs; - }); - } + public static

P of(Class

type, String name) { + name = Objects.requireNonNullElse(name, ""); - private static final Map CACHE = new ConcurrentHashMap(); - - public static

P by(Class

type, String key) { - PreferencesList

list = list(type); + PreferencesList

list = CACHE.computeIfAbsent(type, PreferencesList::new); for (P preferences : list) { - if (preferences.name.is(key)) { + if (preferences.name.is(name)) { return preferences; } } P named = I.make(type); - named.name.set(key); + named.name.set(name); named.container = list; + named.auto(); + list.add(named); return named; } - public static

PreferencesList

list(Class

type) { + /** + * Search all user preferences of the specified type. + * + * @param

+ * @param type + * @return + */ + public static

List

all(Class

type) { return CACHE.computeIfAbsent(type, PreferencesList::new); } diff --git a/src/main/java/viewtify/model/PreferencesList.java b/src/main/java/viewtify/model/PreferencesList.java index 5c24f062b..9446a7864 100644 --- a/src/main/java/viewtify/model/PreferencesList.java +++ b/src/main/java/viewtify/model/PreferencesList.java @@ -9,22 +9,18 @@ */ package viewtify.model; -import java.util.ArrayList; - -import javafx.collections.ModifiableObservableListBase; +import java.util.concurrent.CopyOnWriteArrayList; import kiss.Managed; import kiss.Storable; import viewtify.Viewtify; -final class PreferencesList extends ModifiableObservableListBase implements Storable> { +@SuppressWarnings("serial") +final class PreferencesList extends CopyOnWriteArrayList implements Storable> { /** The model id. */ private final String id; - /** The actual container. */ - private final ArrayList list = new ArrayList(); - /** * Hide constructor. */ @@ -41,67 +37,19 @@ final class PreferencesList extends ModifiableObservableL } } - sync(); - } - - /** - * Synchronize data from/to source. - */ - protected final void sync() { + // Synchronize data from/to source. Viewtify.UserPreference.observing().to(x -> { // Not all property values are preserved in the restore source, so they must always be // reset before restoring. If not reset, some properties may continue to apply the // previous user's values to the new user. clear(); restore(); - }); - } - - /** - * {@inheritDoc} - */ - @Override - public E get(int index) { - return list.get(index); - } - - /** - * {@inheritDoc} - */ - @Override - public int size() { - return list.size(); - } - /** - * {@inheritDoc} - */ - @Override - protected void doAdd(int index, E model) { - if (model != null) { - System.out.println("Add " + model); - model.sync2(); - list.add(index, model); - } - } - - /** - * {@inheritDoc} - */ - @Override - protected E doSet(int index, E model) { - System.out.println("Set " + model); - model.sync2(); - - return list.set(index, model); - } - - /** - * {@inheritDoc} - */ - @Override - protected E doRemove(int model) { - return list.remove(model); + for (E item : this) { + item.container = this; + item.auto(); + } + }); } /** diff --git a/src/main/java/viewtify/ui/calendar/CalendarSettingView.java b/src/main/java/viewtify/ui/calendar/CalendarSettingView.java index 3c4839308..bd02688c1 100644 --- a/src/main/java/viewtify/ui/calendar/CalendarSettingView.java +++ b/src/main/java/viewtify/ui/calendar/CalendarSettingView.java @@ -122,7 +122,7 @@ protected ViewDSL declareUI() { */ @Override protected void initialize() { - TimeEventSourceSetting setting = Preferences.by(TimeEventSourceSetting.class, source.name()); + TimeEventSourceSetting setting = Preferences.of(TimeEventSourceSetting.class, source.name()); enable.text(source.name()).sync(setting.enable); color.disableWhen(enable.isNotSelected()).sync(setting.color, FXUtils::color, FXUtils::color); diff --git a/src/main/java/viewtify/ui/calendar/TimeEventSourceSetting.java b/src/main/java/viewtify/ui/calendar/TimeEventSourceSetting.java index c9d884c5d..799049921 100644 --- a/src/main/java/viewtify/ui/calendar/TimeEventSourceSetting.java +++ b/src/main/java/viewtify/ui/calendar/TimeEventSourceSetting.java @@ -9,12 +9,12 @@ */ package viewtify.ui.calendar; -import viewtify.model.NamedPreferences; +import viewtify.model.Preferences; /** * Preference for calendar. */ -public class TimeEventSourceSetting extends NamedPreferences { +public class TimeEventSourceSetting extends Preferences { /** The availability. */ public final Preference enable = initialize(true); diff --git a/src/main/java/viewtify/ui/view/PreferenceView.java b/src/main/java/viewtify/ui/view/PreferenceView.java index f4a303c82..84daf0294 100644 --- a/src/main/java/viewtify/ui/view/PreferenceView.java +++ b/src/main/java/viewtify/ui/view/PreferenceView.java @@ -86,12 +86,12 @@ protected ViewDSL declareUI() { */ interface style extends StyleDSL { Style left = () -> { - display.width(200, px); - padding.vertical(40, px).horizontal(10, px); + display.minWidth(180, px); + padding.vertical(40, px).left(10, px); }; Style navi = () -> { - display.width(180, px); + display.minWidth(160, px); font.size(14, px).smooth.grayscale(); padding.vertical(10, px).left(20, px); cursor.pointer();