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.misc;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.resources.IContainer;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.IWorkspace;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.jface.viewers.ITreeContentProvider;
021: import org.eclipse.jface.viewers.Viewer;
022:
023: /**
024: * Provides content for a tree viewer that shows only containers.
025: */
026: public class ContainerContentProvider implements ITreeContentProvider {
027: private boolean showClosedProjects = true;
028:
029: /**
030: * Creates a new ContainerContentProvider.
031: */
032: public ContainerContentProvider() {
033: }
034:
035: /**
036: * The visual part that is using this content provider is about
037: * to be disposed. Deallocate all allocated SWT resources.
038: */
039: public void dispose() {
040: }
041:
042: /*
043: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
044: */
045: public Object[] getChildren(Object element) {
046: if (element instanceof IWorkspace) {
047: // check if closed projects should be shown
048: IProject[] allProjects = ((IWorkspace) element).getRoot()
049: .getProjects();
050: if (showClosedProjects) {
051: return allProjects;
052: }
053:
054: ArrayList accessibleProjects = new ArrayList();
055: for (int i = 0; i < allProjects.length; i++) {
056: if (allProjects[i].isOpen()) {
057: accessibleProjects.add(allProjects[i]);
058: }
059: }
060: return accessibleProjects.toArray();
061: } else if (element instanceof IContainer) {
062: IContainer container = (IContainer) element;
063: if (container.isAccessible()) {
064: try {
065: List children = new ArrayList();
066: IResource[] members = container.members();
067: for (int i = 0; i < members.length; i++) {
068: if (members[i].getType() != IResource.FILE) {
069: children.add(members[i]);
070: }
071: }
072: return children.toArray();
073: } catch (CoreException e) {
074: // this should never happen because we call #isAccessible before invoking #members
075: }
076: }
077: }
078: return new Object[0];
079: }
080:
081: /*
082: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
083: */
084: public Object[] getElements(Object element) {
085: return getChildren(element);
086: }
087:
088: /*
089: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
090: */
091: public Object getParent(Object element) {
092: if (element instanceof IResource) {
093: return ((IResource) element).getParent();
094: }
095: return null;
096: }
097:
098: /*
099: * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
100: */
101: public boolean hasChildren(Object element) {
102: return getChildren(element).length > 0;
103: }
104:
105: /*
106: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged
107: */
108: public void inputChanged(Viewer viewer, Object oldInput,
109: Object newInput) {
110: }
111:
112: /**
113: * Specify whether or not to show closed projects in the tree
114: * viewer. Default is to show closed projects.
115: *
116: * @param show boolean if false, do not show closed projects in the tree
117: */
118: public void showClosedProjects(boolean show) {
119: showClosedProjects = show;
120: }
121:
122: }
|