• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 /**
34  * Label provider for the XML outlines and quick outlines: Use our own icons,
35  * when available, and and include the most important attribute (id, name, or
36  * text)
37  */
38 @SuppressWarnings("restriction")
39 // XML UI API
40 class OutlineLabelProvider extends JFaceNodeLabelProvider {
41     @Override
getImage(Object element)42     public Image getImage(Object element) {
43         if (element instanceof Element) {
44             Element e = (Element) element;
45             String tagName = e.getTagName();
46             IconFactory factory = IconFactory.getInstance();
47             Image img = factory.getIcon(tagName, null);
48             if (img != null) {
49                 return img;
50             }
51         }
52         return super.getImage(element);
53     }
54 
55     @Override
getText(Object element)56     public String getText(Object element) {
57         String text = super.getText(element);
58         if (element instanceof Element) {
59             Element e = (Element) element;
60             String id = getAttributeNS(e, ANDROID_URI, ATTR_ID);
61             if (id == null || id.length() == 0) {
62                 id = getAttributeNS(e, ANDROID_URI, ATTR_NAME);
63                 if (id == null || id.length() == 0) {
64                     id = e.getAttribute(ATTR_NAME);
65                     if (id == null || id.length() == 0) {
66                         id = getAttributeNS(e, ANDROID_URI, ATTR_TEXT);
67                         if (id != null && id.length() > 15) {
68                             id = id.substring(0, 12) + "...";
69                         }
70                         if (id == null || id.length() == 0) {
71                             id = getAttributeNS(e, ANDROID_URI, ATTR_SRC);
72                             if (id != null && id.length() > 0) {
73                                 if (id.startsWith(DRAWABLE_PREFIX)) {
74                                     id = id.substring(DRAWABLE_PREFIX.length());
75                                 }
76                             } else {
77                                 id = e.getAttribute(ATTR_LAYOUT);
78                                 if (id != null && id.length() > 0) {
79                                     if (id.startsWith(LAYOUT_RESOURCE_PREFIX)) {
80                                         id = id.substring(LAYOUT_RESOURCE_PREFIX.length());
81                                     }
82                                 }
83                             }
84                         }
85                     }
86                 }
87             }
88             if (id != null && id.length() > 0) {
89                 return text + ": " + id; //$NON-NLS-1$
90             }
91         }
92         return text;
93     }
94 
95     /**
96      * Wrapper around {@link Element#getAttributeNS(String, String)}.
97      * <p/>
98      * The implementation used in Eclipse's XML editor sometimes internally
99      * throws an NPE instead of politely returning null.
100      *
101      * @see Element#getAttributeNS(String, String)
102      */
getAttributeNS(Element e, String uri, String name)103     private String getAttributeNS(Element e, String uri, String name) throws DOMException {
104         try {
105             return e.getAttributeNS(uri, name);
106         } catch (NullPointerException ignore) {
107             return null;
108         }
109     }
110 }
111