Package com.storedobject.vaadin
Class Application
java.lang.Object
com.storedobject.vaadin.Application
- Direct Known Subclasses:
Application
Application is the base class for creating a 'single page' web applications. The 'single page' should be defined
by extending
ApplicationView class.
public class Demo extends Application {
@Override
protected ApplicationLayout createLayout() {
return new AppFrame();
}
private static class AppFrame extends ApplicationFrame {
@Override
public void drawMenu(Application application) {
setCaption("Sample Application Ver 1.0.4");
addToNavbar(new SpeakerButton());
ApplicationMenu menu = getMenu();
ApplicationMenuItem ami;
ami = application.createMenuItem("One", () -> Notification.show("Hello World!"));
menu.add(ami);
ami = application.createMenuItem("Two", () -> Notification.show("Hello World 2!"));
menu.add(ami);
ami = application.createMenuItem("Greeting", () -> {
Application a = Application.get();
a.speak("Hello, how are you?");
if(!a.isSpeakerOn()) {
Notification.show("Speaker is off! Click on the speaker button to turn it on.");
}
});
menu.add(ami);
}
}
@Route("")
public static class AppView extends ApplicationView {
@Override
protected Application createApplication() {
return new Demo();
}
}
}
The 'single page' can be any layout component that implements the interface ApplicationLayout. The layout
typically contains a 'menu' area and 'content' area. One can use Vaadin's AppLayout or similar components as the base for this.
The 'menu' area will contain ApplicationMenuItem instances and when a 'menu item' is clicked, the Runnable
action associated with it will be executed. One may associate any Runnable action with a 'menu item' such as generating a report or
invoking a View. If a View is invoked, its associated 'view component' is
displayed in the 'content' area and its 'caption' is inserted as a new 'menu item' in the 'menu' area. The 'content' area displays only the 'active
view' (currently selected or executed view) and hides all previously displayed 'views' but any of those 'views' can be made active again
by licking on its respective 'menu item' created from its 'caption'.- Author:
- Syam
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaccess(com.vaadin.flow.server.Command command) Lock the UI and execute a command.com.vaadin.flow.shared.RegistrationaddBrowserResizedListener(ResizedListener listener) Add a browser resized listener to this application so that the listener will be alerted whenever the browser is resized.com.vaadin.flow.shared.RegistrationaddContentResizedListener(ResizedListener listener) Add a content resized listener so that the listener will be alerted whenever the "content area" of the application is resized.voidalertCountChanged(Object owner) This method is invoked whenever alter count changes for a particular owner.voidattached()Invoked whenever this application is attached to a UI.static voidclearAlert(Alert alert) Clear an alert.static voidClear all alerts.static voidclearAlerts(Object owner) Clear all alerts of the specified owner.voidclose()Close the application by closing all registered "resources".protected ApplicationEnvironmentAn "application environment" may be created to specify certain behaviours of the application.protected abstract ApplicationLayoutThis method is invoked only once to determine the layout of the application.createMenuItem(String label, Runnable menuAction) Create a menu item.createMenuItem(String label, String icon, Runnable menuAction) Create a menu item.createMenuItemGroup(String label) Create a menu item.voiddetached()Invoked whenever this application is detached from its UI (it may get attached again to another UI if the user just refreshed the browser).static voidShow an error message from the parameter passed.static voidShow an error message from the parameter passed.voidExecute a command.voidExecute a command.static Applicationget()Get the current application.static Applicationget(com.vaadin.flow.component.UI ui) Get the application for the given UI.Get the currently activeView.intstatic intgetAlertCount(Object owner) Get the number of alerts for a specified owner.Get alert list for a specified owner.Get the caption for this application.Get theAlertthat can be used for showing common messages.intGet the height of the "content area" of the application.intGet the width of the "content area" of the application.Get ISO 2-character the country-code of the user in capital letters.<T> TGet the data that was previously set in this application.intGet the device (browser) height.intGet the device (browser) width.final ApplicationEnvironmentGet the current "application environment".Get an identifier of the application (can be used for logging etc.).Get the IP address of the application.Get the "context path" of the application.Retrieves the current locale used in the application view.intGet the major version number (from browser information).intGet the minor version number (from browser information).final com.vaadin.flow.component.page.PagegetPage()Get the browser Page.getQueryParameter(String key) Get the query parameter value passed to this application.final com.vaadin.flow.component.UIgetUI()Get the UI associated with this Application.protected Consumer<com.vaadin.flow.component.UI> Get UI configurator if any so that UI will be passed to it for handling any special configuration.getURL()get tje URL of application.getViewFor(com.vaadin.flow.component.Component component) Get the view in which a particular component is currently appearing.com.vaadin.flow.server.WebBrowserGet the representation of current the "web browser".protected voidinit(com.vaadin.flow.server.VaadinRequest request) This method is invoked byApplicationViewclass.protected booleanThis method is invoked when the application comes up.booleanCheck whether the application is currently in full-screen mode or not.booleanInternally used bySpeakerButtonto check the status of the speaker.booleanCheck whether still speaking the previously set sentences.voidLog something (goes to the System error stream).voidLog something along with an exception (goes to the System error stream).protected final voidloggedin()This method must be called fromlogin()when login credentials are verified.protected voidlogin()This method can be overridden to accept login credentials andloggedin()must be called if successfully logged in.static voidShow a message from the parameter passed.static voidShow a message from the parameter passed.static voidregisterAlert(Alert alert, Object owner) Register an alert with the application.voidregisterResource(Closeable resource) Register a "resource" that will be closed when the application is shutdown.<T> voidremoveData(Class<T> anyClass) Remove the data previously set in this application.<T> voidremoveData(T anything) Remove the data previously set in this application.voidRemove the query parameter value passed to this application.voidRemove all query parameter values passed to this application.voidsetCaption(String caption) Set caption for the application (It will set the caption for theApplicationLayoutand thePage).<T> voidSet some data in this application so that it can be retrieved later.voidSet some data in this application so that it can be retrieved later.voidSet the locale for this Application.voidsetPollInterval(int intervalInMillis) Set the polling interval with this application as the owner.voidsetPollInterval(Object owner, int intervalInMillis) Set polling interval.voidsetPostFocus(com.vaadin.flow.component.Component component) Set a component to be focused later when the current "view" is selected again.static voidShow all alerts.static voidshowAlerts(Object owner) Show all alerts of the specified owner.voidshowNotification(String text) Show a notification on the screen.voidshowNotification(String caption, String text) Show a notification on the screen.voidshowNotification(String caption, Throwable error) Show error notification.voidshowNotification(Throwable error) Show error notification,voidSpeak out the given sentence if theApplicationLayoutis supporting it (The default layoutApplicationFramesupports it).voidstartPolling(Object owner) Start polling by setting the interval to 1000 milliseconds.voidstopPolling(Object owner) Stop polling.static voidShow a message on the tray from the parameter passed.static voidShow a message on the tray from the parameter passed.protected voidviewDetached(View view) This method is invoked whenever a runningViewis detached.static voidShow a warning message from the parameter passed.static voidShow a warning message from the parameter passed.
-
Constructor Details
-
Application
public Application()
-
-
Method Details
-
init
protected void init(com.vaadin.flow.server.VaadinRequest request) This method is invoked byApplicationViewclass. If you want to override this method, make sure thatsuperis called.- Parameters:
request- Vaadin Request
-
getQueryParameter
Get the query parameter value passed to this application. This is typically used by some logic that brings up the opening view of the application. It is advisable to remove the parameter after its usage by invokingremoveQueryParameter(String)orremoveQueryParameters()due to security reasons. Otherwise, some other logic may access it.Note: Only one parameter is allowed per key. If multiple values are passed for the same key, only one will be made available.
- Parameters:
key- Key for which value needs to be obtained.- Returns:
- Value corresponding to the key or
nullif no such key exists.
-
removeQueryParameter
Remove the query parameter value passed to this application.- Parameters:
key- Key for which value needs to be removed.
-
removeQueryParameters
public void removeQueryParameters()Remove all query parameter values passed to this application. -
getWebBrowser
public com.vaadin.flow.server.WebBrowser getWebBrowser()Get the representation of current the "web browser".- Returns:
- Current web browser.
-
getPage
public final com.vaadin.flow.component.page.Page getPage()Get the browser Page.- Returns:
- Current page.
-
addBrowserResizedListener
Add a browser resized listener to this application so that the listener will be alerted whenever the browser is resized.- Parameters:
listener- Listener- Returns:
- Registration.
-
getCommonAlert
-
getUIConfigurator
Get UI configurator if any so that UI will be passed to it for handling any special configuration. This will be invoked beforesetUI(UI)is called.- Returns:
- Default implementation returns
null.
-
getUI
public final com.vaadin.flow.component.UI getUI()Get the UI associated with this Application.- Returns:
- The UI.
-
attached
public void attached()Invoked whenever this application is attached to a UI. Default implementation does nothing. -
detached
public void detached()Invoked whenever this application is detached from its UI (it may get attached again to another UI if the user just refreshed the browser). The default implementation closes the application (close()) after 20 seconds. -
setLocale
Set the locale for this Application.- Parameters:
locale- Locate to set
-
getLocale
Retrieves the current locale used in the application view.- Returns:
- the Locale object representing the current locale.
-
access
Lock the UI and execute a command. After executing the command, UI changes, if any, will be sent to the browser.Note: All errors are silently ignored. If you want any control over errors/exceptions, please handle it in the commands itself.
- Parameters:
command- Command to execute.- Returns:
- A future that can be used to check for task completion and to cancel the command.
-
init
This method is invoked when the application comes up.- Parameters:
link- The context path of the application.- Returns:
- True if application can go ahead. Otherwise, an "Initialization failed" message is displayed.
Default return value is
true.
-
createLayout
This method is invoked only once to determine the layout of the application.- Returns:
- Application layout.
-
createEnvironment
An "application environment" may be created to specify certain behaviours of the application. If this method returnsnull, a default "environment" will be created.- Returns:
- Returns null by default.
-
getEnvironment
Get the current "application environment".- Returns:
- Application environment.
-
createMenuItem
Create a menu item.- Parameters:
label- Label of the menu itemmenuAction- Action to execute when menu item is clicked- Returns:
- Menu item. By default, the menu item will be created from the "application environment"
ApplicationEnvironment.createMenuItem(String, String, Runnable)
-
createMenuItem
Create a menu item.- Parameters:
label- Label of the menu itemicon- Icon to be usedmenuAction- Action to execute when menu item is clicked- Returns:
- Menu item. By default, the menu item will be created from the "application environment"
ApplicationEnvironment.createMenuItem(String, String, Runnable)
-
createMenuItemGroup
Create a menu item.- Parameters:
label- Label of the menu item- Returns:
- Menu item. By default, the menu item will be created from the "application environment"
ApplicationEnvironment.createMenuItemGroup(String)
-
close
public void close()Close the application by closing all registered "resources". If the associated session is not closed and no other Application instance exists, it will also be closed. -
registerResource
Register a "resource" that will be closed when the application is shutdown.- Parameters:
resource- Resource to close.
-
get
-
get
Get the application for the given UI.- Parameters:
ui- UI for which application needs to be obtained- Returns:
- Application for the given UI.
-
registerAlert
-
getAlerts
-
getAlertCount
Get the number of alerts for a specified owner.- Parameters:
owner- Owner of the alert- Returns:
- Number of alert. (-1 is returned if application is not reachable)
-
alertCountChanged
This method is invoked whenever alter count changes for a particular owner.- Parameters:
owner- Alert owner
-
showAlerts
public static void showAlerts()Show all alerts. -
showAlerts
Show all alerts of the specified owner.- Parameters:
owner- Owner
-
clearAlerts
public static void clearAlerts()Clear all alerts. (Alerts with any associated "click action" will not be removed). -
clearAlerts
Clear all alerts of the specified owner. (Alerts associated with any "click action" will not be removed).- Parameters:
owner- Owner
-
clearAlert
-
warning
Show a warning message from the parameter passed. The parameter will be converted toStringby invoking the methodApplicationEnvironment.toDisplay(Object).- Parameters:
message- Message.
-
tray
Show a message on the tray from the parameter passed. The parameter will be converted toStringby invoking the methodApplicationEnvironment.toDisplay(Object).- Parameters:
message- Message.
-
message
Show a message from the parameter passed. The parameter will be converted toStringby invoking the methodApplicationEnvironment.toDisplay(Object).- Parameters:
message- Message.
-
error
Show an error message from the parameter passed. The parameter will be converted toStringby invoking the methodApplicationEnvironment.toDisplay(Object).- Parameters:
message- Message.
-
warning
Show a warning message from the parameter passed. The parameter will be converted toStringby invoking the methodApplicationEnvironment.toDisplay(Object).- Parameters:
owner- Owner of the message.message- Message.
-
tray
Show a message on the tray from the parameter passed. The parameter will be converted toStringby invoking the methodApplicationEnvironment.toDisplay(Object).- Parameters:
owner- Owner of the message.message- Message.
-
message
Show a message from the parameter passed. The parameter will be converted toStringby invoking the methodApplicationEnvironment.toDisplay(Object).- Parameters:
owner- Owner of the message.message- Message.
-
error
Show an error message from the parameter passed. The parameter will be converted toStringby invoking the methodApplicationEnvironment.toDisplay(Object).- Parameters:
owner- Owner of the message.message- Message.
-
log
-
log
-
getLinkName
-
getDeviceHeight
public int getDeviceHeight()Get the device (browser) height.- Returns:
- Device height.
-
getDeviceWidth
public int getDeviceWidth()Get the device (browser) width.- Returns:
- Device width.
-
getURL
-
getContentHeight
public int getContentHeight()Get the height of the "content area" of the application.- Returns:
- Pixel height of the content area.
-
getContentWidth
public int getContentWidth()Get the width of the "content area" of the application.- Returns:
- Pixel width of the content area.
-
addContentResizedListener
Add a content resized listener so that the listener will be alerted whenever the "content area" of the application is resized.- Parameters:
listener- Listener- Returns:
- Registration.
-
showNotification
Show a notification on the screen.- Parameters:
text- Text of the notification
-
showNotification
-
showNotification
-
showNotification
-
setCaption
Set caption for the application (It will set the caption for theApplicationLayoutand thePage).- Parameters:
caption- Caption to be set
-
speak
Speak out the given sentence if theApplicationLayoutis supporting it (The default layoutApplicationFramesupports it). Please note that the speaker must have been switched on before calling this (SeeSpeakerButton).- Parameters:
sentence- Sentence to speak out.
-
isSpeaking
public boolean isSpeaking()Check whether still speaking the previously set sentences.- Returns:
- True/false.
-
isSpeakerOn
public boolean isSpeakerOn()Internally used bySpeakerButtonto check the status of the speaker.- Returns:
- True if the speaker is on.
-
login
protected void login()This method can be overridden to accept login credentials andloggedin()must be called if successfully logged in. -
getIPAddress
-
getIdentifier
Get an identifier of the application (can be used for logging etc.).- Returns:
- Ab identifier derived from the browser information.
-
getMajorVersion
public int getMajorVersion()Get the major version number (from browser information).- Returns:
- Major version number.
-
getMinorVersion
public int getMinorVersion()Get the minor version number (from browser information).- Returns:
- Minor version number.
-
loggedin
protected final void loggedin()This method must be called fromlogin()when login credentials are verified. -
setPostFocus
public void setPostFocus(com.vaadin.flow.component.Component component) Set a component to be focused later when the current "view" is selected again. This is useful in situations like bringing back to the current "data entry" screen to a particular field.- Parameters:
component- Component to be focused later.
-
getActiveView
-
setPollInterval
public void setPollInterval(int intervalInMillis) Set the polling interval with this application as the owner. (Please seesetPollInterval(Object, int)).- Parameters:
intervalInMillis- Interval in milliseconds
-
setPollInterval
Set polling interval. Several owners may be requesting it and the value actually set will the lowest value. It is the owner's responsibility to release polling by invoking stopPolling when polling is no more required.- Parameters:
owner- Object that is invoking the requestintervalInMillis- Interval in milliseconds- See Also:
-
startPolling
Start polling by setting the interval to 1000 milliseconds.- Parameters:
owner- Object that is invoking the request
-
stopPolling
Stop polling. Polling will not be stopped if requests are there from other owners.- Parameters:
owner- Owner who requested for polling earlier
-
getViewFor
Get the view in which a particular component is currently appearing.- Parameters:
component- Component to be checked- Returns:
- View in which the component is appearing or
nullif it's not displayed in any of the views.
-
viewDetached
-
getActiveViewCount
-
getActiveViews
-
execute
Execute a command. (The command will be executed only if the current UI is associated with this application).- Parameters:
command- Command to execute by invoking itsRunnable.run()method.clearViews- Iftrueis passed, allViews will be closed (by invokingView.abort()calls) before the command is executed.
-
execute
Execute a command. (The command will be executed only if the current UI is associated with this application).- Parameters:
command- Command to execute by invoking itsRunnable.run()method.
-
setData
Set some data in this application so that it can be retrieved later.- Parameters:
anything- Data (An instance of anyClass).
-
setData
Set some data in this application so that it can be retrieved later.- Type Parameters:
T- Type of data.- Parameters:
anyClass- Kind of data.anything- Data (An instance of anyClass).
-
removeData
public <T> void removeData(T anything) Remove the data previously set in this application.- Type Parameters:
T- Type of data.- Parameters:
anything- Data to remove.
-
removeData
Remove the data previously set in this application.- Type Parameters:
T- Type of data.- Parameters:
anyClass- Class of the data to remove.
-
getData
Get the data that was previously set in this application.- Type Parameters:
T- Type of data.- Parameters:
anyClass- Class of the data to get.- Returns:
- Data if exists, otherwise
null.
-
getCountry
Get ISO 2-character the country-code of the user in capital letters. (Examples: "US" for US, "IN" for India). This information is used for formatting the numbers inNumericField.- Returns:
- ISO country code. (Default is "US").
-
getCaption
Get the caption for this application. By default,ApplicationLayout.getCaption()is returned.- Returns:
- Caption.
-
isFullScreenMode
public boolean isFullScreenMode()Check whether the application is currently in full-screen mode or not.- Returns:
- True/false.
-