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.SdkConstants.ANDROID_NS_NAME_PREFIX; 19 import static com.android.SdkConstants.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 setSuperClass(descriptor.getSuperClassDesc()); 51 } 52 53 /** 54 * Returns a String which contains a comma separated list of name=value tokens, 55 * where the name can start with "android:" to indicate a property in the android namespace, 56 * or no prefix for plain attributes. 57 * 58 * @return the initialization string, which can be empty but never null 59 */ 60 public String getInitializedAttributes() { 61 return mInitString != null ? mInitString : ""; //$NON-NLS-1$ 62 } 63 64 @Override 65 public Image getGenericIcon() { 66 if (mIconName != null) { 67 IconFactory factory = IconFactory.getInstance(); 68 Image icon = factory.getIcon(mIconName); 69 if (icon != null) { 70 return icon; 71 } 72 } 73 74 return super.getGenericIcon(); 75 } 76 77 /** 78 * Initializes a new {@link SimpleElement} with the palette initialization 79 * configuration 80 * 81 * @param element the new element to initialize 82 */ 83 public void initializeNew(SimpleElement element) { 84 initializeNew(element, null); 85 } 86 87 /** 88 * Initializes a new {@link Element} with the palette initialization configuration 89 * 90 * @param element the new element to initialize 91 */ 92 public void initializeNew(Element element) { 93 initializeNew(null, element); 94 } 95 96 private void initializeNew(SimpleElement simpleElement, Element domElement) { 97 String initializedAttributes = mInitString; 98 if (initializedAttributes != null && initializedAttributes.length() > 0) { 99 for (String s : initializedAttributes.split(",")) { //$NON-NLS-1$ 100 String[] nameValue = s.split("="); //$NON-NLS-1$ 101 String name = nameValue[0]; 102 String value = nameValue[1]; 103 String nameSpace = ""; //$NON-NLS-1$ 104 if (name.startsWith(ANDROID_NS_NAME_PREFIX)) { 105 name = name.substring(ANDROID_NS_NAME_PREFIX.length()); 106 nameSpace = ANDROID_URI; 107 } 108 109 if (simpleElement != null) { 110 SimpleAttribute attr = new SimpleAttribute(nameSpace, name, value); 111 simpleElement.addAttribute(attr); 112 } 113 114 if (domElement != null) { 115 domElement.setAttributeNS(nameSpace, name, value); 116 } 117 } 118 } 119 } 120 } 121