Class SizeConfig

  • All Implemented Interfaces:
    IPersistable

    public class SizeConfig
    extends Object
    implements IPersistable
    This class stores the size configuration of rows/columns within the NatTable. Mixed mode (fixed/percentage sizing):
    The mixed mode is only working if percentage sizing is enabled globally, and the fixed sized positions are marked separately.
    • Field Detail

      • PERSISTENCE_KEY_RESIZABLE_BY_DEFAULT

        public static final String PERSISTENCE_KEY_RESIZABLE_BY_DEFAULT
        See Also:
        Constant Field Values
      • PERSISTENCE_KEY_PERCENTAGE_SIZING_INDEXES

        public static final String PERSISTENCE_KEY_PERCENTAGE_SIZING_INDEXES
        See Also:
        Constant Field Values
      • PERSISTENCE_KEY_PERCENTAGE_SIZES

        public static final String PERSISTENCE_KEY_PERCENTAGE_SIZES
        Persistence key for percentage size configuration map.
        Since:
        1.6
        See Also:
        Constant Field Values
      • PERSISTENCE_KEY_DISTRIBUTE_REMAINING_SPACE

        public static final String PERSISTENCE_KEY_DISTRIBUTE_REMAINING_SPACE
        Persistence key for distributeRemainingSpace property.
        Since:
        1.6
        See Also:
        Constant Field Values
      • PERSISTENCE_KEY_DEFAULT_MIN_SIZE

        public static final String PERSISTENCE_KEY_DEFAULT_MIN_SIZE
        Persistence key for default min size property.
        Since:
        1.6
        See Also:
        Constant Field Values
      • PERSISTENCE_KEY_MIN_SIZES

        public static final String PERSISTENCE_KEY_MIN_SIZES
        Persistence key for min size configuration map.
        Since:
        1.6
        See Also:
        Constant Field Values
      • defaultSize

        protected int defaultSize
        The global default size of this SizeConfig.
      • defaultSizeMap

        protected final org.eclipse.collections.api.map.primitive.MutableIntIntMap defaultSizeMap
        Map that contains default sizes per column.
      • sizeMap

        protected final org.eclipse.collections.api.map.primitive.MutableIntIntMap sizeMap
        Map that contains sizes per column.
      • resizablesMap

        protected final org.eclipse.collections.api.map.primitive.MutableIntBooleanMap resizablesMap
        Map that contains the resizable information per row/column.
      • resizableByDefault

        protected boolean resizableByDefault
        The global resizable information of this SizeConfig.
      • percentageSizeMap

        protected final org.eclipse.collections.api.map.primitive.MutableIntDoubleMap percentageSizeMap
        Map that contains percentage values per position for percentage sizing.
        Since:
        1.6
      • percentageSizingMap

        protected final org.eclipse.collections.api.map.primitive.MutableIntBooleanMap percentageSizingMap
        Map that contains the percentage sizing information per row/column.
      • percentageSizing

        protected boolean percentageSizing
        Flag to tell whether the sizing is done for pixel or percentage values.
      • availableSpace

        protected int availableSpace
        The available space needed for percentage calculation on resizing.
      • realSizeMap

        protected final org.eclipse.collections.api.map.primitive.MutableIntIntMap realSizeMap
        Map that contains the real pixel size. Will only be used on percentage sizing. This map is not persisted as it will be calculated on resize.
      • aggregatedSizeCacheMap

        protected final org.eclipse.collections.api.map.primitive.MutableIntIntMap aggregatedSizeCacheMap
        Map that contains the cached aggregated sizes.
      • isAggregatedSizeCacheValid

        protected boolean isAggregatedSizeCacheValid
        Flag that indicates if the aggregated size cache is valid or if it needs to get recalculated.
    • Constructor Detail

      • SizeConfig

        public SizeConfig​(int defaultSize)
        Create a new SizeConfig with the given default size.
        Parameters:
        defaultSize - The default size to use.
    • Method Detail

      • saveState

        public void saveState​(String prefix,
                              Properties properties)
        Description copied from interface: IPersistable
        Saves the state to the given Properties using the specified prefix. Note: The prefix must be prepended to the property key to support multiple states within one Properties instance.
        Specified by:
        saveState in interface IPersistable
        Parameters:
        prefix - The prefix to use for the state keys. Is also used as the state configuration name.
        properties - The Properties instance to save the state to.
      • loadState

        public void loadState​(String prefix,
                              Properties properties)
        Description copied from interface: IPersistable
        Restore the state out of the given Properties identified by the specified prefix. Note: The prefix must be prepended to the property key to support multiple states within one Properties instance.
        Specified by:
        loadState in interface IPersistable
        Parameters:
        prefix - The prefix to use for the state keys. Is also used as the state configuration name.
        properties - The Properties instance to load the state from.
      • setDefaultSize

        public void setDefaultSize​(int size)
        Set the default size that should be used in case there is no position based size configured.
        Parameters:
        size - The default size to set.
      • getDefaultSize

        public int getDefaultSize()
        Returns:
        The default size that is used in case there is no position based size configured.
      • setDefaultSize

        public void setDefaultSize​(int position,
                                   int size)
      • getAggregateSize

        public int getAggregateSize​(int position)
      • getSize

        public int getSize​(int position)
      • getMinSize

        public int getMinSize​(int position)
        Returns the minimum size for the given position. If no specific value is configured for the given position, the default minimum size is returned.
        Parameters:
        position - The position for which the minimum size is requested.
        Returns:
        The minimum size for the given position.
        Since:
        1.6
        See Also:
        getDefaultMinSize()
      • setMinSize

        public void setMinSize​(int position,
                               int size)
        Set the minimum size for the given position. Will affect percentage sizing to avoid sizes smaller than the given minimum value.
        Parameters:
        position - The position for which the minimum size should be set.
        size - The minimum size for the given position.
        Throws:
        IllegalArgumentException - if size is less than 0.
        Since:
        1.6
      • getDefaultMinSize

        public int getDefaultMinSize()
        Returns:
        The default minimum size. Default value is 0.
        Since:
        1.6
      • setDefaultMinSize

        public void setDefaultMinSize​(int defaultMinSize)
        Set the default minimum size. Will affect percentage sizing to avoid sizes smaller than the given minimum value.
        Parameters:
        defaultMinSize - The default minimum size to use, can not be less than 0.
        Throws:
        IllegalArgumentException - if defaultMinSize is less than 0.
        Since:
        1.6
      • isMinSizeConfigured

        public boolean isMinSizeConfigured()
        Returns:
        true if the default min size or at least one position has a min size configured, false if no min size configuration is set.
        Since:
        1.6
      • isMinSizeConfigured

        public boolean isMinSizeConfigured​(int position)
        Parameters:
        position - The position for which it should be checked if a minimum size is configured.
        Returns:
        true if the given position has a minimum size configured or a default minimum size is configured, false if not
        Since:
        1.6
      • setSize

        public void setSize​(int position,
                            int size)
        Sets the given size for the given position. This method can be called manually for configuration via DataLayer and will be called on resizing within the rendered UI. This is why there is a check for percentage configuration. If this SizeConfig is configured to not use percentage sizing, the size is taken as is. If percentage sizing is enabled, the given size will be calculated to percentage value based on the already known pixel values.

        If you want to use percentage sizing you should use setPercentage(int, int) for manual size configuration to avoid unnecessary calculations.

        Parameters:
        position - The position for which the size should be set.
        size - The size in pixels to set for the given position.
      • setPercentage

        public void setPercentage​(int position,
                                  int percentage)
        Will set the given percentage size information for the given position and will set the given position to be sized via percentage value.
        Parameters:
        position - The positions whose percentage sizing information should be set.
        percentage - The percentage value to set, always dependent on the available space for percentage sizing, which can be less than the real available space in case there are also positions that are configured for fixed size.
      • setPercentage

        public void setPercentage​(int position,
                                  double percentage)
        Will set the given percentage size information for the given position and will set the given position to be sized via percentage value.
        Parameters:
        position - The positions whose percentage sizing information should be set.
        percentage - The percentage value to set, always dependent on the available space for percentage sizing, which can be less than the real available space in case there are also positions that are configured for fixed size.
        Since:
        1.6
      • isResizableByDefault

        public boolean isResizableByDefault()
        Returns:
        The global resizable information of this SizeConfig.
      • isPositionResizable

        public boolean isPositionResizable​(int position)
        Checks if there is a special resizable configuration for the given position. If not the global resizable information is returned.
        Parameters:
        position - The position of the row/column for which the resizable information is requested.
        Returns:
        true if the given row/column position is resizable, false if not.
      • setPositionResizable

        public void setPositionResizable​(int position,
                                         boolean resizable)
        Sets the resizable configuration for the given row/column position.
        Parameters:
        position - The position of the row/column for which the resizable configuration should be set.
        resizable - true if the given row/column position should be resizable, false if not.
      • setResizableByDefault

        public void setResizableByDefault​(boolean resizableByDefault)
        Sets the global resizable configuration. Will reset all special resizable configurations.
        Parameters:
        resizableByDefault - true if all rows/columns should be resizable, false if no row/column should be resizable.
      • isAllPositionsSameSize

        public boolean isAllPositionsSameSize()
      • isPercentageSizing

        public boolean isPercentageSizing()
        Returns:
        true if the size of at least one position is interpreted in percentage, false if the size of all positions is interpreted by pixel.
      • setPercentageSizing

        public void setPercentageSizing​(boolean percentageSizing)
        Configure whether the positions should be interpreted as percentage values or pixel values.

        Note: The configuration of this flag impacts the size calculation in mixed mode. If this flag is set to false, positions that are configured for fixed percentages will use the full available space for percentage calculation. Setting it to true will cause using the remaining space for percentage calculation. This means if also fixed pixel sized positions are configured, they will be subtracted from the full available space.

        Parameters:
        percentageSizing - true if the size of the positions should be interpreted as percentage values, false if the size of the positions should be interpreted by pixel.
      • isPercentageSizing

        public boolean isPercentageSizing​(int position)
        Checks if there is a special percentage sizing configuration for the given position. If not the global percentage sizing information is returned.
        Parameters:
        position - The position of the row/column for which the percentage sizing information is requested.
        Returns:
        true if the given row/column position is sized by percentage value, false if not.
      • setPercentageSizing

        public void setPercentageSizing​(int position,
                                        boolean percentageSizing)
        Sets the percentage sizing configuration for the given row/column position.
        Parameters:
        position - The position of the row/column for which the percentage sizing configuration should be set.
        percentageSizing - true if the given row/column position should be interpreted in percentage, false if not.
      • calculatePercentages

        public void calculatePercentages​(int space,
                                         int positionCount)
        Will calculate the real pixel values for the positions if percentage sizing is enabled.
        Parameters:
        space - The space that is available for rendering.
        positionCount - The number of positions that should be handled by this SizeConfig
      • calculateAvailableSpace

        protected int calculateAvailableSpace​(int space)
        Calculates the available space for percentage size calculation. This is necessary to support mixed mode of sizing, e.g. if two columns are configured to have fixed size of 50 pixels and one column that should take the rest of the available space of 500 pixels, the available space for percentage sizing is 400 pixels.
        Parameters:
        space - The whole available space for rendering.
        Returns:
        The available space for percentage sizing. Might be negative if the width of all fixed sized positions is greater than the available space.
      • correctPercentageValues

        protected int[] correctPercentageValues​(double sum,
                                                int positionCount)
        This method is used to correct the calculated percentage values in case a user configured more than 100 percent. In that case the set percentage values are scaled down to not exceed 100 percent.
        Parameters:
        sum - The sum of all configured percentage sized positions.
        positionCount - The number of positions to check.
        Returns:
        Integer array with the sum value at first position and the new calculated real pixel sum at second position in case a corrections took place. Will return null in case no correction happened.
        Since:
        1.6
      • updatePercentageValues

        public void updatePercentageValues​(int positionCount)
        Recalculate the percentage values for the given amount of columns. Needed for structural changes that aren't caused by a client are resize, e.g. adding a column.
        Parameters:
        positionCount - The number of columns that should be used to calculate the percentage values.
      • upScale

        public int upScale​(int value)
        Calculates the size value dependent on a possible configured scaling from pixel to DPI value.
        Parameters:
        value - The value that should be up scaled.
        Returns:
        The scaled value if a IDpiConverter is configured, the value itself if no IDpiConverter is set.
        Since:
        1.6
        See Also:
        IDpiConverter.convertPixelToDpi(int)
      • downScale

        public int downScale​(int value)
        Calculates the size value dependent on a possible configured scaling from DPI to pixel value.

        This method is used for percentage sizing calculations.

        Parameters:
        value - The value that should be down scaled.
        Returns:
        The scaled value if a IDpiConverter is configured, the value itself if no IDpiConverter is set.
        Since:
        1.6
        See Also:
        IDpiConverter.convertDpiToPixel(int)
      • setDpiConverter

        public void setDpiConverter​(IDpiConverter dpiConverter)
        Parameters:
        dpiConverter - The IDpiConverter to use for size scaling.
      • reset

        public void reset()
        Resets the configured size values held by this SizeConfig.
        Since:
        1.6
      • resetConfiguredSize

        public void resetConfiguredSize​(int position)
        Resets the custom configured size of the given position. This way the default size will be applied for that position afterwards.
        Parameters:
        position - The position for which the size configuration should be reset.
        Since:
        1.6
      • resetConfiguredMinSize

        public void resetConfiguredMinSize​(int position)
        Resets the custom configured min size of the given position. This way the default min size will be applied for that position afterwards.
        Parameters:
        position - The position for which the min size configuration should be reset.
        Since:
        1.6
      • getConfiguredSize

        public int getConfiguredSize​(int position)
        Returns the configured size value for the given position or -1 if no custom size was configured and therefore the default size is used for that position.
        Parameters:
        position - The position for which the configured size should be returned.
        Returns:
        The configured size or -1 in case the default size is used for that position.
        Since:
        1.6
      • getConfiguredPercentageSize

        public double getConfiguredPercentageSize​(int position)
        Returns the configured percentage size value for the given position or -1 if no custom percentage value was configured and therefore the size is dynamic for that position.
        Parameters:
        position - The position for which the configured size should be returned.
        Returns:
        The configured size or -1 in case the default size is used for that position.
        Since:
        1.6
      • getConfiguredMinSize

        public int getConfiguredMinSize​(int position)
        Returns the configured minimum size value for the given position or -1 if no custom minimum size was configured and therefore the default minimum size is used for that position.
        Parameters:
        position - The position for which the configured minimum size should be returned.
        Returns:
        The configured minimum size or -1 in case the default minimum size is used for that position.
        Since:
        1.6
      • isDistributeRemainingSpace

        public boolean isDistributeRemainingSpace()
        Returns:
        true if remaining space on fixed percentage sizing is distributed to other percentage sized positions, false if not. Default is false.
        Since:
        1.6
      • setDistributeRemainingSpace

        public void setDistributeRemainingSpace​(boolean distributeRemaining)
        Configure the percentage sizing behavior when manually specifying percentages and not having 100% configured. By default the remaining space is not distributed to the configured positions. That means for example that 25% of 100 pixels will be 25, regardless of the other positions. When setting this flag to true the 25% will be increased so the whole available space is filled.

        Note: For dynamic percentage sized positions this flag should also be set to true to avoid sizing issues because of rounding issues after resizing.

        Parameters:
        distributeRemaining - true if remaining space on fixed percentage sizing should be distributed to other percentage sized positions, false if not.
        Since:
        1.6
      • isFixPercentageValuesOnResize

        public boolean isFixPercentageValuesOnResize()
        Return whether percentage sized positions should be fixed on any resize or not. This means, if positions are configured for percentage sizing without a specific percentage value, the size is calculated based on the space that is still available. If this flag is set to false only the position that is resized will get a fixed value. The other positions will still be dynamic and therefore will also resize as the available space is changed. Setting this flag to true will cause that all positions with dynamic percentage configuration will get a fixed percentage value to have a deterministic resize behavior for the user that triggers the resize. Also percentage sized positions with a minimum size, where the minimum is bigger than the calculated percentage value will be recalculated to set the percentage value that matches the current state. Default is true.
        Returns:
        true if calculating the fix percentage value for dynamic percentage sized positions and position with a configured minimum on resize, false if the dynamic percentage sized positions stay dynamic on resize.
        Since:
        1.6
      • setFixPercentageValuesOnResize

        public void setFixPercentageValuesOnResize​(boolean enabled)
        Configure whether percentage sized positions should be fixed on any resize or not. This means, if positions are configured for percentage sizing without a specific percentage value, the size is calculated based on the space that is still available. If this flag is set to false only the position that is resized will get a fixed value. The other positions will still be dynamic and therefore will also resize as the available space is changed. Setting this flag to true will cause that all positions with dynamic percentage configuration will get a fixed percentage value to have a deterministic resize behavior for the user that triggers the resize. Also percentage sized positions with a minimum size, where the minimum is bigger than the calculated percentage value will be recalculated to set the percentage value that matches the current state. Default is true.
        Parameters:
        enabled - true to calculate the fix percentage value for dynamic percentage sized positions and position with a configured minimum on resize, false if the dynamic percentage sized positions should stay dynamic on resize.
        Since:
        1.6