KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > view > PluginsLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.view;
12
13 import org.eclipse.core.resources.IStorage;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.jdt.core.IClassFile;
16 import org.eclipse.jdt.core.ICompilationUnit;
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.ModelEntry;
31 import org.eclipse.pde.internal.ui.PDELabelProvider;
32 import org.eclipse.pde.internal.ui.PDEPlugin;
33 import org.eclipse.pde.internal.ui.PDEUIMessages;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.ui.ISharedImages;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.ide.IDE;
38
39 public class PluginsLabelProvider extends LabelProvider {
40     private PDELabelProvider sharedProvider;
41     private Image projectImage;
42     private Image folderImage;
43
44     /**
45      * Constructor for PluginsLabelProvider.
46      */

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