1 /* 2 * Copyright (C) 2012 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; 17 18 import static com.android.ide.common.layout.LayoutConstants.ANDROID_URI; 19 import static com.android.ide.common.layout.LayoutConstants.ATTR_ID; 20 import static com.android.ide.common.layout.LayoutConstants.ATTR_NAME; 21 import static com.android.ide.common.layout.LayoutConstants.ATTR_SRC; 22 import static com.android.ide.common.layout.LayoutConstants.ATTR_TEXT; 23 import static com.android.ide.common.layout.LayoutConstants.DRAWABLE_PREFIX; 24 import static com.android.ide.common.layout.LayoutConstants.LAYOUT_PREFIX; 25 26 import com.android.ide.eclipse.adt.internal.editors.layout.descriptors.LayoutDescriptors; 27 28 import org.eclipse.swt.graphics.Image; 29 import org.eclipse.wst.xml.ui.internal.contentoutline.JFaceNodeLabelProvider; 30 import org.w3c.dom.DOMException; 31 import org.w3c.dom.Element; 32 33 /** Label provider for the XML outlines and quick outlines: Use our own icons, 34 * when available, and and include the most important attribute (id, name, or text) */ 35 @SuppressWarnings("restriction") // XML UI API 36 class OutlineLabelProvider extends JFaceNodeLabelProvider { 37 @Override getImage(Object element)38 public Image getImage(Object element) { 39 if (element instanceof Element) { 40 Element e = (Element) element; 41 String tagName = e.getTagName(); 42 IconFactory factory = IconFactory.getInstance(); 43 Image img = factory.getIcon(tagName, null); 44 if (img != null) { 45 return img; 46 } 47 } 48 return super.getImage(element); 49 } 50 51 @Override getText(Object element)52 public String getText(Object element) { 53 String text = super.getText(element); 54 if (element instanceof Element) { 55 Element e = (Element) element; 56 String id = getAttributeNS(e, ANDROID_URI, ATTR_ID); 57 if (id == null || id.length() == 0) { 58 id = getAttributeNS(e, ANDROID_URI, ATTR_NAME); 59 if (id == null || id.length() == 0) { 60 id = e.getAttribute(ATTR_NAME); 61 if (id == null || id.length() == 0) { 62 id = getAttributeNS(e, ANDROID_URI, ATTR_TEXT); 63 if (id != null && id.length() > 15) { 64 id = id.substring(0, 12) + "..."; 65 } 66 if (id == null || id.length() == 0) { 67 id = getAttributeNS(e, ANDROID_URI, ATTR_SRC); 68 if (id != null && id.length() > 0) { 69 if (id.startsWith(DRAWABLE_PREFIX)) { 70 id = id.substring(DRAWABLE_PREFIX.length()); 71 } 72 } else { 73 id = e.getAttribute(LayoutDescriptors.ATTR_LAYOUT); 74 if (id != null && id.length() > 0) { 75 if (id.startsWith(LAYOUT_PREFIX)) { 76 id = id.substring(LAYOUT_PREFIX.length()); 77 } 78 } 79 } 80 } 81 } 82 } 83 } 84 85 if (id != null && id.length() > 0) { 86 return text + ": " + id; //$NON-NLS-1$ 87 } 88 } 89 return text; 90 } 91 92 /** 93 * Wrapper around {@link Element#getAttributeNS(String, String)}. 94 * <p/> 95 * The implementation used in Eclipse's XML editor sometimes internally throws 96 * an NPE instead of politely returning null. 97 * 98 * @see Element#getAttributeNS(String, String) 99 */ getAttributeNS(Element e, String uri, String name)100 private String getAttributeNS(Element e, String uri, String name) throws DOMException { 101 try { 102 return e.getAttributeNS(uri, name); 103 } catch (NullPointerException ignore) { 104 return null; 105 } 106 } 107 } 108