Class AiMessageAction<A extends AiMessageAction<A>>

java.lang.Object
io.jmix.flowui.kit.action.AbstractAction
io.jmix.aichat.action.AiMessageAction<A>
Type Parameters:
A - the concrete action type (self-type, for fluent setters)
All Implemented Interfaces:
Action, HasShortcutCombination
Direct Known Subclasses:
AiMessageCopyAction, AiMessageRegenerateAction

public class AiMessageAction<A extends AiMessageAction<A>> extends AbstractAction
An abstract base class for the actions shown under a message in an AiMessageList.

One instance is assigned to a role — AiMessageList.setUserActions(java.util.List<? extends io.jmix.aichat.action.AiMessageAction<?>>) / AiMessageList.setAssistantActions(java.util.List<? extends io.jmix.aichat.action.AiMessageAction<?>>) — and reused for every message of that role, so it holds no per-message state: the message reaches the handler through AiMessageActionPerformedEvent instead, which carries both the rendered item and the domain message it was rendered from. Business logic goes in execute(AiMessageActionPerformedEvent); a listener registered with addActionPerformedListener(java.util.function.Consumer<io.jmix.aichat.action.AiMessageActionPerformedEvent>) overrides it.

Only the members this component can render are supported. The action's icon (an Icon, SvgIcon, FontIcon or Image), its description — used as the button's tooltip and accessible name — and its class names are pushed to the client; text, enabled, visible, variant and the shortcut combination have no client-side representation and their setters throw UnsupportedOperationException rather than being silently ignored.

The class is concrete and fluent, so a general-purpose action needs no subclass:


 new AiMessageAction<>("regenerate")
         .withIcon(new Icon(VaadinIcon.REFRESH))
         .withDescription("Regenerate")
         .withHandler(event -> ...);
 
This is also what a view descriptor's action element without a type produces. Subclass it to register an ActionType of your own — AiMessageCopyAction is the built-in client-side copy.
  • Field Details

    • classNames

      protected Set<String> classNames
  • Constructor Details

    • AiMessageAction

      public AiMessageAction(String id)
      Creates the action with the given id.
      Parameters:
      id - the action id
  • Method Details

    • initAction

      protected void initAction()
      Hook for subclasses to set defaults (icon, description, class names). Empty by default.

      Called from the constructor, so it runs before the subclass's own field initializers: set state through the inherited setters, not through subclass fields.

    • getClassNames

      public Set<String> getClassNames()
      CSS class names applied to the action's button on the client. Lets an action style itself — for example supply its icon through a CSS mask instead of an icon component.
      Returns:
      the class names to apply (never null; empty by default)
    • addClassNames

      public void addClassNames(String... classNames)
      Adds CSS class names to the action's button. Additive on purpose: a declared classNames must not discard the class a built-in action type contributed in initAction().
      Parameters:
      classNames - the class names to add, not null
    • refreshState

      public void refreshState()
      Description copied from interface: Action
      Refreshes internal state of the action to initialize enabled, visible, text, icon, etc. properties depending on programmatically set values and user permissions set at runtime.

      For example, this method is called by visual components holding actions when they are bound to data. At this moment the action can find out what entity it is connected to and change its state according to the user permissions.

    • actionPerform

      public void actionPerform(AiMessageList messageList, AiMessageListItem item)
      Performs the action on a message. Called by the host list when the action's button is clicked. Fires AiMessageActionPerformedEvent when a listener is registered, and calls execute(AiMessageActionPerformedEvent) otherwise.
      Parameters:
      messageList - the host message list, not null
      item - the rendered item the action was invoked on, not null
    • execute

      protected void execute(AiMessageActionPerformedEvent event)
      Business logic of the action. Empty by default.
      Parameters:
      event - the performed event, carrying the rendered item, the domain message it was rendered from, and the host list
    • addActionPerformedListener

      public com.vaadin.flow.shared.Registration addActionPerformedListener(Consumer<AiMessageActionPerformedEvent> listener)
      Adds a listener to be notified when the action is performed.
      Parameters:
      listener - listener to add
      Returns:
      a registration object for removing an event listener
    • withIcon

      public A withIcon(@Nullable com.vaadin.flow.component.Component icon)
      Sets the icon of the action's button.
      Parameters:
      icon - the icon to render: an Icon, SvgIcon, FontIcon or Image, or null to remove
      Returns:
      this object
    • withDescription

      public A withDescription(@Nullable String description)
      Sets the description used as the button's tooltip and accessible name.
      Parameters:
      description - the description to set, or null to remove
      Returns:
      this object
    • withClassNames

      public A withClassNames(String... classNames)
      Adds CSS class names to the action's button.
      Parameters:
      classNames - the class names to add
      Returns:
      this object
    • withHandler

      public A withHandler(@Nullable Consumer<AiMessageActionPerformedEvent> handler)
      Adds a listener to be notified when the action is performed. Like every listener, it overrides execute(AiMessageActionPerformedEvent).
      Parameters:
      handler - listener to add, or null to remove all
      Returns:
      this object
    • self

      protected A self()
    • actionPerform

      public void actionPerform(com.vaadin.flow.component.Component component)
      Description copied from interface: Action
      Executes action logic.
      Parameters:
      component - Component that triggered this action
      Throws:
      UnsupportedOperationException - always — a message action is meaningless without a target message, so it cannot be triggered by a component. Use actionPerform(AiMessageList, AiMessageListItem).
    • setText

      public void setText(@Nullable String text)
      Description copied from interface: Action
      Sets the text property value of an action. May be used by components to initialize their appearance.
      Specified by:
      setText in interface Action
      Overrides:
      setText in class AbstractAction
      Parameters:
      text - text to set or null to remove
      Throws:
      UnsupportedOperationException - always — the action's button is icon-only.
    • setEnabled

      public void setEnabled(boolean enabled)
      Description copied from interface: Action
      Sets the enabled property value of an action. May be used by components to initialize their appearance.
      Specified by:
      setEnabled in interface Action
      Overrides:
      setEnabled in class AbstractAction
      Parameters:
      enabled - whether the action is currently enabled
      Throws:
      UnsupportedOperationException - always — the client has no disabled action state.
    • setVisible

      public void setVisible(boolean visible)
      Description copied from interface: Action
      Sets the visible property value of an action. May be used by components to initialize their appearance.
      Specified by:
      setVisible in interface Action
      Overrides:
      setVisible in class AbstractAction
      Parameters:
      visible - whether the action is currently visible
      Throws:
      UnsupportedOperationException - always — the client has no hidden action state.
    • setVariant

      public void setVariant(ActionVariant variant)
      Description copied from interface: Action
      Sets the variant property value of an action. May be used by components to initialize their appearance.
      Specified by:
      setVariant in interface Action
      Overrides:
      setVariant in class AbstractAction
      Parameters:
      variant - variant to set
      Throws:
      UnsupportedOperationException - always — the action's button has no variants.
    • setShortcutCombination

      public void setShortcutCombination(@Nullable KeyCombination shortcutCombination)
      Description copied from interface: HasShortcutCombination
      Sets object that stores information about keys, modifiers and additional settings that describe shortcut combinations.
      Specified by:
      setShortcutCombination in interface HasShortcutCombination
      Overrides:
      setShortcutCombination in class AbstractAction
      Parameters:
      shortcutCombination - key combination to set or null to remove
      Throws:
      UnsupportedOperationException - always — a per-role action bar has no shortcut scope.
    • unsupported

      protected UnsupportedOperationException unsupported(String property)