Module org.autogui

Class TextCellRenderer<ValueType>

Type Parameters:
ValueType - the type of cell values
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, ListCellRenderer<ValueType>, TableCellRenderer
Direct Known Subclasses:
GuiSwingLogEntryException.ExceptionMessageRenderer, GuiSwingLogEntryException.ExceptionStackTraceRenderer, GuiSwingLogEntryProgress.ProgressMessageRenderer, GuiSwingLogEntryProgress.ProgressStatusRenderer, GuiSwingLogEntryString.GuiSwingLogStringRenderer

public class TextCellRenderer<ValueType> extends JPanel implements TableCellRenderer, ListCellRenderer<ValueType>
multi-line text cell renderer for JList and JTable
See Also:
  • Field Details

    • selected

      protected boolean selected
    • selectionStart

      protected int selectionStart
    • selectionEnd

      protected int selectionEnd
    • lines

      protected List<TextCellRenderer.LineInfo> lines
    • testImage

      protected BufferedImage testImage
    • baseWidth

      protected float baseWidth
    • baseHeight

      protected float baseHeight
    • value

      protected ValueType value
    • forMouseEvents

      protected boolean forMouseEvents
    • text

      protected String text
    • findText

      protected String findText
    • findPattern

      protected Pattern findPattern
    • maxWidth

      protected int maxWidth
    • lastMatch

      protected TextCellRenderer.LineInfoMatch lastMatch
    • selectionForeground

      protected Color selectionForeground
    • selectionBackground

      protected Color selectionBackground
    • originalBackground

      protected Color originalBackground
      Since:
      1.2
    • textToBackToColor

      protected Map<Color,Map<Color,Color>> textToBackToColor
  • Constructor Details

    • TextCellRenderer

      public TextCellRenderer()
  • Method Details

    • initColor

      protected void initColor()
    • initBorder

      protected void initBorder()
    • getText

      public String getText()
    • getTextSelection

      public String getTextSelection()
    • getValue

      public ValueType getValue()
    • getLines

      public List<TextCellRenderer.LineInfo> getLines()
    • getFindPattern

      public Pattern getFindPattern()
    • getListCellRendererComponent

      public Component getListCellRendererComponent(JList<? extends ValueType> list, ValueType value, int index, boolean isSelected, boolean cellHasFocus)
      Specified by:
      getListCellRendererComponent in interface ListCellRenderer<ValueType>
    • setProperty

      public void setProperty(JList<?> list)
    • setProperty

      public void setProperty(JTable list)
    • setBackground

      public void setBackground(Color bg)
      Overrides:
      setBackground in class JComponent
    • setCellDefaultProperties

      public static void setCellDefaultProperties(JComponent cellComponent)
      setting properties for renderers
      Parameters:
      cellComponent - the target component
      Since:
      1.6
    • setCellListColor

      public static void setCellListColor(JList<?> list, JComponent component, boolean isSelected, boolean hasFocus, int row)
      Parameters:
      list - the target list, nullable
      component - the cell component
      isSelected - true if the cell is selected
      hasFocus - true if the cell has focus
      row - the row index of the cell
      Since:
      1.2
    • setCellListColorDropTarget

      public static boolean setCellListColorDropTarget(JList<?> list, JComponent component, int row)
      Parameters:
      list - the list
      component - the cell component
      row - the row index
      Returns:
      true if drop target
      Since:
      1.2
    • setCellTableColor

      public static void setCellTableColor(JTable table, JComponent component, boolean isSelected, boolean hasFocus, int row, int column)
      Parameters:
      table - the target table, nullable
      component - the cell component
      isSelected - true if the cell is selected
      hasFocus - true if the cell has focus
      row - the row index of the cell
      column - the column index of the cell
      Since:
      1.2
    • setCellTableColorDropTarget

      public static boolean setCellTableColorDropTarget(JTable table, JComponent component, int row, int column)
      Parameters:
      table - the table
      component - the cell component
      row - the row index
      column - the column index
      Returns:
      true if drop target
      Since:
      1.2
    • setCellTableBorderWithMargin

      public static void setCellTableBorderWithMargin(JTable table, JComponent cell, boolean isSelected, boolean hasFocus, int row, int column)
           ----------------------------
           |  matte(alternativeColor) top=1, left=(2 or 0), right=(2 or 0), bottom=2
           |   matte(cell.background)   top=1, left=10, right=5, bottom=1
           |    empty top=10, left=10, right=10, bottom=5
           |      [cell]
       
      Parameters:
      table - the table
      cell - the target cell
      isSelected - true if the cell is selected
      hasFocus - true if the cell has focus
      row - the row index of the cell
      column - the column index of the cell
      Since:
      1.6
    • setCellTableBorder

      public static boolean setCellTableBorder(JTable table, JComponent cell, boolean isSelected, boolean hasFocus, int row, int column)
      the method totally replacing the border with a new one when it returns true.
           --------------------------------
           |       empty top=7, left=8, right=8, bottom=8
           |         [cell]
           |    ...
           ---------------------------------------
        
      Currently, the true case is only the macOS SystemLAF.
      Parameters:
      table - the table
      cell - the target cell
      isSelected - true if the cell is selected
      hasFocus - true if the cell has focus
      row - the row index of the cell
      column - the column index of the cell
      Returns:
      true if succeeded
      Since:
      1.2
    • createCellTableBorderOuter

      public static Border createCellTableBorderOuter(JTable table, int column, Color color)
           --------------------------------
           |    matte(color) top=1, left=(2 or 0), right=(2 or 0), bottom=2 //changed by the column index
           |     [cell]
           |
           ---------------------------------------
        
      Parameters:
      table - the table
      column - the target column
      color - the border color
      Returns:
      matte border
      Since:
      1.6
    • isCellTableEnd

      public static boolean isCellTableEnd(JTable table, int column, boolean left)
      Parameters:
      table - the table
      column - the target column
      left - the checked end
      Returns:
      true if index == 0 and left, or index is count-1 and !left
      Since:
      1.6
    • setCellTableMargin

      public static void setCellTableMargin(JComponent cell)
           ------------------------
           |  existing-border
           |   empty top=7, left=8, right=8, bottom=8
           |    [cell]
       
      Parameters:
      cell - the target cell
      Since:
      1.6
    • getGridColor

      public static Color getGridColor(Color background)
      modify the background color for grid-separator: a bright color becomes darker, a dark color becomes brighter
      Parameters:
      background - the color obtained from Component.getBackground()
      Returns:
      the modified color
      Since:
      1.6
    • wrapBorder

      public static void wrapBorder(JComponent component, Border border, boolean inside)
      Parameters:
      component - the border holder
      border - the appended border
      inside - if true the border will be the inside of the existing border
      Since:
      1.6
    • createBorder

      public static Border createBorder(int top, int left, int bottom, int right)
      the sizes of the border will be scaled; if non 0 size is specified, the minimum becomes 1.
      Parameters:
      top - the top size
      left - the left size
      bottom - the bottom size
      right - the right size
      Returns:
      empty-border
      Since:
      1.6
    • createBorder

      public static Border createBorder(int top, int left, int bottom, int right, Color background)
      the sizes of the border will be scaled; if non 0 size is specified, the minimum becomes 1.
      Parameters:
      top - the top size
      left - the left size
      bottom - the bottom size
      right - the right size
      background - the border color
      Returns:
      matte-border
      Since:
      1.6
    • getCellBackground

      public static Color getCellBackground(JComponent component, boolean isTable, int row)
      Parameters:
      component - the table or list
      isTable - true if the component is a table
      row - the row index
      Returns:
      background color for the row
      Since:
      1.2
    • setCellBorderDefault

      public static void setCellBorderDefault(boolean isTable, JComponent cell, boolean isSelected, boolean hasFocus)
      Parameters:
      isTable - true if a table cell
      cell - the cell component
      isSelected - the cell is selected
      hasFocus - the cell has focus
      Since:
      1.2
    • paintRowsBorderSelection

      public static void paintRowsBorderSelection(JTable table, Graphics2D g2)
      draw additional round highlighting rectangles for selected rows. the method can be called after execution of JComponent.paintComponent(Graphics)
      Parameters:
      table - the target table
      g2 - the target context
      Since:
      1.6
    • getTableCellRendererComponent

      public Component getTableCellRendererComponent(JTable tableNullable, Object value, boolean isSelected, boolean hasFocus, int row, int column)
      Specified by:
      getTableCellRendererComponent in interface TableCellRenderer
    • setValue

      public boolean setValue(ValueType value, boolean forMouseEvents)
      Parameters:
      value - a new value
      forMouseEvents - setSelectionFromValue(Object) will be skipped
      Returns:
      true if updated
    • isValueSame

      public boolean isValueSame(ValueType value, boolean forMouseEvents)
    • format

      public String format(ValueType value)
      Parameters:
      value - the source value
      Returns:
      the formatted line: should be processed by formatPreProcess(String)
    • buildFromValue

      public void buildFromValue()
    • buildLines

      public int buildLines(String text, List<TextCellRenderer.LineInfo> lines)
      Parameters:
      text - source text
      lines - cleared and appended
      Returns:
      max size of TextCellRenderer.LineInfo.getWidth() in the lines
    • formatPreProcess

      public String formatPreProcess(String line)
      remove tabs from the line; for avoiding displaying tofu by AttributedString
      Parameters:
      line - a line string without newlines
      Returns:
      remove tabs
      Since:
      1.2
    • createLine

      public TextCellRenderer.LineInfo createLine(TextCellRenderer.LineInfo prevLine, int lineIndex, int start, String line)
      create a line-info
      Parameters:
      prevLine - a previous line-info or null for first line
      lineIndex - the line number index
      start - the start position of the line
      line - the line string without new-line
      Returns:
      a created line info for the line
    • buildSize

      public float[] buildSize()
      multiply maxWidth set by buildFromValue() and size of lines by base char size (getBaseSize()) , and border size by getBorderSize()
      Returns:
      {width, height}
    • setSelectionFromValue

      public void setSelectionFromValue(ValueType value)
      in the class, just call setFindHighlights()
      Parameters:
      value - the new value
    • clearSelectionRange

      public void clearSelectionRange()
    • setSelectionRange

      public void setSelectionRange(int from, int to)
    • setFindHighlights

      public int setFindHighlights()
      constructs find-ranges for each line matched by findPattern. thus, updateFindPattern(String) or updateFindPattern(Pattern) needs to be called before.
      Returns:
      number of matched portions
    • updateFindPattern

      public boolean updateFindPattern(Pattern newPattern)
      update findPattern. findText becomes null
      Parameters:
      newPattern - a new pattern
      Returns:
      true if updated.
    • updateFindPattern

      public boolean updateFindPattern(String findKeyword)
      update findText and findPattern
      Parameters:
      findKeyword - a new string, nullable
      Returns:
      true if the string is updated. if true, it needs to be called setFindHighlights()
    • getFocusNextFound

      public Object getFocusNextFound(ValueType value, Object prevIndex, boolean forward)
      focus next matched range and return it.
             m = null;
             m = getFocusNextFound(v, m, true);
             m = getFocusNextFound(v, m, true);
             ...
         
      Parameters:
      value - the value to be set
      prevIndex - the previous focus object or initially null
      forward - true for forward or false for backward
      Returns:
      a focus object TextCellRenderer.LineInfoMatch or null
    • mouseUpdateForComposition

      @SafeVarargs public static <V> void mouseUpdateForComposition(V value, Map<TextCellRenderer<?>,int[]> ranges, boolean pressInit, Component pointComponent, Point point, TextCellRenderer<V>... rs)
      change selection range by mouse pressing, dragging or releasing. each rs values will be updated by setValue(Object, boolean) with forMouseEvents=true. update map entries of range and selection-range of rs by setSelectionRange(int, int)
      Type Parameters:
      V - the type of cell values
      Parameters:
      value - the value to be set for each rs
      ranges - updated ranges
      pressInit - if true, it means first pressing and clear ranges and updates both "from" and "to". otherwise just updates "to"
      pointComponent - the coordinate base of point
      point - a point in pointComponent
      rs - components within pointComponent
    • updateFindPatternForComposition

      @SafeVarargs public static <V> boolean updateFindPatternForComposition(String findKeyword, TextCellRenderer<V>... rs)
    • findTextForComposition

      @SafeVarargs public static <V> int findTextForComposition(V value, String findKeyword, TextCellRenderer<V>... rs)
    • getFocusNextFoundForComposition

      @SafeVarargs public static <V> Object getFocusNextFoundForComposition(V value, Object prevIndex, boolean forward, TextCellRenderer<V>... rs)
    • setValueForComposition

      @SafeVarargs public static <V> void setValueForComposition(V value, Map<TextCellRenderer<?>,int[]> rangesOpt, boolean forMouseEvents, TextCellRenderer<V>... rs)
    • getSelectedTextForComposition

      @SafeVarargs public static <V> String getSelectedTextForComposition(V value, boolean entireText, Map<TextCellRenderer<?>,int[]> rangesOpt, TextCellRenderer<V>... rs)
    • getBaseSize

      public float[] getBaseSize()
    • setDefaultAttributes

      protected void setDefaultAttributes(AttributedString a, int len)
    • getBorderSize

      public int[] getBorderSize()
    • getSelectionBackground

      public Color getSelectionBackground()
    • getSelectionForeground

      public Color getSelectionForeground()
    • setSelectionBackground

      public void setSelectionBackground(Color selectionBackground)
    • setSelectionForeground

      public void setSelectionForeground(Color selectionForeground)
    • getLineFindColor

      public Color getLineFindColor()
    • getLineFindMatchColor

      public Color getLineFindMatchColor()
    • paintOrLayoutComponentLines

      protected float[] paintOrLayoutComponentLines(Graphics g, boolean paint)
      Parameters:
      g - the target graphics
      paint - if true, do drawing. if false, it just calculates the size
      Returns:
      width and height
      Since:
      1.2
    • paintComponent

      protected void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
    • paintSetUpGraphics

      public void paintSetUpGraphics(Graphics2D g2)
    • paintLineSelection

      public void paintLineSelection(Graphics2D g2, TextCellRenderer.LineInfo line, TextLayout l, Color selectionColor, float lineX)
    • paintLineFinds

      public void paintLineFinds(Graphics2D g2, TextCellRenderer.LineInfo line, TextLayout l, int lineIndex, Color findColor, Color findMatchColor, float lineX)
    • isLastMatch

      public boolean isLastMatch(int lineIndex, int rangeIndex)
    • paintStartX

      public float paintStartX(int lineIndex, TextCellRenderer.LineInfo prev, float prevX, TextCellRenderer.LineInfo line, TextLayout l, FontRenderContext frc)
    • paintCellSelection

      public void paintCellSelection(Graphics g, Color selectionColor)
    • getIndex

      public int getIndex(Point point)
      Parameters:
      point - the component coordinate
      Returns:
      the character index for the point