1 /*
2  * Copyright (C) 2008 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.project;
18 
19 import com.android.SdkConstants;
20 import com.android.ide.eclipse.adt.AdtConstants;
21 import com.android.ide.eclipse.adt.AdtPlugin;
22 
23 import org.eclipse.core.resources.IFolder;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.jface.resource.ImageDescriptor;
28 import org.eclipse.jface.viewers.IDecoration;
29 import org.eclipse.jface.viewers.ILabelDecorator;
30 import org.eclipse.jface.viewers.ILabelProviderListener;
31 import org.eclipse.jface.viewers.ILightweightLabelDecorator;
32 
33 /**
34  * A {@link ILabelDecorator} associated with an org.eclipse.ui.decorators extension.
35  * This is used to add android icons in some special folders in the package explorer.
36  */
37 public class FolderDecorator implements ILightweightLabelDecorator {
38 
39     private ImageDescriptor mDescriptor;
40 
FolderDecorator()41     public FolderDecorator() {
42         mDescriptor = AdtPlugin.getImageDescriptor("/icons/android_project.png"); //$NON-NLS-1$
43     }
44 
45     @Override
decorate(Object element, IDecoration decoration)46     public void decorate(Object element, IDecoration decoration) {
47         if (element instanceof IFolder) {
48             IFolder folder = (IFolder)element;
49 
50             // get the project and make sure this is an android project
51             IProject project = folder.getProject();
52             if (project == null || !project.exists() || !folder.exists()) {
53                 return;
54             }
55 
56             try {
57                 if (project.hasNature(AdtConstants.NATURE_DEFAULT)) {
58                     // check the folder is directly under the project.
59                     if (folder.getParent().getType() == IResource.PROJECT) {
60                         String name = folder.getName();
61                         if (name.equals(SdkConstants.FD_ASSETS)) {
62                             doDecoration(decoration, null);
63                         } else if (name.equals(SdkConstants.FD_RESOURCES)) {
64                             doDecoration(decoration, null);
65                         } else if (name.equals(SdkConstants.FD_GEN_SOURCES)) {
66                             doDecoration(decoration, " [Generated Java Files]");
67                         } else if (name.equals(SdkConstants.FD_NATIVE_LIBS)) {
68                             doDecoration(decoration, null);
69                         } else if (name.equals(SdkConstants.FD_OUTPUT)) {
70                             doDecoration(decoration, null);
71                         }
72                     }
73                 }
74             } catch (CoreException e) {
75                 // log the error
76                 AdtPlugin.log(e, "Unable to get nature of project '%s'.", project.getName());
77             }
78         }
79     }
80 
doDecoration(IDecoration decoration, String suffix)81     public void doDecoration(IDecoration decoration, String suffix) {
82         decoration.addOverlay(mDescriptor, IDecoration.TOP_LEFT);
83 
84         if (suffix != null) {
85             decoration.addSuffix(suffix);
86         }
87     }
88 
89     @Override
isLabelProperty(Object element, String property)90     public boolean isLabelProperty(Object element, String property) {
91         // Property change do not affect the label
92         return false;
93     }
94 
95     @Override
addListener(ILabelProviderListener listener)96     public void addListener(ILabelProviderListener listener) {
97         // No state change will affect the rendering.
98     }
99 
100     @Override
removeListener(ILabelProviderListener listener)101     public void removeListener(ILabelProviderListener listener) {
102         // No state change will affect the rendering.
103     }
104 
105     @Override
dispose()106     public void dispose() {
107         // nothing to dispose
108     }
109 }
110