Class SwingUtils

java.lang.Object
uno.anahata.asi.swing.internal.SwingUtils

public class SwingUtils extends Object
A collection of general-purpose Swing utility methods, primarily for image manipulation and UI component creation.

This utility provides high-fidelity component discovery and specialized event redispatching to ensure a seamless interaction between nested host-assembled frames and the core conversation UI.

Author:
anahata
  • Field Details

    • THUMBNAIL_MAX_SIZE

      private static final int THUMBNAIL_MAX_SIZE
      The authoritative maximum dimension (width or height) for generated thumbnails to ensure UI responsiveness.
      See Also:
  • Constructor Details

    • SwingUtils

      public SwingUtils()
  • Method Details

    • createThumbnail

      public static Image createThumbnail(BufferedImage original)
      Creates a scaled thumbnail image from an original BufferedImage, maintaining the aspect ratio and ensuring the largest dimension does not exceed THUMBNAIL_MAX_SIZE.
      Parameters:
      original - The original image.
      Returns:
      The scaled thumbnail image.
    • encodeToPng

      public static byte[] encodeToPng(BufferedImage img) throws IOException
      Encodes a buffered Image to png
      Parameters:
      img - the image
      Returns:
      the png
      Throws:
      IOException - hopefully not
    • toHtmlColor

      public static String toHtmlColor(Color color)
      Converts a Java Color object to its HTML hexadecimal string representation.
      Parameters:
      color - The Color object to convert.
      Returns:
      The HTML hexadecimal string (e.g., "#RRGGBB").
    • findComponent

      public static <T extends Component> T findComponent(Container container, Class<T> type)
      Recursively searches the containment hierarchy for a component of a specific type.

      This is an essential tool for Domain-UI Binding, allowing logic to locate deep-nested components (like a specific scroll pane) without maintaining explicit references.

      Type Parameters:
      T - The type of component to find.
      Parameters:
      container - The container to search.
      type - The class of the component type.
      Returns:
      The first matching component found, or null if none exist.
    • findComponentLeaf

      public static Component findComponentLeaf(Component component)
      Recursively finds the innermost "leaf" component that is not a container, or a container that specifically doesn't have components.

      For high-fidelity frames, this method traverses viewports to find the actual text area (RSyntaxTextArea or JEditorPane).

      Parameters:
      component - The root component to start search from.
      Returns:
      The innermost leaf component.
    • copyToClipboard

      public static void copyToClipboard(String text)
      Copies the given text to the system clipboard.
      Parameters:
      text - The text to copy.
    • copyImageToClipboard

      public static void copyImageToClipboard(Image image)
      Copies a Java Image to the system clipboard.

      Implementation details: Supports both DataFlavor.imageFlavor and DataFlavor.javaFileListFlavor. The latter is achieved by saving a temporary PNG of the image, allowing it to be pasted directly into the OS filesystem (e.g., File Explorer).

      Parameters:
      image - The image to copy. Must not be null.
    • redispatchMouseWheelEvent

      public static void redispatchMouseWheelEvent(Component component, MouseWheelEvent e)
      Redispatches a MouseWheelEvent to the appropriate ancestor scroll pane.

      Boundary Awareness: If the immediately enclosing scroll pane has vertical scrolling enabled, the event is only forwarded if the scroll pane is at its boundary (Top while rolling up, or Bottom while rolling down).

      Parameters:
      component - The component receiving the event.
      e - The mouse wheel event.
    • showCodeBlockDialog

      public static void showCodeBlockDialog(Component parent, String title, String text, String language)
      Displays a modal dialog with a syntax-highlighted code block.

      This method dynamically resolves the appropriate renderer based on the provided language. It supports high-fidelity rendering for technical content (via RSyntaxTextArea) and the Singularity Path for Mermaid diagrams.

      Parameters:
      parent - The parent component.
      title - The dialog title.
      text - The text to display.
      language - The language for syntax highlighting.
    • runInEDT

      public static void runInEDT(Runnable runnable)
      Executes a task on the EDT.

      Optimization: If the current thread is already the EDT, the task is executed synchronously to avoid unnecessary event queue overhead. Otherwise, it is scheduled via SwingUtilities.invokeLater(Runnable).

      Parameters:
      runnable - The code to execute.
    • runInEDTAndWait

      public static void runInEDTAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException
      Executes the given runnable on the Event Dispatch Thread (EDT) and waits for it to complete.

      Synchronization: This method blocks the calling thread. Use with extreme caution inside tools to avoid deadlocks. It is intended for scenarios where the tool must wait for user interaction or component validation.

      Parameters:
      runnable - The code to execute.
      Throws:
      InterruptedException - if the thread is interrupted while waiting.
      InvocationTargetException - if an exception occurs during execution.