]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added possibility to disable text input for PopupDateField (#6790) 72/272/2
authorMikael Grankvist <mgrankvi@vaadin.com>
Thu, 15 Nov 2012 09:21:14 +0000 (11:21 +0200)
committerArtur Signell <artur@vaadin.com>
Mon, 19 Nov 2012 18:30:19 +0000 (20:30 +0200)
Change-Id: I3275fce74c26b1409aa00505c06f459d1ae2e6ce

client/src/com/vaadin/client/ui/VPopupCalendar.java
client/src/com/vaadin/client/ui/datefield/PopupDateFieldConnector.java
server/src/com/vaadin/ui/PopupDateField.java
shared/src/com/vaadin/shared/ui/datefield/PopupDateFieldState.java
uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldTest.html [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldTest.java
uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldTextEnabled.java [new file with mode: 0644]

index 941978d1297c62fcbf35feebaabdb39248a891df..c5680e3662c310f6e49878e75c8442bd335c07c7 100644 (file)
@@ -66,6 +66,8 @@ public class VPopupCalendar extends VTextualDate implements Field,
 
     private boolean open = false;
 
+    private boolean textFieldEnabled = true;
+
     public VPopupCalendar() {
         super();
 
@@ -156,6 +158,34 @@ public class VPopupCalendar extends VTextualDate implements Field,
         }
     }
 
+    /**
+     * Checks whether the text field is enabled.
+     * 
+     * @see VPopupCalendar#setTextFieldEnabled(boolean)
+     * @return The current state of the text field.
+     */
+    public boolean isTextFieldEnabled() {
+        return textFieldEnabled;
+    }
+
+    /**
+     * Sets the state of the text field of this component. By default the text
+     * field is enabled. Disabling it causes only the button for date selection
+     * to be active, thus preventing the user from entering invalid dates. See
+     * {@link http://dev.vaadin.com/ticket/6790}.
+     * 
+     * @param state
+     */
+    public void setTextFieldEnabled(boolean textFieldEnabled) {
+        this.textFieldEnabled = textFieldEnabled;
+        text.setEnabled(textFieldEnabled);
+        if (textFieldEnabled) {
+            calendarToggle.setTabIndex(-1);
+        } else {
+            calendarToggle.setTabIndex(0);
+        }
+    }
+
     /*
      * (non-Javadoc)
      * 
@@ -335,6 +365,9 @@ public class VPopupCalendar extends VTextualDate implements Field,
         if (!parsable) {
             setText(previousValue);
         }
+
+        // superclass sets the text field independently when building date
+        text.setEnabled(isEnabled() && isTextFieldEnabled());
     }
 
     /**
index 3859a2204693bed05045eec0549b45749b76cbee..1ff1e58e72ba65cb1cdefe0ccf51526029875544 100644 (file)
@@ -21,6 +21,7 @@ import java.util.Date;
 import com.vaadin.client.ApplicationConnection;
 import com.vaadin.client.DateTimeService;
 import com.vaadin.client.UIDL;
+import com.vaadin.client.communication.StateChangeEvent;
 import com.vaadin.client.ui.VCalendarPanel.FocusChangeListener;
 import com.vaadin.client.ui.VCalendarPanel.TimeChangeListener;
 import com.vaadin.client.ui.VPopupCalendar;
@@ -131,6 +132,12 @@ public class PopupDateFieldConnector extends TextualDateConnector {
         return (PopupDateFieldState) super.getState();
     }
 
+    @Override
+    public void onStateChanged(StateChangeEvent stateChangeEvent) {
+        super.onStateChanged(stateChangeEvent);
+        getWidget().setTextFieldEnabled(getState().textFieldEnabled);
+    }
+
     @Override
     protected void setWidgetStyleName(String styleName, boolean add) {
         super.setWidgetStyleName(styleName, add);
index 39e0578301f6b0ae622ffae7f4c96215d75d061a..8d26fa8e25b159bfeabacd6b9a43eb45875b2fa6 100644 (file)
@@ -21,6 +21,7 @@ import java.util.Date;
 import com.vaadin.data.Property;
 import com.vaadin.server.PaintException;
 import com.vaadin.server.PaintTarget;
+import com.vaadin.shared.ui.datefield.PopupDateFieldState;
 
 /**
  * <p>
@@ -87,4 +88,34 @@ public class PopupDateField extends DateField {
         markAsDirty();
     }
 
+    @Override
+    protected PopupDateFieldState getState() {
+        return (PopupDateFieldState) super.getState();
+    }
+
+    /**
+     * Checks whether the text field is enabled (default) or not.
+     * 
+     * @see PopupDateField#setTextFieldEnabled(boolean);
+     * 
+     * @return <b>true</b> if the text field is enabled, <b>false</b> otherwise.
+     */
+    public boolean isTextFieldEnabled() {
+        return getState().textFieldEnabled;
+    }
+
+    /**
+     * Enables or disables the text field. By default the text field is enabled.
+     * Disabling it causes only the button for date selection to be active, thus
+     * preventing the user from entering invalid dates.
+     * 
+     * See {@link http://dev.vaadin.com/ticket/6790}.
+     * 
+     * @param state
+     *            <b>true</b> to enable text field, <b>false</b> to disable it.
+     */
+    public void setTextFieldEnabled(boolean state) {
+        getState().textFieldEnabled = state;
+    }
+
 }
index c0d3b20c28b11a91c3cb5a0b0005c04ed4fd83b0..deed182d8e47cf9f819bff210a9e63c20f92a483 100644 (file)
@@ -4,4 +4,7 @@ public class PopupDateFieldState extends TextualDateFieldState {
     {
         primaryStyleName = "v-datefield";
     }
+
+    public boolean textFieldEnabled = true;
+
 }
diff --git a/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldTest.html b/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldTest.html
new file mode 100644 (file)
index 0000000..eb9859b
--- /dev/null
@@ -0,0 +1,102 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head profile="http://selenium-ide.openqa.org/profiles/test-case">
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<link rel="selenium.base" href="http://localhost:8888/" />
+<title>PopupDateFieldTest</title>
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">PopupDateFieldTest</td></tr>
+</thead><tbody>
+<tr>
+       <td>open</td>
+       <td>/run/com.vaadin.tests.components.datefield.PopupDateFieldTest?restartApplication</td>
+       <td></td>
+</tr>
+<tr>
+       <td>assertEditable</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::/VVerticalLayout[0]/VVerticalLayout[0]/VPopupCalendar[0]#field</td>
+       <td></td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::/VVerticalLayout[0]/VVerticalLayout[0]/VMenuBar[0]#item0</td>
+       <td>29,9</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::Root/VOverlay[0]/VMenuBar[0]#item4</td>
+       <td>26,9</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::Root/VOverlay[1]/VMenuBar[0]#item6</td>
+       <td>84,9</td>
+</tr>
+<tr>
+       <td>assertNotEditable</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::/VVerticalLayout[0]/VVerticalLayout[0]/VPopupCalendar[0]#field</td>
+       <td></td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::/VVerticalLayout[0]/VVerticalLayout[0]/VMenuBar[0]#item0</td>
+       <td>31,6</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::Root/VOverlay[0]/VMenuBar[0]#item0</td>
+       <td>29,7</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::Root/VOverlay[1]/VMenuBar[0]#item1</td>
+       <td>42,7</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::/VVerticalLayout[0]/VVerticalLayout[0]/VMenuBar[0]#item0</td>
+       <td>45,7</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::Root/VOverlay[0]/VMenuBar[0]#item0</td>
+       <td>43,7</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::Root/VOverlay[1]/VMenuBar[0]#item1</td>
+       <td>39,6</td>
+</tr>
+<tr>
+       <td>assertNotEditable</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::/VVerticalLayout[0]/VVerticalLayout[0]/VPopupCalendar[0]#field</td>
+       <td></td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::/VVerticalLayout[0]/VVerticalLayout[0]/VMenuBar[0]#item0</td>
+       <td>36,7</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::Root/VOverlay[0]/VMenuBar[0]#item4</td>
+       <td>34,6</td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::Root/VOverlay[1]/VMenuBar[0]#item6</td>
+       <td>71,7</td>
+</tr>
+<tr>
+       <td>assertEditable</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldPopupDateFieldTest::/VVerticalLayout[0]/VVerticalLayout[0]/VPopupCalendar[0]#field</td>
+       <td></td>
+</tr>
+
+</tbody></table>
+</body>
+</html>
index 3e8b0678a798d71210613e5b1bf5f05ffd83cfc4..6e3250a8254b2e87991b21ba3f39f839403a39b5 100644 (file)
@@ -16,6 +16,7 @@ public class PopupDateFieldTest extends DateFieldTest<PopupDateField> {
         super.createActions();
 
         createInputPromptSelectAction(CATEGORY_FEATURES);
+        createTextEnabledAction(CATEGORY_FEATURES);
     }
 
     private void createInputPromptSelectAction(String category) {
@@ -36,4 +37,16 @@ public class PopupDateFieldTest extends DateFieldTest<PopupDateField> {
                 });
     }
 
+    private void createTextEnabledAction(String category) {
+        this.createBooleanAction("Text field enabled", category, true,
+                new Command<PopupDateField, Boolean>() {
+
+                    @Override
+                    public void execute(PopupDateField c, Boolean value,
+                            Object data) {
+                        c.setTextFieldEnabled(value);
+                    }
+
+                });
+    }
 }
diff --git a/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldTextEnabled.java b/uitest/src/com/vaadin/tests/components/datefield/PopupDateFieldTextEnabled.java
new file mode 100644 (file)
index 0000000..731869f
--- /dev/null
@@ -0,0 +1,43 @@
+package com.vaadin.tests.components.datefield;
+
+import com.vaadin.data.Property;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.CheckBox;
+import com.vaadin.ui.PopupDateField;
+
+public class PopupDateFieldTextEnabled extends TestBase {
+
+    private static final String ENABLED = "DateField text box enabled";
+    private static final String DISABLED = "DateField text box disabled";
+    @Override
+    public void setup() {
+        final PopupDateField field = new PopupDateField();
+        final CheckBox box = new CheckBox(ENABLED, true);
+        box.addListener(new Property.ValueChangeListener() {
+
+            @Override
+            public void valueChange(Property.ValueChangeEvent event) {
+                field.setTextFieldEnabled((Boolean) event.getProperty()
+                        .getValue());
+                if(field.isTextFieldEnabled()){
+                    box.setCaption(ENABLED);
+                }else{
+                    box.setCaption(DISABLED);
+                }
+            }
+        });
+        addComponent(box);
+        addComponent(field);
+    }
+
+    @Override
+    protected String getDescription() {
+        return "tests whether or not enabling text field in the component works";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 6790;
+    }
+
+}
\ No newline at end of file