public class Navigator extends Object implements Serializable
The view switching can be based e.g. on URI fragments containing the view name and parameters to the view. There are two types of parameters for views: an optional parameter string that is included in the fragment (may be bookmarkable).
Views can be explicitly registered or dynamically generated and listening to view changes is possible.
Note that Navigator
is not a component itself but uses a
ViewDisplay
to update contents based on the state.
Modifier and Type | Class and Description |
---|---|
static class |
Navigator.ClassBasedViewProvider
A ViewProvider which maps a single view name to a class to instantiate
for the view.
|
static class |
Navigator.ComponentContainerViewDisplay
A ViewDisplay that replaces the contents of a
ComponentContainer
with the active View . |
static class |
Navigator.EmptyView
Empty view component.
|
static class |
Navigator.SingleComponentContainerViewDisplay
A ViewDisplay that replaces the contents of a
SingleComponentContainer with the active View . |
static class |
Navigator.StaticViewProvider
A ViewProvider which supports mapping a single view name to a single
pre-initialized view instance.
|
static class |
Navigator.UriFragmentManager
A
NavigationStateManager using hashbang fragments in the Page
location URI to track views and enable listening to view changes. |
Modifier | Constructor and Description |
---|---|
protected |
Navigator()
Creates a navigator.
|
|
Navigator(UI ui,
ComponentContainer container)
Creates a navigator that is tracking the active view using URI fragments
of the
Page containing the given UI and replacing the contents of
a ComponentContainer with the active view. |
|
Navigator(UI ui,
NavigationStateManager stateManager,
ViewDisplay display)
Creates a navigator.
|
|
Navigator(UI ui,
SingleComponentContainer container)
Creates a navigator that is tracking the active view using URI fragments
of the
Page containing the given UI and replacing the contents of
a SingleComponentContainer with the active view. |
|
Navigator(UI ui,
ViewDisplay display)
Creates a navigator that is tracking the active view using URI fragments
of the
Page containing the given UI. |
Modifier and Type | Method and Description |
---|---|
void |
addProvider(ViewProvider provider)
Registers a view provider (factory).
|
void |
addView(String viewName,
Class<? extends View> viewClass)
Registers a view class for a view name.
|
void |
addView(String viewName,
View view)
Registers a static, pre-initialized view instance for a view name.
|
void |
addViewChangeListener(ViewChangeListener listener)
Listen to changes of the active view.
|
protected boolean |
beforeViewChange(ViewChangeListener.ViewChangeEvent event)
Check whether view change is allowed by view change listeners (
ViewChangeListener.beforeViewChange(ViewChangeEvent) ). |
void |
destroy()
Creates view change event for given
view , viewName and
parameters . |
protected void |
fireAfterViewChange(ViewChangeListener.ViewChangeEvent event)
Fires an event after the current view has changed.
|
protected boolean |
fireBeforeViewChange(ViewChangeListener.ViewChangeEvent event)
Fires an event before an imminent view change.
|
View |
getCurrentView()
Return the currently active view.
|
ViewDisplay |
getDisplay()
Return the
ViewDisplay used by the navigator. |
String |
getState()
Returns the current navigation state reported by this Navigator's
NavigationStateManager . |
protected NavigationStateManager |
getStateManager()
Returns the
NavigationStateManager that is used to get, listen to
and manipulate the navigation state used by this Navigator. |
UI |
getUI() |
protected void |
init(UI ui,
NavigationStateManager stateManager,
ViewDisplay display)
Initializes a navigator created with the no arguments constructor.
|
void |
navigateTo(String navigationState)
Navigates to a view and initialize the view with given parameters.
|
protected void |
navigateTo(View view,
String viewName,
String parameters)
Internal method activating a view, setting its parameters and calling
listeners.
|
void |
removeProvider(ViewProvider provider)
Unregister a view provider (factory).
|
void |
removeView(String viewName)
Removes a view from navigator.
|
void |
removeViewChangeListener(ViewChangeListener listener)
Removes a view change listener.
|
protected void |
revertNavigation()
Revert the changes to the navigation state.
|
void |
setErrorProvider(ViewProvider provider)
Registers a view provider that is queried for a view when no other view
matches the navigation state.
|
void |
setErrorView(Class<? extends View> viewClass)
Registers a view class that is instantiated when no other view matches
the navigation state.
|
void |
setErrorView(View view)
Registers a view that is displayed when no other view matches the
navigation state.
|
protected void |
switchView(ViewChangeListener.ViewChangeEvent event)
Update the internal state of the navigator to reflect the actual
switching of views.
|
protected void |
updateNavigationState(ViewChangeListener.ViewChangeEvent event)
Update the internal state of the navigator (parameters, previous
successful URL fragment navigated to) when navigation succeeds.
|
public Navigator(UI ui, ComponentContainer container)
Page
containing the given UI and replacing the contents of
a ComponentContainer
with the active view.
All components of the container are removed each time before adding the
active View
. Views must implement Component
when using
this constructor.
Navigation is automatically initiated after UI.init()
if a
navigator was created. If at a later point changes are made to the
navigator, navigator.navigateTo(navigator.getState())
may need to
be explicitly called to ensure the current view matches the navigation
state.
ui
- The UI to which this Navigator is attached.container
- The ComponentContainer whose contents should be replaced with
the active view on view changepublic Navigator(UI ui, SingleComponentContainer container)
Page
containing the given UI and replacing the contents of
a SingleComponentContainer
with the active view.
Views must implement Component
when using this constructor.
Navigation is automatically initiated after UI.init()
if a
navigator was created. If at a later point changes are made to the
navigator, navigator.navigateTo(navigator.getState())
may need to
be explicitly called to ensure the current view matches the navigation
state.
ui
- The UI to which this Navigator is attached.container
- The SingleComponentContainer whose contents should be replaced
with the active view on view changepublic Navigator(UI ui, ViewDisplay display)
Page
containing the given UI.
Navigation is automatically initiated after UI.init()
if a
navigator was created. If at a later point changes are made to the
navigator, navigator.navigateTo(navigator.getState())
may need to
be explicitly called to ensure the current view matches the navigation
state.
ui
- The UI to which this Navigator is attached.display
- The ViewDisplay used to display the views.public Navigator(UI ui, NavigationStateManager stateManager, ViewDisplay display)
When a custom navigation state manager is not needed, use one of the other constructors which use a URI fragment based state manager.
Navigation is automatically initiated after UI.init()
if a
navigator was created. If at a later point changes are made to the
navigator, navigator.navigateTo(navigator.getState())
may need to
be explicitly called to ensure the current view matches the navigation
state.
ui
- The UI to which this Navigator is attached.stateManager
- The NavigationStateManager keeping track of the active view
and enabling bookmarking and direct navigation or null to use
the default implementationdisplay
- The ViewDisplay used to display the views handled by this
navigatorprotected Navigator()
init(UI, NavigationStateManager, ViewDisplay)
before use.protected void init(UI ui, NavigationStateManager stateManager, ViewDisplay display)
When a custom navigation state manager is not needed, use null to create a default one based on URI fragments.
Navigation is automatically initiated after UI.init()
if a
navigator was created. If at a later point changes are made to the
navigator, navigator.navigateTo(navigator.getState())
may need to
be explicitly called to ensure the current view matches the navigation
state.
ui
- The UI to which this Navigator is attached.stateManager
- The NavigationStateManager keeping track of the active view
and enabling bookmarking and direct navigation or null for
defaultdisplay
- The ViewDisplay used to display the views handled by this
navigatorpublic void navigateTo(String navigationState)
The view string consists of a view name optionally followed by a slash and a parameters part that is passed as-is to the view. ViewProviders are used to find and create the correct type of view.
If multiple providers return a matching view, the view with the longest name is selected. This way, e.g. hierarchies of subviews can be registered like "admin/", "admin/users", "admin/settings" and the longest match is used.
If the view being deactivated indicates it wants a confirmation for the navigation operation, the user is asked for the confirmation.
Registered ViewChangeListener
s are called upon successful view
change.
navigationState
- view name and parametersIllegalArgumentException
- if navigationState
does not map to a known view
and no error view is registeredprotected void navigateTo(View view, String viewName, String parameters)
This method also verifies that the user is allowed to perform the navigation operation.
view
- view to activateviewName
- (optional) name of the view or null not to change the
navigation stateparameters
- parameters passed in the navigation state to the viewprotected boolean beforeViewChange(ViewChangeListener.ViewChangeEvent event)
ViewChangeListener.beforeViewChange(ViewChangeEvent)
).
This method can be overridden to extend the behavior, and should not be
called directly except by navigateTo(View, String, String)
.event
- the event to fire as the before view change eventprotected void revertNavigation()
navigateTo(View, String, String)
to
revert the URL fragment to point to the previous view to which navigation
succeeded.
This method should only be called by
navigateTo(View, String, String)
. Normally it should not be
overridden, but can be by frameworks that need to hook into view change
cancellations of this type.protected void updateNavigationState(ViewChangeListener.ViewChangeEvent event)
navigateTo(View, String, String)
.event
- a view change event with details of the changeprotected void switchView(ViewChangeListener.ViewChangeEvent event)
navigateTo(View, String, String)
between showing the view and
calling View#enter(ViewChangeEvent)
. If this method is
overridden, the overriding version must call the super method.event
- a view change event with details of the changeprotected boolean fireBeforeViewChange(ViewChangeListener.ViewChangeEvent event)
Listeners are called in registration order. If any listener returns
false
, the rest of the listeners are not called and the view
change is blocked.
The view change listeners may also e.g. open a warning or question dialog and save the parameters to re-initiate the navigation operation upon user action.
event
- view change event (not null, view change not yet performed)protected NavigationStateManager getStateManager()
NavigationStateManager
that is used to get, listen to
and manipulate the navigation state used by this Navigator.public String getState()
NavigationStateManager
.public ViewDisplay getDisplay()
ViewDisplay
used by the navigator.public UI getUI()
public View getCurrentView()
protected void fireAfterViewChange(ViewChangeListener.ViewChangeEvent event)
Listeners are called in registration order.
event
- view change event (not null)public void addView(String viewName, View view)
Registering another view with a name that is already registered overwrites the old registration of the same type.
Note that a view should not be shared between UIs (for instance, it should not be a static field in a UI subclass).
viewName
- String that identifies a view (not null nor empty string)view
- View
instance (not null)public void addView(String viewName, Class<? extends View> viewClass)
Registering another view with a name that is already registered overwrites the old registration of the same type.
A new view instance is created every time a view is requested.
viewName
- String that identifies a view (not null nor empty string)viewClass
- View
class to instantiate when a view is requested
(not null)public void removeView(String viewName)
This method only applies to views registered using
addView(String, View)
or addView(String, Class)
.
viewName
- name of the view to removepublic void addProvider(ViewProvider provider)
Providers are called in order of registration until one that can handle the requested view name is found.
provider
- provider to register, not null
IllegalArgumentException
- if the provided view provider is null
public void removeProvider(ViewProvider provider)
provider
- provider to unregisterpublic void setErrorView(Class<? extends View> viewClass)
setErrorProvider(ViewProvider)
call.
Note that an error view should not be shared between UIs (for instance, it should not be a static field in a UI subclass).
viewClass
- The View class whose instance should be used as the error
view.public void setErrorView(View view)
setErrorProvider(ViewProvider)
call.view
- The View that should be used as the error view.public void setErrorProvider(ViewProvider provider)
getViewName(String navigationState)
should return
navigationState
.provider
- public void addViewChangeListener(ViewChangeListener listener)
Registered listeners are invoked in registration order before (
beforeViewChange()
) and after (
afterViewChange()
) a view change occurs.
listener
- Listener to invoke during a view change.public void removeViewChangeListener(ViewChangeListener listener)
listener
- Listener to remove.public void destroy()
view
, viewName
and
parameters
.Copyright © 2019 Vaadin Ltd. All rights reserved.