| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2008 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.ui.internal.ide.model; |
| 12 | |
| 13 | import org.eclipse.core.resources.IFile; |
| 14 | import org.eclipse.core.resources.IResource; |
| 15 | import org.eclipse.core.runtime.CoreException; |
| 16 | import org.eclipse.core.runtime.QualifiedName; |
| 17 | import org.eclipse.core.runtime.content.IContentType; |
| 18 | import org.eclipse.jface.resource.ImageDescriptor; |
| 19 | import org.eclipse.ui.ISharedImages; |
| 20 | import org.eclipse.ui.PlatformUI; |
| 21 | import org.eclipse.ui.ide.IDE; |
| 22 | import org.eclipse.ui.internal.WorkbenchPlugin; |
| 23 | |
| 24 | /** |
| 25 | * An IWorkbenchAdapter that represents IFiles. |
| 26 | */ |
| 27 | public class WorkbenchFile extends WorkbenchResource { |
| 28 | |
| 29 | /** |
| 30 | * Constant that is used as the key of a session property on IFile objects |
| 31 | * to cache the result of doing a proper content type lookup. This will be |
| 32 | * set by the ContentTypeDecorator (if enabled) and used instead of the |
| 33 | * "guessed" content type in {@link #getBaseImage(IResource)}. |
| 34 | * |
| 35 | * @since 3.4 |
| 36 | */ |
| 37 | public static QualifiedName IMAGE_CACHE_KEY = new QualifiedName(WorkbenchPlugin.PI_WORKBENCH, "WorkbenchFileImage"); //$NON-NLS-1$ |
| 38 | |
| 39 | /** |
| 40 | * Answer the appropriate base image to use for the passed resource, optionally |
| 41 | * considering the passed open status as well iff appropriate for the type of |
| 42 | * passed resource |
| 43 | */ |
| 44 | protected ImageDescriptor getBaseImage(IResource resource) { |
| 45 | IContentType contentType = null; |
| 46 | // do we need to worry about checking here? |
| 47 | if (resource instanceof IFile) { |
| 48 | IFile file = (IFile)resource; |
| 49 | // cached images come from ContentTypeDecorator |
| 50 | ImageDescriptor cached; |
| 51 | try { |
| 52 | cached = (ImageDescriptor) file.getSessionProperty(IMAGE_CACHE_KEY); |
| 53 | if (cached != null) { |
| 54 | return cached; |
| 55 | } |
| 56 | } catch (CoreException e) { |
| 57 | // ignore - not having a cached image descriptor is not fatal |
| 58 | } |
| 59 | contentType = IDE.guessContentType(file); |
| 60 | } |
| 61 | // @issue move IDE specific images |
| 62 | ImageDescriptor image = PlatformUI.getWorkbench().getEditorRegistry() |
| 63 | .getImageDescriptor(resource.getName(), contentType); |
| 64 | if (image == null) { |
| 65 | image = PlatformUI.getWorkbench().getSharedImages() |
| 66 | .getImageDescriptor(ISharedImages.IMG_OBJ_FILE); |
| 67 | } |
| 68 | return image; |
| 69 | } |
| 70 | } |