Package org.vaadin.miki.shared.text
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
FieldsModifier and TypeFieldDescriptionstatic final StringDefines the name of the HTML attribute that contains the selected text. -
Constructor Summary
ConstructorsConstructorDescriptionTextSelectionDelegate(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 TypeMethodDescriptioncom.vaadin.flow.shared.RegistrationaddTextSelectionListener(TextSelectionListener<C> listener) Adds the listener.voidHandles selection change on value change if there are no client notifications.voidfireTextSelectionEvent(boolean fromClient, int start, int end, String text) Fires text selection event.protected final CGets the source component.protected final com.vaadin.flow.dom.ElementGets the element for the source component.voidinformClientAboutSendingEvents(boolean value) Sends information to the client side about whether or not it should forward text selection change events.booleanCheck if client will inform server on selection change.voidonAttach(com.vaadin.flow.component.AttachEvent event, Consumer<com.vaadin.flow.component.AttachEvent> originalMethod) Informs client about sending events and calls the original method.voidonDetach(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.voidThis method should be called in response to@ClientCallable void reinitialiseListeners()on the owning object.voidselect(int from, int to) Selects text starting from indexfrom(inclusive) and ending at indexto(exclusive).voidSelects entire text in the component.voidRemoves the current selection and selects no text.voidsetReceivingSelectionEventsFromClient(boolean receivingSelectionEventsFromClient) Configures sending events by the client-side component.
-
Field Details
-
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 aString.
-
-
Method Details
-
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- Whentrue, client-side will notify server about changes in text selection.
-
fireTextSelectionEvent
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:CanSelectTextSelects entire text in the component.- Specified by:
selectAllin interfaceCanSelectText
-
selectNone
public void selectNone()Description copied from interface:CanSelectTextRemoves the current selection and selects no text.- Specified by:
selectNonein interfaceCanSelectText
-
select
public void select(int from, int to) Description copied from interface:CanSelectTextSelects text starting from indexfrom(inclusive) and ending at indexto(exclusive).- Specified by:
selectin interfaceCanSelectText- 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 benull.
-
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 benull.
-
addTextSelectionListener
public com.vaadin.flow.shared.Registration addTextSelectionListener(TextSelectionListener<C> listener) Description copied from interface:TextSelectionNotifierAdds the listener.- Specified by:
addTextSelectionListenerin interfaceTextSelectionNotifier<C extends com.vaadin.flow.component.Component & CanSelectText & CanReceiveSelectionEventsFromClient>- Parameters:
listener- A listener to add.- Returns:
- A
Registrationthat can be used to stop listening to the event.
-
isReceivingSelectionEventsFromClient
public boolean isReceivingSelectionEventsFromClient()Description copied from interface:CanReceiveSelectionEventsFromClientCheck if client will inform server on selection change. Note: this feature is by default turned off.- Specified by:
isReceivingSelectionEventsFromClientin interfaceCanReceiveSelectionEventsFromClient- Returns:
- When
true, each selection change in the client-side component will result in this component broadcasting aTextSelectionEvent.
-
setReceivingSelectionEventsFromClient
public void setReceivingSelectionEventsFromClient(boolean receivingSelectionEventsFromClient) Description copied from interface:CanReceiveSelectionEventsFromClientConfigures sending events by the client-side component. Note: this feature is by default turned off.- Specified by:
setReceivingSelectionEventsFromClientin interfaceCanReceiveSelectionEventsFromClient- Parameters:
receivingSelectionEventsFromClient- Whenfalse, selecting text in client-side component will not send an event to server-side component. Whentrue, it will.
-
reinitialiseListeners
public void reinitialiseListeners()This method should be called in response to@ClientCallable void reinitialiseListeners()on the owning object.
-