Class SwingUtils
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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe authoritative maximum dimension (width or height) for generated thumbnails to ensure UI responsiveness. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopyImageToClipboard(Image image) Copies a Java Image to the system clipboard.static voidcopyToClipboard(String text) Copies the given text to the system clipboard.static ImagecreateThumbnail(BufferedImage original) Creates a scaled thumbnail image from an original BufferedImage, maintaining the aspect ratio and ensuring the largest dimension does not exceedTHUMBNAIL_MAX_SIZE.static byte[]encodeToPng(BufferedImage img) Encodes a buffered Image to pngstatic <T extends Component>
TfindComponent(Container container, Class<T> type) Recursively searches the containment hierarchy for a component of a specific type.static ComponentfindComponentLeaf(Component component) Recursively finds the innermost "leaf" component that is not a container, or a container that specifically doesn't have components.static voidredispatchMouseWheelEvent(Component component, MouseWheelEvent e) Redispatches aMouseWheelEventto the appropriate ancestor scroll pane.static voidExecutes a task on the EDT.static voidrunInEDTAndWait(Runnable runnable) Executes the given runnable on the Event Dispatch Thread (EDT) and waits for it to complete.static voidshowCodeBlockDialog(Component parent, String title, String text, String language) Displays a modal dialog with a syntax-highlighted code block.static StringtoHtmlColor(Color color) Converts a Java Color object to its HTML hexadecimal string representation.
-
Field Details
-
THUMBNAIL_MAX_SIZE
private static final int THUMBNAIL_MAX_SIZEThe authoritative maximum dimension (width or height) for generated thumbnails to ensure UI responsiveness.- See Also:
-
-
Constructor Details
-
SwingUtils
public SwingUtils()
-
-
Method Details
-
createThumbnail
Creates a scaled thumbnail image from an original BufferedImage, maintaining the aspect ratio and ensuring the largest dimension does not exceedTHUMBNAIL_MAX_SIZE.- Parameters:
original- The original image.- Returns:
- The scaled thumbnail image.
-
encodeToPng
Encodes a buffered Image to png- Parameters:
img- the image- Returns:
- the png
- Throws:
IOException- hopefully not
-
toHtmlColor
-
findComponent
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
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
Copies the given text to the system clipboard.- Parameters:
text- The text to copy.
-
copyImageToClipboard
Copies a Java Image to the system clipboard.Implementation details: Supports both
DataFlavor.imageFlavorandDataFlavor.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
Redispatches aMouseWheelEventto 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
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.
-

