From 5c2a9918e091f526ba2e17e2c70f8bcbbf47c1f5 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 3 Apr 2012 00:29:44 +0300 Subject: [PATCH] Automatically create state on server side based on getState return type --- src/com/vaadin/ui/AbstractComponent.java | 34 +++++++++++++++++++---- src/com/vaadin/ui/AbstractField.java | 4 --- src/com/vaadin/ui/AbstractSplitPanel.java | 5 ---- src/com/vaadin/ui/Button.java | 6 ---- src/com/vaadin/ui/Panel.java | 4 --- src/com/vaadin/ui/Window.java | 4 --- 6 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index 189142519d..8c26911a9a 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -18,6 +18,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -879,18 +881,34 @@ public abstract class AbstractComponent implements Component, MethodEventSource /** * Creates the shared state bean to be used in server to client * communication. - * - * Subclasses should implement this method and return a new instance of the - * correct state class. - * - * All configuration of the values of the state should be performed in - * {@link #getState()}, not in {@link #createState()}. + *

+ * By default a state object of the defined return type of + * {@link #getState()} is created. Subclasses can override this method and + * return a new instance of the correct state class but this should rarely + * be necessary. + *

+ *

+ * No configuration of the values of the state should be performed in + * {@link #createState()}. * * @since 7.0 * * @return new shared state object */ protected ComponentState createState() { + try { + Method m = getClass().getMethod("getState", (Class[]) null); + Class type = (Class) m + .getReturnType(); + return type.newInstance(); + } catch (Exception e) { + getLogger().log( + Level.INFO, + "Error determining state object class for " + + getClass().getName()); + } + + // Fall back to ComponentState if detection fails for some reason. return new ComponentState(); } @@ -1743,4 +1761,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource } return connectorId; } + + private Logger getLogger() { + return Logger.getLogger(AbstractComponent.class.getName()); + } } diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index aecb07e108..361e65fa34 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -1592,8 +1592,4 @@ public abstract class AbstractField extends AbstractComponent implements getState().setHideErrors(shouldHideErrors()); } - @Override - protected AbstractFieldState createState() { - return new AbstractFieldState(); - } } diff --git a/src/com/vaadin/ui/AbstractSplitPanel.java b/src/com/vaadin/ui/AbstractSplitPanel.java index 2e31436ee2..b576008344 100644 --- a/src/com/vaadin/ui/AbstractSplitPanel.java +++ b/src/com/vaadin/ui/AbstractSplitPanel.java @@ -11,7 +11,6 @@ import java.util.Iterator; import com.vaadin.event.ComponentEventListener; import com.vaadin.event.MouseEvents.ClickEvent; import com.vaadin.terminal.Sizeable; -import com.vaadin.terminal.gwt.client.ComponentState; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.ui.AbstractSplitPanelConnector.AbstractSplitPanelRPC; import com.vaadin.terminal.gwt.client.ui.AbstractSplitPanelConnector.AbstractSplitPanelState; @@ -388,8 +387,4 @@ public abstract class AbstractSplitPanel extends AbstractLayout { return (AbstractSplitPanelState) super.getState(); } - @Override - protected ComponentState createState() { - return new AbstractSplitPanelState(); - } } diff --git a/src/com/vaadin/ui/Button.java b/src/com/vaadin/ui/Button.java index 7deb048655..5c66d57939 100644 --- a/src/com/vaadin/ui/Button.java +++ b/src/com/vaadin/ui/Button.java @@ -18,7 +18,6 @@ import com.vaadin.event.ShortcutAction; import com.vaadin.event.ShortcutAction.KeyCode; import com.vaadin.event.ShortcutAction.ModifierKey; import com.vaadin.event.ShortcutListener; -import com.vaadin.terminal.gwt.client.ComponentState; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.ui.ButtonConnector.ButtonServerRpc; import com.vaadin.terminal.gwt.client.ui.ButtonState; @@ -496,11 +495,6 @@ public class Button extends AbstractComponent implements super.focus(); } - @Override - protected ComponentState createState() { - return new ButtonState(); - } - @Override public ButtonState getState() { return (ButtonState) super.getState(); diff --git a/src/com/vaadin/ui/Panel.java b/src/com/vaadin/ui/Panel.java index 26c7e63908..ad0ff4222f 100644 --- a/src/com/vaadin/ui/Panel.java +++ b/src/com/vaadin/ui/Panel.java @@ -546,8 +546,4 @@ public class Panel extends AbstractComponentContainer implements Scrollable, return (PanelState) super.getState(); } - @Override - protected PanelState createState() { - return new PanelState(); - } } diff --git a/src/com/vaadin/ui/Window.java b/src/com/vaadin/ui/Window.java index aa49a36b55..f15b46ce1d 100644 --- a/src/com/vaadin/ui/Window.java +++ b/src/com/vaadin/ui/Window.java @@ -903,8 +903,4 @@ public class Window extends Panel implements FocusNotifier, BlurNotifier { return (WindowState) super.getState(); } - @Override - protected WindowState createState() { - return new WindowState(); - } } -- 2.39.5