Class DPIHelper

java.lang.Object
org.vanted.scaling.DPIHelper

public class DPIHelper
extends Object
An utility helper that takes care of processing the DPI from a provided value (Slider value) and is responsible for managing the scaling preferences among some other useful functions in hand.
Author:
D. Garkov
  • Field Summary

    Fields
    Modifier and Type Field Description
    static boolean PREFERENCES_GET
    Specify when you get Preferences value.
    static boolean PREFERENCES_SET
    Specify when you set Preferences value.
    static int STANDARD_VALUE
    Standard value, when no scaling is to be performed.
    static int VALUE_DEFAULT
    Default value, when not setting any particular value.
    static int VALUE_UNSET_INTERNAL
    Internal value for checking if any values are stored.
  • Constructor Summary

    Constructors
    Constructor Description
    DPIHelper()  
  • Method Summary

    Modifier and Type Method Description
    static void addScalingComponents​(AbstractOptionPane pane, Container main, int min, int max)
    Insert the preferencing Components into the specified pane.
    static void adjustWindowDecoratations()
    By transferring all Window decorations from OS to LookAndFeel, those could be now scaled too.
    void displayLifesaver()
    Displays a reset dialog that prompts the user to reset the scaling or not, when the previous setting has gone out of reasonable scaling bounds and proper viewing has become impossible.
    static void flushPreferences()
    Write preferences to disk.
    static DPIHelper getInstance()  
    static boolean getLifesaverBoolean()  
    static void initWindowResizer()
    Attaches a WindowResizer listener that will resize any newly opened windows.
    static boolean isAvoidable()
    Test whether scaling is currently necessary.
    static void loadPane()
    Load the Preferences Pane.
    static Preferences loadPreferences​(Class<?> clazz)
    Fetches the respective Preferences for clazz.
    static URL loadResource​(Class<?> clazz, String filename)
    A resource loading utility method for resources placed in the resource package.
    static int managePreferences​(int val, boolean get)
    This is a two-way internal method, used for all scaling-only preferences-related procedures.
    static float processEmulatedDPIValue​(int sValue)
    Converts a slider value into an Emulated DPI value.
    static void putLifesaverValue​(boolean value)  
    static int scaleCssPixels​(int pixels)
    The CSS pixel px tries to match the reference pixel, which depends on screen DPI and viewer distance from screen.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • STANDARD_VALUE

      public static final int STANDARD_VALUE
      Standard value, when no scaling is to be performed. Useful for resetting.
    • VALUE_DEFAULT

      public static final int VALUE_DEFAULT
      Default value, when not setting any particular value.
      See Also:
      Constant Field Values
    • VALUE_UNSET_INTERNAL

      public static final int VALUE_UNSET_INTERNAL
      Internal value for checking if any values are stored.
      See Also:
      Constant Field Values
    • PREFERENCES_GET

      public static final boolean PREFERENCES_GET
      Specify when you get Preferences value.
      See Also:
      Constant Field Values
    • PREFERENCES_SET

      public static final boolean PREFERENCES_SET
      Specify when you set Preferences value.
      See Also:
      Constant Field Values
  • Constructor Details

    • DPIHelper

      public DPIHelper()
  • Method Details

    • getInstance

      public static DPIHelper getInstance()
    • processEmulatedDPIValue

      public static float processEmulatedDPIValue​(int sValue)
      Converts a slider value into an Emulated DPI value. We firstly determine standard constants and we use in the process the default slider values. For different than default, one should initialize a ScalingSlider with such new values.
      Parameters:
      sValue - sliderValue (get it from Preferences)
      Returns:
      float DPI value
    • isAvoidable

      public static boolean isAvoidable()
      Test whether scaling is currently necessary.
      Returns:
      true if Scaling could be skipped.
    • addScalingComponents

      public static void addScalingComponents​(AbstractOptionPane pane, Container main, int min, int max)
      Insert the preferencing Components into the specified pane.
      Parameters:
      pane - to add into
    • scaleCssPixels

      public static int scaleCssPixels​(int pixels)
      The CSS pixel px tries to match the reference pixel, which depends on screen DPI and viewer distance from screen. Here we obtain DPPX - the device pixel ratio. Thus we map pixels to provided number of pixels.
      Parameters:
      pixels -
      Returns:
    • initWindowResizer

      public static void initWindowResizer()
      Attaches a WindowResizer listener that will resize any newly opened windows. This does not resize any already visible windows.
    • displayLifesaver

      public void displayLifesaver()
      Displays a reset dialog that prompts the user to reset the scaling or not, when the previous setting has gone out of reasonable scaling bounds and proper viewing has become impossible. There is also a 'Disable' option.
    • getLifesaverBoolean

      public static boolean getLifesaverBoolean()
    • putLifesaverValue

      public static void putLifesaverValue​(boolean value)
    • managePreferences

      public static int managePreferences​(int val, boolean get)
      This is a two-way internal method, used for all scaling-only preferences-related procedures. It handles both setting/putting and getting of values. To set values use DPIHelper.PREFERENCES_SET as get parameter. To get: DPIHelper.PREFERENCES_GET, respectively.
      Parameters:
      val - Our new ScalingSlider value. Used only, when putting into (i.e. get is false). Meaning when querying values, you could just use DPIHelper.VALUE_DEFAULT.
      get - When true, it returns the previously stored value.
      Returns:
      Stored value under 'Scaling Preferences' or DPIHelper.VALUE_DEFAULT for potential error checking, if get is false.
    • flushPreferences

      public static void flushPreferences() throws BackingStoreException
      Write preferences to disk.
      Throws:
      BackingStoreException
    • loadPane

      public static void loadPane()
      Load the Preferences Pane.
    • adjustWindowDecoratations

      public static void adjustWindowDecoratations()
      By transferring all Window decorations from OS to LookAndFeel, those could be now scaled too. Should be called before initialization of any JFrames or JDialogs, since it affects only subsequently created ones.
    • loadResource

      public static URL loadResource​(Class<?> clazz, String filename)
      A resource loading utility method for resources placed in the resource package.
      Parameters:
      clazz - delegates to the respective class loader
      filename - the resource name
      Returns:
      the loaded resource
    • loadPreferences

      public static Preferences loadPreferences​(Class<?> clazz)
      Fetches the respective Preferences for clazz.
      Parameters:
      clazz -
      Returns:
      preferences instance