| 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.ui.internal.ide.model; |
| 12 | |
| 13 | import java.util.HashMap; |
| 14 | |
| 15 | import org.eclipse.core.resources.IProject; |
| 16 | import org.eclipse.core.resources.IResource; |
| 17 | import org.eclipse.core.runtime.CoreException; |
| 18 | import org.eclipse.jface.resource.ImageDescriptor; |
| 19 | import org.eclipse.swt.graphics.Point; |
| 20 | import org.eclipse.ui.IProjectActionFilter; |
| 21 | import org.eclipse.ui.ide.IDE; |
| 22 | import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages; |
| 23 | import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; |
| 24 | import org.eclipse.ui.internal.ide.misc.OverlayIcon; |
| 25 | |
| 26 | /** |
| 27 | * An IWorkbenchAdapter that represents IProject. |
| 28 | */ |
| 29 | public class WorkbenchProject extends WorkbenchResource implements |
| 30 | IProjectActionFilter { |
| 31 | HashMap imageCache = new HashMap(11); |
| 32 | |
| 33 | /** |
| 34 | * Answer the appropriate base image to use for the passed resource, optionally |
| 35 | * considering the passed open status as well iff appropriate for the type of |
| 36 | * passed resource |
| 37 | */ |
| 38 | protected ImageDescriptor getBaseImage(IResource resource) { |
| 39 | IProject project = (IProject) resource; |
| 40 | boolean isOpen = project.isOpen(); |
| 41 | String baseKey = isOpen ? IDE.SharedImages.IMG_OBJ_PROJECT |
| 42 | : IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED; |
| 43 | if (isOpen) { |
| 44 | try { |
| 45 | String[] natureIds = project.getDescription().getNatureIds(); |
| 46 | for (int i = 0; i < natureIds.length; ++i) { |
| 47 | // Have to use a cache because OverlayIcon does not define its own equality criteria, |
| 48 | // so WorkbenchLabelProvider would always create a new image otherwise. |
| 49 | String imageKey = natureIds[i]; |
| 50 | ImageDescriptor overlayImage = (ImageDescriptor) imageCache |
| 51 | .get(imageKey); |
| 52 | if (overlayImage != null) { |
| 53 | return overlayImage; |
| 54 | } |
| 55 | ImageDescriptor natureImage = IDEWorkbenchPlugin |
| 56 | .getDefault().getProjectImageRegistry() |
| 57 | .getNatureImage(natureIds[i]); |
| 58 | if (natureImage != null) { |
| 59 | ImageDescriptor baseImage = IDEInternalWorkbenchImages |
| 60 | .getImageDescriptor(baseKey); |
| 61 | overlayImage = new OverlayIcon(baseImage, |
| 62 | new ImageDescriptor[][] { { natureImage } }, |
| 63 | new Point(16, 16)); |
| 64 | imageCache.put(imageKey, overlayImage); |
| 65 | return overlayImage; |
| 66 | } |
| 67 | } |
| 68 | } catch (CoreException e) { |
| 69 | } |
| 70 | } |
| 71 | return IDEInternalWorkbenchImages.getImageDescriptor(baseKey); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the children of this container. |
| 76 | */ |
| 77 | public Object[] getChildren(Object o) { |
| 78 | IProject project = (IProject) o; |
| 79 | if (project.isOpen()) { |
| 80 | try { |
| 81 | return project.members(); |
| 82 | } catch (CoreException e) { |
| 83 | //don't get the children if there are problems with the project |
| 84 | } |
| 85 | } |
| 86 | return NO_CHILDREN; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns whether the specific attribute matches the state of the target |
| 91 | * object. |
| 92 | * |
| 93 | * @param target the target object |
| 94 | * @param name the attribute name |
| 95 | * @param value the attriute value |
| 96 | * @return <code>true</code> if the attribute matches; <code>false</code> otherwise |
| 97 | */ |
| 98 | public boolean testAttribute(Object target, String name, String value) { |
| 99 | if (!(target instanceof IProject)) { |
| 100 | return false; |
| 101 | } |
| 102 | IProject proj = (IProject) target; |
| 103 | if (name.equals(NATURE)) { |
| 104 | try { |
| 105 | return proj.isAccessible() && proj.hasNature(value); |
| 106 | } catch (CoreException e) { |
| 107 | return false; |
| 108 | } |
| 109 | } else if (name.equals(OPEN)) { |
| 110 | value = value.toLowerCase(); |
| 111 | return (proj.isOpen() == value.equals("true"));//$NON-NLS-1$ |
| 112 | } |
| 113 | return super.testAttribute(target, name, value); |
| 114 | } |
| 115 | } |