Home | History | Annotate | Download | only in gre
      1 /*
      2  * Copyright (C) 2011 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 package com.android.ide.eclipse.adt.internal.editors.layout.gre;
     17 
     18 import static com.android.ide.common.layout.LayoutConstants.ANDROID_NS_NAME_PREFIX;
     19 import static com.android.ide.common.layout.LayoutConstants.ANDROID_URI;
     20 
     21 import com.android.ide.eclipse.adt.internal.editors.IconFactory;
     22 import com.android.ide.eclipse.adt.internal.editors.layout.descriptors.ViewElementDescriptor;
     23 import com.android.ide.eclipse.adt.internal.editors.layout.gle2.SimpleAttribute;
     24 import com.android.ide.eclipse.adt.internal.editors.layout.gle2.SimpleElement;
     25 
     26 import org.eclipse.swt.graphics.Image;
     27 import org.w3c.dom.Element;
     28 
     29 /**
     30  * Special version of {@link ViewElementDescriptor} which is initialized by the palette
     31  * with specific metadata for how to instantiate particular variations of an existing
     32  * {@link ViewElementDescriptor} with initial values.
     33  */
     34 public class PaletteMetadataDescriptor extends ViewElementDescriptor {
     35     private String mInitString;
     36     private String mIconName;
     37 
     38     public PaletteMetadataDescriptor(ViewElementDescriptor descriptor, String displayName,
     39             String initString, String iconName) {
     40         super(descriptor.getXmlName(),
     41                 displayName,
     42                 descriptor.getFullClassName(),
     43                 descriptor.getTooltip(),
     44                 descriptor.getSdkUrl(),
     45                 descriptor.getAttributes(),
     46                 descriptor.getLayoutAttributes(),
     47                 descriptor.getChildren(), descriptor.getMandatory() == Mandatory.MANDATORY);
     48         mInitString = initString;
     49         mIconName = iconName;
     50     }
     51 
     52     /**
     53      * Returns a String which contains a comma separated list of name=value tokens,
     54      * where the name can start with "android:" to indicate a property in the android namespace,
     55      * or no prefix for plain attributes.
     56      *
     57      * @return the initialization string, which can be empty but never null
     58      */
     59     public String getInitializedAttributes() {
     60         return mInitString != null ? mInitString : ""; //$NON-NLS-1$
     61     }
     62 
     63     @Override
     64     public Image getGenericIcon() {
     65         if (mIconName != null) {
     66             IconFactory factory = IconFactory.getInstance();
     67             Image icon = factory.getIcon(mIconName);
     68             if (icon != null) {
     69                 return icon;
     70             }
     71         }
     72 
     73         return super.getGenericIcon();
     74     }
     75 
     76     /**
     77      * Initializes a new {@link SimpleElement} with the palette initialization
     78      * configuration
     79      *
     80      * @param element the new element to initialize
     81      */
     82     public void initializeNew(SimpleElement element) {
     83         initializeNew(element, null);
     84     }
     85 
     86     /**
     87      * Initializes a new {@link Element} with the palette initialization configuration
     88      *
     89      * @param element the new element to initialize
     90      */
     91     public void initializeNew(Element element) {
     92         initializeNew(null, element);
     93     }
     94 
     95     private void initializeNew(SimpleElement simpleElement, Element domElement) {
     96         String initializedAttributes = mInitString;
     97         if (initializedAttributes != null && initializedAttributes.length() > 0) {
     98             for (String s : initializedAttributes.split(",")) { //$NON-NLS-1$
     99                 String[] nameValue = s.split("="); //$NON-NLS-1$
    100                 String name = nameValue[0];
    101                 String value = nameValue[1];
    102                 String nameSpace = ""; //$NON-NLS-1$
    103                 if (name.startsWith(ANDROID_NS_NAME_PREFIX)) {
    104                     name = name.substring(ANDROID_NS_NAME_PREFIX.length());
    105                     nameSpace = ANDROID_URI;
    106                 }
    107 
    108                 if (simpleElement != null) {
    109                     SimpleAttribute attr = new SimpleAttribute(nameSpace, name, value);
    110                     simpleElement.addAttribute(attr);
    111                 }
    112 
    113                 if (domElement != null) {
    114                     domElement.setAttributeNS(nameSpace, name, value);
    115                 }
    116             }
    117         }
    118     }
    119 }
    120