|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.vaadin.Application
public abstract class Application
Base class required for all Vaadin applications. This class provides all the
basic services required by Vaadin. These services allow external discovery
and manipulation of the user, windows
and
themes, and starting and stopping the application.
As mentioned, all Vaadin applications must inherit this class. However, this
is almost all of what one needs to do to create a fully functional
application. The only thing a class inheriting the Application
needs to do is implement the init
method where it creates the
windows it needs to perform its function. Note that all applications must
have at least one window: the main window. The first unnamed window
constructed by an application automatically becomes the main window which
behaves just like other windows with one exception: when accessing windows
using URLs the main window corresponds to the application URL whereas other
windows correspond to a URL gotten by catenating the window's name to the
application URL.
See the class com.vaadin.demo.HelloWorld
for a simple example of
a fully working application.
Window access. Application
provides methods to
list, add and remove the windows it contains.
Execution control. This class includes method to start and finish the execution of the application. Being finished means basically that no windows will be available from the application anymore.
Theme selection. The theme selection process allows a theme
to be specified at three different levels. When a window's theme needs to be
found out, the window itself is queried for a preferred theme. If the window
does not prefer a specific theme, the application containing the window is
queried. If neither the application prefers a theme, the default theme for
the terminal
is used. The terminal
always defines a default theme.
Nested Class Summary | |
---|---|
class |
Application.ApplicationError
Application error is an error message defined on the application level. |
static class |
Application.CustomizedSystemMessages
Contains the system messages used to notify the user about various critical situations that can occur. |
static class |
Application.SystemMessages
Contains the system messages used to notify the user about various critical situations that can occur. |
class |
Application.UserChangeEvent
An event that characterizes a change in the current selection. |
static interface |
Application.UserChangeListener
The UserChangeListener interface for listening application
user changes. |
class |
Application.WindowAttachEvent
Window attach event. |
static interface |
Application.WindowAttachListener
Window attach listener interface. |
class |
Application.WindowDetachEvent
Window detach event. |
static interface |
Application.WindowDetachListener
Window detach listener interface. |
Nested classes/interfaces inherited from interface com.vaadin.terminal.URIHandler |
---|
URIHandler.ErrorEvent |
Constructor Summary | |
---|---|
Application()
|
Method Summary | |
---|---|
void |
addListener(Application.UserChangeListener listener)
Adds the user change listener. |
void |
addListener(Application.WindowAttachListener listener)
Adds the window attach listener. |
void |
addListener(Application.WindowDetachListener listener)
Adds the window detach listener. |
void |
addResource(ApplicationResource resource)
Adds new resource to the application. |
void |
addWindow(Window window)
Adds a new window to the application. |
void |
close()
Ends the Application. |
ApplicationContext |
getContext()
Gets the application context. |
Terminal.ErrorListener |
getErrorHandler()
Gets the application error handler. |
Locale |
getLocale()
Gets the default locale for this application. |
String |
getLogoutURL()
Returns the URL user is redirected to on application close. |
Window |
getMainWindow()
Gets the mainWindow of the application. |
String |
getProperty(String name)
Searches for the property with the specified name in this application. |
Enumeration<?> |
getPropertyNames()
Returns an enumeration of all the names in this application. |
String |
getRelativeLocation(ApplicationResource resource)
Deprecated. this method is intended to be used by the terminal only. It may be removed or moved in the future. |
static Application.SystemMessages |
getSystemMessages()
Gets the SystemMessages for this application. |
String |
getTheme()
Gets the application's theme. |
URL |
getURL()
Gets the URL of the application. |
Object |
getUser()
Gets the user of the application. |
String |
getVersion()
Override this method to return correct version number of your Application. |
Window |
getWindow(String name)
Gets a window by name. |
Collection<Window> |
getWindows()
Gets the set of windows contained by the application. |
DownloadStream |
handleURI(URL context,
String relativeUri)
Deprecated. this method is called be the terminal implementation only and might be removed or moved in the future. Instead of overriding this method, add your URIHandler to a top
level Window (eg.
getMainWindow().addUriHanler(handler) instead. |
abstract void |
init()
Main initializer of the application. |
boolean |
isRunning()
Tests if the application is running or if it has been finished. |
void |
removeListener(Application.UserChangeListener listener)
Removes the user change listener. |
void |
removeListener(Application.WindowAttachListener listener)
Removes the window attach listener. |
void |
removeListener(Application.WindowDetachListener listener)
Removes the window detach listener. |
void |
removeResource(ApplicationResource resource)
Removes the resource from the application. |
void |
removeWindow(Window window)
Removes the specified window from the application. |
void |
setErrorHandler(Terminal.ErrorListener errorHandler)
Sets the application error handler. |
void |
setLocale(Locale locale)
Sets the default locale for this application. |
void |
setLogoutURL(String logoutURL)
Sets the URL user is redirected to on application close. |
void |
setMainWindow(Window mainWindow)
Sets the mainWindow. |
void |
setTheme(String theme)
Sets the application's theme. |
void |
setUser(Object user)
Sets the user of the application instance. |
void |
start(URL applicationUrl,
Properties applicationProperties,
ApplicationContext context)
Starts the application on the given URL. |
void |
terminalError(Terminal.ErrorEvent event)
Invoked by the terminal on any exception that occurs in application and is thrown by the setVariable to the terminal. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Application()
Method Detail |
---|
public Window getWindow(String name)
Gets a window by name. Returns null
if the application is
not running or it does not contain a window corresponding to the name.
All windows can be referenced by their names in url
http://host:port/foo/bar/
where
http://host:port/foo/
is the application url as returned by
getURL() and bar
is the name of the window.
One should note that this method can, as a side effect create new windows if needed by the application. This can be achieved by overriding the default implementation.
If for some reason user opens another window with same url that is already open, the name is modified by adding a "_N" postfix to the name, where N is a running number starting from 1. One can decide to create another window-object for those windows (recommended) or to discard the postfix. If the user has two browser windows pointing to the same window-object on server, synchronization errors are likely to occur.
If no browser-level windowing is used, all defaults are fine and this
method can be left as is. In case browser-level windows are needed, it is
recommended to create new window-objects on this method from their names
if the super.getWindow() does not find existing windows. See below for
implementation example:
// If we already have the requested window, use it
Window w = super.getWindow(name);
if (w == null) {
// If no window found, create it
w = new Window(name);
// set windows name to the one requested
w.setName(name);
// add it to this application
addWindow(w);
// ensure use of window specific url
w.open(new ExternalResource(w.getURL().toString()));
// add some content
w.addComponent(new Label("Test window"));
}
return w;
Note that all returned Window objects must be added to this application instance.
The method should return null if the window does not exists (and is not created as a side-effect) or if the application is not running anymore.
name
- the name of the window.
null
public void addWindow(Window window) throws IllegalArgumentException, NullPointerException
This implicitly invokes the
Window.setApplication(Application)
method.
Note that all application-level windows can be accessed by their names in
url http://host:port/foo/bar/
where
http://host:port/foo/
is the application url as returned by
getURL() and bar
is the name of the window. Also note that
not all windows should be added to application - one can also add windows
inside other windows - these windows show as smaller windows inside those
windows.
window
- the new Window
to add. If the name of the window
is null
, an unique name is automatically given
for the window.
IllegalArgumentException
- if a window with the same name as the new window already
exists in the application.
NullPointerException
- if the given Window
is null
.public void removeWindow(Window window)
Removing the main window of the Application also sets the main window to
null. One must another window to be the main window after this with
setMainWindow(Window)
.
Note that removing window from the application does not close the browser window - the window is only removed from the server-side.
window
- the window to be removed.public Object getUser()
Vaadin doesn't define of use user object in any way - it only provides
this getter and setter methods for convenience. The user is any object
that has been stored to the application with setUser(Object)
.
public void setUser(Object user)
Sets the user of the application instance. An application instance may have a user associated to it. This can be set in login procedure or application initialization.
A component performing the user login procedure can assign the user property of the application and make the user object available to other components of the application.
Vaadin doesn't define of use user object in any way - it only provides
getter and setter methods for convenience. The user reference stored to
the application can be read with getUser()
.
user
- the new user.public URL getURL()
This is the URL what can be entered to a browser window to start the
application. Navigating to the application URL shows the main window (
getMainWindow()
) of the application. Note that the main window
can also be shown by navigating to the window url (
Window.getURL()
).
public void close()
In effect this will cause the application stop returning any windows when
asked. When the application is closed, its state is removed from the
session and the browser window is redirected to the application logout
url set with setLogoutURL(String)
. If the logout url has not
been set, the browser window is reloaded and the application is
restarted.
public void start(URL applicationUrl, Properties applicationProperties, ApplicationContext context)
This method is called by Vaadin framework when a user navigates to the application. After this call the application corresponds to the given URL and it will return windows when asked for them. There is no need to call this method directly.
Application properties are defined by servlet configuration object
ServletConfig
and they are overridden by
context-wide initialization parameters
ServletContext
.
applicationUrl
- the URL the application should respond to.applicationProperties
- the Application properties as specified by the servlet
configuration.context
- the context application will be running in.public boolean isRunning()
Application starts running when its
start(URL, Properties, ApplicationContext)
method has been
called and stops when the close()
is called.
true
if the application is running,
false
if not.public Collection<Window> getWindows()
Note that the returned set of windows can not be modified.
public abstract void init()
Main initializer of the application. The init
method is
called by the framework when the application is started, and it should
perform whatever initialization operations the application needs, such as
creating windows and adding components to them.
public String getTheme()
null
is returned.
public void setTheme(String theme)
Note that this theme can be overridden in the the application level
windows with Window.setTheme(String)
. Setting theme
to be null
selects the default theme. For the available
theme names, see the contents of the VAADIN/themes directory.
theme
- the new theme for this application.public Window getMainWindow()
The main window is the window attached to the application URL (
getURL()
) and thus which is show by default to the user.
Note that each application must have at least one main window.
public void setMainWindow(Window mainWindow)
Sets the mainWindow. If the main window is not explicitly set, the main window defaults to first created window. Setting window as a main window of this application also adds the window to this application.
mainWindow
- the mainWindow to set.public Enumeration<?> getPropertyNames()
See start(URL, Properties, ApplicationContext)
how properties
are defined.
public String getProperty(String name)
null
if the property is not found.
See start(URL, Properties, ApplicationContext)
how properties
are defined.
name
- the name of the property.
public void addResource(ApplicationResource resource)
resource
- the resource to add.public void removeResource(ApplicationResource resource)
resource
- the resource to remove.@Deprecated public String getRelativeLocation(ApplicationResource resource)
resource
- the resource to get relative location.
@Deprecated public DownloadStream handleURI(URL context, String relativeUri)
URIHandler
to a top
level Window
(eg.
getMainWindow().addUriHanler(handler) instead.
This method gets called by terminal. It has lots of duties like to pass uri handler to proper uri handlers registered to windows etc.
In most situations developers should NOT OVERRIDE this method. Instead developers should implement and register uri handlers to windows.
handleURI
in interface URIHandler
context
- the base URLrelativeUri
- a URI relative to context
public Locale getLocale()
public void setLocale(Locale locale)
locale
- the Locale object.public void addListener(Application.UserChangeListener listener)
setUser(Object)
is
called.
listener
- the user change listener to add.public void removeListener(Application.UserChangeListener listener)
listener
- the user change listener to remove.public void addListener(Application.WindowAttachListener listener)
addWindow(Window)
.
listener
- the window attach listener to add.public void addListener(Application.WindowDetachListener listener)
removeWindow(Window)
.
listener
- the window detach listener to add.public void removeListener(Application.WindowAttachListener listener)
listener
- the window attach listener to remove.public void removeListener(Application.WindowDetachListener listener)
listener
- the window detach listener to remove.public String getLogoutURL()
null
, the application is closed normally as defined by the
application running environment.
Desktop application just closes the application window and web-application redirects the browser to application main URL.
public void setLogoutURL(String logoutURL)
null
, the application is closed normally as defined by the
application running environment: Desktop application just closes the
application window and web-application redirects the browser to
application main URL.
logoutURL
- the logoutURL to set.public static Application.SystemMessages getSystemMessages()
Application.CustomizedSystemMessages
. To "override" this method, re-implement
this method in your application (the class that extends
Application
). Even though overriding static methods is not
possible in Java, Vaadin selects to call the static method from the
subclass instead of the original getSystemMessages()
if such a
method exists.
public void terminalError(Terminal.ErrorEvent event)
Invoked by the terminal on any exception that occurs in application and
is thrown by the setVariable
to the terminal. The default
implementation sets the exceptions as ComponentErrors
to the
component that initiated the exception and prints stack trace to standard
error stream.
You can safely override this method in your application in order to direct the errors to some other destination (for example log).
terminalError
in interface Terminal.ErrorListener
event
- the change event.Terminal.ErrorListener.terminalError(com.vaadin.terminal.Terminal.ErrorEvent)
public ApplicationContext getContext()
The application context is the environment where the application is
running in. The actual implementation class of may contains quite a lot
more functionality than defined in the ApplicationContext
interface.
By default, when you are deploying your application to a servlet
container, the implementation class is WebApplicationContext
-
you can safely cast to this class and use the methods from there. When
you are deploying your application as a portlet, context implementation
is PortletApplicationContext
.
public String getVersion()
public Terminal.ErrorListener getErrorHandler()
public void setErrorHandler(Terminal.ErrorListener errorHandler)
errorHandler
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |