]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use shared state in ComboBox (#19229)
authorHenri Sara <hesara@vaadin.com>
Thu, 5 Nov 2015 08:51:56 +0000 (10:51 +0200)
committerVaadin Code Review <review@vaadin.com>
Mon, 8 Aug 2016 06:47:17 +0000 (06:47 +0000)
This change uses shared state for the read-only flag, text
input allowed flag and input prompt.

Change-Id: If770a3d9be96d10c3a19654b398bc2f5ddfb7e67

client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java
server/src/main/java/com/vaadin/ui/ComboBox.java
shared/src/main/java/com/vaadin/shared/ui/combobox/ComboBoxState.java

index 131485bc7b75ccb97fa3e3e0af5f1d799deadb51..61c25f1724cb893b8f16f29fa41bf4eda229a5d4 100644 (file)
@@ -23,7 +23,9 @@ import com.google.gwt.core.client.Scheduler;
 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
 import com.vaadin.client.ApplicationConnection;
 import com.vaadin.client.Paintable;
+import com.vaadin.client.Profiler;
 import com.vaadin.client.UIDL;
+import com.vaadin.client.communication.StateChangeEvent;
 import com.vaadin.client.ui.AbstractFieldConnector;
 import com.vaadin.client.ui.SimpleManagedLayout;
 import com.vaadin.client.ui.VFilterSelect;
@@ -42,6 +44,28 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
     // update textbox text by a changed item caption.
     private boolean oldSuggestionTextMatchTheOldSelection;
 
+    @Override
+    public void onStateChanged(StateChangeEvent stateChangeEvent) {
+        super.onStateChanged(stateChangeEvent);
+
+        Profiler.enter("ComboBoxConnector.onStateChanged update content");
+
+        getWidget().readonly = isReadOnly();
+        getWidget().updateReadOnly();
+
+        getWidget().immediate = getState().immediate;
+
+        getWidget().setTextInputEnabled(getState().textInputAllowed);
+
+        if (getState().inputPrompt != null) {
+            getWidget().inputPrompt = getState().inputPrompt;
+        } else {
+            getWidget().inputPrompt = "";
+        }
+
+        Profiler.leave("ComboBoxConnector.onStateChanged update content");
+    }
+
     /*
      * (non-Javadoc)
      * 
@@ -54,20 +78,10 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
         getWidget().client = client;
         getWidget().paintableId = uidl.getId();
 
-        getWidget().readonly = isReadOnly();
-        getWidget().updateReadOnly();
-
         if (!isRealUpdate(uidl)) {
             return;
         }
 
-        // Inverse logic here to make the default case (text input enabled)
-        // work without additional UIDL messages
-        boolean noTextInput = uidl
-                .hasAttribute(ComboBoxConstants.ATTR_NO_TEXT_INPUT)
-                && uidl.getBooleanAttribute(ComboBoxConstants.ATTR_NO_TEXT_INPUT);
-        getWidget().setTextInputEnabled(!noTextInput);
-
         // not a FocusWidget -> needs own tabindex handling
         getWidget().tb.setTabIndex(getState().tabIndex);
 
@@ -76,8 +90,6 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
                     .getStringAttribute("filteringmode"));
         }
 
-        getWidget().immediate = getState().immediate;
-
         getWidget().nullSelectionAllowed = uidl.hasAttribute("nullselect");
 
         getWidget().nullSelectItem = uidl.hasAttribute("nullselectitem")
@@ -89,21 +101,6 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
             getWidget().pageLength = uidl.getIntAttribute("pagelength");
         }
 
-        if (uidl.hasAttribute(ComboBoxConstants.ATTR_INPUTPROMPT)) {
-            // input prompt changed from server
-            getWidget().inputPrompt = uidl
-                    .getStringAttribute(ComboBoxConstants.ATTR_INPUTPROMPT);
-        } else {
-            getWidget().inputPrompt = "";
-        }
-
-        if (uidl.hasAttribute("suggestionPopupWidth")) {
-            getWidget().suggestionPopupWidth = uidl
-                    .getStringAttribute("suggestionPopupWidth");
-        } else {
-            getWidget().suggestionPopupWidth = null;
-        }
-
         if (uidl.hasAttribute("suggestionPopupWidth")) {
             getWidget().suggestionPopupWidth = uidl
                     .getStringAttribute("suggestionPopupWidth");
@@ -195,8 +192,8 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
                     && uidl.hasAttribute("selectedCaption")) {
                 // scrolling to correct page is disabled, caption is passed as a
                 // special parameter
-                getWidget().setSelectedCaption(
-                        uidl.getStringAttribute("selectedCaption"));
+                getWidget().setSelectedCaption(uidl
+                        .getStringAttribute("selectedCaption"));
             } else {
                 resetSelection();
             }
@@ -329,13 +326,13 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
                 // just clear the input if the value has changed from something
                 // else to null
                 if (getWidget().selectedOptionKey != null
-                        || (getWidget().allowNewItem && !getWidget().tb
-                                .getValue().isEmpty())) {
+                        || (getWidget().allowNewItem
+                                && !getWidget().tb.getValue().isEmpty())) {
 
                     boolean openedPopupWithNonScrollingMode = (getWidget().popupOpenerClicked
                             && getWidget().getSelectedCaption() != null);
                     if (!openedPopupWithNonScrollingMode) {
-                    getWidget().tb.setValue("");
+                        getWidget().tb.setValue("");
                     } else {
                         getWidget().tb
                                 .setValue(getWidget().getSelectedCaption());
index 562173a9bfe9b996e8dada0e551026390f6e5026..a6a562f2d2a2394681089f4197cc16eba49d96c4 100644 (file)
@@ -34,7 +34,6 @@ import com.vaadin.event.FieldEvents.FocusListener;
 import com.vaadin.server.PaintException;
 import com.vaadin.server.PaintTarget;
 import com.vaadin.server.Resource;
-import com.vaadin.shared.ui.combobox.ComboBoxConstants;
 import com.vaadin.shared.ui.combobox.ComboBoxState;
 import com.vaadin.shared.ui.combobox.FilteringMode;
 
@@ -75,8 +74,6 @@ public class ComboBox extends AbstractSelect implements
         public String getStyle(ComboBox source, Object itemId);
     }
 
-    private String inputPrompt = null;
-
     /**
      * Holds value of property pageLength. 0 disables paging.
      */
@@ -166,7 +163,7 @@ public class ComboBox extends AbstractSelect implements
      * @return the current input prompt, or null if not enabled
      */
     public String getInputPrompt() {
-        return inputPrompt;
+        return getState(false).inputPrompt;
     }
 
     /**
@@ -177,8 +174,7 @@ public class ComboBox extends AbstractSelect implements
      *            the desired input prompt, or null to disable
      */
     public void setInputPrompt(String inputPrompt) {
-        this.inputPrompt = inputPrompt;
-        markAsDirty();
+        getState().inputPrompt = inputPrompt;
     }
 
     private boolean isFilteringNeeded() {
@@ -190,15 +186,6 @@ public class ComboBox extends AbstractSelect implements
     public void paintContent(PaintTarget target) throws PaintException {
         isPainting = true;
         try {
-            if (inputPrompt != null) {
-                target.addAttribute(ComboBoxConstants.ATTR_INPUTPROMPT,
-                        inputPrompt);
-            }
-
-            if (!textInputAllowed) {
-                target.addAttribute(ComboBoxConstants.ATTR_NO_TEXT_INPUT, true);
-            }
-
             // clear caption change listeners
             getCaptionChangeListener().clear();
 
@@ -375,8 +362,7 @@ public class ComboBox extends AbstractSelect implements
      *            selection
      */
     public void setTextInputAllowed(boolean textInputAllowed) {
-        this.textInputAllowed = textInputAllowed;
-        markAsDirty();
+        getState().textInputAllowed = textInputAllowed;
     }
 
     /**
@@ -388,7 +374,7 @@ public class ComboBox extends AbstractSelect implements
      * @return
      */
     public boolean isTextInputAllowed() {
-        return textInputAllowed;
+        return getState(false).textInputAllowed;
     }
 
     @Override
@@ -396,6 +382,11 @@ public class ComboBox extends AbstractSelect implements
         return (ComboBoxState) super.getState();
     }
 
+    @Override
+    protected ComboBoxState getState(boolean markAsDirty) {
+        return (ComboBoxState) super.getState(markAsDirty);
+    }
+
     /**
      * Returns the filtered options for the current page using a container
      * filter.
index 46e24aa26627abfdca35d5b35769f20137165972..1856aac00d14f218d84a64a6bb0074f69711962d 100644 (file)
@@ -26,4 +26,22 @@ public class ComboBoxState extends AbstractSelectState {
     {
         primaryStyleName = "v-filterselect";
     }
+
+    /**
+     * If text input is not allowed, the ComboBox behaves like a pretty
+     * NativeSelect - the user can not enter any text and clicking the text
+     * field opens the drop down with options.
+     * 
+     * @since
+     */
+    public boolean textInputAllowed = true;
+
+    /**
+     * A textual prompt that is displayed when the select would otherwise be
+     * empty, to prompt the user for input.
+     * 
+     * @since
+     */
+    public String inputPrompt = null;
+
 }