Browse Source

Allow configuring the root class to use, remove RootLayout

tags/7.0.0.alpha1
Leif Åstrand 12 years ago
parent
commit
9f9ccd8d00

+ 2
- 2
WebContent/WEB-INF/web.xml View File

@@ -33,8 +33,8 @@
<servlet-name>RootsTester</servlet-name>
<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
<init-param>
<param-name>rootLayout</param-name>
<param-value>com.vaadin.RootTestLayout</param-value>
<param-name>root</param-name>
<param-value>com.vaadin.RootTest</param-value>
</init-param>
</servlet>

+ 15
- 13
src/com/vaadin/Application.java View 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,

+ 17
- 0
src/com/vaadin/RootTest.java View File

@@ -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);
}
}

+ 0
- 40
src/com/vaadin/RootTestApplication.java View File

@@ -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;
// }

}

+ 0
- 85
src/com/vaadin/RootTestLayout.java View File

@@ -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);
}
}

+ 14
- 15
src/com/vaadin/terminal/gwt/server/ApplicationServlet.java View 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");

+ 56
- 9
src/com/vaadin/ui/Root.java View 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) {

+ 0
- 7
src/com/vaadin/ui/RootLayout.java View File

@@ -1,7 +0,0 @@
package com.vaadin.ui;

import com.vaadin.terminal.WrappedRequest;

public interface RootLayout extends Component {
public void init(WrappedRequest request);
}

Loading…
Cancel
Save