Class TextSelectionDelegate<C extends com.vaadin.flow.component.Component & CanSelectText & CanReceiveSelectionEventsFromClient>

java.lang.Object
org.vaadin.miki.shared.text.TextSelectionDelegate<C>
All Implemented Interfaces:
Serializable, TextSelectionNotifier<C>, CanReceiveSelectionEventsFromClient, CanSelectText
Direct Known Subclasses:
TextModificationDelegate

public class TextSelectionDelegate<C extends com.vaadin.flow.component.Component & CanSelectText & CanReceiveSelectionEventsFromClient> extends Object implements Serializable, CanSelectText, CanReceiveSelectionEventsFromClient, TextSelectionNotifier<C>
A class that handles common behaviour related to text selection. This is mostly for internal use. There are a lot of steps that need to be taken care of to integrate this with any component. Please check project's wiki. The methods onAttach onDetach must be overwritten in the delegating class and delegated here. The source component must implement CanSelectText and CanReceiveSelectionEventsFromClient and delegate them to this object. The client-side component must mix in text-selection-mixin.js or otherwise react to needed JS method calls. Finally, the delegating class must implement methods: @ClientCallable void selectionChanged(int, int, String) and @ClientCallable void reinitialiseEventListening(). The first method should call this object's fireTextSelectionEvent(boolean, int, int, String) and the second method should just be delegated to this object.
Since:
2020-06-01
Author:
miki
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Defines the name of the HTML attribute that contains the selected text.
  • Constructor Summary

    Constructors
    Constructor
    Description
    TextSelectionDelegate(C source, com.vaadin.flow.component.ComponentEventBus eventBus, com.vaadin.flow.function.SerializableSupplier<String> stringValueSupplier)
    Creates the delegate for a given component.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.vaadin.flow.shared.Registration
    Adds the listener.
    void
    Handles selection change on value change if there are no client notifications.
    void
    fireTextSelectionEvent(boolean fromClient, int start, int end, String text)
    Fires text selection event.
    protected final C
    Gets the source component.
    protected final com.vaadin.flow.dom.Element
    Gets the element for the source component.
    void
    Sends information to the client side about whether or not it should forward text selection change events.
    boolean
    Check if client will inform server on selection change.
    void
    onAttach(com.vaadin.flow.component.AttachEvent event, Consumer<com.vaadin.flow.component.AttachEvent> originalMethod)
    Informs client about sending events and calls the original method.
    void
    onDetach(com.vaadin.flow.component.DetachEvent event, Consumer<com.vaadin.flow.component.DetachEvent> originalMethod)
    Informs client to not send events (if needed) and calls the original method.
    void
    This method should be called in response to @ClientCallable void reinitialiseListeners() on the owning object.
    void
    select(int from, int to)
    Selects text starting from index from (inclusive) and ending at index to (exclusive).
    void
    Selects entire text in the component.
    void
    Removes the current selection and selects no text.
    void
    setReceivingSelectionEventsFromClient(boolean receivingSelectionEventsFromClient)
    Configures sending events by the client-side component.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SELECTED_TEXT_ATTRIBUTE_NAME

      public static final String SELECTED_TEXT_ATTRIBUTE_NAME
      Defines the name of the HTML attribute that contains the selected text.
      See Also:
  • Constructor Details

    • TextSelectionDelegate

      public TextSelectionDelegate(C source, com.vaadin.flow.component.ComponentEventBus eventBus, com.vaadin.flow.function.SerializableSupplier<String> stringValueSupplier)
      Creates the delegate for a given component.
      Parameters:
      source - Source of all events, data, etc.
      eventBus - Event bus to use for firing events. Typically, source.getEventBus().
      stringValueSupplier - Method to obtain current value of the component as a String.
  • Method Details

    • getSource

      protected final C getSource()
      Gets the source component.
      Returns:
      Source component.
    • getSourceElement

      protected final com.vaadin.flow.dom.Element getSourceElement()
      Gets the element for the source component.
      Returns:
      The element.
    • informClientAboutSendingEvents

      public void informClientAboutSendingEvents(boolean value)
      Sends information to the client side about whether or not it should forward text selection change events.
      Parameters:
      value - When true, client-side will notify server about changes in text selection.
    • fireTextSelectionEvent

      public void fireTextSelectionEvent(boolean fromClient, int start, int end, String text)
      Fires text selection event.
      Parameters:
      fromClient - Whether or not the event originates from the client.
      start - Selection start.
      end - Selection end.
      text - Selection text.
    • selectAll

      public void selectAll()
      Description copied from interface: CanSelectText
      Selects entire text in the component.
      Specified by:
      selectAll in interface CanSelectText
    • selectNone

      public void selectNone()
      Description copied from interface: CanSelectText
      Removes the current selection and selects no text.
      Specified by:
      selectNone in interface CanSelectText
    • select

      public void select(int from, int to)
      Description copied from interface: CanSelectText
      Selects text starting from index from (inclusive) and ending at index to (exclusive).
      Specified by:
      select in interface CanSelectText
      Parameters:
      from - Starting index (inclusive).
      to - Ending index (exclusive).
    • clearSelectionOnValueChange

      public void clearSelectionOnValueChange()
      Handles selection change on value change if there are no client notifications. Does nothing if the component is receiving client-side notifications.
    • onAttach

      public void onAttach(com.vaadin.flow.component.AttachEvent event, Consumer<com.vaadin.flow.component.AttachEvent> originalMethod)
      Informs client about sending events and calls the original method.
      Parameters:
      event - Event.
      originalMethod - Method to call. Must not be null.
    • onDetach

      public void onDetach(com.vaadin.flow.component.DetachEvent event, Consumer<com.vaadin.flow.component.DetachEvent> originalMethod)
      Informs client to not send events (if needed) and calls the original method.
      Parameters:
      event - Event.
      originalMethod - Method to call. Must not be null.
    • addTextSelectionListener

      public com.vaadin.flow.shared.Registration addTextSelectionListener(TextSelectionListener<C> listener)
      Description copied from interface: TextSelectionNotifier
      Adds the listener.
      Specified by:
      addTextSelectionListener in interface TextSelectionNotifier<C extends com.vaadin.flow.component.Component & CanSelectText & CanReceiveSelectionEventsFromClient>
      Parameters:
      listener - A listener to add.
      Returns:
      A Registration that can be used to stop listening to the event.
    • isReceivingSelectionEventsFromClient

      public boolean isReceivingSelectionEventsFromClient()
      Description copied from interface: CanReceiveSelectionEventsFromClient
      Check if client will inform server on selection change. Note: this feature is by default turned off.
      Specified by:
      isReceivingSelectionEventsFromClient in interface CanReceiveSelectionEventsFromClient
      Returns:
      When true, each selection change in the client-side component will result in this component broadcasting a TextSelectionEvent.
    • setReceivingSelectionEventsFromClient

      public void setReceivingSelectionEventsFromClient(boolean receivingSelectionEventsFromClient)
      Description copied from interface: CanReceiveSelectionEventsFromClient
      Configures sending events by the client-side component. Note: this feature is by default turned off.
      Specified by:
      setReceivingSelectionEventsFromClient in interface CanReceiveSelectionEventsFromClient
      Parameters:
      receivingSelectionEventsFromClient - When false, selecting text in client-side component will not send an event to server-side component. When true, it will.
    • reinitialiseListeners

      public void reinitialiseListeners()
      This method should be called in response to @ClientCallable void reinitialiseListeners() on the owning object.