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