]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use a custom field factory for editor row (#16513)
authorArtur Signell <artur@vaadin.com>
Tue, 27 Jan 2015 16:16:03 +0000 (18:16 +0200)
committerVaadin Code Review <review@vaadin.com>
Tue, 3 Feb 2015 12:03:46 +0000 (12:03 +0000)
Change-Id: I0102d93c7f661993a5a07d2bcdf511f433419300

server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java
server/src/com/vaadin/ui/Grid.java
uitest/src/com/vaadin/tests/fieldgroup/BasicCrudGridEditorRowTest.java [new file with mode: 0644]

index 8e32585a47c26e0cb99bb7233f39df5e2fced028..b6bf97e68e1b676e69fafd468572bbe0fcfa0402 100644 (file)
@@ -129,10 +129,6 @@ public class DefaultFieldGroupFieldFactory implements FieldGroupFieldFactory {
         return (T) field;
     }
 
-    private boolean anyField(Class<?> fieldType) {
-        return fieldType == Field.class || fieldType == AbstractField.class;
-    }
-
     protected AbstractSelect createCompatibleSelect(
             Class<? extends AbstractSelect> fieldType) {
         AbstractSelect select;
@@ -157,7 +153,23 @@ public class DefaultFieldGroupFieldFactory implements FieldGroupFieldFactory {
         return select;
     }
 
-    private boolean anySelect(Class<? extends Field> fieldType) {
+    /**
+     * @since 7.4
+     * @param fieldType
+     *            the type of the field
+     * @return true if any AbstractField can be assigned to the field
+     */
+    protected boolean anyField(Class<?> fieldType) {
+        return fieldType == Field.class || fieldType == AbstractField.class;
+    }
+
+    /**
+     * @since 7.4
+     * @param fieldType
+     *            the type of the field
+     * @return true if any AbstractSelect can be assigned to the field
+     */
+    protected boolean anySelect(Class<? extends Field> fieldType) {
         return anyField(fieldType) || fieldType == AbstractSelect.class;
     }
 
index adb08e0fcc42972067dff697d952d5e35c9066ab..3c0afbc484100ebde4a571ea7e46c24e507be4d5 100644 (file)
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -47,7 +48,9 @@ import com.vaadin.data.Item;
 import com.vaadin.data.Property;
 import com.vaadin.data.RpcDataProviderExtension;
 import com.vaadin.data.RpcDataProviderExtension.DataProviderKeyMapper;
+import com.vaadin.data.fieldgroup.DefaultFieldGroupFieldFactory;
 import com.vaadin.data.fieldgroup.FieldGroup;
+import com.vaadin.data.fieldgroup.FieldGroup.BindException;
 import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
 import com.vaadin.data.fieldgroup.FieldGroupFieldFactory;
 import com.vaadin.data.sort.Sort;
@@ -166,6 +169,11 @@ public class Grid extends AbstractComponent implements SelectionNotifier,
      * been bound.
      */
     private final class CustomFieldGroup extends FieldGroup {
+
+        public CustomFieldGroup() {
+            setFieldFactory(EditorFieldFactory.get());
+        }
+
         @Override
         protected Class<?> getPropertyType(Object propertyId)
                 throws BindException {
@@ -177,6 +185,59 @@ public class Grid extends AbstractComponent implements SelectionNotifier,
         }
     }
 
+    /**
+     * Field factory used by default in the editor.
+     * 
+     * Aims to fields of suitable type and with suitable size for use in the
+     * editor row.
+     */
+    public static class EditorFieldFactory extends
+            DefaultFieldGroupFieldFactory {
+        private static final EditorFieldFactory INSTANCE = new EditorFieldFactory();
+
+        protected EditorFieldFactory() {
+        }
+
+        /**
+         * Returns the singleton instance
+         * 
+         * @return the singleton instance
+         */
+        public static EditorFieldFactory get() {
+            return INSTANCE;
+        }
+
+        @Override
+        public <T extends Field> T createField(Class<?> type, Class<T> fieldType) {
+            T f = super.createField(type, fieldType);
+            if (f != null) {
+                f.setWidth("100%");
+            }
+            return f;
+        }
+
+        @Override
+        protected AbstractSelect createCompatibleSelect(
+                Class<? extends AbstractSelect> fieldType) {
+            if (anySelect(fieldType)) {
+                return super.createCompatibleSelect(ComboBox.class);
+            }
+            return super.createCompatibleSelect(fieldType);
+        }
+
+        @Override
+        protected void populateWithEnumData(AbstractSelect select,
+                Class<? extends Enum> enumClass) {
+            // Use enums directly and the EnumToStringConverter to be consistent
+            // with what is shown in the Grid
+            @SuppressWarnings("unchecked")
+            EnumSet<?> enumSet = EnumSet.allOf(enumClass);
+            for (Object r : enumSet) {
+                select.addItem(r);
+            }
+        }
+    }
+
     /**
      * Selection modes representing built-in {@link SelectionModel
      * SelectionModels} that come bundled with {@link Grid}.
diff --git a/uitest/src/com/vaadin/tests/fieldgroup/BasicCrudGridEditorRowTest.java b/uitest/src/com/vaadin/tests/fieldgroup/BasicCrudGridEditorRowTest.java
new file mode 100644 (file)
index 0000000..3ccd547
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.fieldgroup;
+
+import org.junit.Test;
+import org.openqa.selenium.interactions.Actions;
+
+import com.vaadin.testbench.elements.GridElement;
+import com.vaadin.testbench.elements.GridElement.GridCellElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class BasicCrudGridEditorRowTest extends MultiBrowserTest {
+
+    @Test
+    public void lookAndFeel() throws Exception {
+        openTestURL();
+        GridElement grid = $(GridElement.class).first();
+        GridCellElement ritaBirthdate = grid.getCell(2, 3);
+        compareScreen("grid");
+
+        // Open editor row
+        new Actions(getDriver()).doubleClick(ritaBirthdate).perform();
+        compareScreen("editorrow");
+
+    }
+}