From 042c30f5ac200d3475abdcbfb7e26fcffdf50272 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Sun, 4 Mar 2012 12:19:31 +0200 Subject: [PATCH] Use ComponentState for storing the state, not just as a communication mechanism. Changed isDisabled to more logical isEnabled in state. --- .../terminal/gwt/client/ComponentState.java | 42 +++++-- .../vaadin/terminal/gwt/client/VCaption.java | 2 +- .../client/ui/AbstractComponentConnector.java | 4 +- .../client/ui/AbstractDateFieldConnector.java | 2 +- .../ui/AbstractSplitPanelConnector.java | 2 +- .../gwt/client/ui/ComboBoxConnector.java | 2 +- .../terminal/gwt/client/ui/LinkConnector.java | 2 +- .../gwt/client/ui/MenuBarConnector.java | 2 +- .../client/ui/OptionGroupBaseConnector.java | 2 +- .../client/ui/ProgressIndicatorConnector.java | 4 +- .../gwt/client/ui/SliderConnector.java | 2 +- .../gwt/client/ui/TableConnector.java | 2 +- .../gwt/client/ui/TabsheetBaseConnector.java | 2 +- .../terminal/gwt/client/ui/TreeConnector.java | 2 +- .../gwt/client/ui/UploadConnector.java | 2 +- .../terminal/gwt/client/ui/VFormLayout.java | 2 +- .../richtextarea/RichTextAreaConnector.java | 2 +- src/com/vaadin/ui/AbstractComponent.java | 105 ++++-------------- 18 files changed, 74 insertions(+), 109 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/ComponentState.java b/src/com/vaadin/terminal/gwt/client/ComponentState.java index 4cf7c7fe13..e42afd6d03 100644 --- a/src/com/vaadin/terminal/gwt/client/ComponentState.java +++ b/src/com/vaadin/terminal/gwt/client/ComponentState.java @@ -5,6 +5,7 @@ package com.vaadin.terminal.gwt.client; import com.vaadin.terminal.gwt.client.communication.SharedState; +import com.vaadin.ui.Component; /** * Default shared state implementation for UI components. @@ -19,11 +20,12 @@ public class ComponentState extends SharedState { private boolean readOnly = false; private boolean immediate = false; private String style = ""; - private boolean disabled = false; + private boolean enabled = true; private String description = ""; // Note: for the caption, there is a difference between null and an empty // string! private String caption = null; + private boolean visible = true; /** * Returns the component height as set by the server. @@ -180,26 +182,26 @@ public class ComponentState extends SharedState { } /** - * Returns true if the component is disabled. + * Returns true if the component is enabled. * * @see com.vaadin.ui.Component#isEnabled() * - * @return true if the component is disabled + * @return true if the component is enabled */ - public boolean isDisabled() { - return disabled; + public boolean isEnabled() { + return enabled; } /** - * Disables or enables the component. + * Enables or disables the component. * * @see com.vaadin.ui.Component#setEnabled(boolean) * - * @param disabled + * @param enabled * new mode for the component */ - public void setDisabled(boolean disabled) { - this.disabled = disabled; + public void setEnabled(boolean enabled) { + this.enabled = enabled; } /** @@ -264,4 +266,26 @@ public class ComponentState extends SharedState { this.caption = caption; } + /** + * Returns the visibility state of the component. Note that this state is + * related to the component only, not its parent. This might differ from + * what {@link Component#isVisible()} returns as this takes the hierarchy + * into account. + * + * @return The visibility state. + */ + public boolean isVisible() { + return visible; + } + + /** + * Sets the visibility state of the component. + * + * @param visible + * The new visibility state. + */ + public void setVisible(boolean visible) { + this.visible = visible; + } + } diff --git a/src/com/vaadin/terminal/gwt/client/VCaption.java b/src/com/vaadin/terminal/gwt/client/VCaption.java index 73ef574321..b7d66612a9 100644 --- a/src/com/vaadin/terminal/gwt/client/VCaption.java +++ b/src/com/vaadin/terminal/gwt/client/VCaption.java @@ -113,7 +113,7 @@ public class VCaption extends HTML { style += " " + CLASSNAME + "-" + styles[i]; } } - if (owner.getState().isDisabled()) { + if (!owner.getState().isEnabled()) { style += " " + ApplicationConnection.DISABLED_CLASSNAME; } setStyleName(style); diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java index f648428c6a..446d941399 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java @@ -173,7 +173,7 @@ public abstract class AbstractComponentConnector extends AbstractConnector ((Focusable) getWidget()).setTabIndex(uidl .getIntAttribute("tabindex")); } - setEnabled(!getState().isDisabled()); + setEnabled(getState().isEnabled()); // Style names String styleName = getStyleNameFromUIDL(getWidget() @@ -358,7 +358,7 @@ public abstract class AbstractComponentConnector extends AbstractConnector */ protected static String getStyleNameFromUIDL(String primaryStyleName, UIDL uidl, ComponentState state, boolean field) { - boolean enabled = !state.isDisabled(); + boolean enabled = state.isEnabled(); StringBuffer styleBuf = new StringBuffer(); styleBuf.append(primaryStyleName); diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractDateFieldConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractDateFieldConnector.java index c2652adbd4..0068eb8960 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractDateFieldConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractDateFieldConnector.java @@ -28,7 +28,7 @@ public class AbstractDateFieldConnector extends AbstractComponentConnector { getWidget().immediate = getState().isImmediate(); getWidget().readonly = getState().isReadOnly(); - getWidget().enabled = !getState().isDisabled(); + getWidget().enabled = getState().isEnabled(); if (uidl.hasAttribute("locale")) { final String locale = uidl.getStringAttribute("locale"); diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java index 91ee5a148a..2f0674b58d 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java @@ -75,7 +75,7 @@ public abstract class AbstractSplitPanelConnector extends if (!isRealUpdate(uidl)) { return; } - getWidget().setEnabled(!getState().isDisabled()); + getWidget().setEnabled(getState().isEnabled()); clickEventHandler.handleEventHandlerRegistration(client); if (getState().hasStyles()) { diff --git a/src/com/vaadin/terminal/gwt/client/ui/ComboBoxConnector.java b/src/com/vaadin/terminal/gwt/client/ui/ComboBoxConnector.java index 0781ddf3f9..d8fb48dc8d 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/ComboBoxConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/ComboBoxConnector.java @@ -30,7 +30,7 @@ public class ComboBoxConnector extends AbstractComponentConnector implements getWidget().paintableId = uidl.getId(); getWidget().readonly = getState().isReadOnly(); - getWidget().enabled = !getState().isDisabled(); + getWidget().enabled = getState().isEnabled(); getWidget().tb.setEnabled(getWidget().enabled); getWidget().updateReadOnly(); diff --git a/src/com/vaadin/terminal/gwt/client/ui/LinkConnector.java b/src/com/vaadin/terminal/gwt/client/ui/LinkConnector.java index 9d23053a5b..a8c10650c1 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/LinkConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/LinkConnector.java @@ -29,7 +29,7 @@ public class LinkConnector extends AbstractComponentConnector { getWidget().client = client; - getWidget().enabled = !getState().isDisabled(); + getWidget().enabled = getState().isEnabled(); getWidget().readonly = getState().isReadOnly(); if (uidl.hasAttribute("name")) { diff --git a/src/com/vaadin/terminal/gwt/client/ui/MenuBarConnector.java b/src/com/vaadin/terminal/gwt/client/ui/MenuBarConnector.java index 3000445a88..62b1cb69a7 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/MenuBarConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/MenuBarConnector.java @@ -38,7 +38,7 @@ public class MenuBarConnector extends AbstractComponentConnector implements getWidget().openRootOnHover = uidl .getBooleanAttribute(VMenuBar.OPEN_ROOT_MENU_ON_HOWER); - getWidget().enabled = !getState().isDisabled(); + getWidget().enabled = getState().isEnabled(); // For future connections getWidget().client = client; diff --git a/src/com/vaadin/terminal/gwt/client/ui/OptionGroupBaseConnector.java b/src/com/vaadin/terminal/gwt/client/ui/OptionGroupBaseConnector.java index 7cdb55457d..41844da348 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/OptionGroupBaseConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/OptionGroupBaseConnector.java @@ -25,7 +25,7 @@ public abstract class OptionGroupBaseConnector extends getWidget().selectedKeys = uidl.getStringArrayVariableAsSet("selected"); getWidget().readonly = getState().isReadOnly(); - getWidget().disabled = getState().isDisabled(); + getWidget().disabled = !getState().isEnabled(); getWidget().multiselect = "multi".equals(uidl .getStringAttribute("selectmode")); getWidget().immediate = getState().isImmediate(); diff --git a/src/com/vaadin/terminal/gwt/client/ui/ProgressIndicatorConnector.java b/src/com/vaadin/terminal/gwt/client/ui/ProgressIndicatorConnector.java index eda691e357..e5cd8f6d4d 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/ProgressIndicatorConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/ProgressIndicatorConnector.java @@ -30,7 +30,7 @@ public class ProgressIndicatorConnector extends AbstractComponentConnector { if (getWidget().indeterminate) { String basename = VProgressIndicator.CLASSNAME + "-indeterminate"; getWidget().addStyleName(basename); - if (getState().isDisabled()) { + if (!getState().isEnabled()) { getWidget().addStyleName(basename + "-disabled"); } else { getWidget().removeStyleName(basename + "-disabled"); @@ -46,7 +46,7 @@ public class ProgressIndicatorConnector extends AbstractComponentConnector { } } - if (!getState().isDisabled()) { + if (getState().isEnabled()) { getWidget().interval = uidl.getIntAttribute("pollinginterval"); getWidget().poller.scheduleRepeating(getWidget().interval); } diff --git a/src/com/vaadin/terminal/gwt/client/ui/SliderConnector.java b/src/com/vaadin/terminal/gwt/client/ui/SliderConnector.java index b854aae818..900e3af633 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/SliderConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/SliderConnector.java @@ -25,7 +25,7 @@ public class SliderConnector extends AbstractComponentConnector { } getWidget().immediate = getState().isImmediate(); - getWidget().disabled = getState().isDisabled(); + getWidget().disabled = !getState().isEnabled(); getWidget().readonly = getState().isReadOnly(); getWidget().vertical = uidl.hasAttribute("vertical"); diff --git a/src/com/vaadin/terminal/gwt/client/ui/TableConnector.java b/src/com/vaadin/terminal/gwt/client/ui/TableConnector.java index 0aa6879802..eadefc94b3 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/TableConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/TableConnector.java @@ -55,7 +55,7 @@ public class TableConnector extends AbstractComponentContainerConnector return; } - getWidget().enabled = !getState().isDisabled(); + getWidget().enabled = getState().isEnabled(); if (BrowserInfo.get().isIE8() && !getWidget().enabled) { /* diff --git a/src/com/vaadin/terminal/gwt/client/ui/TabsheetBaseConnector.java b/src/com/vaadin/terminal/gwt/client/ui/TabsheetBaseConnector.java index 3475f2d716..8a52d5bb49 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/TabsheetBaseConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/TabsheetBaseConnector.java @@ -31,7 +31,7 @@ public abstract class TabsheetBaseConnector extends // Update member references getWidget().id = uidl.getId(); - getWidget().disabled = getState().isDisabled(); + getWidget().disabled = !getState().isEnabled(); // Render content final UIDL tabs = uidl.getChildUIDL(0); diff --git a/src/com/vaadin/terminal/gwt/client/ui/TreeConnector.java b/src/com/vaadin/terminal/gwt/client/ui/TreeConnector.java index 9b16e76a49..118b01fe03 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/TreeConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/TreeConnector.java @@ -43,7 +43,7 @@ public class TreeConnector extends AbstractComponentConnector { getWidget().immediate = getState().isImmediate(); - getWidget().disabled = getState().isDisabled(); + getWidget().disabled = !getState().isEnabled(); getWidget().readonly = getState().isReadOnly(); getWidget().dragMode = uidl.hasAttribute("dragMode") ? uidl diff --git a/src/com/vaadin/terminal/gwt/client/ui/UploadConnector.java b/src/com/vaadin/terminal/gwt/client/ui/UploadConnector.java index db94387525..05a5e7d73d 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/UploadConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/UploadConnector.java @@ -41,7 +41,7 @@ public class UploadConnector extends AbstractComponentConnector { } getWidget().fu.setName(getWidget().paintableId + "_file"); - if (getState().isDisabled() || getState().isReadOnly()) { + if (!getState().isEnabled() || getState().isReadOnly()) { getWidget().disableUpload(); } else if (!uidl.getBooleanAttribute("state")) { // Enable the button only if an upload is not in progress diff --git a/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java b/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java index 713a0ae55f..b322be6601 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java @@ -61,7 +61,7 @@ public class VFormLayout extends SimplePanel { } } - if (state.isDisabled()) { + if (!state.isEnabled()) { styles.add(ApplicationConnection.DISABLED_CLASSNAME); } diff --git a/src/com/vaadin/terminal/gwt/client/ui/richtextarea/RichTextAreaConnector.java b/src/com/vaadin/terminal/gwt/client/ui/richtextarea/RichTextAreaConnector.java index da32c1730e..f60ed53813 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/richtextarea/RichTextAreaConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/richtextarea/RichTextAreaConnector.java @@ -28,7 +28,7 @@ public class RichTextAreaConnector extends AbstractComponentConnector implements } } if (isRealUpdate(uidl)) { - getWidget().setEnabled(!getState().isDisabled()); + getWidget().setEnabled(getState().isEnabled()); } super.updateFromUIDL(uidl, client); diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index 12b67355ce..92e064a33f 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -64,11 +64,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource */ private ArrayList styles; - /** - * Caption text. - */ - private String caption; - /** * Application specific data object. The component does not use or modify * this. @@ -80,26 +75,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource */ private Resource icon; - /** - * Is the component enabled (its normal usage is allowed). - */ - private boolean enabled = true; - - /** - * Is the component visible (it is rendered). - */ - private boolean visible = true; - - /** - * Is the component read-only ? - */ - private boolean readOnly = false; - - /** - * Description of the usage (XML). - */ - private String description = null; - /** * The container this component resides in. */ @@ -120,12 +95,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource */ private ErrorMessage componentError = null; - /** - * Immediate mode: if true, all variable changes are required to be sent - * from the terminal immediately. - */ - private boolean immediate = false; - /** * Locale of this component. */ @@ -306,7 +275,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource * the default documentation from implemented interface. */ public String getCaption() { - return caption; + return getState().getCaption(); } /** @@ -319,7 +288,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource * the new caption String for the component. */ public void setCaption(String caption) { - this.caption = caption; + getState().setCaption(caption); requestRepaint(); } @@ -393,7 +362,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource * here, we use the default documentation from implemented interface. */ public boolean isEnabled() { - return enabled && (parent == null || parent.isEnabled()) && isVisible(); + return getState().isEnabled() + && (getParent() == null || getParent().isEnabled()); } /* @@ -401,29 +371,9 @@ public abstract class AbstractComponent implements Component, MethodEventSource * use the default documentation from implemented interface. */ public void setEnabled(boolean enabled) { - if (this.enabled != enabled) { - boolean wasEnabled = this.enabled; - boolean wasEnabledInContext = isEnabled(); - - this.enabled = enabled; - - boolean isEnabled = enabled; - boolean isEnabledInContext = isEnabled(); - - // If the actual enabled state (as rendered, in context) has not - // changed we do not need to repaint except if the parent is - // invisible. - // If the parent is invisible we must request a repaint so the - // component is repainted with the new enabled state when the parent - // is set visible again. This workaround is needed as isEnabled - // checks isVisible. - boolean needRepaint = (wasEnabledInContext != isEnabledInContext) - || (wasEnabled != isEnabled && (getParent() == null || !getParent() - .isVisible())); - - if (needRepaint) { - requestRepaint(); - } + if (getState().isEnabled() != enabled) { + getState().setEnabled(enabled); + requestRepaint(); } } @@ -433,7 +383,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource * interface. */ public boolean isImmediate() { - return immediate; + return getState().isImmediate(); } /** @@ -447,7 +397,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource * @see Component#isImmediate() */ public void setImmediate(boolean immediate) { - this.immediate = immediate; + getState().setImmediate(immediate); requestRepaint(); } @@ -457,7 +407,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource * @see com.vaadin.ui.Component#isVisible() */ public boolean isVisible() { - return visible && (getParent() == null || getParent().isVisible()); + return getState().isVisible() + && (getParent() == null || getParent().isVisible()); } /* @@ -467,8 +418,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource */ public void setVisible(boolean visible) { - if (this.visible != visible) { - this.visible = visible; + if (getState().isVisible() != visible) { + getState().setVisible(visible); // Instead of requesting repaint normally we // fire the event directly to assure that the // event goes through event in the component might @@ -535,7 +486,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource * @return component's description String */ public String getDescription() { - return description; + return getState().getDescription(); } /** @@ -552,7 +503,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource * the new description string for the component. */ public void setDescription(String description) { - this.description = description; + getState().setDescription(description); requestRepaint(); } @@ -638,7 +589,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource * here, we use the default documentation from implemented interface. */ public boolean isReadOnly() { - return readOnly; + return getState().isReadOnly(); } /* @@ -646,7 +597,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource * use the default documentation from implemented interface. */ public void setReadOnly(boolean readOnly) { - this.readOnly = readOnly; + getState().setReadOnly(readOnly); requestRepaint(); } @@ -669,7 +620,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource */ public void attach() { requestRepaint(); - if (!visible) { + if (!getState().isVisible()) { /* * Bypass the repaint optimization in childRequestedRepaint method * when attaching. When reattaching (possibly moving) -> must @@ -874,13 +825,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource } // basic state: caption, size, enabled, ... - if (!isVisible()) { - return null; - } - - // TODO for now, this superclass always recreates the state from - // scratch, whereas subclasses should only modify it - + // TODO This logic should be on the client side and the state should + // simply be a data object with "width" and "height". if (getHeight() >= 0 && (getHeightUnits() != Unit.PERCENTAGE || ComponentSizeValidator .parentCanDefineHeight(this))) { @@ -897,15 +843,10 @@ public abstract class AbstractComponent implements Component, MethodEventSource sharedState.setWidth(""); } - sharedState.setImmediate(isImmediate()); - sharedState.setReadOnly(isReadOnly()); - sharedState.setDisabled(!isEnabled()); - + // TODO This should be an array in state and the logic for converting + // array -> class name should be on the client side sharedState.setStyle(getStyleName()); - sharedState.setCaption(getCaption()); - sharedState.setDescription(getDescription()); - // TODO icon also in shared state - how to convert Resource? return sharedState; @@ -942,7 +883,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource Collection alreadyNotified) { // Invisible components (by flag in this particular component) do not // need repaints - if (!visible) { + if (!getState().isVisible()) { return; } -- 2.39.5