1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.ide.common.layout; 17 18 import static com.android.ide.common.layout.LayoutConstants.ANDROID_URI; 19 import static com.android.ide.common.layout.LayoutConstants.ATTR_ID; 20 21 import com.android.ide.common.api.IDragElement; 22 import com.android.ide.common.api.Rect; 23 24 import java.util.ArrayList; 25 import java.util.HashMap; 26 import java.util.List; 27 import java.util.Map; 28 29 /** Test/mock implementation of {@link IDragElement} */ 30 public class TestDragElement implements IDragElement { 31 private Rect mRect; 32 33 private final String mFqcn; 34 35 private Map<String, TestAttribute> mAttributes = new HashMap<String, TestAttribute>(); 36 37 private List<TestDragElement> mChildren = new ArrayList<TestDragElement>(); 38 39 private TestDragElement mParent; 40 TestDragElement(String mFqcn, Rect mRect, List<TestDragElement> mChildren, TestDragElement mParent)41 public TestDragElement(String mFqcn, Rect mRect, List<TestDragElement> mChildren, 42 TestDragElement mParent) { 43 super(); 44 this.mRect = mRect; 45 this.mFqcn = mFqcn; 46 this.mChildren = mChildren; 47 this.mParent = mParent; 48 } 49 TestDragElement(String fqn)50 public TestDragElement(String fqn) { 51 this(fqn, null, null, null); 52 } 53 setBounds(Rect bounds)54 public TestDragElement setBounds(Rect bounds) { 55 this.mRect = bounds; 56 57 return this; 58 } 59 60 // Builder stuff set(String uri, String name, String value)61 public TestDragElement set(String uri, String name, String value) { 62 if (mAttributes == null) { 63 mAttributes = new HashMap<String, TestAttribute>(); 64 } 65 66 mAttributes.put(uri + name, new TestAttribute(uri, name, value)); 67 68 return this; 69 } 70 add(TestDragElement... children)71 public TestDragElement add(TestDragElement... children) { 72 if (mChildren == null) { 73 mChildren = new ArrayList<TestDragElement>(); 74 } 75 76 for (TestDragElement child : children) { 77 mChildren.add(child); 78 child.mParent = this; 79 } 80 81 return this; 82 } 83 id(String id)84 public TestDragElement id(String id) { 85 return set(ANDROID_URI, ATTR_ID, id); 86 } 87 create(String fqn, Rect bounds)88 public static TestDragElement create(String fqn, Rect bounds) { 89 return create(fqn).setBounds(bounds); 90 } 91 create(String fqn)92 public static TestDragElement create(String fqn) { 93 return new TestDragElement(fqn); 94 } 95 create(TestDragElement... elements)96 public static IDragElement[] create(TestDragElement... elements) { 97 return elements; 98 } 99 100 // ==== IDragElement ==== 101 102 @Override getAttribute(String uri, String localName)103 public IDragAttribute getAttribute(String uri, String localName) { 104 if (mAttributes == null) { 105 return new TestAttribute(uri, localName, ""); 106 } 107 108 return mAttributes.get(uri + localName); 109 } 110 111 @Override getAttributes()112 public IDragAttribute[] getAttributes() { 113 return mAttributes.values().toArray(new IDragAttribute[mAttributes.size()]); 114 } 115 116 @Override getBounds()117 public Rect getBounds() { 118 return mRect; 119 } 120 121 @Override getFqcn()122 public String getFqcn() { 123 return mFqcn; 124 } 125 126 @Override getInnerElements()127 public IDragElement[] getInnerElements() { 128 if (mChildren == null) { 129 return new IDragElement[0]; 130 } 131 132 return mChildren.toArray(new IDragElement[mChildren.size()]); 133 } 134 135 @Override getParentBounds()136 public Rect getParentBounds() { 137 return mParent != null ? mParent.getBounds() : null; 138 } 139 140 @Override getParentFqcn()141 public String getParentFqcn() { 142 return mParent != null ? mParent.getFqcn() : null; 143 } 144 145 @Override toString()146 public String toString() { 147 return "TestDragElement [fqn=" + mFqcn + ", attributes=" + mAttributes + ", bounds=" 148 + mRect + "]"; 149 } 150 151 152 }