From 9f9ccd8d002b65db3e3e0430b4d5788a90b04c5c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Wed, 16 Nov 2011 11:01:32 +0200 Subject: [PATCH] Allow configuring the root class to use, remove RootLayout --- WebContent/WEB-INF/web.xml | 4 +- src/com/vaadin/Application.java | 28 +++--- src/com/vaadin/RootTest.java | 17 ++++ src/com/vaadin/RootTestApplication.java | 40 --------- src/com/vaadin/RootTestLayout.java | 85 ------------------- .../gwt/server/ApplicationServlet.java | 29 +++---- src/com/vaadin/ui/Root.java | 65 ++++++++++++-- src/com/vaadin/ui/RootLayout.java | 7 -- 8 files changed, 104 insertions(+), 171 deletions(-) create mode 100644 src/com/vaadin/RootTest.java delete mode 100644 src/com/vaadin/RootTestApplication.java delete mode 100644 src/com/vaadin/RootTestLayout.java delete mode 100644 src/com/vaadin/ui/RootLayout.java diff --git a/WebContent/WEB-INF/web.xml b/WebContent/WEB-INF/web.xml index 42f3b2df87..4878311eb8 100644 --- a/WebContent/WEB-INF/web.xml +++ b/WebContent/WEB-INF/web.xml @@ -33,8 +33,8 @@ RootsTester com.vaadin.terminal.gwt.server.ApplicationServlet - rootLayout - com.vaadin.RootTestLayout + root + com.vaadin.RootTest diff --git a/src/com/vaadin/Application.java b/src/com/vaadin/Application.java index 21e642f0e1..de68bff9bc 100644 --- a/src/com/vaadin/Application.java +++ b/src/com/vaadin/Application.java @@ -38,7 +38,6 @@ import com.vaadin.terminal.gwt.server.ChangeVariablesErrorEvent; import com.vaadin.terminal.gwt.server.WebApplicationContext; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Root; -import com.vaadin.ui.RootLayout; import com.vaadin.ui.Window; /** @@ -97,6 +96,8 @@ import com.vaadin.ui.Window; @SuppressWarnings("serial") public class Application implements Terminal.ErrorListener, Serializable { + public static final String ROOT_PARAMETER = "root"; + private final static Logger logger = Logger.getLogger(Application.class .getName()); @@ -1481,26 +1482,27 @@ public class Application implements Terminal.ErrorListener, Serializable { } protected Root createRoot(WrappedRequest request) { - Object rootLayoutClassNameObj = properties.get("rootLayout"); - if (rootLayoutClassNameObj instanceof String) { - String rootLayoutClassName = (String) rootLayoutClassNameObj; + Object rootClassNameObj = properties.get(ROOT_PARAMETER); + if (rootClassNameObj instanceof String) { + String rootClassName = (String) rootClassNameObj; try { - Class rootLayoutClass = Class.forName( - rootLayoutClassName).asSubclass(RootLayout.class); + Class rootClass = Class.forName(rootClassName) + .asSubclass(Root.class); try { - RootLayout rootLayout = rootLayoutClass.newInstance(); - return new Root(rootLayout); + Root root = rootClass.newInstance(); + return root; } catch (Exception e) { throw new RuntimeException( - "Could instantiate rootLayout class " - + rootLayoutClassName, e); + "Could not instantiate root class " + rootClassName, + e); } } catch (ClassNotFoundException e) { - throw new RuntimeException("Could not load rootLayout class " - + rootLayoutClassName, e); + throw new RuntimeException("Could not load root class " + + rootClassName, e); } } - throw new RuntimeException("No rootLayout defined in web.xml"); + throw new RuntimeException("No " + ROOT_PARAMETER + + " defined in web.xml"); } public boolean handleRequest(WrappedRequest request, diff --git a/src/com/vaadin/RootTest.java b/src/com/vaadin/RootTest.java new file mode 100644 index 0000000000..32fb41eded --- /dev/null +++ b/src/com/vaadin/RootTest.java @@ -0,0 +1,17 @@ +package com.vaadin; + +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Label; +import com.vaadin.ui.Root; +import com.vaadin.ui.VerticalLayout; + +public class RootTest extends Root { + @Override + public void init(WrappedRequest request) { + VerticalLayout layout = new VerticalLayout(); + + layout.addComponent(new Label("Hello root")); + + setContent(layout); + } +} diff --git a/src/com/vaadin/RootTestApplication.java b/src/com/vaadin/RootTestApplication.java deleted file mode 100644 index 88c9763733..0000000000 --- a/src/com/vaadin/RootTestApplication.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.vaadin; - -import java.io.IOException; -import java.io.PrintWriter; - -import com.vaadin.terminal.RequestHandler; -import com.vaadin.terminal.WrappedRequest; -import com.vaadin.terminal.WrappedResponse; - -public class RootTestApplication extends Application { - @Override - public void init() { - addRequestHandler(new RequestHandler() { - public boolean handleRequest(Application application, - WrappedRequest request, WrappedResponse response) { - if (request.getParameter("myhandler") != null) { - response.setContentType("text/plain"); - try { - PrintWriter writer = response.getWriter(); - writer.println("Roots, bloody roots"); - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } - return true; - } else { - return false; - } - } - }); - } - - // @Override - // protected Root createRoot(WrappedRequest request) { - // String rootText = request.getParameter("rootText"); - // Root root = new Root(new RootTestLayout(rootText)); - // return root; - // } - -} diff --git a/src/com/vaadin/RootTestLayout.java b/src/com/vaadin/RootTestLayout.java deleted file mode 100644 index 230dbda6f9..0000000000 --- a/src/com/vaadin/RootTestLayout.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.vaadin; - -import java.io.ByteArrayInputStream; -import java.io.UnsupportedEncodingException; - -import com.vaadin.terminal.ApplicationResource; -import com.vaadin.terminal.DownloadStream; -import com.vaadin.terminal.WrappedRequest; -import com.vaadin.ui.Button; -import com.vaadin.ui.Button.ClickEvent; -import com.vaadin.ui.Label; -import com.vaadin.ui.Link; -import com.vaadin.ui.LoginForm; -import com.vaadin.ui.LoginForm.LoginEvent; -import com.vaadin.ui.RootLayout; -import com.vaadin.ui.VerticalLayout; - -public class RootTestLayout extends VerticalLayout implements RootLayout { - private final String rootText; - - public RootTestLayout() { - this("Default root text"); - } - - public RootTestLayout(String rootText) { - this.rootText = rootText; - } - - public void init(WrappedRequest request) { - if (rootText != null && rootText.trim().length() != 0) { - addComponent(new Label(rootText)); - } - addComponent(new Button("Roots, bloody roots", - new Button.ClickListener() { - public void buttonClick(ClickEvent event) { - event.getButton().getRoot() - .executeJavaScript("window.alert(\"Here\");"); - } - })); - ApplicationResource resource = new ApplicationResource() { - - public String getMIMEType() { - return "text/plain"; - } - - public DownloadStream getStream() { - try { - return new DownloadStream(new ByteArrayInputStream( - "Roots".getBytes("UTF-8")), getMIMEType(), - getFilename()); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - - public String getFilename() { - return "roots.txt"; - } - - public long getCacheTime() { - return 60 * 60 * 1000; - } - - public int getBufferSize() { - return 0; - } - - public Application getApplication() { - return RootTestLayout.this.getApplication(); - } - }; - getApplication().addResource(resource); - addComponent(new Link("Resource", resource)); - - LoginForm loginForm = new LoginForm(); - loginForm.addListener(new LoginForm.LoginListener() { - public void onLogin(LoginEvent event) { - System.out.println("User: " - + event.getLoginParameter("username") + ", Password: " - + event.getLoginParameter("password")); - } - }); - addComponent(loginForm); - } -} \ No newline at end of file diff --git a/src/com/vaadin/terminal/gwt/server/ApplicationServlet.java b/src/com/vaadin/terminal/gwt/server/ApplicationServlet.java index ae0dab24f1..763117f3dd 100644 --- a/src/com/vaadin/terminal/gwt/server/ApplicationServlet.java +++ b/src/com/vaadin/terminal/gwt/server/ApplicationServlet.java @@ -8,7 +8,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import com.vaadin.Application; -import com.vaadin.ui.RootLayout; +import com.vaadin.ui.Root; /** * This servlet connects a Vaadin Application to Web. @@ -48,11 +48,11 @@ public class ApplicationServlet extends AbstractApplicationServlet { final String applicationClassName = servletConfig .getInitParameter("application"); if (applicationClassName == null) { - String rootLayoutParam = servletConfig - .getInitParameter("rootLayout"); + String rootParam = servletConfig + .getInitParameter(Application.ROOT_PARAMETER); // Validate the parameter value - verifyRootLayoutClass(rootLayoutParam); + verifyRootClass(rootParam); // Application can be used if a valid rootLayout is defined applicationClass = Application.class; @@ -68,27 +68,26 @@ public class ApplicationServlet extends AbstractApplicationServlet { } } - private void verifyRootLayoutClass(String className) - throws ServletException { + private void verifyRootClass(String className) throws ServletException { if (className == null) { - throw new ServletException( - "rootLayout servlet parameter not defined"); + throw new ServletException(Application.ROOT_PARAMETER + + " servlet parameter not defined"); } // Check that the root layout class can be found try { - Class rootLayoutClass = getClassLoader().loadClass(className); - if (!RootLayout.class.isAssignableFrom(rootLayoutClass)) { + Class rootClass = getClassLoader().loadClass(className); + if (!Root.class.isAssignableFrom(rootClass)) { throw new ServletException(className - + " does not implement RootLayout"); + + " does not implement Root"); } // Try finding a default constructor, else throw exception - rootLayoutClass.getConstructor(); + rootClass.getConstructor(); } catch (ClassNotFoundException e) { - throw new ServletException("rootLayout class could not be loaded", - e); + throw new ServletException(className + " could not be loaded", e); } catch (SecurityException e) { - throw new ServletException("Could not access rootLayout class", e); + throw new ServletException("Could not access " + className + + " class", e); } catch (NoSuchMethodException e) { throw new ServletException(className + " doesn't have a public no-args constructor"); diff --git a/src/com/vaadin/ui/Root.java b/src/com/vaadin/ui/Root.java index 596c2b32a2..e23aa9cd55 100644 --- a/src/com/vaadin/ui/Root.java +++ b/src/com/vaadin/ui/Root.java @@ -38,7 +38,7 @@ public class Root extends AbstractComponentContainer { */ public static final int BORDER_DEFAULT = 2; - private final RootLayout rootLayout; + private ComponentContainer content; private Terminal terminal; private Application application; @@ -74,9 +74,21 @@ public class Root extends AbstractComponentContainer { private static final ThreadLocal currentRoot = new ThreadLocal(); - public Root(RootLayout rootLayout) { - this.rootLayout = rootLayout; - addComponent(rootLayout); + public Root() { + // Nothing to do here? + } + + public Root(ComponentContainer content) { + this.content = content; + } + + public Root(String caption) { + setCaption(caption); + } + + public Root(String caption, ComponentContainer content) { + this(caption); + this.content = content; } @Override @@ -95,7 +107,10 @@ public class Root extends AbstractComponentContainer { @Override public void paintContent(PaintTarget target) throws PaintException { - getRootLayout().paint(target); + ComponentContainer content = getContent(); + if (content != null) { + content.paint(target); + } // Paint subwindows for (final Iterator i = windows.iterator(); i.hasNext();) { @@ -172,7 +187,7 @@ public class Root extends AbstractComponentContainer { } public Iterator getComponentIterator() { - return Collections.singleton((Component) getRootLayout()).iterator(); + return Collections.singleton((Component) getContent()).iterator(); } public String getName() { @@ -479,12 +494,44 @@ public class Root extends AbstractComponentContainer { requestRepaint(); } - public RootLayout getRootLayout() { - return rootLayout; + public ComponentContainer getContent() { + return content; + } + + public void setContent(ComponentContainer content) { + if (this.content != null) { + super.removeComponent(content); + } + this.content = content; + if (content != null) { + super.addComponent(content); + } + } + + @Override + public void addComponent(Component c) { + // Use the thread local as the instance field might not yet be inited + if (Application.getCurrentApplication() instanceof Application.LegacyApplication) { + getContent().addComponent(c); + } else { + throw new UnsupportedOperationException( + "Add components to the Root's content instead"); + } + } + + @Override + public void removeComponent(Component c) { + // Use the thread local as the instance field might not yet be inited + if (Application.getCurrentApplication() instanceof Application.LegacyApplication) { + getContent().removeComponent(c); + } else { + throw new UnsupportedOperationException( + "Remove components from the Root's content instead"); + } } public void init(WrappedRequest request) { - getRootLayout().init(request); + } public static void setCurrentRoot(Root root) { diff --git a/src/com/vaadin/ui/RootLayout.java b/src/com/vaadin/ui/RootLayout.java deleted file mode 100644 index 16dd034f91..0000000000 --- a/src/com/vaadin/ui/RootLayout.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.vaadin.ui; - -import com.vaadin.terminal.WrappedRequest; - -public interface RootLayout extends Component { - public void init(WrappedRequest request); -} -- 2.39.5