001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.views.plugins;
011:
012: import org.eclipse.core.resources.IStorage;
013: import org.eclipse.jdt.core.IClassFile;
014: import org.eclipse.jdt.core.ICompilationUnit;
015: import org.eclipse.jdt.core.IJarEntryResource;
016: import org.eclipse.jdt.core.IJavaElement;
017: import org.eclipse.jdt.core.IPackageFragment;
018: import org.eclipse.jdt.core.IPackageFragmentRoot;
019: import org.eclipse.jdt.core.JavaModelException;
020: import org.eclipse.jdt.ui.JavaUI;
021: import org.eclipse.jface.resource.ImageDescriptor;
022: import org.eclipse.jface.viewers.LabelProvider;
023: import org.eclipse.osgi.util.NLS;
024: import org.eclipse.pde.core.plugin.IFragment;
025: import org.eclipse.pde.core.plugin.IPlugin;
026: import org.eclipse.pde.core.plugin.IPluginModel;
027: import org.eclipse.pde.core.plugin.IPluginModelBase;
028: import org.eclipse.pde.internal.core.FileAdapter;
029: import org.eclipse.pde.internal.core.PDECore;
030: import org.eclipse.pde.internal.core.SearchablePluginsManager;
031: import org.eclipse.pde.internal.ui.PDELabelProvider;
032: import org.eclipse.pde.internal.ui.PDEPlugin;
033: import org.eclipse.pde.internal.ui.PDEUIMessages;
034: import org.eclipse.swt.graphics.Image;
035: import org.eclipse.ui.ISharedImages;
036: import org.eclipse.ui.PlatformUI;
037: import org.eclipse.ui.ide.IDE;
038:
039: public class PluginsLabelProvider extends LabelProvider {
040: private PDELabelProvider sharedProvider;
041:
042: private Image projectImage;
043:
044: private Image folderImage;
045:
046: /**
047: * Constructor for PluginsLabelProvider.
048: */
049: public PluginsLabelProvider() {
050: super ();
051: sharedProvider = PDEPlugin.getDefault().getLabelProvider();
052: folderImage = PlatformUI.getWorkbench().getSharedImages()
053: .getImage(ISharedImages.IMG_OBJ_FOLDER);
054: projectImage = PlatformUI.getWorkbench().getSharedImages()
055: .getImage(IDE.SharedImages.IMG_OBJ_PROJECT);
056: sharedProvider.connect(this );
057: }
058:
059: public void dispose() {
060: sharedProvider.disconnect(this );
061: super .dispose();
062: }
063:
064: public String getText(Object obj) {
065: if (obj instanceof IPluginModelBase) {
066: return getText((IPluginModelBase) obj);
067: }
068:
069: if (obj instanceof FileAdapter) {
070: return getText((FileAdapter) obj);
071: }
072:
073: if (obj instanceof IPackageFragmentRoot) {
074: // use the short name
075: return ((IPackageFragmentRoot) obj).getPath().lastSegment();
076: }
077:
078: if (obj instanceof IJavaElement) {
079: return ((IJavaElement) obj).getElementName();
080: }
081:
082: if (obj instanceof IStorage) {
083: return ((IStorage) obj).getName();
084: }
085:
086: return super .getText(obj);
087: }
088:
089: public Image getImage(Object obj) {
090: if (obj instanceof IPluginModelBase) {
091: return getImage((IPluginModelBase) obj);
092: }
093:
094: if (obj instanceof FileAdapter) {
095: return getImage((FileAdapter) obj);
096: }
097:
098: if (obj instanceof IPackageFragmentRoot) {
099: IPackageFragmentRoot root = (IPackageFragmentRoot) obj;
100: boolean hasSource = false;
101:
102: try {
103: hasSource = root.getSourceAttachmentPath() != null;
104: } catch (JavaModelException e) {
105: }
106: return JavaUI
107: .getSharedImages()
108: .getImage(
109: hasSource ? org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE_WITH_SOURCE
110: : org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE);
111: }
112:
113: if (obj instanceof IPackageFragment) {
114: return JavaUI.getSharedImages().getImage(
115: org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_PACKAGE);
116: }
117:
118: if (obj instanceof ICompilationUnit) {
119: return JavaUI.getSharedImages().getImage(
120: org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CUNIT);
121: }
122:
123: if (obj instanceof IClassFile) {
124: return JavaUI.getSharedImages().getImage(
125: org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CFILE);
126: }
127:
128: if (obj instanceof IStorage) {
129: if (obj instanceof IJarEntryResource
130: && !((IJarEntryResource) obj).isFile())
131: return folderImage;
132: return getFileImage(((IStorage) obj).getName());
133: }
134:
135: return null;
136: }
137:
138: private String getText(IPluginModelBase model) {
139: String text = sharedProvider.getText(model);
140: if (!model.isEnabled())
141: text = NLS.bind(PDEUIMessages.PluginsView_disabled, text);
142: return text;
143: }
144:
145: private String getText(FileAdapter file) {
146: return file.getFile().getName();
147: }
148:
149: private Image getImage(IPluginModelBase model) {
150: if (model.getUnderlyingResource() != null)
151: return projectImage;
152:
153: if (model instanceof IPluginModel)
154: return sharedProvider.getObjectImage((IPlugin) model
155: .getPluginBase(), true, isInJavaSearch(model));
156:
157: return sharedProvider.getObjectImage((IFragment) model
158: .getPluginBase(), true, isInJavaSearch(model));
159: }
160:
161: private boolean isInJavaSearch(IPluginModelBase model) {
162: String id = model.getPluginBase().getId();
163: SearchablePluginsManager manager = PDECore.getDefault()
164: .getSearchablePluginsManager();
165: return manager.isInJavaSearch(id);
166: }
167:
168: private Image getImage(FileAdapter fileAdapter) {
169: if (fileAdapter.isDirectory()) {
170: return folderImage;
171: }
172: return getFileImage(fileAdapter.getFile().getName());
173: }
174:
175: private Image getFileImage(String fileName) {
176: ImageDescriptor desc = PlatformUI.getWorkbench()
177: .getEditorRegistry().getImageDescriptor(fileName);
178: return sharedProvider.get(desc);
179: }
180: }
|