Home | History | Annotate | Download | only in resources
      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.resources;
     18 
     19 /**
     20  * Enum representing a type of compiled resource.
     21  */
     22 public enum ResourceType {
     23     ANIM("anim", "Animation"), //$NON-NLS-1$
     24     ARRAY("array", "Array", "string-array", "integer-array"), //$NON-NLS-1$ //$NON-NLS-3$ //$NON-NLS-4$
     25     ATTR("attr", "Attr"), //$NON-NLS-1$
     26     COLOR("color", "Color"), //$NON-NLS-1$
     27     DIMEN("dimen", "Dimension"), //$NON-NLS-1$
     28     DRAWABLE("drawable", "Drawable"), //$NON-NLS-1$
     29     ID("id", "ID"), //$NON-NLS-1$
     30     LAYOUT("layout", "Layout"), //$NON-NLS-1$
     31     MENU("menu", "Menu"), //$NON-NLS-1$
     32     RAW("raw", "Raw"), //$NON-NLS-1$
     33     STRING("string", "String"), //$NON-NLS-1$
     34     STYLE("style", "Style"), //$NON-NLS-1$
     35     STYLEABLE("styleable", "Styleable"), //$NON-NLS-1$
     36     XML("xml", "XML"); //$NON-NLS-1$
     37 
     38     private final String mName;
     39     private final String mDisplayName;
     40     private final String[] mAlternateXmlNames;
     41 
     42     ResourceType(String name, String displayName, String... alternateXmlNames) {
     43         mName = name;
     44         mDisplayName = displayName;
     45         mAlternateXmlNames = alternateXmlNames;
     46     }
     47 
     48     /**
     49      * Returns the resource type name, as used by XML files.
     50      */
     51     public String getName() {
     52         return mName;
     53     }
     54 
     55     /**
     56      * Returns a translated display name for the resource type.
     57      */
     58     public String getDisplayName() {
     59         return mDisplayName;
     60     }
     61 
     62     /**
     63      * Returns the enum by its name as it appears in the XML or the R class.
     64      * @param name name of the resource
     65      * @return the matching {@link ResourceType} or <code>null</code> if no match was found.
     66      */
     67     public static ResourceType getEnum(String name) {
     68         for (ResourceType rType : values()) {
     69             if (rType.mName.equals(name)) {
     70                 return rType;
     71             } else if (rType.mAlternateXmlNames != null) {
     72                 // if there are alternate Xml Names, we test those too
     73                 for (String alternate : rType.mAlternateXmlNames) {
     74                     if (alternate.equals(name)) {
     75                         return rType;
     76                     }
     77                 }
     78             }
     79         }
     80         return null;
     81     }
     82 
     83     /**
     84      * Returns a formatted string usable in an XML to use the specified {@link ResourceItem}.
     85      * @param resourceItem The resource item.
     86      * @param system Whether this is a system resource or a project resource.
     87      * @return a string in the format @[type]/[name]
     88      */
     89     public String getXmlString(ResourceItem resourceItem, boolean system) {
     90         if (this == ID && resourceItem instanceof IIdResourceItem) {
     91             IIdResourceItem idResource = (IIdResourceItem)resourceItem;
     92             if (idResource.isDeclaredInline()) {
     93                 return (system?"@android:":"@+") + mName + "/" + resourceItem.getName(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
     94             }
     95         }
     96 
     97         return (system?"@android:":"@") + mName + "/" + resourceItem.getName(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
     98     }
     99 
    100     /**
    101      * Returns an array with all the names defined by this enum.
    102      */
    103     public static String[] getNames() {
    104         ResourceType[] values = values();
    105         String[] names = new String[values.length];
    106         for (int i = values.length - 1; i >= 0; --i) {
    107             names[i] = values[i].getName();
    108         }
    109         return names;
    110     }
    111 }
    112