OSDN Git Service

Only set the text attribute on views that define it
authorTor Norbye <tnorbye@google.com>
Mon, 11 Jul 2011 19:49:40 +0000 (12:49 -0700)
committerTor Norbye <tnorbye@google.com>
Mon, 11 Jul 2011 19:49:40 +0000 (12:49 -0700)
Ensure that we only set the text attribute on widgets that define
it. Now that we preserve unknown attributes this meant that we'd end
up setting text attributes on all widgets rather than just those that
define it.

Change-Id: Ief67a777ca40149e0be370b7a561f80fbffa3d5d

eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/descriptors/DescriptorsUtils.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/descriptors/ElementDescriptor.java

index 62cd172..8950c1a 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.android.ide.eclipse.adt.internal.editors.descriptors;
 
+import static com.android.ide.common.layout.LayoutConstants.ANDROID_URI;
 import static com.android.ide.common.layout.LayoutConstants.ATTR_ID;
 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_BELOW;
 import static com.android.ide.common.layout.LayoutConstants.ATTR_LAYOUT_HEIGHT;
@@ -47,8 +48,8 @@ import org.eclipse.swt.graphics.Image;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
 import java.util.Map.Entry;
+import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -710,8 +711,11 @@ public final class DescriptorsUtils {
                     false /* override */);
         }
 
-        // Don't set default text value into edit texts - they typically start out blank
-        if (!descriptor.getXmlLocalName().equals(EDIT_TEXT)) {
+        // Set a text attribute on textual widgets -- but only on those that define a text
+        // attribute
+        if (descriptor.definesAttribute(ANDROID_URI, ATTR_TEXT)
+                // Don't set default text value into edit texts - they typically start out blank
+                && !descriptor.getXmlLocalName().equals(EDIT_TEXT)) {
             String type = getBasename(descriptor.getUiName());
             node.setAttributeValue(
                 ATTR_TEXT,
index e0f6959..ce3d59a 100644 (file)
@@ -446,6 +446,24 @@ public class ElementDescriptor implements Comparable<ElementDescriptor> {
         return new String(c).replace("-", " ");  //$NON-NLS-1$  //$NON-NLS-2$
     }
 
+    /**
+     * Returns true if this node defines the given attribute
+     *
+     * @param namespaceUri the namespace URI of the target attribute
+     * @param attributeName the attribute name
+     * @return true if this element defines an attribute of the given name and namespace
+     */
+    public boolean definesAttribute(String namespaceUri, String attributeName) {
+        for (AttributeDescriptor desc : mAttributes) {
+            if (desc.getXmlLocalName().equals(attributeName) &&
+                    desc.getNamespaceUri().equals(namespaceUri)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     // Implements Comparable<ElementDescriptor>:
     public int compareTo(ElementDescriptor o) {
         return mUiName.compareToIgnoreCase(o.mUiName);