From 585c9ec24dc6f71f008218929aa91cabdc54c000 Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 12 Jun 2017 13:19:12 +0300 Subject: Add support for Views which are not components --- .../main/java/com/vaadin/navigator/Navigator.java | 16 +---- .../src/main/java/com/vaadin/navigator/View.java | 20 +++++- .../tests/server/navigator/NavigatorTest.java | 73 ++++++++++++++++++++-- 3 files changed, 91 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/com/vaadin/navigator/Navigator.java b/server/src/main/java/com/vaadin/navigator/Navigator.java index 567c271211..2341479832 100644 --- a/server/src/main/java/com/vaadin/navigator/Navigator.java +++ b/server/src/main/java/com/vaadin/navigator/Navigator.java @@ -170,13 +170,8 @@ public class Navigator implements Serializable { @Override 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); - } + container.removeAllComponents(); + container.addComponent(view.getViewComponent()); } } @@ -204,12 +199,7 @@ public class Navigator implements Serializable { @Override public void showView(View view) { - if (view instanceof Component) { - container.setContent((Component) view); - } else { - throw new IllegalArgumentException( - "View is not a component: " + view); - } + container.setContent(view.getViewComponent()); } } diff --git a/server/src/main/java/com/vaadin/navigator/View.java b/server/src/main/java/com/vaadin/navigator/View.java index 8e083c6833..72e60dfdc4 100644 --- a/server/src/main/java/com/vaadin/navigator/View.java +++ b/server/src/main/java/com/vaadin/navigator/View.java @@ -25,7 +25,8 @@ import com.vaadin.ui.Component; * Interface for all views controlled by the navigator. * * Each view added to the navigator must implement this interface. Typically, a - * view is a {@link Component}. + * view is a {@link Component}, if it is not then you should override + * {@link #getViewComponent()} to define the component to show for the view. * * @author Vaadin Ltd * @since 7.0 @@ -46,4 +47,21 @@ public interface View extends Serializable { * */ public void enter(ViewChangeEvent event); + + /** + * Gets the component to show when navigating to the view. + * + * By default casts this View to a {@link Component} if possible, otherwise + * throws an IllegalStateException. + * + * @since + * @return the component to show, by default the view instance itself + */ + public default Component getViewComponent() { + if (!(this instanceof Component)) { + throw new IllegalStateException( + "View is not a Component. Override getViewComponent() to return the root view component"); + } + return (Component) this; + } } diff --git a/server/src/test/java/com/vaadin/tests/server/navigator/NavigatorTest.java b/server/src/test/java/com/vaadin/tests/server/navigator/NavigatorTest.java index 9ac9492d95..6991fd1b9e 100644 --- a/server/src/test/java/com/vaadin/tests/server/navigator/NavigatorTest.java +++ b/server/src/test/java/com/vaadin/tests/server/navigator/NavigatorTest.java @@ -40,9 +40,12 @@ import com.vaadin.navigator.ViewProvider; import com.vaadin.server.Page; import com.vaadin.server.VaadinRequest; import com.vaadin.shared.Registration; +import com.vaadin.shared.ui.ui.PageState; import com.vaadin.tests.server.navigator.ClassBasedViewProviderTest.TestView; import com.vaadin.tests.server.navigator.ClassBasedViewProviderTest.TestView2; import com.vaadin.ui.Component; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; @@ -462,12 +465,15 @@ public class NavigatorTest { @Test public void testComponentContainerViewDisplay() { - abstract class TestView implements Component, View { + class TestView extends VerticalLayout implements View { + @Override + public void enter(ViewChangeEvent event) { + + } } - TestView tv1 = EasyMock.createNiceMock(TestView.class); - TestView tv2 = EasyMock.createNiceMock(TestView.class); - EasyMock.replay(tv1, tv2); + TestView tv1 = new TestView(); + TestView tv2 = new TestView(); VerticalLayout container = new VerticalLayout(); ViewDisplay display = new Navigator.ComponentContainerViewDisplay( @@ -904,4 +910,63 @@ public class NavigatorTest { navigator.navigateTo(viewName); Assert.assertEquals(1, count[0]); } + + public static class ViewIsNotAComponent implements View { + + private HorizontalLayout layout = new HorizontalLayout( + new Label("Hello")); + + @Override + public Component getViewComponent() { + return layout; + } + + @Override + public void enter(ViewChangeEvent event) { + + } + } + + @Test + public void viewWhichIsNotAComponent() { + UI ui = new UI() { + + private Page page; + { + page = new Page(this, new PageState()) { + private String fragment = ""; + + @Override + public String getUriFragment() { + return fragment; + }; + + @Override + public void setUriFragment(String newUriFragment, + boolean fireEvents) { + fragment = newUriFragment; + }; + }; + } + + @Override + protected void init(VaadinRequest request) { + } + + @Override + public Page getPage() { + return page; + } + }; + + Navigator navigator = new Navigator(ui, ui); + ui.setNavigator(navigator); + navigator.addView("foo", ViewIsNotAComponent.class); + navigator.navigateTo("foo"); + + Assert.assertEquals(HorizontalLayout.class, ui.getContent().getClass()); + Assert.assertEquals("Hello", + ((Label) ((HorizontalLayout) ui.getContent()).getComponent(0)) + .getValue()); + } } -- cgit v1.2.3