OSDN Git Service

Update ViewInfo in the layoutlib API.
authorXavier Ducrohet <xav@android.com>
Fri, 12 Nov 2010 02:17:37 +0000 (18:17 -0800)
committerXavier Ducrohet <xav@android.com>
Fri, 12 Nov 2010 04:37:13 +0000 (20:37 -0800)
- support for View and LayoutParams
- support for default property value map.

Change-Id: I70028710b1f76329a8bd501428fbd68a14fafa1e

eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CanvasSelection.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CanvasViewInfo.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ClipboardSupport.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutCanvas.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/OutlinePage2.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/SelectionManager.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/ViewHierarchy.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gre/NodeFactory.java
layoutlib_api/src/com/android/layoutlib/api/LayoutScene.java
layoutlib_api/src/com/android/layoutlib/api/ViewInfo.java

index cd927ec..26e960e 100755 (executable)
@@ -155,7 +155,7 @@ import java.util.List;
             return "";
         }
 
-        String name = gre.callGetDisplayName(canvasViewInfo.getUiViewKey());
+        String name = gre.callGetDisplayName(canvasViewInfo.getUiViewNode());
 
         if (name == null) {
             // The name is typically a fully-qualified class name. Let's make it a tad shorter.
@@ -192,7 +192,7 @@ import java.util.List;
         LayoutEditor layoutEditor = canvas.getLayoutEditor();
         for (CanvasSelection cs : selection) {
             CanvasViewInfo vi = cs.getViewInfo();
-            UiViewElementNode key = vi.getUiViewKey();
+            UiViewElementNode key = vi.getUiViewNode();
             Node node = key.getXmlNode();
             String t = layoutEditor.getXmlText(node);
             if (t != null) {
index bff1606..2da2467 100755 (executable)
@@ -59,7 +59,7 @@ public class CanvasViewInfo implements IPropertySource {
     private final Rectangle mAbsRect;
     private final Rectangle mSelectionRect;
     private final String mName;
-    private final UiViewElementNode mUiViewKey;
+    private final UiViewElementNode mUiViewNode;
     private final CanvasViewInfo mParent;
     private final ArrayList<CanvasViewInfo> mChildren = new ArrayList<CanvasViewInfo>();
 
@@ -85,13 +85,13 @@ public class CanvasViewInfo implements IPropertySource {
         mParent = parent;
         mName = viewInfo.getClassName();
 
-        // The ViewInfo#getViewKey() method returns a key which depends on the
-        // IXmlPullParser used to parse the layout files. In this case, the parser is
-        // guaranteed to be an UiElementPullParser, which creates keys that are of type
-        // UiViewElementNode.
+        // The ViewInfo#getViewKey() method returns a cookie uniquely identifying the object
+        // they represent on this side of the API.
+        // In this case, the parser is guaranteed to be an UiElementPullParser, which creates
+        // cookies that are of type UiViewElementNode.
         // We'll simply crash if the type is not right, as this is not supposed to happen
         // and nothing could work if there's a type mismatch.
-        mUiViewKey  = (UiViewElementNode) viewInfo.getViewKey();
+        mUiViewNode  = (UiViewElementNode) viewInfo.getCookie();
 
         int x = viewInfo.getLeft();
         int y = viewInfo.getTop();
@@ -110,7 +110,7 @@ public class CanvasViewInfo implements IPropertySource {
                 // Only use children which have a ViewKey of the correct type.
                 // We can't interact with those when they have a null key or
                 // an incompatible type.
-                if (child.getViewKey() instanceof UiViewElementNode) {
+                if (child.getCookie() instanceof UiViewElementNode) {
                     mChildren.add(new CanvasViewInfo(child, this, x, y));
                 }
             }
@@ -153,12 +153,12 @@ public class CanvasViewInfo implements IPropertySource {
     }
 
     /**
-     * Returns the view key. Could be null, although unlikely.
+     * Returns the view node. Could be null, although unlikely.
      * @return An {@link UiViewElementNode} that uniquely identifies the object in the XML model.
-     * @see ViewInfo#getViewKey()
+     * @see ViewInfo#getCookie()
      */
-    public UiViewElementNode getUiViewKey() {
-        return mUiViewKey;
+    public UiViewElementNode getUiViewNode() {
+        return mUiViewNode;
     }
 
     /**
@@ -211,7 +211,7 @@ public class CanvasViewInfo implements IPropertySource {
     // ---- Implementation of IPropertySource
 
     public Object getEditableValue() {
-        UiViewElementNode uiView = getUiViewKey();
+        UiViewElementNode uiView = getUiViewNode();
         if (uiView != null) {
             return ((IPropertySource) uiView).getEditableValue();
         }
@@ -219,7 +219,7 @@ public class CanvasViewInfo implements IPropertySource {
     }
 
     public IPropertyDescriptor[] getPropertyDescriptors() {
-        UiViewElementNode uiView = getUiViewKey();
+        UiViewElementNode uiView = getUiViewNode();
         if (uiView != null) {
             return ((IPropertySource) uiView).getPropertyDescriptors();
         }
@@ -227,7 +227,7 @@ public class CanvasViewInfo implements IPropertySource {
     }
 
     public Object getPropertyValue(Object id) {
-        UiViewElementNode uiView = getUiViewKey();
+        UiViewElementNode uiView = getUiViewNode();
         if (uiView != null) {
             return ((IPropertySource) uiView).getPropertyValue(id);
         }
@@ -235,7 +235,7 @@ public class CanvasViewInfo implements IPropertySource {
     }
 
     public boolean isPropertySet(Object id) {
-        UiViewElementNode uiView = getUiViewKey();
+        UiViewElementNode uiView = getUiViewNode();
         if (uiView != null) {
             return ((IPropertySource) uiView).isPropertySet(id);
         }
@@ -243,14 +243,14 @@ public class CanvasViewInfo implements IPropertySource {
     }
 
     public void resetPropertyValue(Object id) {
-        UiViewElementNode uiView = getUiViewKey();
+        UiViewElementNode uiView = getUiViewNode();
         if (uiView != null) {
             ((IPropertySource) uiView).resetPropertyValue(id);
         }
     }
 
     public void setPropertyValue(Object id, Object value) {
-        UiViewElementNode uiView = getUiViewKey();
+        UiViewElementNode uiView = getUiViewNode();
         if (uiView != null) {
             ((IPropertySource) uiView).setPropertyValue(id, value);
         }
@@ -263,7 +263,7 @@ public class CanvasViewInfo implements IPropertySource {
      * @return The XML node corresponding to this info object, or null
      */
     public Node getXmlNode() {
-        UiViewElementNode uiView = getUiViewKey();
+        UiViewElementNode uiView = getUiViewNode();
         if (uiView != null) {
             return uiView.getXmlNode();
         }
@@ -281,8 +281,8 @@ public class CanvasViewInfo implements IPropertySource {
         // The root element is the one whose GRAND parent
         // is null (because the parent will be a -document-
         // node).
-        return mUiViewKey == null || mUiViewKey.getUiParent() == null ||
-            mUiViewKey.getUiParent().getUiParent() == null;
+        return mUiViewNode == null || mUiViewNode.getUiParent() == null ||
+            mUiViewNode.getUiParent().getUiParent() == null;
     }
 
     /**
@@ -295,7 +295,7 @@ public class CanvasViewInfo implements IPropertySource {
      */
     public boolean isInvisibleParent() {
         if (mAbsRect.width < SELECTION_MIN_SIZE || mAbsRect.height < SELECTION_MIN_SIZE) {
-            return mUiViewKey != null && mUiViewKey.getDescriptor().hasChildren();
+            return mUiViewNode != null && mUiViewNode.getDescriptor().hasChildren();
         }
 
         return false;
@@ -331,7 +331,7 @@ public class CanvasViewInfo implements IPropertySource {
      */
     /* package */ SimpleElement toSimpleElement() {
 
-        UiViewElementNode uiNode = getUiViewKey();
+        UiViewElementNode uiNode = getUiViewNode();
 
         String fqcn = SimpleXmlTransfer.getFqcn(uiNode.getDescriptor());
         String parentFqcn = null;
index 00b2518..680c12e 100644 (file)
@@ -184,7 +184,7 @@ public class ClipboardSupport {
                     CanvasViewInfo vi = cs.getViewInfo();
                     // You can't delete the root element
                     if (vi != null && !vi.isRoot()) {
-                        UiViewElementNode ui = vi.getUiViewKey();
+                        UiViewElementNode ui = vi.getUiViewNode();
                         if (ui != null) {
                             ui.deleteXmlNode();
                         }
index e313f00..0374fce 100755 (executable)
@@ -620,7 +620,7 @@ class LayoutCanvas extends Canvas {
                     // O(n^2) here, but both the selection size and especially the
                     // invisibleParents size are expected to be small
                     if (invisibleParents.contains(viewInfo)) {
-                        UiViewElementNode node = viewInfo.getUiViewKey();
+                        UiViewElementNode node = viewInfo.getUiViewNode();
                         if (node != null) {
                             if (result == null) {
                                 result = new HashSet<UiElementNode>();
index 0a267d6..198e507 100755 (executable)
@@ -129,7 +129,7 @@ public class OutlinePage2 extends ContentOutlinePage
         tv.setComparer(new IElementComparer() {
             public int hashCode(Object element) {
                 if (element instanceof CanvasViewInfo) {
-                    UiViewElementNode key = ((CanvasViewInfo) element).getUiViewKey();
+                    UiViewElementNode key = ((CanvasViewInfo) element).getUiViewNode();
                     if (key != null) {
                         return key.hashCode();
                     }
@@ -142,8 +142,8 @@ public class OutlinePage2 extends ContentOutlinePage
 
             public boolean equals(Object a, Object b) {
                 if (a instanceof CanvasViewInfo && b instanceof CanvasViewInfo) {
-                    UiViewElementNode keyA = ((CanvasViewInfo) a).getUiViewKey();
-                    UiViewElementNode keyB = ((CanvasViewInfo) b).getUiViewKey();
+                    UiViewElementNode keyA = ((CanvasViewInfo) a).getUiViewNode();
+                    UiViewElementNode keyB = ((CanvasViewInfo) b).getUiViewNode();
                     if (keyA != null) {
                         return keyA.equals(keyB);
                     }
@@ -350,7 +350,7 @@ public class OutlinePage2 extends ContentOutlinePage
          */
         public Image getImage(Object element) {
             if (element instanceof CanvasViewInfo) {
-                element = ((CanvasViewInfo) element).getUiViewKey();
+                element = ((CanvasViewInfo) element).getUiViewNode();
             }
 
             if (element instanceof UiElementNode) {
@@ -376,7 +376,7 @@ public class OutlinePage2 extends ContentOutlinePage
          */
         public String getText(Object element) {
             if (element instanceof CanvasViewInfo) {
-                element = ((CanvasViewInfo) element).getUiViewKey();
+                element = ((CanvasViewInfo) element).getUiViewNode();
             }
 
             if (element instanceof UiElementNode) {
index 3db65f1..3b85a3a 100644 (file)
@@ -555,7 +555,7 @@ public class SelectionManager implements ISelectionProvider {
 
             // Check if the selected object still exists
             ViewHierarchy viewHierarchy = mCanvas.getViewHierarchy();
-            Object key = s.getViewInfo().getUiViewKey();
+            Object key = s.getViewInfo().getUiViewNode();
             CanvasViewInfo vi = viewHierarchy.findViewInfoKey(key, lastValidViewInfoRoot);
 
             // Remove the previous selection -- if the selected object still exists
@@ -625,7 +625,7 @@ public class SelectionManager implements ISelectionProvider {
         for (Iterator<CanvasSelection> it = selection.iterator(); it.hasNext(); ) {
             CanvasSelection cs = it.next();
             CanvasViewInfo vi = cs.getViewInfo();
-            UiViewElementNode key = vi == null ? null : vi.getUiViewKey();
+            UiViewElementNode key = vi == null ? null : vi.getUiViewNode();
             Node node = key == null ? null : key.getXmlNode();
             if (node == null) {
                 // Missing ViewInfo or view key or XML, discard this.
index e7c718f..c737fc4 100644 (file)
@@ -153,7 +153,7 @@ public class ViewHierarchy {
             return;
         }
 
-        UiViewElementNode key = vi.getUiViewKey();
+        UiViewElementNode key = vi.getUiViewNode();
 
         if (key != null) {
             mCanvas.getNodeFactory().create(vi);
@@ -187,7 +187,7 @@ public class ViewHierarchy {
         if (vi.isInvisibleParent()) {
             mInvisibleParents.add(vi);
         } else if (invisibleNodes != null) {
-            UiViewElementNode key = vi.getUiViewKey();
+            UiViewElementNode key = vi.getUiViewNode();
 
             if (key != null && invisibleNodes.contains(key)) {
                 vi.setExploded(true);
@@ -461,7 +461,7 @@ public class ViewHierarchy {
         if (canvasViewInfo == null) {
             return null;
         }
-        if (canvasViewInfo.getUiViewKey() == viewKey) {
+        if (canvasViewInfo.getUiViewNode() == viewKey) {
             return canvasViewInfo;
         }
 
@@ -545,7 +545,7 @@ public class ViewHierarchy {
 
         Set<UiElementNode> nodes = new HashSet<UiElementNode>(mInvisibleParents.size());
         for (CanvasViewInfo info : mInvisibleParents) {
-            UiViewElementNode node = info.getUiViewKey();
+            UiViewElementNode node = info.getUiViewNode();
             if (node != null) {
                 nodes.add(node);
             }
index f4ebc23..825a0e4 100755 (executable)
@@ -41,7 +41,7 @@ public class NodeFactory {
      * {@link CanvasViewInfo}. The bounds of the node are set to the canvas view bounds.
      */
     public NodeProxy create(CanvasViewInfo canvasViewInfo) {
-        return create(canvasViewInfo.getUiViewKey(), canvasViewInfo.getAbsRect());
+        return create(canvasViewInfo.getUiViewNode(), canvasViewInfo.getAbsRect());
     }
 
     /**
index 8e0fe73..85c7365 100644 (file)
@@ -19,6 +19,7 @@ package com.android.layoutlib.api;
 import com.android.layoutlib.api.SceneResult.LayoutStatus;
 
 import java.awt.image.BufferedImage;
+import java.util.Map;
 
 /**
  * An object allowing interaction with an Android layout.
@@ -81,6 +82,17 @@ public class LayoutScene {
         return null;
     }
 
+
+    /**
+     * Returns a map of (XML attribute name, attribute value) containing only default attribute
+     * values, for the given view Object.
+     * @param viewObject the view object.
+     * @return a map of the default property values or null.
+     */
+    public Map<String, String> getDefaultViewPropertyValues(Object viewObject) {
+        return null;
+    }
+
     /**
      * Re-renders the layout as-is.
      * In case of success, this should be followed by calls to {@link #getRootView()} and
index d991cc8..57be46f 100644 (file)
@@ -18,35 +18,48 @@ package com.android.layoutlib.api;
 
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
 
 /**
  * Layout information for a specific view object
  */
-public class ViewInfo {
+public final class ViewInfo {
 
-    protected final Object mKey;
-    protected final String mName;
-    protected final int mLeft;
-    protected final int mRight;
-    protected final int mTop;
-    protected final int mBottom;
-    protected List<ViewInfo> mChildren = Collections.emptyList();
+    private final Object mCookie;
+    private final String mName;
+    private final int mLeft;
+    private final int mRight;
+    private final int mTop;
+    private final int mBottom;
+    private List<ViewInfo> mChildren = Collections.emptyList();
+    private final Object mViewObject;
+    private final Object mLayoutParamsObject;
 
-    public ViewInfo(String name, Object key, int left, int top, int right, int bottom) {
+    public ViewInfo(String name, Object cookie, int left, int top, int right, int bottom) {
+        this(name, cookie, left, top, right, bottom, null /*viewObject*/,
+                null /*layoutParamsObject*/);
+    }
+
+    public ViewInfo(String name, Object cookie, int left, int top, int right, int bottom,
+            Object viewObject, Object layoutParamsObject) {
         mName = name;
-        mKey = key;
+        mCookie = cookie;
         mLeft = left;
         mRight = right;
         mTop = top;
         mBottom = bottom;
+        mViewObject = viewObject;
+        mLayoutParamsObject = layoutParamsObject;
     }
 
     /**
      * Sets the list of children {@link ViewInfo}.
      */
     public void setChildren(List<ViewInfo> children) {
-        mChildren = Collections.unmodifiableList(children);
+        if (children != null) {
+            mChildren = Collections.unmodifiableList(children);
+        } else {
+            mChildren = Collections.emptyList();
+        }
     }
 
     /**
@@ -57,12 +70,12 @@ public class ViewInfo {
     }
 
     /**
-     * Returns the key associated with the node. Can be null.
+     * Returns the cookie associated with the XML node. Can be null.
      *
      * @see IXmlPullParser#getViewKey()
      */
-    public Object getViewKey() {
-        return mKey;
+    public Object getCookie() {
+        return mCookie;
     }
 
     /**
@@ -101,11 +114,12 @@ public class ViewInfo {
     }
 
     /**
-     * Returns a map of default values for some properties. The map key is the property name,
-     * as found in the XML.
+     * Returns the actual android.view.View (or child class) object. This can be used
+     * to query the object properties that are not in the XML and not in the map returned
+     * by {@link #getDefaultPropertyValues()}.
      */
-    public Map<String, String> getDefaultPropertyValues() {
-        return null;
+    public Object getViewObject() {
+        return mViewObject;
     }
 
     /**
@@ -113,7 +127,7 @@ public class ViewInfo {
      * to query the object properties that are not in the XML and not in the map returned
      * by {@link #getDefaultPropertyValues()}.
      */
-    public Object getViewObject() {
-        return null;
+    public Object getLayoutParamsObject() {
+        return mLayoutParamsObject;
     }
 }