Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Advanced Web Application Topics

most cases of navigation. Views managed by the navigator automatically get a distinct URI fragment, which can be used to be able to bookmark the views and their states and to go back and forward in the browser history.

11.9.1. Setting Up for Navigation

The Navigator class manages a collection of views that implement the View interface.The views can be either registered beforehand or acquired from a view provider.When registering, the views must have a name identifier and be added to a navigator with addView().You can register new views at any point. Once registered, you can navigate to them with navigateTo().

Navigator manages navigation in a component container, which can be either a

ComponentContainer (most layouts) or a SingleComponentContainer (UI, Panel, or

Window). The component container is managed through a ViewDisplay. Two view displays are defined: ComponentContainerViewDisplay and SingleComponentContainerViewDisplay, for the respective component container types. Normally, you can let the navigator create the view display internally, as we do in the example below, but you can also create it yourself to customize it.

Let us consider the following UI with two views: start and main. Here, we define their names with enums to be typesafe. We manage the navigation with the UI class itself, which is a

SingleComponentContainer.

public class NavigatorUI extends UI { Navigator navigator;

protected static final String MAINVIEW = "main";

@Override

protected void init(VaadinRequest request) { getPage().setTitle("Navigation Example");

//Create a navigator to control the views navigator = new Navigator(this, this);

//Create and register the views navigator.addView("", new StartView()); navigator.addView(MAINVIEW, new MainView());

}

}

The Navigator automatically sets the URI fragment of the application URL. It also registers a URIFragmentChangedListener in the page (see Section 11.10, “URI Fragment and History Management with UriFragmentUtility”) to show the view identified by the URI fragment if entered or navigated to in the browser. This also enables browser navigation history in the application.

View Providers

You can create new views dynamically using a view provider that implements the ViewProvider interface. A provider is registered in Navigator with addProvider().

The ClassBasedViewProvider is a view provider that can dynamically create new instances of a specified view class based on the view name.

The StaticViewProvider returns an existing view instance based on the view name. The addView() in Navigator is actually just a shorthand for creating a static view provider for each registered view.

298

Setting Up for Navigation

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]