PreferencesHandler.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * SteelBlue
  6.  * http://steelblue.tidalwave.it - git clone git@bitbucket.org:tidalwave/steelblue-src.git
  7.  * %%
  8.  * Copyright (C) 2015 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  *
  11.  * *********************************************************************************************************************
  12.  *
  13.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  14.  * the License. You may obtain a copy of the License at
  15.  *
  16.  *     http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  19.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  20.  * specific language governing permissions and limitations under the License.
  21.  *
  22.  * *********************************************************************************************************************
  23.  *
  24.  *
  25.  *
  26.  * *********************************************************************************************************************
  27.  * #L%
  28.  */
  29. package it.tidalwave.util;

  30. import javax.annotation.Nonnull;
  31. import java.nio.file.Path;
  32. import java.util.Optional;
  33. import java.util.concurrent.atomic.AtomicReference;
  34. import it.tidalwave.util.impl.DefaultPreferencesHandler;

  35. /***********************************************************************************************************************
  36.  *
  37.  * @author  Fabrizio Giudici
  38.  *
  39.  **********************************************************************************************************************/
  40. public interface PreferencesHandler
  41.   {
  42.     public static final String PROP_APP_NAME = PreferencesHandler.class.getPackage().getName() + ".appName";

  43.     // FIXME: make private as soon as the right Java version is required
  44.     public static final String __BASE_NAME = "it.tidalwave.javafx";
  45.     public static AtomicReference<PreferencesHandler> __INSTANCE = new AtomicReference<>();

  46.     /** A property representing the initial main window size as a percentual of the screen size. */
  47.     public static final Key<Double> KEY_INITIAL_SIZE = Key.of(__BASE_NAME + ".initialSize", Double.class);

  48.     /** Whether the application should start at full screen. */
  49.     public static final Key<Boolean> KEY_FULL_SCREEN = Key.of(__BASE_NAME + ".fullScreen", Boolean.class);

  50.     @Nonnull
  51.     public Path getAppFolder();

  52.     @Nonnull
  53.     public Path getLogFolder();

  54.     /*******************************************************************************************************************
  55.      *
  56.      * Sets the application name. This method must be called at boot from the {@code main} method before doing
  57.      * anything else.
  58.      *
  59.      * @param name    the property name
  60.      *
  61.      ******************************************************************************************************************/
  62.     public static void setAppName (@Nonnull final String name)
  63.       {
  64.         System.setProperty(PROP_APP_NAME, name);
  65.       }

  66.     /*******************************************************************************************************************
  67.      *
  68.      * Gets a property.
  69.      *
  70.      * @param <T>     the property type
  71.      * @param name    the property name
  72.      * @return        the property value
  73.      *
  74.      ******************************************************************************************************************/
  75.     @Nonnull
  76.     public <T> Optional<T> getProperty (@Nonnull Key<T> name);

  77.     /*******************************************************************************************************************
  78.      *
  79.      * Sets a property, overriding the current value.
  80.      *
  81.      * @param <T>     the property type
  82.      * @param name    the property name
  83.      * @param value   the property value
  84.      * @return        the property value
  85.      *
  86.      ******************************************************************************************************************/
  87.     public <T> void setProperty (@Nonnull final Key<T> name, @Nonnull final T value);

  88.     /**
  89.      *
  90.      * Sets a property, unless it has been already set.
  91.      *
  92.      * @param <T>     the property type
  93.      * @param name    the property name
  94.      * @param value   the property value
  95.      * @return        the property value
  96.      *
  97.      ******************************************************************************************************************/
  98.     public <T> void setDefaultProperty (@Nonnull final Key<T> name, @Nonnull final T value);

  99.     /*******************************************************************************************************************
  100.      *
  101.      * main() probably needs it and dI has not booted yet, so this class can be accessed also by this factory
  102.      * method. Note that Spring instantiates the bean by calling this method, so we really have a singleton.
  103.      *
  104.      ******************************************************************************************************************/
  105.     @Nonnull
  106.     public static PreferencesHandler getInstance()
  107.       {
  108.         synchronized (PreferencesHandler.class)
  109.           {
  110.             if (__INSTANCE.get() == null)
  111.               {
  112.                 __INSTANCE.set(new DefaultPreferencesHandler());
  113.               }

  114.             return __INSTANCE.get();
  115.           }
  116.       }
  117.   }