1 /* 2 * Copyright (C) 2011 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.eclipse.adt.internal.editors.animator; 17 18 import static com.android.ide.common.layout.LayoutConstants.ANDROID_NS_NAME; 19 20 import com.android.ide.common.resources.platform.DeclareStyleableInfo; 21 import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor; 22 import com.android.ide.eclipse.adt.internal.editors.descriptors.DescriptorsUtils; 23 import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor; 24 import com.android.ide.eclipse.adt.internal.editors.descriptors.IDescriptorProvider; 25 import com.android.ide.eclipse.adt.internal.editors.descriptors.XmlnsAttributeDescriptor; 26 import com.android.sdklib.SdkConstants; 27 28 import java.util.ArrayList; 29 import java.util.HashMap; 30 import java.util.List; 31 import java.util.Map; 32 33 /** 34 * Descriptors for /res/animator XML files. 35 */ 36 public class AnimatorDescriptors implements IDescriptorProvider { 37 /** The root element descriptor */ 38 private ElementDescriptor mDescriptor; 39 /** The root element descriptors */ 40 private ElementDescriptor[] mRootDescriptors; 41 private Map<String, ElementDescriptor> nameToDescriptor; 42 43 /** @return the root descriptor. */ getDescriptor()44 public ElementDescriptor getDescriptor() { 45 if (mDescriptor == null) { 46 mDescriptor = new ElementDescriptor("", getRootElementDescriptors()); //$NON-NLS-1$ 47 } 48 49 return mDescriptor; 50 } 51 getRootElementDescriptors()52 public ElementDescriptor[] getRootElementDescriptors() { 53 return mRootDescriptors; 54 } 55 getElementDescriptor(String mRootTag)56 public ElementDescriptor getElementDescriptor(String mRootTag) { 57 if (nameToDescriptor == null) { 58 nameToDescriptor = new HashMap<String, ElementDescriptor>(); 59 for (ElementDescriptor descriptor : getRootElementDescriptors()) { 60 nameToDescriptor.put(descriptor.getXmlName(), descriptor); 61 } 62 } 63 64 ElementDescriptor descriptor = nameToDescriptor.get(mRootTag); 65 if (descriptor == null) { 66 descriptor = getDescriptor(); 67 } 68 return descriptor; 69 } 70 updateDescriptors(Map<String, DeclareStyleableInfo> styleMap)71 public synchronized void updateDescriptors(Map<String, DeclareStyleableInfo> styleMap) { 72 if (styleMap == null) { 73 return; 74 } 75 76 XmlnsAttributeDescriptor xmlns = new XmlnsAttributeDescriptor(ANDROID_NS_NAME, 77 SdkConstants.NS_RESOURCES); 78 79 List<ElementDescriptor> descriptors = new ArrayList<ElementDescriptor>(); 80 81 String sdkUrl = 82 "http://developer.android.com/guide/topics/graphics/animation.html"; //$NON-NLS-1$ 83 84 ElementDescriptor set = addElement(descriptors, styleMap, 85 "set", "Animator Set", "AnimatorSet", null, //$NON-NLS-1$ //$NON-NLS-3$ 86 null /* tooltip */, sdkUrl, 87 xmlns, null, true /*mandatory*/); 88 89 ElementDescriptor objectAnimator = addElement(descriptors, styleMap, 90 "objectAnimator", "Object Animator", //$NON-NLS-1$ 91 "PropertyAnimator", "Animator", //$NON-NLS-1$ //$NON-NLS-2$ 92 null /* tooltip */, sdkUrl, 93 xmlns, null, true /*mandatory*/); 94 95 ElementDescriptor animator = addElement(descriptors, styleMap, 96 "animator", "Animator", "Animator", null, //$NON-NLS-1$ //$NON-NLS-3$ 97 null /* tooltip */, sdkUrl, 98 xmlns, null, true /*mandatory*/); 99 100 mRootDescriptors = descriptors.toArray(new ElementDescriptor[descriptors.size()]); 101 102 // Allow arbitrary nesting: the children of all of these element can include 103 // any of the others 104 if (objectAnimator != null) { 105 objectAnimator.setChildren(mRootDescriptors); 106 } 107 if (animator != null) { 108 animator.setChildren(mRootDescriptors); 109 } 110 if (set != null) { 111 set.setChildren(mRootDescriptors); 112 } 113 } 114 115 /** 116 * Looks up the given style, and if found creates a new {@link ElementDescriptor} 117 * corresponding to the style. It can optionally take an extra style to merge in 118 * additional attributes from, and an extra attribute to add in as well. The new 119 * element, if it exists, can also be optionally added into a list. 120 * 121 * @param descriptors an optional list to add the element into, or null 122 * @param styleMap The map style => attributes from the attrs.xml file 123 * @param xmlName the XML tag name to use for the element 124 * @param uiName the UI name to display the element as 125 * @param styleName the name of the style which must exist for this style 126 * @param extraStyle an optional extra style to merge in attributes from, or null 127 * @param tooltip the tooltip or documentation for this element, or null 128 * @param sdkUrl an optional SDK url to display for the element, or null 129 * @param extraAttribute an extra attribute to add to the attributes list, or null 130 * @param childrenElements an array of children allowed by this element, or null 131 * @param mandatory if true, this element is mandatory 132 * @return a newly created element, or null if the style does not exist 133 */ addElement( List<ElementDescriptor> descriptors, Map<String, DeclareStyleableInfo> styleMap, String xmlName, String uiName, String styleName, String extraStyle, String tooltip, String sdkUrl, AttributeDescriptor extraAttribute, ElementDescriptor[] childrenElements, boolean mandatory)134 public static ElementDescriptor addElement( 135 List<ElementDescriptor> descriptors, 136 Map<String, DeclareStyleableInfo> styleMap, 137 String xmlName, String uiName, String styleName, String extraStyle, 138 String tooltip, String sdkUrl, 139 AttributeDescriptor extraAttribute, 140 ElementDescriptor[] childrenElements, 141 boolean mandatory) { 142 DeclareStyleableInfo style = styleMap.get(styleName); 143 if (style == null) { 144 return null; 145 } 146 ElementDescriptor element = new ElementDescriptor(xmlName, uiName, tooltip, sdkUrl, 147 null, childrenElements, mandatory); 148 149 ArrayList<AttributeDescriptor> descs = new ArrayList<AttributeDescriptor>(); 150 151 DescriptorsUtils.appendAttributes(descs, 152 null, // elementName 153 SdkConstants.NS_RESOURCES, 154 style.getAttributes(), 155 null, // requiredAttributes 156 null); // overrides 157 element.setTooltip(style.getJavaDoc()); 158 159 if (extraStyle != null) { 160 style = styleMap.get(extraStyle); 161 if (style != null) { 162 DescriptorsUtils.appendAttributes(descs, 163 null, // elementName 164 SdkConstants.NS_RESOURCES, 165 style.getAttributes(), 166 null, // requiredAttributes 167 null); // overrides 168 } 169 } 170 171 if (extraAttribute != null) { 172 descs.add(extraAttribute); 173 } 174 175 element.setAttributes(descs.toArray(new AttributeDescriptor[descs.size()])); 176 if (descriptors != null) { 177 descriptors.add(element); 178 } 179 180 return element; 181 } 182 } 183