Class WButton
- java.lang.Object
-
- com.github.bordertech.wcomponents.AbstractWComponent
-
- com.github.bordertech.wcomponents.WBeanComponent
-
- com.github.bordertech.wcomponents.WButton
-
- All Implemented Interfaces:
AccessKeyable
,AjaxTarget
,AjaxTrigger
,BeanAware
,BeanBound
,BeanProviderBound
,Container
,DataBound
,DialogOpenTrigger
,Disableable
,SubordinateTarget
,WComponent
,WebComponent
,Serializable
- Direct Known Subclasses:
UicProfileButton
,WCancelButton
,WConfirmationButton
,WPrintButton
public class WButton extends WBeanComponent implements Container, Disableable, AjaxTrigger, AjaxTarget, SubordinateTarget, DialogOpenTrigger, AccessKeyable
A WButton is used to submit the contents of the form to the server. An
Action
can be associated with the button to execute application-specific code when the button is pressed.// Create a button with the text "Submit". WButton button = new WButton("Submit"); // Set an action to run when the button is clicked. button.setAction(new Action() { public void execute(final ActionEvent event) { System.out.println("The button was clicked at " + new Date()); } });
- Since:
- 1.0.0
- Author:
- James Gifford
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
WButton.ButtonModel
Holds the extrinsic state information of a WButton.static class
WButton.ImagePosition
This is used to control the position of the image on the button relative to the text label.-
Nested classes/interfaces inherited from class com.github.bordertech.wcomponents.AbstractWComponent
AbstractWComponent.WComponentRef
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
NO_VALUE
The default value to use when no value has been explicitly specified.-
Fields inherited from interface com.github.bordertech.wcomponents.WComponent
DEFAULT_APPLICATION_ID, DEFAULT_INTERNAL_ID, DEFAULT_NO_ID, ID_CONTEXT_SEPERATOR, ID_FRAMEWORK_ASSIGNED_SEPERATOR, ID_VALIDATION_PATTERN
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
afterActionExecute(Request request)
Called after the button action has been executed.protected void
beforeActionExecute(Request request)
Called before the button action is executed.protected void
clearPressed()
Clear button is pressed flag.protected void
focusMe()
Sets the focus back to this button unless explicity set elsewhere.char
getAccessKey()
Action
getAction()
String
getActionCommand()
Same as getValue().Object
getActionObject()
Returns the data object that has been associated with this button, else null.AjaxTarget
getAjaxTarget()
WComponent
getChildAt(int index)
Retrieves a child component by its index.int
getChildCount()
List<WComponent>
getChildren()
Retrieves a list of this Container's.protected WButton.ButtonModel
getComponentModel()
Returns the effective component model for this component.Image
getImage()
Return the image to display on the button.WImage
getImageHolder()
WButton.ImagePosition
getImagePosition()
String
getImageUrl()
Return the URL of the image to display on the button.int
getIndexOfChild(WComponent childComponent)
Retrieves the index of the given child.String
getMessage()
protected WButton.ButtonModel
getOrCreateComponentModel()
Retrieves the model for this component so that it can be modified.String
getText()
Return the button text.String
getValue()
Return the button value.void
handleRequest(Request request)
Override handleRequest in order to perform processing for this component.boolean
isAjax()
Indicates whether this button is AJAX enabled.boolean
isCancel()
boolean
isClientCommandOnly()
boolean
isDisabled()
Indicates whether this button is disabled in the given context.boolean
isPopupTrigger()
Indicates whether this button will trigger a WPopup when used.boolean
isPressed()
Indicates whether this button has been pressed for the current request.boolean
isRenderAsLink()
boolean
isUnsavedChanges()
Indicates whether there are unsaved changes in the given context.protected WButton.ButtonModel
newComponentModel()
Creates a new component model appropriate for this component.protected void
preparePaintComponent(Request request)
Override preparePaintComponent to register an AJAX operation if this button is AJAX enabled.void
setAccessKey(char accessKey)
Set the access key on the component.void
setAction(Action action)
Sets the action that you want run if the button is pressed.void
setActionCommand(String actionCommand)
Same as setValue().void
setActionObject(Serializable data)
Associate this button with a data object that can be easily accessed in the execute() method of the button's associated Action.void
setAjaxTarget(AjaxTarget ajaxTarget)
Sets the AJAX target for the button.void
setCancel(boolean cancel)
Sets the button as a cancel button.void
setClientCommandOnly(boolean client)
Sets whether this button should fire a client command without submitting the form.void
setDisabled(boolean disabled)
Sets whether this button is disabled when loaded.void
setImage(Image image)
Sets the image to display on the button.void
setImage(String image)
Sets the image to display on the button.void
setImagePosition(WButton.ImagePosition imagePosition)
The position of the image on the button relative to the button text.void
setImageUrl(String imageUrl)
Sets the URL of the image to display on the button.void
setMessage(String message, Serializable... args)
Sets the confirmation message for the button.void
setPopupTrigger(boolean popupTrigger)
Sets whether this button will trigger a WPopup when used.protected void
setPressed(boolean pressed, Request request)
Sets whether this button is pressed.void
setRenderAsLink(boolean renderAsLink)
Sets whether this button should be rendered like a link.void
setText(String text, Serializable... args)
Sets the button text.void
setUnsavedChanges(boolean hasUnsavedChanges)
This feature exists to pass information to the client to inform it that the server side thinks there are unsaved changes.void
setValue(String value)
Set the button value.String
toString()
Creates a String representation of this component; usually for debugging purposes.-
Methods inherited from class com.github.bordertech.wcomponents.WBeanComponent
addBeanToScratchMap, doUpdateBeanValue, getBean, getBeanFromScratchMap, getBeanId, getBeanProperty, getBeanProvider, getBeanScratchMap, getBeanValue, getData, isBeanInScratchMap, isChanged, isSearchAncestors, isUseRequestScopeScratchMap, removeBeanFromScratchMap, resetData, setBean, setBeanId, setBeanProperty, setBeanProvider, setData, setSearchAncestors, updateBeanValue
-
Methods inherited from class com.github.bordertech.wcomponents.AbstractWComponent
addHtmlClass, addHtmlClass, addNotify, afterPaint, assertAddSupported, beforePaint, createErrorDiagnostic, createErrorDiagnostic, forward, getAccessibleText, getAttribute, getBaseUrl, getDefaultModel, getEnvironment, getHeaders, getHtmlClass, getHtmlClasses, getId, getIdName, getInternalId, getLabel, getName, getParent, getScratchMap, getTabIndex, getTag, getTemplate, getTemplateMarkUp, getToolTip, hasNoComponentModel, hasTabIndex, initialiseComponentModel, invokeLater, invokeLaters, isDebugStructure, isDefaultState, isFlagSet, isHidden, isInitialised, isLocked, isTracking, isTrackingEnabled, isValidate, isVisible, paint, paintComponent, preparePaint, removeAttribute, removeComponentModel, removeHtmlClass, removeHtmlClass, removeNotify, replaceWComponent, reset, serviceRequest, setAccessibleText, setAttribute, setEnvironment, setFlag, setFocussed, setHidden, setHtmlClass, setHtmlClass, setIdName, setInitialised, setLocked, setTag, setToolTip, setTrackingEnabled, setValidate, setVisible, showErrorIndicators, showErrorIndicatorsForComponent, showWarningIndicators, showWarningIndicatorsForComponent, tidyUpUIContext, tidyUpUIContextForTree, validate, validateComponent, writeReplace
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.github.bordertech.wcomponents.AccessKeyable
getAccessKeyAsString
-
Methods inherited from interface com.github.bordertech.wcomponents.WComponent
addHtmlClass, addHtmlClass, forward, getAccessibleText, getAttribute, getBaseUrl, getEnvironment, getHeaders, getHtmlClass, getHtmlClasses, getId, getIdName, getInternalId, getLabel, getName, getParent, getTabIndex, getTag, getToolTip, hasTabIndex, invokeLater, isDefaultState, isHidden, isInitialised, isLocked, isTracking, isTrackingEnabled, isValidate, isVisible, paint, preparePaint, removeAttribute, removeHtmlClass, removeHtmlClass, reset, serviceRequest, setAccessibleText, setAttribute, setEnvironment, setFocussed, setHtmlClass, setHtmlClass, setIdName, setInitialised, setLocked, setTag, setToolTip, setTrackingEnabled, setValidate, setVisible, showErrorIndicators, showWarningIndicators, tidyUpUIContextForTree, validate
-
-
-
-
Field Detail
-
NO_VALUE
protected static final String NO_VALUE
The default value to use when no value has been explicitly specified.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
WButton
public WButton()
Creates a WButton with no text or image. The button text must be set after construction.
-
WButton
public WButton(String text)
Creates a WButton with the specified text.- Parameters:
text
- the button text, usingMessageFormat
syntax.// Will create a button with the text "Hello world" new WButton("Hello world");
-
WButton
public WButton(String text, char accessKey)
Constructor. Set the button text and access key. Access keys are not case sensitive.- Parameters:
text
- The button text.accessKey
- The shortcut key that activates the button.
-
-
Method Detail
-
isDisabled
public boolean isDisabled()
Indicates whether this button is disabled in the given context.- Specified by:
isDisabled
in interfaceDisableable
- Returns:
- true if this button is disabled, otherwise false.
-
setDisabled
public void setDisabled(boolean disabled)
Sets whether this button is disabled when loaded. If your button is disabled by aWSubordinateControl
then it is not necessary to explicitly set it has disabled.- Specified by:
setDisabled
in interfaceDisableable
- Parameters:
disabled
- if true, this button is disabled. If false, it is enabled
-
handleRequest
public void handleRequest(Request request)
Override handleRequest in order to perform processing for this component. This implementation checks whether the button has been pressed in the request.- Specified by:
handleRequest
in interfaceWComponent
- Overrides:
handleRequest
in classAbstractWComponent
- Parameters:
request
- the request being responded to.
-
preparePaintComponent
protected void preparePaintComponent(Request request)
Override preparePaintComponent to register an AJAX operation if this button is AJAX enabled.- Overrides:
preparePaintComponent
in classAbstractWComponent
- Parameters:
request
- the request being responded to
-
clearPressed
protected void clearPressed()
Clear button is pressed flag.
-
isPressed
public boolean isPressed()
Indicates whether this button has been pressed for the current request.- Returns:
- true if this button is pressed, false otherwise
-
setPressed
protected void setPressed(boolean pressed, Request request)
Sets whether this button is pressed. You probably do not want to invoke this manually, it is called fromhandleRequest(com.github.bordertech.wcomponents.Request)
. If the button is pressed its Action is queued so it is invoked after the entire request has been handled.- Parameters:
pressed
- true for pressed, false for not pressedrequest
- the Request that is being responded to
-
beforeActionExecute
protected void beforeActionExecute(Request request)
Called before the button action is executed. Subclasses may override. Provides an opportunity to do preparation work before the action.execute- Parameters:
request
- the request that is being responded to.
-
afterActionExecute
protected void afterActionExecute(Request request)
Called after the button action has been executed. Subclasses may override. Provides an opportunity to do cleanup work after the action.execute.- Parameters:
request
- the request that is being responded to.
-
focusMe
protected void focusMe()
Sets the focus back to this button unless explicity set elsewhere.
-
getAction
public Action getAction()
- Returns:
- the action to execute when the button is pressed
-
setAction
public void setAction(Action action)
Sets the action that you want run if the button is pressed.- Parameters:
action
- the action to execute when the button is pressed// Causes a message to be printed to the log whenever the button is pressed. setAction(new Action() { public void execute(final ActionEvent event) { LOG.info("The button was clicked at " + new Date()); } });
-
getText
public String getText()
Return the button text. Returns:- user text if set; otherwise
- shared text if set;
- user value if set;
- bean value if present; or
- shared value.
- Returns:
- the button text
-
setText
public void setText(String text, Serializable... args)
Sets the button text. A button must have text even if it only displays an image. In this case the text is used to provide a text alternative to the image.- Parameters:
text
- the button text, usingMessageFormat
syntaxargs
- optional arguments for the button text format string// Sets the button text to "Hello world" myButton.setText("Hello world");
// Sets the button text to "Secret agent James Bond, 007" myButton.setText("Secret agent {0}, {1,number,000}", "James Bond", 7);
-
getValue
public String getValue()
Return the button value. By default the value is the same as the text placed on the button.- Returns:
- the button value
-
setValue
public void setValue(String value)
Set the button value.- Parameters:
value
- the button value
-
getActionCommand
public String getActionCommand()
Same as getValue(). This method exists simply to clarify the relationship between the WButton, its Action, and the ActionEvent sent to the execute() method of the Action.- Returns:
- the action command
-
setActionCommand
public void setActionCommand(String actionCommand)
Same as setValue(). This method exists simply to clarify the relationship between the WButton, its Action, and the ActionEvent sent to the execute() method of the Action.- Parameters:
actionCommand
- the action command
-
getActionObject
public Object getActionObject()
Returns the data object that has been associated with this button, else null. For convenience, this data object is passed to the execute() method of the button's associated Action, in the ActionEvent parameter.- Returns:
- the action object
-
setActionObject
public void setActionObject(Serializable data)
Associate this button with a data object that can be easily accessed in the execute() method of the button's associated Action.- Parameters:
data
- the action object
-
getImage
public Image getImage()
Return the image to display on the button.- Returns:
- the image
-
setImage
public void setImage(Image image)
Sets the image to display on the button. The text alternative for the image is generated from the button text.- Parameters:
image
- the image, or null for no image
-
setImage
public void setImage(String image)
Sets the image to display on the button. The image will be read from the application's class path rather than from its web docs. The text alternative for the image is generated from the button text.- Parameters:
image
- the relative path to the image resource, or null for no image
-
getImageHolder
public WImage getImageHolder()
Return theWImage
used by this button to hold theImage
resource.If the button is not using an Image resource, it will return null.
- Returns:
- the WImage holding the Image resource or null if the button is not using an Image resource
-
getImageUrl
public String getImageUrl()
Return the URL of the image to display on the button.- Returns:
- the image url
-
setImageUrl
public void setImageUrl(String imageUrl)
Sets the URL of the image to display on the button. This is the optimum way to set an image on a button as it allows the image to be served from a CDN and cached on the client for future use. In this way the same "image" may be used for many buttons (or other components) with no extra bandwidth use. The text alternative for the image is generated from the button text.- Parameters:
imageUrl
- the image url, or null for no image
-
getImagePosition
public WButton.ImagePosition getImagePosition()
- Returns:
- the position of the image
-
setImagePosition
public void setImagePosition(WButton.ImagePosition imagePosition)
The position of the image on the button relative to the button text. If the button has an image and this is not set then the button text is used as the text alternative for the image and is not displayed on the button.- Parameters:
imagePosition
- the position of the image
-
isRenderAsLink
public boolean isRenderAsLink()
- Returns:
- true if this button should be rendered as a link.
-
setRenderAsLink
public void setRenderAsLink(boolean renderAsLink)
Sets whether this button should be rendered like a link. This is a visual analog to a link, it does not cause the button to output a html 'a' element since the button still has to have a button element to accessibly undertake performance of any action which is not navigation. If you want to navigate with a link then you should be using WLink.- Parameters:
renderAsLink
- true if this button should be rendered like a link.
-
isClientCommandOnly
public boolean isClientCommandOnly()
- Returns:
true
if the button fires a client side command without causing a submit action. This is not necessarily incompatible with being an AjaxTrigger.
-
setClientCommandOnly
public void setClientCommandOnly(boolean client)
Sets whether this button should fire a client command without submitting the form. What this actually does is indicates to the client that the button element is not a submit button so a form submit will not be triggered when the button is pressed. If the button is also an AjaxTrigger then the Ajax request will still fire unless the custom command specifically prevents this.If this is true then interacting with the button on the client will not submit the form. A custom command will fire if one is attached to the button through custom JavaScript.
- Parameters:
client
- indicates if the current button is to trigger a client action without a submit
-
isPopupTrigger
public boolean isPopupTrigger()
Indicates whether this button will trigger a WPopup when used.- Returns:
- true if this button triggers a popup, false otherwise.
-
setPopupTrigger
public void setPopupTrigger(boolean popupTrigger)
Sets whether this button will trigger a WPopup when used. This flag is used to provide information to the client on the button's intent. The actual invocation of the WPopup is done elsewhere, e.g. in the button's action.This must be set on every button which has a submit action or causes an ajax action if the resulting payload contains a WPopup. This is mandatory to meet accessibility guidelines as it is the only way the UI can forewarn assistive technologies that a button will cause a pop-up.
- Parameters:
popupTrigger
- The popupTrigger to set.
-
getAccessKey
public char getAccessKey()
- Specified by:
getAccessKey
in interfaceAccessKeyable
- Returns:
- the component's access key.
-
setAccessKey
public void setAccessKey(char accessKey)
Description copied from interface:AccessKeyable
Set the access key on the component.- Specified by:
setAccessKey
in interfaceAccessKeyable
- Parameters:
accessKey
- the key that will form a keyboard shortcut to the component.
-
isAjax
public boolean isAjax()
Indicates whether this button is AJAX enabled. A button is an AJAX button if it has atarget set
.- Returns:
- true if this button is AJAX enabled, false otherwise
-
getAjaxTarget
public AjaxTarget getAjaxTarget()
- Returns:
- the default AJAX target for this button
-
setAjaxTarget
public void setAjaxTarget(AjaxTarget ajaxTarget)
Sets the AJAX target for the button. If a target is supplied, a an AJAX request is made rather than a round-trip to the server. The AJAX response will only contain the (possibly updated) target element rather than the entire UI. If a button has to target more than one component aWAjaxControl
should be used instead.- Parameters:
ajaxTarget
- the AJAX target.
-
isUnsavedChanges
public boolean isUnsavedChanges()
Indicates whether there are unsaved changes in the given context.- Returns:
- true if there are unsaved changes, false if not.
-
setUnsavedChanges
public void setUnsavedChanges(boolean hasUnsavedChanges)
This feature exists to pass information to the client to inform it that the server side thinks there are unsaved changes. It is used when a particular view is part of a multi-page process and the steps are not saved individually. The warning is triggered by a button or menu item withcancel set true
.- Parameters:
hasUnsavedChanges
- true if there are unsaved changes, false if not.
-
getMessage
public String getMessage()
- Returns:
- the confirmation message for the button.
-
setMessage
public void setMessage(String message, Serializable... args)
Sets the confirmation message for the button. If a button has a confirmation message then the user is asked to confirm each invocation of the button unless the button also hascancel set true
in which case the confirmation is only required if either there areunsaved changes on the server
or the user has changed the value of any control in the current view without saving.- Parameters:
message
- the confirmation message to display, usingMessageFormat
syntaxargs
- optional arguments for the message format string
-
isCancel
public boolean isCancel()
- Returns:
- true if the button is a cancel control and will warn the user of unsaved changes, otherwise false
-
setCancel
public void setCancel(boolean cancel)
Sets the button as a cancel button. A cancel button is the same as any other button but has the following characteristics:- it can trigger an unsaved changes warning; and
- it will not invoke client side validation;
The action applied to a cancel button is normally one to undo the current process and possibly navigate to an initial view. A cancel button could be used in contexts such as going backwards through a multi-step process; returning to a previous view without committing changes in the current view; or logging out of an application.
- Parameters:
cancel
- true if the button is a cancel control and will warn the user of unsaved changes, otherwise false
-
getChildCount
public int getChildCount()
- Specified by:
getChildCount
in interfaceContainer
- Returns:
- the number of child components currently contained within this component.
-
getChildAt
public WComponent getChildAt(int index)
Retrieves a child component by its index.- Specified by:
getChildAt
in interfaceContainer
- Parameters:
index
- the index of the child component to be retrieved.- Returns:
- the child component at the given index.
-
getIndexOfChild
public int getIndexOfChild(WComponent childComponent)
Retrieves the index of the given child.- Specified by:
getIndexOfChild
in interfaceContainer
- Parameters:
childComponent
- the child component to retrieve the index for.- Returns:
- the index of the given child component, or -1 if the component is not a child of this component.
-
getChildren
public List<WComponent> getChildren()
Description copied from interface:Container
Retrieves a list of this Container's.- Specified by:
getChildren
in interfaceContainer
- Returns:
- an immutable list of this Container.
-
toString
public String toString()
Description copied from class:AbstractWComponent
Creates a String representation of this component; usually for debugging purposes.- Overrides:
toString
in classAbstractWComponent
- Returns:
- a String representation of this component for debugging purposes
-
newComponentModel
protected WButton.ButtonModel newComponentModel()
Creates a new component model appropriate for this component.- Overrides:
newComponentModel
in classWBeanComponent
- Returns:
- a new ButtonModel
-
getComponentModel
protected WButton.ButtonModel getComponentModel()
Returns the effective component model for this component. Subclass may override this method to narrow the return type to their specific model type.- Overrides:
getComponentModel
in classWBeanComponent
- Returns:
- the effective component model
-
getOrCreateComponentModel
protected WButton.ButtonModel getOrCreateComponentModel()
Retrieves the model for this component so that it can be modified. If this method is called during request processing, and a session specific model does not yet exist, then a new model is created. Subclasses may override this method to narrow the return type to their specific model type.- Overrides:
getOrCreateComponentModel
in classWBeanComponent
- Returns:
- the model for this component
-
-