001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ui.internal.ide.model;
011:
012: import java.util.HashMap;
013:
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.resources.IResource;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.jface.resource.ImageDescriptor;
018: import org.eclipse.swt.graphics.Point;
019: import org.eclipse.ui.IProjectActionFilter;
020: import org.eclipse.ui.ide.IDE;
021: import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
022: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
023: import org.eclipse.ui.internal.ide.misc.OverlayIcon;
024:
025: /**
026: * An IWorkbenchAdapter that represents IProject.
027: */
028: public class WorkbenchProject extends WorkbenchResource implements
029: IProjectActionFilter {
030: HashMap imageCache = new HashMap(11);
031:
032: /**
033: * Answer the appropriate base image to use for the passed resource, optionally
034: * considering the passed open status as well iff appropriate for the type of
035: * passed resource
036: */
037: protected ImageDescriptor getBaseImage(IResource resource) {
038: IProject project = (IProject) resource;
039: boolean isOpen = project.isOpen();
040: String baseKey = isOpen ? IDE.SharedImages.IMG_OBJ_PROJECT
041: : IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED;
042: if (isOpen) {
043: try {
044: String[] natureIds = project.getDescription()
045: .getNatureIds();
046: for (int i = 0; i < natureIds.length; ++i) {
047: // Have to use a cache because OverlayIcon does not define its own equality criteria,
048: // so WorkbenchLabelProvider would always create a new image otherwise.
049: String imageKey = natureIds[i];
050: ImageDescriptor overlayImage = (ImageDescriptor) imageCache
051: .get(imageKey);
052: if (overlayImage != null) {
053: return overlayImage;
054: }
055: ImageDescriptor natureImage = IDEWorkbenchPlugin
056: .getDefault().getProjectImageRegistry()
057: .getNatureImage(natureIds[i]);
058: if (natureImage != null) {
059: ImageDescriptor baseImage = IDEInternalWorkbenchImages
060: .getImageDescriptor(baseKey);
061: overlayImage = new OverlayIcon(
062: baseImage,
063: new ImageDescriptor[][] { { natureImage } },
064: new Point(16, 16));
065: imageCache.put(imageKey, overlayImage);
066: return overlayImage;
067: }
068: }
069: } catch (CoreException e) {
070: }
071: }
072: return IDEInternalWorkbenchImages.getImageDescriptor(baseKey);
073: }
074:
075: /**
076: * Returns the children of this container.
077: */
078: public Object[] getChildren(Object o) {
079: IProject project = (IProject) o;
080: if (project.isOpen()) {
081: try {
082: return project.members();
083: } catch (CoreException e) {
084: //don't get the children if there are problems with the project
085: }
086: }
087: return NO_CHILDREN;
088: }
089:
090: /**
091: * Returns whether the specific attribute matches the state of the target
092: * object.
093: *
094: * @param target the target object
095: * @param name the attribute name
096: * @param value the attriute value
097: * @return <code>true</code> if the attribute matches; <code>false</code> otherwise
098: */
099: public boolean testAttribute(Object target, String name,
100: String value) {
101: if (!(target instanceof IProject)) {
102: return false;
103: }
104: IProject proj = (IProject) target;
105: if (name.equals(NATURE)) {
106: try {
107: return proj.isAccessible() && proj.hasNature(value);
108: } catch (CoreException e) {
109: return false;
110: }
111: } else if (name.equals(OPEN)) {
112: value = value.toLowerCase();
113: return (proj.isOpen() == value.equals("true"));//$NON-NLS-1$
114: }
115: return super.testAttribute(target, name, value);
116: }
117: }
|