Class GridColumnScanner<T>

java.lang.Object
org.dellroad.stuff.vaadin24.grid.GridColumnScanner<T>
Type Parameters:
T - Java class to be introspected

public class GridColumnScanner<T> extends Object
Scans a Java class hierarchy for @GridColumn annotated getter methods, allowing for declarative auto-generation of Grid.Columns.

See @GridColumn for an example of usage.

See Also:
  • Constructor Details

  • Method Details

    • getType

      public Class<T> getType()
      Get the type associated with this instance.
      Returns:
      backing object type
    • getColumnMap

      Get the annotations found through introspection indexed by column key.

      This represents static information gathered by this instance by scanning the class hierarchy during construction.

      The returned map is mutable, e.g., if you delete unwanted entries then buildGrid() will skip them.

      Returns:
      annotations keyed by column key, and sorted based on GridColumn.order(), then key
    • buildGrid

      public Grid<T> buildGrid()
      Create a new Grid with columns generated from @GridColumn annotations.

      No columns are included for bean properties that are not annotated.

      During the execution of this method, in particular when any custom Renderer, ValueProvider, etc., classes are being instantiated, the Grid being configured is available via currentGrid().

      Returns:
      newly built Grid with auto-generated columns
    • addColumnsTo

      public void addColumnsTo(Grid<?> grid)
      Augment the given Grid with columns auto-generated from @GridColumn annotations.

      This method applies to target Grids with any model type and gracefully handles mismatches: annotated methods declared in Java types that are not supertype of grid's model type always "return" null.

      This allows populating a Grid with columns even when only some subtypes of the model type can actually provide values for those columns.

      Any existing columns with conflicting column keys will be replaced.

      During the execution of this method, in particular when any custom Renderer, ValueProvider, etc., classes are being instantiated, the supplied grid is available via currentGrid().

      Parameters:
      grid - target grid
      Throws:
      IllegalArgumentException - if grid is null
    • addVisbilityMenuItems

      public boolean addVisbilityMenuItems(Grid<?> grid, HasMenuItems menu)
      Add menu items that enable/disable the visibility of individual columns for for which visibilityMenu() was true to the given menu.

      This method adds menu items for all columns in the given grid for which visibilityMenu() was true, according to the most recent call to buildGrid() or addColumnsTo().

      The menu item labels come from header(), if any, otherwise key() via SharedUtil.camelCaseToHumanFriendly().

      To use the menu, the caller will need to assign it to some target, e.g., via ContextMenuBase.setTarget(com.vaadin.flow.component.Component).

      Parameters:
      grid - grid containing previously scanned columns
      menu - the menu to add visibility items to
      Returns:
      true if any menu items were added, otherwise false
      Throws:
      IllegalArgumentException - if either parameter is null
    • addColumn

      public static <T> Grid.Column<T> addColumn(Grid<T> grid, String key, GridColumn annotation, String description, ValueProvider<T,?> valueProvider, boolean selfRendering)
      Add and configure a single column to the given Grid.

      During the execution of this method, in particular when any custom Renderer, ValueProvider, etc., classes are being instantiated, the supplied grid is available via currentGrid().

      Type Parameters:
      T - underlying bean type
      Parameters:
      grid - target Grid
      key - the column's unique column key
      annotation - @GridColumn annotation
      description - description of what we're configuring (for debug purposes)
      valueProvider - ValueProvider providing the return value from the annotated method, or null if the annotated method returns void or Void
      selfRendering - true if the annotated method (and therefore valueProvider) returns a Component
      Returns:
      newly added column
      Throws:
      IllegalArgumentException - if any parameter is null
    • currentGrid

      public static Grid<?> currentGrid()
      Obtain the Grid being configured in the current thread.

      This method only works when the current thread is executing in buildGrid(), addColumn(), or addColumnsTo().

      Returns:
      current Grid being configured
      Throws:
      IllegalStateException - if no Grid is currently being configured by this class
    • determineColumnKey

      protected String determineColumnKey(MethodAnnotationScanner<T,GridColumn>.MethodInfo methodInfo)
      Determine the column key from the annotation.
      Parameters:
      methodInfo - method info
      Returns:
      property name