]> source.dussan.org Git - vaadin-framework.git/commitdiff
Allow configuring the root class to use, remove RootLayout
authorLeif Åstrand <leif@vaadin.com>
Wed, 16 Nov 2011 09:01:32 +0000 (11:01 +0200)
committerLeif Åstrand <leif@vaadin.com>
Wed, 16 Nov 2011 09:01:32 +0000 (11:01 +0200)
WebContent/WEB-INF/web.xml
src/com/vaadin/Application.java
src/com/vaadin/RootTest.java [new file with mode: 0644]
src/com/vaadin/RootTestApplication.java [deleted file]
src/com/vaadin/RootTestLayout.java [deleted file]
src/com/vaadin/terminal/gwt/server/ApplicationServlet.java
src/com/vaadin/ui/Root.java
src/com/vaadin/ui/RootLayout.java [deleted file]

index 42f3b2df8720eeee48e0fec0c582a8e9308dfb94..4878311eb8d2b8a53716a0190404be17bc2f441d 100644 (file)
@@ -33,8 +33,8 @@
                <servlet-name>RootsTester</servlet-name>\r
                <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>\r
                <init-param>\r
-                       <param-name>rootLayout</param-name>\r
-                   <param-value>com.vaadin.RootTestLayout</param-value>\r
+                       <param-name>root</param-name>\r
+                   <param-value>com.vaadin.RootTest</param-value>\r
                </init-param>           \r
        </servlet>\r
 \r
index 21e642f0e12bc028e89d3d213bd8cf13ac084e23..de68bff9bc657420f10106c8aa945dc40de39db9 100644 (file)
@@ -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<? extends RootLayout> rootLayoutClass = Class.forName(
-                        rootLayoutClassName).asSubclass(RootLayout.class);
+                Class<? extends Root> 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 (file)
index 0000000..32fb41e
--- /dev/null
@@ -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 (file)
index 88c9763..0000000
+++ /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 (file)
index 230dbda..0000000
+++ /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
index ae0dab24f1205245619225ddef117d9143c89214..763117f3ddece952349307f4952391503e5a1384 100644 (file)
@@ -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");
index 596c2b32a29354c77502e8912c29bc85e06cdea5..e23aa9cd55d485617241e2f6ceed325095038b27 100644 (file)
@@ -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<Root> currentRoot = new ThreadLocal<Root>();
 
-    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<Window> i = windows.iterator(); i.hasNext();) {
@@ -172,7 +187,7 @@ public class Root extends AbstractComponentContainer {
     }
 
     public Iterator<Component> 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 (file)
index 16dd034..0000000
+++ /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);
-}