]> source.dussan.org Git - vaadin-framework.git/commitdiff
Create Root initialized with RootLayout defined in web.xml
authorLeif Åstrand <leif@vaadin.com>
Thu, 10 Nov 2011 16:06:19 +0000 (18:06 +0200)
committerLeif Åstrand <leif@vaadin.com>
Thu, 10 Nov 2011 16:06:19 +0000 (18:06 +0200)
src/com/vaadin/Application.java
src/com/vaadin/RootTestApplication.java
src/com/vaadin/RootTestLayout.java [new file with mode: 0644]

index 336635ce3f7c4ce8d44b3ec85bec5405e9c01478..e5415a3e7dcc46f760ddd92474fa1fad1efc2dd5 100644 (file)
@@ -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<? extends RootLayout> 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 {
index 714e09f1c23164e29ec13c935d1b14d941129145..88c97637335b17fd3c15aefc395027969230e8c8 100644 (file)
@@ -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 (file)
index 0000000..230dbda
--- /dev/null
@@ -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