]> source.dussan.org Git - vaadin-framework.git/commitdiff
Allow setting local id via DesignContext (#16584).
authorMika Murtojarvi <mika@vaadin.com>
Wed, 8 Apr 2015 08:05:23 +0000 (11:05 +0300)
committerMika Murtojarvi <mika@vaadin.com>
Wed, 8 Apr 2015 08:36:37 +0000 (11:36 +0300)
Change-Id: Ie599e8517823ca431d2d9e9232217db53444d7aa

server/src/com/vaadin/ui/declarative/DesignContext.java
server/tests/src/com/vaadin/tests/design/DesignContextLocalIdTest.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/design/local-ids.html [new file with mode: 0644]

index 54be5dcea3b600d6645b90dbb496bc50b569e282..fe3abcfb778802c4c343988b068f39d759d1cd9a 100644 (file)
@@ -170,16 +170,16 @@ public class DesignContext implements Serializable {
      * component, the mapping from c to the string is removed. Similarly, if
      * component was mapped to some string s different from localId, the mapping
      * from s to component is removed.
-     * 
-     * @param localId
-     *            The new local id of the component.
      * @param component
      *            The component whose local id is to be set.
+     * @param localId
+     *            The new local id of the component.
+     * 
      * @return true, if there already was a local id mapping from the string to
      *         some component or from the component to some string. Otherwise
      *         returns false.
      */
-    private boolean mapLocalId(String localId, Component component) {
+    public boolean setComponentLocalId(Component component, String localId) {
         return twoWayMap(localId, component, localIdToComponent,
                 componentToLocalId);
     }
@@ -466,7 +466,7 @@ public class DesignContext implements Serializable {
         // from the attributes of componentDesign
         if (attributes.hasKey(LOCAL_ID_ATTRIBUTE)) {
             String localId = attributes.get(LOCAL_ID_ATTRIBUTE);
-            boolean mappingExists = mapLocalId(localId, component);
+            boolean mappingExists = setComponentLocalId(component, localId);
             if (mappingExists) {
                 throw new DesignException(
                         "the following local id is not unique: " + localId);
diff --git a/server/tests/src/com/vaadin/tests/design/DesignContextLocalIdTest.java b/server/tests/src/com/vaadin/tests/design/DesignContextLocalIdTest.java
new file mode 100644 (file)
index 0000000..c3d7e6d
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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.design;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+
+import org.junit.Test;
+
+import com.vaadin.ui.Button;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.declarative.Design;
+import com.vaadin.ui.declarative.DesignContext;
+
+/**
+ * Tests that setting local id via DesignContext works as intended.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public class DesignContextLocalIdTest {
+
+    @Test
+    public void testSetLocalId() throws FileNotFoundException {
+        DesignContext ctx = Design.read(new FileInputStream(
+                "server/tests/src/com/vaadin/tests/design/local-ids.html"),
+                new VerticalLayout());
+        TextField tf = (TextField) ctx.getComponentByLocalId("foo");
+        Button b = (Button) ctx.getComponentByLocalId("bar");
+        // A duplicate id should be handled by removing the id from the old
+        // component.
+        ctx.setComponentLocalId(b, "foo");
+        assertEquals("Found the wrong component by local id.", ctx
+                .getComponentByLocalId("foo").getClass(), Button.class);
+        assertEquals("Found the wrong component by local id.",
+                ctx.getComponentByLocalId("bar"), null);
+        // Set an id also for the text field.
+        ctx.setComponentLocalId(tf, "bar");
+        assertEquals("Found the wrong component by local id.", ctx
+                .getComponentByLocalId("foo").getClass(), Button.class);
+        assertEquals("Found the wrong component by local id.", ctx
+                .getComponentByLocalId("bar").getClass(), TextField.class);
+    }
+}
diff --git a/server/tests/src/com/vaadin/tests/design/local-ids.html b/server/tests/src/com/vaadin/tests/design/local-ids.html
new file mode 100644 (file)
index 0000000..638d004
--- /dev/null
@@ -0,0 +1,4 @@
+<v-vertical-layout>
+    <v-text-field caption="Enter your name" _id="foo"/>
+    <v-button _id="bar">Say hello</v-button>
+</v-vertical-layout>