Class CodeBlockSegmentRenderer
- Direct Known Subclasses:
MermaidCodeBlockSegmentRenderer
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 Summary
FieldsModifier and TypeFieldDescriptionprotected JButtonThe button used to cancel editing.protected booleanWhether this code block is allowed to be edited.protected JButtonThe button used to toggle between edit and view modes.protected booleanWhether the code block is currently in edit mode.protected booleanWhether the header panel (Language, Copy, Edit) is visible.protected final StringThe programming language of the code block.Callback triggered when the user clicks 'Save' after editing.protected booleanWhether vertical scrolling is enabled in the scroll pane.protected AbstractTextResourceViewerThe host-specific high-fidelity viewer component.Fields inherited from class AbstractTextSegmentRenderer
agiConfig, agiPanel, closed, component, currentContent, lastRenderedContent -
Constructor Summary
ConstructorsConstructorDescriptionCodeBlockSegmentRenderer(AgiPanel agiPanel, String initialContent, String language) Constructs a new code block renderer. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddExtraHeaderButtons(JPanel leftHeaderPanel) Hook for subclasses to inject specialized buttons into the header area.protected JComponentTemplate method for subclasses to define the central component.protected StringRetrieves the current content from the inner component.Accessor for the scroll pane within the assembled frame.protected voidInitializes the host-native high-fidelity viewer via the registry.booleanmatches(TextSegmentDescriptor descriptor) Determines if this renderer can handle the given segment descriptor.booleanrender()Renders or updates the segment's JComponent based on the current content.protected voidsetComponentEditable(boolean editable) Sets the editability of the inner component.protected voidToggles the edit mode of the renderer.protected voidupdateComponentContent(String content) Pushes incremental content updates to the visual component.Methods inherited from class AbstractTextSegmentRenderer
contentRendered, hasContentChanged, updateContent, updateTheme
-
Field Details
-
language
The programming language of the code block. -
viewer
The host-specific high-fidelity viewer component. -
editing
protected boolean editingWhether the code block is currently in edit mode. -
editable
protected boolean editableWhether this code block is allowed to be edited. Defaults to false. -
headerVisible
protected boolean headerVisibleWhether the header panel (Language, Copy, Edit) is visible. Defaults to true. -
verticalScrollEnabled
protected boolean verticalScrollEnabledWhether vertical scrolling is enabled in the scroll pane. Defaults to false for conversation view. -
editButton
The button used to toggle between edit and view modes. -
cancelButton
The button used to cancel editing. -
onSave
-
-
Constructor Details
-
CodeBlockSegmentRenderer
-
-
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
ResourceUIstrategy to provide the actual editor component.- Specified by:
renderin classAbstractTextSegmentRenderer- 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
Template method for subclasses to define the central component. Defaults to the high-fidelity viewer.- Returns:
- The central JComponent.
-
addExtraHeaderButtons
Hook for subclasses to inject specialized buttons into the header area.- Parameters:
leftHeaderPanel- The panel containing the header buttons.
-
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
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:
updateComponentContentin classAbstractTextSegmentRenderer- Parameters:
content- The new segment content.
-
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
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:
matchesin classAbstractTextSegmentRenderer- Parameters:
descriptor- TheTextSegmentDescriptorto check.- Returns:
- True if this renderer can handle the descriptor, false otherwise.
-

