PreferencesFX icon indicating copy to clipboard operation
PreferencesFX copied to clipboard

Feature request: PreferencesFx.of(java.util.prefs.Preferences...

Open trixon opened this issue 4 years ago • 3 comments

While adding PreferencesFX to a NetBeans Platform application I noticed that my settings where scattered.

On Linux, a NetBeans Platform application store its settings in a directory like ~/.MYAPP while PreferencesFX defaults to ~/.java/.userPrefs/...

With NbPreferences.forModule(Class c) https://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/NbPreferences.html#forModule-java.lang.Class- I get a java.util.prefs.Preferences object I would like to use when calling PreferencesFx.of(). Is it possible to add that functionality to the framework?

While reading the code I got to PreferencesBasedStorageHandler and the row https://github.com/dlsc-software-consulting-gmbh/PreferencesFX/blob/d09c87bc32d74afcd3e33ff7c3ebc73cb1fffc44/preferencesfx/src/main/java/com/dlsc/preferencesfx/util/PreferencesBasedStorageHandler.java#L42

So, I guess I could make my own StorageHandler, but I believe it makes more sence to have a simple PreferencesFx.of(java.util.prefs.Preferences... in the framework.

trixon avatar Sep 15 '19 15:09 trixon

+1, in order to export preferences, I have to use java reflection to access model -> storage handler -> preferences, may be a public StorageHandler ref will suffice.

fujohnwang avatar Nov 10 '20 02:11 fujohnwang

+1 for this, I'd really prefer my settings to be saved in a settings file, not the registry.

marvk avatar Dec 31 '20 01:12 marvk

First, inject a custom storage handler that overrides the default behaviours:

PreferencesFx.of( createStorageHandler(), createCategories() )
  .instantPersistent( false )

  // ... along with ...

  private StorageHandler createStorageHandler() {
    return new XmlStorageHandler();
  }

Next, implement that custom XmlStorageHandler to perform no actions. That is, PreferencesFX will no longer use its own storage handling routines.

Then, define application Keys to help ensure type safety when referencing settings. This avoids hard-coding strings for key names everywhere and allows for defining structured relationships between keys, such as a hierarchy of settings.

Following that, create a Workspace concept that handles persisting of settings data. This is where you'd populate default values for every setting (to be null-hostile).

Up next, map the keys to PreferencesFX settings and widgets. Check out the PreferencesController class to see one way to accomplish this mapping.

Finally, when persisting the data, you now have control over the settings file format, the file location and name, and the ability to maintain backwards compatibility with previous versions.

ghost avatar Dec 19 '21 05:12 ghost