Class SwatchGridCanvas

All Implemented Interfaces:
Drawable

public class SwatchGridCanvas extends Canvas
A grid of color cells on a Canvas (CC-COLOR-CHOOSER-SWT-PANELS, 2026-10-10): the swatch palette and the recent colors of the Swatches tab. A cell of value zero is empty: painted with the background, without tooltip, and picking it does nothing.

Mouse button 1 picks a cell. The keyboard moves a visible focus cell with the arrow keys (Home and End to the ends of the row), Space picks the focus cell, and Enter picks it too and leaves the Return traversal to the chooser, which accepts; Tab leaves the grid. The tooltip of a cell is #RRGGBB r,g,b. Sizes are in points, scaled by SWT. The background and the shade lines of the cells are the theme's field background and border colors (ChooserColors, CC-FIX-COLOR-CHOOSER-DARK); the cells are the real colors.

Author:
Christopher Mindus
  • Constructor Details

    • SwatchGridCanvas

      public SwatchGridCanvas(Composite parent, int columns, int rows, IntUnaryOperator colorAt, ChooserColors colors)
      Creates the grid.
      Parameters:
      parent - The parent.
      columns - The columns, at least 1.
      rows - The rows, at least 1.
      colorAt - The color of a cell by row-major index, zero for empty.
      colors - The theme colors: the field background behind the cells, the border for the shade lines of a cell.
  • Method Details

    • getColumns

      public int getColumns()
      Gets the columns.
    • getRows

      public int getRows()
      Gets the rows.
    • getCellSize

      public int getCellSize()
      Gets the cell size in points.
    • setCellSize

      public void setCellSize(int size)
      Sets the cell size in points, at least ChooserMetrics.MIN_CELL; the parent must lay out again.
    • computeSize

      public Point computeSize(int wHint, int hHint, boolean changed)
      Overrides:
      computeSize in class Composite
    • sizeFor

      public Point sizeFor(int size)
      Gets the size of the grid at a cell size: the cells, the gaps and the margin for the focus ring, in points.
      Parameters:
      size - The cell size in points.
    • cellAt

      public int cellAt(int x, int y)
      Gets the cell index at a point of the canvas, -1 outside the cells.
    • cellBounds

      public Rectangle cellBounds(int index)
      Gets the bounds of a cell in the canvas.
    • cellColor

      public int cellColor(int index)
      Gets the color of a cell, zero for an empty cell.
      Parameters:
      index - The row-major index.
    • getFocusCell

      public int getFocusCell()
      Gets the focus cell: the one the keyboard acts on.
    • setFocusCell

      public void setFocusCell(int index)
      Sets the focus cell, clamped to the grid.
    • addPickListener

      public void addPickListener(SwatchGridCanvas.IPickListener listener)
      Adds a pick listener.
    • removePickListener

      public void removePickListener(SwatchGridCanvas.IPickListener listener)
      Removes a pick listener.
    • pick

      public boolean pick(int index)
      Picks a cell: an empty or invalid cell does nothing.
      Parameters:
      index - The row-major index.
      Returns:
      true if a color was picked.
    • pickFocusCell

      public boolean pickFocusCell()
      Picks the focus cell (Space, Enter).
      Returns:
      true if a color was picked.