aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/menu/descriptors/MenuDescriptors.java
blob: b7bab1bd34ff6ff3512862f07fa7d0489bcb586b (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (C) 2008 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.menu.descriptors;

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

import com.android.ide.common.resources.platform.DeclareStyleableInfo;
import com.android.ide.eclipse.adt.AdtUtils;
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.Map;

/**
 * Complete description of the menu structure.
 */
public final class MenuDescriptors implements IDescriptorProvider {

    /** The root element descriptor. */
    private ElementDescriptor mDescriptor = null;

    /** @return the root descriptor. */
    @Override
    public ElementDescriptor getDescriptor() {
        return mDescriptor;
    }

    @Override
    public ElementDescriptor[] getRootElementDescriptors() {
        return mDescriptor.getChildren();
    }

    /**
     * Updates the document descriptor.
     * <p/>
     * It first computes the new children of the descriptor and then updates them
     * all at once.
     *
     * @param styleMap The map style => attributes from the attrs.xml file
     */
    public synchronized void updateDescriptors(Map<String, DeclareStyleableInfo> styleMap) {

        // There are 3 elements: menu, item and group.
        // The root element MUST be a menu.
        // A top menu can contain items or group:
        //  - top groups can contain top items
        //  - top items can contain sub-menus
        // A sub menu can contains sub items or sub groups:
        //  - sub groups can contain sub items
        //  - sub items cannot contain anything

        if (mDescriptor == null) {
            mDescriptor = createElement(styleMap,
                TAG_MENU, // xmlName
                "Menu", // uiName,
                null, // TODO SDK URL
                null, // extraAttribute
                null, // childrenElements,
                true /* mandatory */);
        }

        // -- sub menu can have sub_items, sub_groups but not sub_menus

        ElementDescriptor sub_item = createElement(styleMap,
                "item", // xmlName //$NON-NLS-1$
                "Item", // uiName,
                null, // TODO SDK URL
                null, // extraAttribute
                null, // childrenElements,
                false /* mandatory */);

        ElementDescriptor sub_group = createElement(styleMap,
                "group", // xmlName //$NON-NLS-1$
                "Group", // uiName,
                null, // TODO SDK URL
                null, // extraAttribute
                new ElementDescriptor[] { sub_item }, // childrenElements,
                false /* mandatory */);

        ElementDescriptor sub_menu = createElement(styleMap,
                TAG_MENU, // xmlName
                "Sub-Menu", // uiName,
                null, // TODO SDK URL
                null, // extraAttribute
                new ElementDescriptor[] { sub_item, sub_group }, // childrenElements,
                true /* mandatory */);

        // -- top menu can have all top groups and top items (which can have sub menus)

        ElementDescriptor top_item = createElement(styleMap,
                "item", // xmlName //$NON-NLS-1$
                "Item", // uiName,
                null, // TODO SDK URL
                null, // extraAttribute
                new ElementDescriptor[] { sub_menu }, // childrenElements,
                false /* mandatory */);

        ElementDescriptor top_group = createElement(styleMap,
                "group", // xmlName //$NON-NLS-1$
                "Group", // uiName,
                null, // TODO SDK URL
                null, // extraAttribute
                new ElementDescriptor[] { top_item }, // childrenElements,
                false /* mandatory */);

        XmlnsAttributeDescriptor xmlns = new XmlnsAttributeDescriptor(ANDROID_NS_NAME,
                ANDROID_URI);

        updateElement(mDescriptor, styleMap, "Menu", xmlns); //$NON-NLS-1$
        mDescriptor.setChildren(new ElementDescriptor[] { top_item, top_group });
    }

    /**
     * Returns a new ElementDescriptor constructed from the information given here
     * and the javadoc & attributes extracted from the style map if any.
     */
    private ElementDescriptor createElement(
            Map<String, DeclareStyleableInfo> styleMap,
            String xmlName, String uiName, String sdkUrl,
            AttributeDescriptor extraAttribute,
            ElementDescriptor[] childrenElements, boolean mandatory) {

        ElementDescriptor element = new ElementDescriptor(xmlName, uiName, null, sdkUrl,
                null, childrenElements, mandatory);

        return updateElement(element, styleMap,
                getStyleName(xmlName),
                extraAttribute);
    }

    /**
     * Updates an ElementDescriptor with the javadoc & attributes extracted from the style
     * map if any.
     */
    private ElementDescriptor updateElement(ElementDescriptor element,
            Map<String, DeclareStyleableInfo> styleMap,
            String styleName,
            AttributeDescriptor extraAttribute) {
        ArrayList<AttributeDescriptor> descs = new ArrayList<AttributeDescriptor>();

        DeclareStyleableInfo style = styleMap != null ? styleMap.get(styleName) : null;
        if (style != null) {
            DescriptorsUtils.appendAttributes(descs,
                    null,   // elementName
                    ANDROID_URI,
                    style.getAttributes(),
                    null,   // requiredAttributes
                    null);  // overrides
            element.setTooltip(style.getJavaDoc());
        }

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

        element.setAttributes(descs.toArray(new AttributeDescriptor[descs.size()]));
        return element;
    }

    /**
     * Returns the style name (i.e. the <declare-styleable> name found in attrs.xml)
     * for a given XML element name.
     * <p/>
     * The rule is that all elements have for style name:
     * - their xml name capitalized
     * - a "Menu" prefix, except for <menu> itself which is just "Menu".
     */
    private String getStyleName(String xmlName) {
        String styleName = AdtUtils.capitalize(xmlName);

        // This is NOT the UI Name but the expected internal style name
        final String MENU_STYLE_BASE_NAME = "Menu"; //$NON-NLS-1$

        if (!styleName.equals(MENU_STYLE_BASE_NAME)) {
            styleName = MENU_STYLE_BASE_NAME + styleName;
        }
        return styleName;
    }
}