1 /*
2  * Copyright (C) 2007 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 
17 package com.android.ide.eclipse.adt.internal.editors.descriptors;
18 
19 import com.android.SdkConstants;
20 import com.android.ide.common.api.IAttributeInfo;
21 import com.android.ide.eclipse.adt.internal.editors.IconFactory;
22 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiAttributeNode;
23 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
24 
25 import org.eclipse.swt.graphics.Image;
26 
27 /**
28  * {@link AttributeDescriptor} describes an XML attribute with its XML attribute name.
29  * <p/>
30  * An attribute descriptor also knows which UI node should be instantiated to represent
31  * this particular attribute (e.g. text field, icon chooser, class selector, etc.)
32  * Some attributes may be hidden and have no user interface at all.
33  * <p/>
34  * This is an abstract class. Derived classes must implement data description and return
35  * the correct UiAttributeNode-derived class.
36  */
37 public abstract class AttributeDescriptor implements Comparable<AttributeDescriptor> {
38     public static final String ATTRIBUTE_ICON_FILENAME = "attribute"; //$NON-NLS-1$
39 
40     private final String mXmlLocalName;
41     private final String mNsUri;
42     private final IAttributeInfo mAttrInfo;
43     private ElementDescriptor mParent;
44 
45     /**
46      * Creates a new {@link AttributeDescriptor}
47      *
48      * @param xmlLocalName The XML name of the attribute (case sensitive)
49      * @param nsUri The URI of the attribute. Can be null if attribute has no namespace.
50      *              See {@link SdkConstants#NS_RESOURCES} for a common value.
51      * @param attrInfo The {@link IAttributeInfo} of this attribute. Can't be null for a "real"
52      *              attribute representing a View element's attribute. Can be null for some
53      *              specialized internal attribute descriptors (e.g. hidden descriptors, XMLNS,
54      *              or attribute separator, all of which do not represent any real attribute.)
55      */
AttributeDescriptor(String xmlLocalName, String nsUri, IAttributeInfo attrInfo)56     public AttributeDescriptor(String xmlLocalName, String nsUri, IAttributeInfo attrInfo) {
57         assert xmlLocalName != null;
58         mXmlLocalName = xmlLocalName;
59         mNsUri = nsUri;
60         mAttrInfo = attrInfo;
61     }
62 
63     /** Returns the XML local name of the attribute (case sensitive). */
getXmlLocalName()64     public final String getXmlLocalName() {
65         return mXmlLocalName;
66     }
67 
68     /** Returns the namespace URI of this attribute. */
getNamespaceUri()69     public final String getNamespaceUri() {
70         return mNsUri;
71     }
72 
73     /** Sets the element descriptor to which this attribute is attached. */
setParent(ElementDescriptor parent)74     final void setParent(ElementDescriptor parent) {
75         mParent = parent;
76     }
77 
78     /** Returns the element descriptor to which this attribute is attached. */
getParent()79     public final ElementDescriptor getParent() {
80         return mParent;
81     }
82 
83     /** Returns whether this attribute is deprecated (based on its attrs.xml javadoc.) */
isDeprecated()84     public boolean isDeprecated() {
85         return mAttrInfo == null ? false : mAttrInfo.getDeprecatedDoc() != null;
86     }
87 
88     /**
89      * Returns the {@link IAttributeInfo} of this attribute.
90      * Can't be null for real attributes.
91      * Can be null for specialized internal attribute descriptors that do not correspond to
92      * any real XML attribute.
93      */
getAttributeInfo()94     public IAttributeInfo getAttributeInfo() {
95         return mAttrInfo;
96     }
97 
98     /**
99      * Returns an optional icon for the attribute.
100      * This icon is generic, that is all attribute descriptors have the same icon
101      * no matter what they represent.
102      *
103      * @return An icon for this element or null.
104      */
getGenericIcon()105     public Image getGenericIcon() {
106         return IconFactory.getInstance().getIcon(ATTRIBUTE_ICON_FILENAME);
107     }
108 
109     /**
110      * @param uiParent The {@link UiElementNode} parent of this UI attribute.
111      * @return A new {@link UiAttributeNode} linked to this descriptor or null if this
112      *         attribute has no user interface.
113      */
createUiNode(UiElementNode uiParent)114     public abstract UiAttributeNode createUiNode(UiElementNode uiParent);
115 
116     // Implements Comparable<AttributeDescriptor>
117     @Override
compareTo(AttributeDescriptor other)118     public int compareTo(AttributeDescriptor other) {
119         return mXmlLocalName.compareTo(other.mXmlLocalName);
120     }
121 }
122