com.vaadin.navigator
Class Navigator

java.lang.Object
  extended by com.vaadin.navigator.Navigator
All Implemented Interfaces:
Serializable

public class Navigator
extends Object
implements Serializable

Navigator utility that allows switching of views in a part of an application. 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 comes with Navigator.SimpleViewDisplay which is a component that displays the selected view as its contents.

Since:
7.0
Author:
Vaadin Ltd
See Also:
Serialized Form

Nested Class Summary
static class Navigator.ClassBasedViewProvider
          View provider which maps a single view name to a class to instantiate for the view.
static class Navigator.ComponentContainerViewDisplay
          View display that replaces the contents of a ComponentContainer with the active View.
static class Navigator.EmptyView
          Empty view component.
static class Navigator.SimpleViewDisplay
          View display that is a component itself and replaces its contents with the view.
static class Navigator.StaticViewProvider
          View provider which supports mapping a single view name to a single pre-initialized view instance.
static class Navigator.UriFragmentManager
          Fragment manager using URI fragments of a Page to track views and enable listening to view changes.
 
Constructor Summary
Navigator(ComponentContainer container)
          Create a navigator that is tracking the active view using URI fragments of the current Page and replacing the contents of a ComponentContainer with the active view.
Navigator(FragmentManager fragmentManager, ViewDisplay display)
          Create a navigator.
Navigator(Page page, ViewDisplay display)
          Create a navigator that is tracking the active view using URI fragments.
 
Method Summary
 void addListener(ViewChangeListener listener)
          Listen to changes of the active view.
 void addView(String viewName, Class<? extends View> viewClass)
          Register for a view name a view class.
 void addView(String viewName, View view)
          Register a static, pre-initialized view instance for a view name.
protected  void fireViewChange(ViewChangeListener.ViewChangeEvent event)
          Fire an event when the current view has changed.
 ViewDisplay getDisplay()
          Returns the ViewDisplay used by the navigator.
protected  FragmentManager getFragmentManager()
          Return the fragment manager that is used to get, listen to and manipulate the URI fragment or other source of navigation information.
protected  boolean isViewChangeAllowed(ViewChangeListener.ViewChangeEvent event)
          Check whether view change is allowed.
 void navigateTo(String viewAndParameters)
          Navigate to a view and initialize the view with given parameters.
protected  void navigateTo(View view, String viewName, String fragmentParameters)
          Internal method activating a view, setting its parameters and calling listeners.
 void registerProvider(ViewProvider provider)
          Register a view provider (factory).
 void removeListener(ViewChangeListener listener)
          Remove a view change listener.
 void removeView(String viewName)
          Remove view from navigator.
 void unregisterProvider(ViewProvider provider)
          Unregister a view provider (factory).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Navigator

public Navigator(ComponentContainer container)
Create a navigator that is tracking the active view using URI fragments of the current Page and replacing the contents of a ComponentContainer with the active view. In case the container is not on the current page, use another Navigator(Page, ViewDisplay) with an explicitly created Navigator.ComponentContainerViewDisplay. All components of the container are removed each time before adding the active Navigator.EmptyView. Views must implement Component when using this constructor.

After all Navigator.EmptyViews and ViewProviders have been registered, the application should trigger navigation to the current fragment using e.g.

 navigator.navigateTo(Page.getCurrent().getFragment());
 

Parameters:
container - ComponentContainer whose contents should be replaced with the active view on view change

Navigator

public Navigator(Page page,
                 ViewDisplay display)
Create a navigator that is tracking the active view using URI fragments.

After all Navigator.EmptyViews and ViewProviders have been registered, the application should trigger navigation to the current fragment using e.g.

 navigator.navigateTo(Page.getCurrent().getFragment());
 

Parameters:
page - whose URI fragments are used
display - where to display the views

Navigator

public Navigator(FragmentManager fragmentManager,
                 ViewDisplay display)
Create a navigator. When a custom fragment manager is not needed, use the constructor Navigator(Page, ViewDisplay) which uses a URI fragment based fragment manager. Note that navigation to the initial view must be performed explicitly by the application after creating a Navigator using this constructor.

Parameters:
fragmentManager - fragment manager keeping track of the active view and enabling bookmarking and direct navigation
display - where to display the views
Method Detail

navigateTo

public void navigateTo(String viewAndParameters)
Navigate to a view and initialize the view with given parameters. The view string consists of a view name optionally followed by a slash and (fragment) parameters. 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 ViewChangeListeners are called upon successful view change.

Parameters:
viewAndParameters - view name and parameters

navigateTo

protected void navigateTo(View view,
                          String viewName,
                          String fragmentParameters)
Internal method activating a view, setting its parameters and calling listeners. This method also verifies that the user is allowed to perform the navigation operation.

Parameters:
view - view to activate
viewName - (optional) name of the view or null not to set the fragment
fragmentParameters - parameters passed in the fragment for the view

isViewChangeAllowed

protected boolean isViewChangeAllowed(ViewChangeListener.ViewChangeEvent event)
Check whether view change is allowed. All related listeners are called. The view change is blocked if any of them wants to block the navigation operation. 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.

Parameters:
event - view change event (not null, view change not yet performed)
Returns:
true if the view change should be allowed, false to silently block the navigation operation

getFragmentManager

protected FragmentManager getFragmentManager()
Return the fragment manager that is used to get, listen to and manipulate the URI fragment or other source of navigation information.

Returns:
fragment manager in use

getDisplay

public ViewDisplay getDisplay()
Returns the ViewDisplay used by the navigator. Unless another display is specified, a Navigator.SimpleViewDisplay (which is a Component) is used by default.

Returns:
current ViewDisplay

fireViewChange

protected void fireViewChange(ViewChangeListener.ViewChangeEvent event)
Fire an event when the current view has changed.

Parameters:
event - view change event (not null)

addView

public void addView(String viewName,
                    View view)
Register a static, pre-initialized view instance for a view name. Registering another view with a name that is already registered overwrites the old registration of the same type.

Parameters:
viewName - String that identifies a view (not null nor empty string)
view - Navigator.EmptyView instance (not null)

addView

public void addView(String viewName,
                    Class<? extends View> viewClass)
Register for a view name a view class. 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.

Parameters:
viewName - String that identifies a view (not null nor empty string)
viewClass - Navigator.EmptyView class to instantiate when a view is requested (not null)

removeView

public void removeView(String viewName)
Remove view from navigator. This method only applies to views registered using addView(String, View) or addView(String, Class).

Parameters:
viewName - name of the view to remove

registerProvider

public void registerProvider(ViewProvider provider)
Register a view provider (factory). Providers are called in order of registration until one that can handle the requested view name is found.

Parameters:
provider - provider to register

unregisterProvider

public void unregisterProvider(ViewProvider provider)
Unregister a view provider (factory).

Parameters:
provider - provider to unregister

addListener

public void addListener(ViewChangeListener listener)
Listen to changes of the active view. The listener will get notified after the view has changed.

Parameters:
listener - Listener to invoke after view changes.

removeListener

public void removeListener(ViewChangeListener listener)
Remove a view change listener.

Parameters:
listener - Listener to remove.


Copyright © 2000-2011 Vaadin Ltd. All Rights Reserved.