import com.vaadin.terminal.Page.FragmentChangedEvent;
import com.vaadin.terminal.Page.FragmentChangedListener;
import com.vaadin.ui.Component;
+import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomComponent;
}
}
+ /**
+ * View display that replaces the contents of a {@link ComponentContainer}
+ * with the active {@link View}.
+ *
+ * All components of the container are removed before adding the new view to
+ * it.
+ *
+ * This display only supports views that are {@link Component}s themselves.
+ * Attempting to display a view that is not a component causes an exception
+ * to be thrown.
+ */
+ public static class ComponentContainerViewDisplay implements ViewDisplay {
+
+ private final ComponentContainer container;
+
+ /**
+ * Create new {@link ViewDisplay} that updates a
+ * {@link ComponentContainer} to show the view.
+ */
+ public ComponentContainerViewDisplay(ComponentContainer container) {
+ this.container = container;
+ }
+
+ public void showView(View view) {
+ if (view instanceof Component) {
+ container.removeAllComponents();
+ container.addComponent((Component) view);
+ } else {
+ throw new IllegalArgumentException("View is not a component: "
+ + view);
+ }
+ }
+ }
+
/**
* View provider which supports mapping a single view name to a single
* pre-initialized view instance.
private List<ViewProvider> providers = new LinkedList<ViewProvider>();
/**
- * Create a navigator that is tracking the active view using URI fragments.
+ * Create a navigator that is tracking the active view using URI fragments
+ * of the current {@link Page} and replacing the contents of a
+ * {@link ComponentContainer} with the active view.
+ *
+ * In case the container is not on the current page, use another
+ * {@link Navigator#Navigator(Page, ViewDisplay)} with an explicitly created
+ * {@link ComponentContainerViewDisplay}.
+ *
+ * All components of the container are removed each time before adding the
+ * active {@link View}. Views must implement {@link Component} when using
+ * this constructor.
*
* <p>
* After all {@link View}s and {@link ViewProvider}s have been registered,
* navigator.navigateTo(Page.getCurrent().getFragment());
* </pre>
*
- * @param page
- * whose URI fragments are used
- * @param display
- * where to display the views
+ * @param container
+ * ComponentContainer whose contents should be replaced with the
+ * active view on view change
*/
- public Navigator(Page page, ViewDisplay display) {
- this.display = display;
- fragmentManager = new UriFragmentManager(page, this);
+ public Navigator(ComponentContainer container) {
+ display = new ComponentContainerViewDisplay(container);
+ fragmentManager = new UriFragmentManager(Page.getCurrent(), this);
}
/**
* Create a navigator that is tracking the active view using URI fragments.
- * By default, a {@link SimpleViewDisplay} is used and can be obtained using
- * {@link #getDisplay()}.
*
* <p>
* After all {@link View}s and {@link ViewProvider}s have been registered,
*
* @param page
* whose URI fragments are used
+ * @param display
+ * where to display the views
*/
- public Navigator(Page page) {
- display = new SimpleViewDisplay();
+ public Navigator(Page page, ViewDisplay display) {
+ this.display = display;
fragmentManager = new UriFragmentManager(page, this);
}
import com.vaadin.navigator.FragmentManager;
import com.vaadin.navigator.Navigator;
-import com.vaadin.navigator.Navigator.SimpleViewDisplay;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.navigator.ViewDisplay;
import com.vaadin.navigator.ViewProvider;
-import com.vaadin.terminal.Page;
import com.vaadin.tests.server.navigator.ClassBasedViewProviderTest.TestView;
import com.vaadin.tests.server.navigator.ClassBasedViewProviderTest.TestView2;
}
}
- public void testDefaultDisplayType() {
- IMocksControl control = EasyMock.createControl();
- Page page = control.createMock(Page.class);
-
- Navigator navigator = new Navigator(page);
-
- assertEquals("Default display should be a SimpleViewDisplay",
- SimpleViewDisplay.class, navigator.getDisplay().getClass());
- }
-
public void testAddViewInstance() throws Exception {
View view = new TestView();