PreferencesHandler.java

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

  28. import javax.annotation.Nonnull;
  29. import java.nio.file.Path;
  30. import java.util.Optional;
  31. import static it.tidalwave.role.impl.ServiceLoaderLocator.lazySupplierOf;

  32. /***********************************************************************************************************************
  33.  *
  34.  * @author  Fabrizio Giudici
  35.  * @since   3.2-ALPHA-17
  36.  *
  37.  **********************************************************************************************************************/
  38. public interface PreferencesHandler
  39.   {
  40.     static class Inner
  41.       {
  42.         private static final LazySupplier<PreferencesHandler> REF = lazySupplierOf(PreferencesHandler.class);
  43.       }

  44.     public static final String PROP_APP_NAME = PreferencesHandler.class.getPackage().getName() + ".appName";

  45.     // FIXME: make private as soon as the right Java version is required
  46.     public static final String __BASE_NAME = "it.tidalwave.javafx";

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

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

  51.     /*******************************************************************************************************************
  52.      *
  53.      ******************************************************************************************************************/
  54.     @Nonnull
  55.     public static PreferencesHandler getInstance()
  56.       {
  57.         return Inner.REF.get();
  58.       }

  59.     /*******************************************************************************************************************
  60.      *
  61.      ******************************************************************************************************************/
  62.     @Nonnull
  63.     public Path getAppFolder();

  64.     /*******************************************************************************************************************
  65.      *
  66.      ******************************************************************************************************************/
  67.     @Nonnull
  68.     public Path getLogFolder();

  69.     /*******************************************************************************************************************
  70.      *
  71.      * Sets the application name. This method must be called at boot from the {@code main} method before doing
  72.      * anything else.
  73.      *
  74.      * @param name    the property name
  75.      *
  76.      ******************************************************************************************************************/
  77.     public static void setAppName (@Nonnull final String name)
  78.       {
  79.         System.setProperty(PROP_APP_NAME, name);
  80.       }

  81.     /*******************************************************************************************************************
  82.      *
  83.      * Gets a property.
  84.      *
  85.      * @param <T>     the property type
  86.      * @param name    the property name
  87.      * @return        the property value
  88.      *
  89.      ******************************************************************************************************************/
  90.     @Nonnull
  91.     public <T> Optional<T> getProperty (@Nonnull Key<T> name);

  92.     /*******************************************************************************************************************
  93.      *
  94.      * Sets a property, overriding the current value.
  95.      *
  96.      * @param <T>     the property type
  97.      * @param name    the property name
  98.      * @param value   the property value
  99.      * @return        the property value
  100.      *
  101.      ******************************************************************************************************************/
  102.     public <T> void setProperty (@Nonnull final Key<T> name, @Nonnull final T value);

  103.     /**
  104.      *
  105.      * Sets a property, unless it has been already set.
  106.      *
  107.      * @param <T>     the property type
  108.      * @param name    the property name
  109.      * @param value   the property value
  110.      * @return        the property value
  111.      *
  112.      ******************************************************************************************************************/
  113.     public <T> void setDefaultProperty (@Nonnull final Key<T> name, @Nonnull final T value);
  114.   }