Class CodeBlockSegmentRenderer

java.lang.Object
uno.anahata.asi.swing.agi.message.part.text.AbstractTextSegmentRenderer
uno.anahata.asi.swing.agi.message.part.text.CodeBlockSegmentRenderer
Direct Known Subclasses:
MermaidCodeBlockSegmentRenderer

public class CodeBlockSegmentRenderer extends AbstractTextSegmentRenderer
The authoritative renderer for all code block segments in the platform.

This class implements the Universal Eye strategy: it wraps code snippets or parameters in a virtual Resource and requests a host-native viewer from the ResourceUiRegistry.

This eliminates environment-specific subclasses and ensures 100% fidelity (NetBeans frames in the IDE, RSyntax in standalone) for every line of code.

Author:
anahata
  • Field Details

    • language

      protected final String language
      The programming language of the code block.
    • viewer

      protected AbstractTextResourceViewer viewer
      The host-specific high-fidelity viewer component.
    • editing

      protected boolean editing
      Whether the code block is currently in edit mode.
    • editable

      protected boolean editable
      Whether this code block is allowed to be edited. Defaults to false.
    • headerVisible

      protected boolean headerVisible
      Whether the header panel (Language, Copy, Edit) is visible. Defaults to true.
    • verticalScrollEnabled

      protected boolean verticalScrollEnabled
      Whether vertical scrolling is enabled in the scroll pane. Defaults to false for conversation view.
    • editButton

      protected JButton editButton
      The button used to toggle between edit and view modes.
    • cancelButton

      protected JButton cancelButton
      The button used to cancel editing.
    • onSave

      protected Consumer<String> onSave
      Callback triggered when the user clicks 'Save' after editing.
  • Constructor Details

    • CodeBlockSegmentRenderer

      public CodeBlockSegmentRenderer(AgiPanel agiPanel, String initialContent, String language)
      Constructs a new code block renderer.
      Parameters:
      agiPanel - The agi panel instance.
      initialContent - The initial code content.
      language - The programming language.
  • Method Details

    • render

      public boolean render()
      Renders or updates the segment's JComponent based on the current content. This method should be called to ensure the component reflects the latest content. It should handle diffing internally to avoid unnecessary re-creation of components.

      Renders the code block, optionally including a header with language metadata and copy/edit actions. It leverages the host's ResourceUI strategy to provide the actual editor component.

      Specified by:
      render in class AbstractTextSegmentRenderer
      Returns:
      True if a visual update occurred, false otherwise.
    • initViewer

      protected void initViewer()
      Initializes the host-native high-fidelity viewer via the registry.

      It configures the viewer for Streaming Fidelity: using the editor card for both modes (previewAsEditor) to prevent text loss during savings and starting in a read-only state.

    • createInnerComponent

      protected JComponent createInnerComponent()
      Template method for subclasses to define the central component. Defaults to the high-fidelity viewer.
      Returns:
      The central JComponent.
    • addExtraHeaderButtons

      protected void addExtraHeaderButtons(JPanel leftHeaderPanel)
      Hook for subclasses to inject specialized buttons into the header area.
      Parameters:
      leftHeaderPanel - The panel containing the header buttons.
    • getScrollPane

      public JScrollPane getScrollPane()
      Accessor for the scroll pane within the assembled frame. Used by dialogs to enable navigation.
      Returns:
      The internal JScrollPane, or null if not found.
    • updateComponentContent

      protected void updateComponentContent(String content)
      Pushes incremental content updates to the visual component. This is the authoritative ingestion point for streaming responses.

      Authoritative streaming ingestion point. It updates the virtual handle of the ephemeral resource and triggers a reload, forcing the high-fidelity viewer (NetBeans or RSyntax) to refresh reactively.

      Overrides:
      updateComponentContent in class AbstractTextSegmentRenderer
      Parameters:
      content - The new segment content.
    • getCurrentContentFromComponent

      protected String getCurrentContentFromComponent()
      Retrieves the current content from the inner component.
      Returns:
      The current code content.
    • setComponentEditable

      protected void setComponentEditable(boolean editable)
      Sets the editability of the inner component.
      Parameters:
      editable - True to enable editing, false to disable.
    • toggleEdit

      protected void toggleEdit()
      Toggles the edit mode of the renderer.

      State Persistence: When exiting edit mode, this method captures the component's content and updates the underlying ephemeral resource handle before switching the UI state. This prevents the viewer's synchronization loop from reverting the text to its original state.

    • matches

      public boolean matches(TextSegmentDescriptor descriptor)
      Determines if this renderer can handle the given segment descriptor. Subclasses must implement this to specify their compatibility.

      Matches the segment if the type is CODE and the language matches.

      Specified by:
      matches in class AbstractTextSegmentRenderer
      Parameters:
      descriptor - The TextSegmentDescriptor to check.
      Returns:
      True if this renderer can handle the descriptor, false otherwise.