From 485104ae6084b9f6bfbefe32a8bcc421ea87f9b8 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Fri, 29 Jun 2012 13:44:13 +0300 Subject: [PATCH] Replace Navigator(Page) with Navigator(ComponentContainer) (#8859) --- src/com/vaadin/navigator/Navigator.java | 68 +++++++++++++++---- .../tests/server/navigator/NavigatorTest.java | 12 ---- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/src/com/vaadin/navigator/Navigator.java b/src/com/vaadin/navigator/Navigator.java index 2c340adaa2..c5e7de836a 100644 --- a/src/com/vaadin/navigator/Navigator.java +++ b/src/com/vaadin/navigator/Navigator.java @@ -14,6 +14,7 @@ import com.vaadin.terminal.Page; 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; @@ -131,6 +132,40 @@ public class Navigator implements Serializable { } } + /** + * 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. @@ -268,7 +303,17 @@ public class Navigator implements Serializable { private List providers = new LinkedList(); /** - * 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. * *

* After all {@link View}s and {@link ViewProvider}s have been registered, @@ -279,20 +324,17 @@ public class Navigator implements Serializable { * navigator.navigateTo(Page.getCurrent().getFragment()); * * - * @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()}. * *

* After all {@link View}s and {@link ViewProvider}s have been registered, @@ -305,9 +347,11 @@ public class Navigator implements Serializable { * * @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); } diff --git a/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java b/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java index 03f49d4ab6..a7eb2e12fd 100644 --- a/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java +++ b/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java @@ -13,13 +13,11 @@ import org.easymock.IMocksControl; 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; @@ -364,16 +362,6 @@ public class NavigatorTest extends TestCase { } } - 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(); -- 2.39.5