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