aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/animator/AnimatorDescriptors.java
blob: 713f6d92ee721deb531c62801f20485978fe8ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ide.eclipse.adt.internal.editors.animator;

import static com.android.SdkConstants.ANDROID_NS_NAME;
import static com.android.SdkConstants.ANDROID_URI;

import com.android.ide.common.resources.platform.DeclareStyleableInfo;
import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor;
import com.android.ide.eclipse.adt.internal.editors.descriptors.DescriptorsUtils;
import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor;
import com.android.ide.eclipse.adt.internal.editors.descriptors.IDescriptorProvider;
import com.android.ide.eclipse.adt.internal.editors.descriptors.XmlnsAttributeDescriptor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Descriptors for /res/animator XML files.
 */
public class AnimatorDescriptors implements IDescriptorProvider {
    /** The root element descriptor */
    private ElementDescriptor mDescriptor;
    /** The root element descriptors */
    private ElementDescriptor[] mRootDescriptors;
    private Map<String, ElementDescriptor> nameToDescriptor;

    /** @return the root descriptor. */
    @Override
    public ElementDescriptor getDescriptor() {
        if (mDescriptor == null) {
            mDescriptor = new ElementDescriptor("", getRootElementDescriptors()); //$NON-NLS-1$
        }

        return mDescriptor;
    }

    @Override
    public ElementDescriptor[] getRootElementDescriptors() {
        return mRootDescriptors;
    }

    ElementDescriptor getElementDescriptor(String rootTag) {
        if (nameToDescriptor == null) {
            nameToDescriptor = new HashMap<String, ElementDescriptor>();
            for (ElementDescriptor descriptor : getRootElementDescriptors()) {
                nameToDescriptor.put(descriptor.getXmlName(), descriptor);
            }
        }

        ElementDescriptor descriptor = nameToDescriptor.get(rootTag);
        if (descriptor == null) {
            descriptor = getDescriptor();
        }
        return descriptor;
    }

    public synchronized void updateDescriptors(Map<String, DeclareStyleableInfo> styleMap) {
        if (styleMap == null) {
            return;
        }

        XmlnsAttributeDescriptor xmlns = new XmlnsAttributeDescriptor(ANDROID_NS_NAME,
                ANDROID_URI);

        List<ElementDescriptor> descriptors = new ArrayList<ElementDescriptor>();

        String sdkUrl =
            "http://developer.android.com/guide/topics/graphics/animation.html"; //$NON-NLS-1$

        ElementDescriptor set = addElement(descriptors, styleMap,
                "set", "Animator Set", "AnimatorSet", null, //$NON-NLS-1$ //$NON-NLS-3$
                null /* tooltip */, sdkUrl,
                xmlns, null, true /*mandatory*/);

        ElementDescriptor objectAnimator = addElement(descriptors, styleMap,
                "objectAnimator", "Object Animator", //$NON-NLS-1$
                "PropertyAnimator", "Animator", //$NON-NLS-1$ //$NON-NLS-2$
                null /* tooltip */, sdkUrl,
                xmlns, null, true /*mandatory*/);

        ElementDescriptor animator = addElement(descriptors, styleMap,
                "animator", "Animator", "Animator", null, //$NON-NLS-1$ //$NON-NLS-3$
                null /* tooltip */, sdkUrl,
                xmlns, null, true /*mandatory*/);

        mRootDescriptors = descriptors.toArray(new ElementDescriptor[descriptors.size()]);

        // Allow arbitrary nesting: the children of all of these element can include
        // any of the others
        if (objectAnimator != null) {
            objectAnimator.setChildren(mRootDescriptors);
        }
        if (animator != null) {
            animator.setChildren(mRootDescriptors);
        }
        if (set != null) {
            set.setChildren(mRootDescriptors);
        }
    }

    /**
     * Looks up the given style, and if found creates a new {@link ElementDescriptor}
     * corresponding to the style. It can optionally take an extra style to merge in
     * additional attributes from, and an extra attribute to add in as well. The new
     * element, if it exists, can also be optionally added into a list.
     *
     * @param descriptors an optional list to add the element into, or null
     * @param styleMap The map style => attributes from the attrs.xml file
     * @param xmlName the XML tag name to use for the element
     * @param uiName the UI name to display the element as
     * @param styleName the name of the style which must exist for this style
     * @param extraStyle an optional extra style to merge in attributes from, or null
     * @param tooltip the tooltip or documentation for this element, or null
     * @param sdkUrl an optional SDK url to display for the element, or null
     * @param extraAttribute an extra attribute to add to the attributes list, or null
     * @param childrenElements an array of children allowed by this element, or null
     * @param mandatory if true, this element is mandatory
     * @return a newly created element, or null if the style does not exist
     */
    public static ElementDescriptor 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) {
        DeclareStyleableInfo style = styleMap.get(styleName);
        if (style == null) {
            return null;
        }
        ElementDescriptor element = new ElementDescriptor(xmlName, uiName, tooltip, sdkUrl,
                null, childrenElements, mandatory);

        ArrayList<AttributeDescriptor> descs = new ArrayList<AttributeDescriptor>();

        DescriptorsUtils.appendAttributes(descs,
                null,   // elementName
                ANDROID_URI,
                style.getAttributes(),
                null,   // requiredAttributes
                null);  // overrides
        element.setTooltip(style.getJavaDoc());

        if (extraStyle != null) {
            style = styleMap.get(extraStyle);
            if (style != null) {
                DescriptorsUtils.appendAttributes(descs,
                        null,   // elementName
                        ANDROID_URI,
                        style.getAttributes(),
                        null,   // requiredAttributes
                        null);  // overrides
            }
        }

        if (extraAttribute != null) {
            descs.add(extraAttribute);
        }

        element.setAttributes(descs.toArray(new AttributeDescriptor[descs.size()]));
        if (descriptors != null) {
            descriptors.add(element);
        }

        return element;
    }
}