Class ComponentRegulator

java.lang.Object
org.vanted.scaling.ComponentRegulator

public class ComponentRegulator
extends Object
Notice: it is advisable to not access it directly, but through the provided ScalingCoordinator, because of factor conversions. This regulator is responsible for delegating the scaling external to the LAF-defaults aspects of Swing components. It could be used for any user-set modifications that have overwritten the LAF defaults. A typical example, are any specified icons. Those are extracted from the component tree and rescaled accordingly (see ComponentScaler and its subtypes).
Author:
D. Garkov
  • Constructor Details

    • ComponentRegulator

      public ComponentRegulator​(float scaleFactor)
      Constructs a ComponentRegulator instance for upcoming component scaling.
      Parameters:
      scaleFactor - the working scaling factor, a ratio between current actual DPI and to be emulated DPI.
  • Method Details

    • init

      public void init​(Container container) throws OutOfMemoryError
      Global system DPI scaling init! Scan and scale the components specifics. This are taken from the specified parent Container container.

      Important: Register any newly implemented component scalers before calling this method! (see: ComponentRegulator#registerNewScaler()}

      Parameters:
      container - , application main frame/window/container
      Throws:
      OutOfMemoryError - when the allocated heap memory is not enough to hold all largely scaled components, such as Icons.
    • scaleComponentsOf

      public void scaleComponentsOf​(Container container)
      Recursively iterates the component tree and initiate scaling for any encountered JComponents.

      Important: Default behaviour is to first run system scaling and only then any other proxy scalers, to change this, call ComponentRegulator#override().

      Parameters:
      container - the container to crawl
    • scaleComponentsOf

      public void scaleComponentsOf​(Component[] components)
      Parameters:
      container - the container to crawl
    • scaleComponentsOf

      public void scaleComponentsOf​(Container container, boolean checkScaled, boolean mark)
      Override of scaleComponentsOf(Container), allowing for checking whether Containers components are scaled and only when those aren't then scaling, and additionally marking them as such or not. scaleComponentsOf(Container) doesn't check and marks all.

      Important: Default behaviour is to first run system scaling and only then any other proxy scalers, to change this, call ComponentRegulator#override().

      Parameters:
      container -
      checkScaled - true to check components for being scaled and avoid double scaling
      mark - true to mark any scaled components as such
    • scaleComponentsOf

      public void scaleComponentsOf​(Component[] components, boolean check, boolean mark)
      Parameters:
      components -
      check - true to check components for being scaled and avoid double scaling
      mark - true to mark any scaled components as such
    • overrideForAll

      public static void overrideForAll()
      Override default behaviour to run proxy scalers before the application / init DPI scaler.
    • scaleHTMLComponent

      public static void scaleHTMLComponent​(JComponent component)
      Similar to conduct(JComponent), but for HTML scaling. It crawls the list of scalers and matches the parameter to its appropriate scaler. It is responsibility of the matched scaler to have implemented such method.

      Note: No need to explicitly call this method, since it is part of the init(Container) workflow. Useful when implementing HTML support for a new scaler.

      Parameters:
      component -
      See Also:
      HTMLScaleSupport
    • addScaledComponent

      public static void addScaledComponent​(JComponent component)
      Add the component to the set of already scaled components.
      Parameters:
      component - to be stored component
    • registerNewScaler

      public static void registerNewScaler​(Class<?> superclass, ComponentScaler scaler)
      Registers new ComponentScaler to use. Does not overwrite previous mappings and just returns. Please, use before init(Container).

      When adding new scalers be aware of the supertype-subtype relationship between the new and the already provided ones. In other words, the more generic the type, further back in the list should be placed.

      Parameters:
      superclass - the super class, or possibly even direct class for overriding purposes.
      scaler - the newly implemented ComponentScaler
    • setFactor

      public void setFactor​(float factor)
    • getFactor

      public float getFactor()
    • isScaled

      public static boolean isScaled​(JComponent component)
      A rough measure to determine whether component had one of his scaling specifics - Font, Icon, Insets; scaled.
      Parameters:
      component - to be checked at runtime
      Returns:
      true if the component has been scaled
    • clearModifiedPool

      public void clearModifiedPool()
      Reset the supporting data structure for reuse.
    • clearScalersMap

      public void clearScalersMap()
      Reset the supporting data structure for reuse.
    • isModifiedPoolRefilled

      public static boolean isModifiedPoolRefilled()
      True, when the modified pool has been recently cleared and re-created. Also tells us whether a new scaling has been performed at runtime.

      Useful for reseting a DPI value that is being kept in the caller class.

      Returns:
      true when the pool with scaled components has been refilled. Once a pool has been refilled (i.e true), it changes its state effectively to filled (i.e false). Not filled also returns false.
      See Also:
      Toolbox.wasScalingPerformed()