KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > views > plugins > PluginsLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.views.plugins;
12
13 import org.eclipse.core.resources.IStorage;
14 import org.eclipse.jdt.core.IClassFile;
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.IJarEntryResource;
17 import org.eclipse.jdt.core.IJavaElement;
18 import org.eclipse.jdt.core.IPackageFragment;
19 import org.eclipse.jdt.core.IPackageFragmentRoot;
20 import org.eclipse.jdt.core.JavaModelException;
21 import org.eclipse.jdt.ui.JavaUI;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.pde.core.plugin.IFragment;
26 import org.eclipse.pde.core.plugin.IPlugin;
27 import org.eclipse.pde.core.plugin.IPluginModel;
28 import org.eclipse.pde.core.plugin.IPluginModelBase;
29 import org.eclipse.pde.internal.core.FileAdapter;
30 import org.eclipse.pde.internal.core.PDECore;
31 import org.eclipse.pde.internal.core.SearchablePluginsManager;
32 import org.eclipse.pde.internal.ui.PDELabelProvider;
33 import org.eclipse.pde.internal.ui.PDEPlugin;
34 import org.eclipse.pde.internal.ui.PDEUIMessages;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.ui.ISharedImages;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.ide.IDE;
39
40 public class PluginsLabelProvider extends LabelProvider {
41     private PDELabelProvider sharedProvider;
42
43     private Image projectImage;
44
45     private Image folderImage;
46
47     /**
48      * Constructor for PluginsLabelProvider.
49      */

50     public PluginsLabelProvider() {
51         super();
52         sharedProvider = PDEPlugin.getDefault().getLabelProvider();
53         folderImage = PlatformUI.getWorkbench().getSharedImages().getImage(
54                 ISharedImages.IMG_OBJ_FOLDER);
55         projectImage = PlatformUI.getWorkbench().getSharedImages().getImage(
56                 IDE.SharedImages.IMG_OBJ_PROJECT);
57         sharedProvider.connect(this);
58     }
59
60     public void dispose() {
61         sharedProvider.disconnect(this);
62         super.dispose();
63     }
64
65     public String JavaDoc getText(Object JavaDoc obj) {
66         if (obj instanceof IPluginModelBase) {
67             return getText((IPluginModelBase) obj);
68         }
69
70         if (obj instanceof FileAdapter) {
71             return getText((FileAdapter) obj);
72         }
73         
74         if (obj instanceof IPackageFragmentRoot) {
75             // use the short name
76
return ((IPackageFragmentRoot) obj).getPath().lastSegment();
77         }
78         
79         if (obj instanceof IJavaElement) {
80             return ((IJavaElement) obj).getElementName();
81         }
82         
83         if (obj instanceof IStorage) {
84             return ((IStorage) obj).getName();
85         }
86         
87         return super.getText(obj);
88     }
89
90     public Image getImage(Object JavaDoc obj) {
91         if (obj instanceof IPluginModelBase) {
92             return getImage((IPluginModelBase) obj);
93         }
94
95         if (obj instanceof FileAdapter) {
96             return getImage((FileAdapter) obj);
97         }
98
99         if (obj instanceof IPackageFragmentRoot) {
100             IPackageFragmentRoot root = (IPackageFragmentRoot) obj;
101             boolean hasSource = false;
102
103             try {
104                 hasSource = root.getSourceAttachmentPath() != null;
105             } catch (JavaModelException e) {
106             }
107             return JavaUI.getSharedImages().getImage(
108                             hasSource ? org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE_WITH_SOURCE
109                                     : org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE);
110         }
111
112         if (obj instanceof IPackageFragment) {
113             return JavaUI.getSharedImages().getImage(
114                     org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_PACKAGE);
115         }
116
117         if (obj instanceof ICompilationUnit) {
118             return JavaUI.getSharedImages().getImage(
119                     org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CUNIT);
120         }
121
122         if (obj instanceof IClassFile) {
123             return JavaUI.getSharedImages().getImage(
124                     org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CFILE);
125         }
126
127         if (obj instanceof IStorage) {
128             if (obj instanceof IJarEntryResource && ! ((IJarEntryResource) obj).isFile())
129                 return folderImage;
130             return getFileImage(((IStorage) obj).getName());
131         }
132
133         return null;
134     }
135
136     private String JavaDoc getText(IPluginModelBase model) {
137         String JavaDoc text = sharedProvider.getText(model);
138         if (!model.isEnabled())
139             text = NLS.bind(PDEUIMessages.PluginsView_disabled, text);
140         return text;
141     }
142
143     private String JavaDoc getText(FileAdapter file) {
144         return file.getFile().getName();
145     }
146
147     private Image getImage(IPluginModelBase model) {
148         if (model.getUnderlyingResource() != null)
149             return projectImage;
150
151         if (model instanceof IPluginModel)
152             return sharedProvider.getObjectImage((IPlugin) model.getPluginBase(), true,
153                     isInJavaSearch(model));
154
155         return sharedProvider.getObjectImage((IFragment) model.getPluginBase(), true,
156                 isInJavaSearch(model));
157     }
158
159     private boolean isInJavaSearch(IPluginModelBase model) {
160         String JavaDoc id = model.getPluginBase().getId();
161         SearchablePluginsManager manager = PDECore.getDefault()
162                 .getSearchablePluginsManager();
163         return manager.isInJavaSearch(id);
164     }
165
166     private Image getImage(FileAdapter fileAdapter) {
167         if (fileAdapter.isDirectory()) {
168             return folderImage;
169         }
170         return getFileImage(fileAdapter.getFile().getName());
171     }
172
173     private Image getFileImage(String JavaDoc fileName) {
174         ImageDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry()
175                 .getImageDescriptor(fileName);
176         return sharedProvider.get(desc);
177     }
178 }
179
Popular Tags