From: Leif Åstrand Date: Thu, 10 Nov 2011 16:06:19 +0000 (+0200) Subject: Create Root initialized with RootLayout defined in web.xml X-Git-Tag: 7.0.0.alpha1~314 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=424d91dc68c86166f5895fa10346ae09e32d5262;p=vaadin-framework.git Create Root initialized with RootLayout defined in web.xml --- diff --git a/src/com/vaadin/Application.java b/src/com/vaadin/Application.java index 336635ce3f..e5415a3e7d 100644 --- a/src/com/vaadin/Application.java +++ b/src/com/vaadin/Application.java @@ -38,6 +38,7 @@ 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; /** @@ -1477,7 +1478,28 @@ public abstract class Application implements Terminal.ErrorListener, return root; } - protected abstract Root createRoot(WrappedRequest request); + protected Root createRoot(WrappedRequest request) { + Object rootLayoutClassNameObj = properties.get("rootLayout"); + if (rootLayoutClassNameObj instanceof String) { + String rootLayoutClassName = (String) rootLayoutClassNameObj; + try { + Class rootLayoutClass = Class.forName( + rootLayoutClassName).asSubclass(RootLayout.class); + try { + RootLayout rootLayout = rootLayoutClass.newInstance(); + return new Root(rootLayout); + } catch (Exception e) { + throw new RuntimeException( + "Could instantiate rootLayout class " + + rootLayoutClassName, e); + } + } catch (ClassNotFoundException e) { + throw new RuntimeException("Could not load rootLayout class " + + rootLayoutClassName, e); + } + } + throw new RuntimeException("No rootLayout defined in web.xml"); + } public boolean handleRequest(WrappedRequest request, WrappedResponse response) throws IOException { diff --git a/src/com/vaadin/RootTestApplication.java b/src/com/vaadin/RootTestApplication.java index 714e09f1c2..88c9763733 100644 --- a/src/com/vaadin/RootTestApplication.java +++ b/src/com/vaadin/RootTestApplication.java @@ -1,95 +1,13 @@ package com.vaadin; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import com.vaadin.terminal.ApplicationResource; -import com.vaadin.terminal.DownloadStream; import com.vaadin.terminal.RequestHandler; import com.vaadin.terminal.WrappedRequest; import com.vaadin.terminal.WrappedResponse; -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.Root; -import com.vaadin.ui.RootLayout; -import com.vaadin.ui.VerticalLayout; public class RootTestApplication extends Application { - private static class MyRootLayout extends VerticalLayout implements - RootLayout { - private final String rootText; - - public MyRootLayout(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 MyRootLayout.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); - } - } - @Override public void init() { addRequestHandler(new RequestHandler() { @@ -112,11 +30,11 @@ public class RootTestApplication extends Application { }); } - @Override - protected Root createRoot(WrappedRequest request) { - String rootText = request.getParameter("rootText"); - Root root = new Root(new MyRootLayout(rootText)); - return root; - } + // @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 new file mode 100644 index 0000000000..230dbda6f9 --- /dev/null +++ b/src/com/vaadin/RootTestLayout.java @@ -0,0 +1,85 @@ +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