Home | History | Annotate | Download | only in layout
      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 
     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 
     50     public TestDragElement(String fqn) {
     51         this(fqn, null, null, null);
     52     }
     53 
     54     public TestDragElement setBounds(Rect bounds) {
     55         this.mRect = bounds;
     56 
     57         return this;
     58     }
     59 
     60     // Builder stuff
     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 
     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 
     84     public TestDragElement id(String id) {
     85         return set(ANDROID_URI, ATTR_ID, id);
     86     }
     87 
     88     public static TestDragElement create(String fqn, Rect bounds) {
     89         return create(fqn).setBounds(bounds);
     90     }
     91 
     92     public static TestDragElement create(String fqn) {
     93         return new TestDragElement(fqn);
     94     }
     95 
     96     public static IDragElement[] create(TestDragElement... elements) {
     97         return elements;
     98     }
     99 
    100     // ==== IDragElement ====
    101 
    102     public IDragAttribute getAttribute(String uri, String localName) {
    103         if (mAttributes == null) {
    104             return new TestAttribute(uri, localName, "");
    105         }
    106 
    107         return mAttributes.get(uri + localName);
    108     }
    109 
    110     public IDragAttribute[] getAttributes() {
    111         return mAttributes.values().toArray(new IDragAttribute[mAttributes.size()]);
    112     }
    113 
    114     public Rect getBounds() {
    115         return mRect;
    116     }
    117 
    118     public String getFqcn() {
    119         return mFqcn;
    120     }
    121 
    122     public IDragElement[] getInnerElements() {
    123         if (mChildren == null) {
    124             return new IDragElement[0];
    125         }
    126 
    127         return mChildren.toArray(new IDragElement[mChildren.size()]);
    128     }
    129 
    130     public Rect getParentBounds() {
    131         return mParent != null ? mParent.getBounds() : null;
    132     }
    133 
    134     public String getParentFqcn() {
    135         return mParent != null ? mParent.getFqcn() : null;
    136     }
    137 
    138     @Override
    139     public String toString() {
    140         return "TestDragElement [fqn=" + mFqcn + ", attributes=" + mAttributes + ", bounds="
    141                 + mRect + "]";
    142     }
    143 
    144 
    145 }