public abstract class AbstractComponent extends AbstractClientConnector implements Component, ContextClickEvent.ContextClickNotifier
Component
interface. Basic UI components that are not derived from an
external component can inherit this class to easily qualify as Vaadin
components. Most components in Vaadin do just that.Component.ErrorEvent, Component.Event, Component.Focusable, Component.Listener
ClientConnector.AttachEvent, ClientConnector.AttachListener, ClientConnector.ConnectorErrorEvent, ClientConnector.DetachEvent, ClientConnector.DetachListener
Sizeable.Unit
Modifier and Type | Field and Description |
---|---|
protected static String |
DESIGN_ATTR_PLAIN_TEXT |
SIZE_UNDEFINED, UNITS_CM, UNITS_EM, UNITS_EX, UNITS_INCH, UNITS_MM, UNITS_PERCENTAGE, UNITS_PICAS, UNITS_PIXELS, UNITS_POINTS
Constructor and Description |
---|
AbstractComponent()
Constructs a new Component.
|
Modifier and Type | Method and Description |
---|---|
Registration |
addContextClickListener(ContextClickEvent.ContextClickListener listener)
Adds a context click listener that gets notified when a context click
happens.
|
Registration |
addListener(Component.Listener listener)
Registers a new (generic) component event listener for the component.
|
Registration |
addShortcutListener(ShortcutListener shortcut) |
void |
addStyleName(String style)
Adds one or more style names to this component.
|
void |
attach()
Notifies the connector that it is connected to a VaadinSession (and
therefore also to a UI).
|
void |
beforeClientResponse(boolean initial)
Called before the shared state and RPC invocations are sent to the
client.
|
void |
detach()
Notifies the connector that it is detached from its VaadinSession.
|
<T extends HasComponents> |
findAncestor(Class<T> parentType)
Returns the closest ancestor with the given type.
|
protected void |
fireComponentErrorEvent()
Emits the component error event.
|
protected void |
fireComponentEvent()
Emits the component event.
|
protected void |
focus()
Sets the focus for this component if the component is
Focusable . |
protected ActionManager |
getActionManager()
Gets the
ActionManager used to manage the
ShortcutListener s added to this component. |
String |
getCaption()
Gets the caption of the component.
|
ErrorMessage |
getComponentError()
Gets the component's error message.
|
protected Collection<String> |
getCustomAttributes()
Returns a collection of attributes that should not be handled by the
basic implementation of the
readDesign(Element, DesignContext)
and writeDesign(Element, DesignContext) methods. |
Object |
getData()
Gets the application specific data.
|
String |
getDebugId()
Deprecated.
As of 7.0. Use
getId() |
String |
getDescription()
Gets the components description, used in tooltips and can be displayed
directly in certain other components such as forms.
|
ErrorMessage |
getErrorMessage()
Gets the error message for this component.
|
float |
getHeight()
Gets the height of the object.
|
Sizeable.Unit |
getHeightUnits()
Gets the height property units.
|
Resource |
getIcon()
Gets the icon resource of the component.
|
String |
getId()
Gets currently set debug identifier.
|
Locale |
getLocale()
Gets the locale of the component.
|
HasComponents |
getParent()
Gets the parent component of the component.
|
String |
getPrimaryStyleName()
Gets the primary style name of the component.
|
protected AbstractComponentState |
getState()
Returns the shared state bean with information to be sent from the server
to the client.
|
protected AbstractComponentState |
getState(boolean markAsDirty)
Returns the shared state for this connector.
|
String |
getStyleName()
Gets all user-defined CSS style names of a component.
|
float |
getWidth()
Gets the width of the object.
|
Sizeable.Unit |
getWidthUnits()
Gets the width property units.
|
boolean |
isCaptionAsHtml()
Checks whether captions are rendered as HTML
|
boolean |
isConnectorEnabled()
Checks if the communicator is enabled.
|
boolean |
isEnabled()
Tests whether the component is enabled or not.
|
protected boolean |
isOrHasAncestor(Component content)
Determine whether a
content component is equal to, or the
ancestor of this component. |
protected boolean |
isReadOnly()
Returns the read-only status from the state of this
AbstractComponent . |
protected boolean |
isRequiredIndicatorVisible()
Checks whether the required indicator is visible or not.
|
boolean |
isResponsive()
Returns true if the component is responsive.
|
boolean |
isVisible()
Tests the visibility property of the component.
|
void |
readDesign(org.jsoup.nodes.Element design,
DesignContext designContext)
Reads the component state from the given design.
|
void |
removeContextClickListener(ContextClickEvent.ContextClickListener listener)
Deprecated.
|
void |
removeListener(Component.Listener listener)
Deprecated.
|
void |
removeShortcutListener(ShortcutListener shortcut)
Deprecated.
|
void |
removeStyleName(String style)
Removes one or more style names from component.
|
void |
setCaption(String caption)
Sets the caption of the component.
|
void |
setCaptionAsHtml(boolean captionAsHtml)
Sets whether the caption is rendered as HTML.
|
void |
setComponentError(ErrorMessage componentError)
Sets the component's error message.
|
void |
setData(Object data)
Sets the data object, that can be used for any application specific data.
|
void |
setDebugId(String id)
Deprecated.
As of 7.0. Use
setId(String) |
void |
setDescription(String description)
Sets the component's description.
|
void |
setDescription(String description,
ContentMode mode)
Sets the component's description using given content
mode . |
void |
setEnabled(boolean enabled)
Enables or disables the component.
|
void |
setHeight(float height,
Sizeable.Unit unit)
Sets the height of the object.
|
void |
setHeight(String height)
Sets the height of the component using String presentation.
|
void |
setHeightUndefined()
Clears any defined height.
|
void |
setIcon(Resource icon)
Sets the component's icon.
|
void |
setId(String id)
Adds an unique id for component that is used in the client-side for
testing purposes.
|
void |
setLocale(Locale locale)
Sets the locale of this component.
|
void |
setParent(HasComponents parent)
Sets the parent connector of the component.
|
void |
setPrimaryStyleName(String style)
Changes the primary style name of the component.
|
protected void |
setReadOnly(boolean readOnly)
Sets the read-only status in the state of this
AbstractComponent . |
protected void |
setRequiredIndicatorVisible(boolean visible)
Sets the visibility of the required indicator.
|
void |
setResponsive(boolean responsive)
Toggles responsiveness of this component.
|
void |
setSizeFull()
Sets the size to 100% x 100%.
|
void |
setSizeUndefined()
Clears any size settings.
|
void |
setStyleName(String style)
Sets one or more user-defined style names of the component, replacing any
previous user-defined styles.
|
void |
setVisible(boolean visible)
Sets the visibility of the component.
|
void |
setWidth(float width,
Sizeable.Unit unit)
Sets the width of the object.
|
void |
setWidth(String width)
Sets the width of the component using String presentation.
|
void |
setWidthUndefined()
Clears any defined width.
|
void |
writeDesign(org.jsoup.nodes.Element design,
DesignContext designContext)
Writes the component state to the given design.
|
addAttachListener, addDetachListener, addExtension, addListener, addListener, addListener, addMethodInvocationToQueue, createState, encodeState, equals, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getListeners, getResource, getRpcManager, getRpcProxy, getSession, getStateType, getUI, handleConnectorRequest, hashCode, hasListeners, isAttached, isThis, markAsDirty, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource, updateDiffstate
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
addStyleNames, getUI, removeStyleNames, setStyleName
addAttachListener, addDetachListener, encodeState, getErrorHandler, getExtensions, getRpcManager, getStateType, handleConnectorRequest, isAttached, markAsDirty, markAsDirtyRecursive, removeAttachListener, removeDetachListener, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler
getConnectorId
protected static final String DESIGN_ATTR_PLAIN_TEXT
public void setId(String id)
Component
public String getId()
Component
@Deprecated public void setDebugId(String id)
setId(String)
@Deprecated public String getDebugId()
getId()
public String getStyleName()
Component
The style names are returned only in the basic form in which they were added; each user-defined style name shows as two CSS style class names in the rendered HTML: one as it was given and one prefixed with the component-specific style name. Only the former is returned.
getStyleName
in interface Component
Component.setStyleName(String)
,
Component.addStyleName(String)
,
Component.removeStyleName(String)
public void setStyleName(String style)
Component
Label label = new Label("This text has a lot of style"); label.setStyleName("myonestyle myotherstyle");
Each style name will occur in two versions: one as specified and one that
is prefixed with the style name of the component. For example, if you
have a Button
component and give it "mystyle
" style, the
component will have both "mystyle
" and "v-button-mystyle
"
styles. You could then style the component either with:
.myonestyle {background: blue;}
or
.v-button-myonestyle {background: blue;}
It is normally a good practice to use addStyleName()
rather than this setter, as different software
abstraction layers can then add their own styles without accidentally
removing those defined in other layers.
setStyleName
in interface Component
style
- the new style or styles of the component as a space-separated
listComponent.getStyleName()
,
Component.addStyleName(String)
,
Component.removeStyleName(String)
public void setPrimaryStyleName(String style)
Component
The primary style name identifies the component when applying the CSS theme to the Component. By changing the style name all CSS rules targeted for that style name will no longer apply, and might result in the component not working as intended.
To preserve the original style of the component when changing to a new primary style you should make your new primary style inherit the old primary style using the SASS @include directive. See more in the SASS tutorials.
setPrimaryStyleName
in interface Component
style
- The new primary style namepublic String getPrimaryStyleName()
Component
Component.setPrimaryStyleName(String)
for a better description of
the primary stylename.getPrimaryStyleName
in interface Component
public void addStyleName(String style)
Component
Label label = new Label("This text has style"); label.addStyleName("mystyle");
Each style name will occur in two versions: one as specified and one that
is prefixed with the style name of the component. For example, if you
have a Button
component and give it "mystyle
" style, the
component will have both "mystyle
" and "v-button-mystyle
"
styles. You could then style the component either with:
.mystyle {font-style: italic;}
or
.v-button-mystyle {font-style: italic;}
addStyleName
in interface Component
style
- the new style to be added to the componentComponent.getStyleName()
,
Component.setStyleName(String)
,
Component.removeStyleName(String)
public void removeStyleName(String style)
Component
The parameter must be a valid CSS style name. Only user-defined style
names added with addStyleName()
or
setStyleName()
can be removed; built-in
style names defined in Vaadin or GWT can not be removed.
removeStyleName
in interface Component
style
- the style name or style names to be removedComponent.getStyleName()
,
Component.setStyleName(String)
,
Component.addStyleName(String)
public String getCaption()
Component
See Component.setCaption(String)
for a detailed description of the
caption.
getCaption
in interface Component
null
if the caption is
not set.Component.setCaption(String)
public void setCaption(String caption)
Component
A caption is an explanatory textual label accompanying a user
interface component, usually shown above, left of, or inside the
component. Icon (see setIcon()
is
closely related to caption and is usually displayed horizontally before
or after it, depending on the component and the containing layout.
The caption can usually also be given as the first parameter to a constructor, though some components do not support it.
RichTextArea area = new RichTextArea(); area.setCaption("You can edit stuff here"); area.setValue("<h1>Helpful Heading</h1>" + "<p>All this is for you to edit.</p>");
The contents of a caption are automatically quoted, so no raw HTML can be rendered in a caption. The validity of the used character encoding, usually UTF-8, is not checked.
The caption of a component is, by default, managed and displayed by the
layout component or component container in which the component is placed.
For example, the VerticalLayout
component shows the captions
left-aligned above the contained components, while the FormLayout
component shows the captions on the left side of the vertically laid
components, with the captions and their associated components
left-aligned in their own columns. The CustomComponent
does not
manage the caption of its composition root, so if the root component has
a caption, it will not be rendered. Some components, such as
Button
and Panel
, manage the caption themselves and
display it inside the component.
setCaption
in interface Component
caption
- the new caption for the component. If the caption is
null
, no caption is shown and it does not normally
take any spacepublic void setCaptionAsHtml(boolean captionAsHtml)
If set to true, the captions are rendered in the browser as HTML and the developer is responsible for ensuring no harmful HTML is used. If set to false, the caption is rendered in the browser as plain text.
The default is false, i.e. to render that caption as plain text.
captionAsHtml
- true if the captions are rendered as HTML, false if rendered
as plain textpublic boolean isCaptionAsHtml()
The default is false, i.e. to render that caption as plain text.
public Locale getLocale()
Component
If a component does not have a locale set, the locale of its parent is
returned, and so on. Eventually, if no parent has locale set, the locale
of the application is returned. If the application does not have a locale
set, it is determined by Locale.getDefault()
.
As the component must be attached before its locale can be acquired, using this method in the internationalization of component captions, etc. is generally not feasible. For such use case, we recommend using an otherwise acquired reference to the application locale.
public void setLocale(Locale locale)
// Component for which the locale is meaningful InlineDateField date = new InlineDateField("Datum"); // German language specified with ISO 639-1 language // code and ISO 3166-1 alpha-2 country code. date.setLocale(new Locale("de", "DE")); date.setResolution(DateField.RESOLUTION_DAY); layout.addComponent(date);
locale
- the locale to become this component's locale.public Resource getIcon()
Component
See Component.setIcon(Resource)
for a detailed description of the icon.
getIcon
in interface Component
null
if the
component has no iconComponent.setIcon(Resource)
public void setIcon(Resource icon)
setIcon
in interface Component
icon
- the icon to be shown with the component's caption.Component.getIcon()
,
Component.setCaption(String)
public boolean isEnabled()
Component
As a security feature, all updates for disabled components are blocked on the server-side.
Note that this method only returns the status of the component and does not take parents into account. Even though this method returns true the component can be disabled to the user if a parent is disabled.
isEnabled
in interface Component
true
if the component and its parent are enabled,
false
otherwise.VariableOwner.isEnabled()
public void setEnabled(boolean enabled)
Component
Button enabled = new Button("Enabled"); enabled.setEnabled(true); // The default layout.addComponent(enabled); Button disabled = new Button("Disabled"); disabled.setEnabled(false); layout.addComponent(disabled);
setEnabled
in interface Component
enabled
- a boolean value specifying if the component should be enabled
or notpublic boolean isConnectorEnabled()
ClientConnector
isConnectorEnabled
in interface ClientConnector
isConnectorEnabled
in class AbstractClientConnector
public boolean isVisible()
Component
Visible components are drawn in the user interface, while invisible ones are not. The effect is not merely a cosmetic CSS change - no information about an invisible component will be sent to the client. The effect is thus the same as removing the component from its parent. Making a component invisible through this property can alter the positioning of other components.
A component is visible only if all its parents are also visible. This is not checked by this method though, so even if this method returns true, the component can be hidden from the user because a parent is set to invisible.
isVisible
in interface Component
true
if the component has been set to be visible in
the user interface, false
if notComponent.setVisible(boolean)
,
Component.attach()
public void setVisible(boolean visible)
Component
Visible components are drawn in the user interface, while invisible ones are not. The effect is not merely a cosmetic CSS change - no information about an invisible component will be sent to the client. The effect is thus the same as removing the component from its parent.
TextField readonly = new TextField("Read-Only"); readonly.setValue("You can't see this!"); readonly.setVisible(false); layout.addComponent(readonly);
A component is visible only if all of its parents are also visible. If a component is explicitly set to be invisible, changes in the visibility of its parents will not change the visibility of the component.
setVisible
in interface Component
visible
- the boolean value specifying if the component should be
visible after the call or not.Component.isVisible()
public String getDescription()
Component
Gets the components description, used in tooltips and can be displayed directly in certain other components such as forms. The description can be used to briefly describe the state of the component to the user. The description string may contain certain XML tags:
Tag | Description | Example |
<b> | bold | bold text |
<i> | italic | italic text |
<u> | underlined | underlined text |
<br> | linebreak | N/A |
<ul> <li>item1 <li>item1 </ul> |
item list |
|
These tags may be nested.
getDescription
in interface Component
String
public void setDescription(String description)
getDescription()
for more
information on what the description is.description
- the new description string for the component.setDescription(String, ContentMode)
public void setDescription(String description, ContentMode mode)
mode
. See
getDescription()
for more information on what the description
is.
If the content mode
is ContentMode.HTML the
description is displayed as HTML in tooltips or directly in certain
components so care should be taken to avoid creating the possibility for
HTML injection and possibly XSS vulnerabilities.
description
- the new description string for the component.mode
- the content mode for the descriptionpublic HasComponents getParent()
Component
Components can be nested but a component can have only one parent. A
component that contains other components, that is, can be a parent,
should usually inherit the ComponentContainer
interface.
public void setParent(HasComponents parent)
Component
This method automatically calls Component.attach()
if the component
becomes attached to the session, regardless of whether it was attached
previously. Conversely, if the component currently is attached to the
session, ClientConnector.detach()
is called for the connector before attaching
it to a new parent.
This method is rarely called directly.
ComponentContainer.addComponent(Component)
or a
HasComponents
specific method is normally used for adding
components to a parent and the used method will call this method
implicitly.
public <T extends HasComponents> T findAncestor(Class<T> parentType)
To find the Window that contains the component, use Window w =
getParent(Window.class);
T
- The type of the ancestorparentType
- The ancestor class we are looking forpublic ErrorMessage getErrorMessage()
public ErrorMessage getComponentError()
public void setComponentError(ErrorMessage componentError)
componentError
- the new ErrorMessage
of the component.public void attach()
ClientConnector
The caller of this method is Connector#setParent(ClientConnector)
if the parent is itself already attached to the session. If not, the
parent will call the ClientConnector.attach()
for all its children when it is
attached to the session. This method is always called before the
connector's data is sent to the client-side for the first time.
The attachment logic is implemented in AbstractClientConnector
.
attach
in interface ClientConnector
attach
in interface Component
attach
in class AbstractClientConnector
public void detach()
AbstractClientConnector
The caller of this method is #setParent(ClientConnector)
if the
parent is in the session. When the parent is detached from the session it
is its responsibility to call ClientConnector.detach()
for each of its children.
The AbstractClientConnector.getSession()
and AbstractClientConnector.getUI()
methods might return
null
after this method is called.
detach
in interface ClientConnector
detach
in class AbstractClientConnector
protected void focus()
Focusable
.protected AbstractComponentState getState()
getState
in class AbstractClientConnector
protected AbstractComponentState getState(boolean markAsDirty)
AbstractClientConnector
getState
in class AbstractClientConnector
markAsDirty
- true if the connector should automatically be marked dirty,
false otherwiseAbstractClientConnector.getState()
public void beforeClientResponse(boolean initial)
ClientConnector
beforeClientResponse
in interface ClientConnector
beforeClientResponse
in class AbstractClientConnector
initial
- true
if the client-side connector will be created
and initialized after this method has been invoked.
false
if there is already an initialized
client-side connector.public Registration addListener(Component.Listener listener)
Component
class Listening extends CustomComponent implements Listener { // Stored for determining the source of an event Button ok; Label status; // For displaying info about the event public Listening() { VerticalLayout layout = new VerticalLayout(); // Some miscellaneous component TextField name = new TextField("Say it all here"); name.addListener(this); layout.addComponent(name); // Handle button clicks as generic events instead // of Button.ClickEvent events ok = new Button("OK"); ok.addListener(this); layout.addComponent(ok); // For displaying information about an event status = new Label(""); layout.addComponent(status); setCompositionRoot(layout); } public void componentEvent(Event event) { // Act according to the source of the event if (event.getSource() == ok) getWindow().showNotification("Click!"); status.setValue( "Event from " + event.getSource().getClass().getName() + ": " + event.getClass().getName()); } } Listening listening = new Listening(); layout.addComponent(listening);
addListener
in interface Component
listener
- the new Listener to be registered.Component.Event
,
Registration
@Deprecated public void removeListener(Component.Listener listener)
Component
removeListener
in interface Component
listener
- the listener to be removed.Component.addListener(Listener)
protected void fireComponentEvent()
protected void fireComponentErrorEvent()
public void setData(Object data)
data
- the Application specific data.public Object getData()
setData(Object)
.public float getHeight()
Sizeable
public Sizeable.Unit getHeightUnits()
Sizeable
getHeightUnits
in interface Sizeable
public float getWidth()
Sizeable
public Sizeable.Unit getWidthUnits()
Sizeable
getWidthUnits
in interface Sizeable
public void setHeight(float height, Sizeable.Unit unit)
Sizeable
public void setSizeFull()
Sizeable
setSizeFull
in interface Sizeable
public void setSizeUndefined()
Sizeable
setSizeUndefined
in interface Sizeable
public void setWidthUndefined()
Sizeable
setWidthUndefined
in interface Sizeable
public void setHeightUndefined()
Sizeable
setHeightUndefined
in interface Sizeable
public void setWidth(float width, Sizeable.Unit unit)
Sizeable
public void setWidth(String width)
Sizeable
public void setHeight(String height)
Sizeable
public void readDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Component
The component is responsible not only for updating its own state but also for ensuring that its children update their state based on the design.
It is assumed that the component is in its default state when this method is called. Reading should only take into consideration attributes specified in the design and not reset any unspecified attributes to their defaults.
This method must not modify the design.
readDesign
in interface Component
design
- The element to obtain the state fromdesignContext
- The DesignContext instance used for parsing the designpublic void setResponsive(boolean responsive)
responsive
- boolean enables responsiveness, false disablespublic boolean isResponsive()
protected void setReadOnly(boolean readOnly)
AbstractComponent
.
This method should be made public in Components
that
implement HasValue
.readOnly
- a boolean value specifying whether the component is put
read-only mode or notprotected boolean isReadOnly()
AbstractComponent
. This method should be made public in
Components
that implement HasValue
.true
if state has read-only on; false
if notsetReadOnly(boolean)
protected Collection<String> getCustomAttributes()
readDesign(Element, DesignContext)
and writeDesign(Element, DesignContext)
methods. Typically these
are handled in a custom way in the overridden versions of the above
methodspublic void writeDesign(org.jsoup.nodes.Element design, DesignContext designContext)
Component
The component is responsible not only for writing its own state but also for ensuring that its children write their state to the design.
This method must not modify the component state.
writeDesign
in interface Component
design
- The element to write the component state to. Any previous
attributes or child nodes are not cleared.designContext
- The DesignContext instance used for writing the designprotected ActionManager getActionManager()
ActionManager
used to manage the
ShortcutListener
s added to this component.public Registration addShortcutListener(ShortcutListener shortcut)
@Deprecated public void removeShortcutListener(ShortcutListener shortcut)
protected boolean isOrHasAncestor(Component content)
content
component is equal to, or the
ancestor of this component.content
- the potential ancestor elementtrue
if the relationship holdspublic Registration addContextClickListener(ContextClickEvent.ContextClickListener listener)
ContextClickEvent.ContextClickNotifier
addContextClickListener
in interface ContextClickEvent.ContextClickNotifier
listener
- the context click listener to add, not nullRegistration
@Deprecated public void removeContextClickListener(ContextClickEvent.ContextClickListener listener)
ContextClickEvent.ContextClickNotifier
#addContextClickListener(ContextClickListener)
.removeContextClickListener
in interface ContextClickEvent.ContextClickNotifier
listener
- the context click listener to removeprotected void setRequiredIndicatorVisible(boolean visible)
If the component supports the required indicator (state extends
AbstractFieldState
), then expose this method and
isRequiredIndicatorVisible()
as public
in the component
and call this method.
This method will throw a IllegalStateException
if the component
state (returned by getState()
) does not inherit
AbstractFieldState
.
visible
- true
to make the required indicator visible,
false
if notprotected boolean isRequiredIndicatorVisible()
This method will throw a IllegalStateException
if the component
state (returned by getState()
) does not inherit
AbstractFieldState
.
true
if visible, false
if notsetRequiredIndicatorVisible(boolean)
Copyright © 2018 Vaadin Ltd. All rights reserved.