KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > misc > ContainerContentProvider


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.misc;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.jface.viewers.ITreeContentProvider;
22 import org.eclipse.jface.viewers.Viewer;
23
24 /**
25  * Provides content for a tree viewer that shows only containers.
26  */

27 public class ContainerContentProvider implements ITreeContentProvider {
28     private boolean showClosedProjects = true;
29
30     /**
31      * Creates a new ContainerContentProvider.
32      */

33     public ContainerContentProvider() {
34     }
35
36     /**
37      * The visual part that is using this content provider is about
38      * to be disposed. Deallocate all allocated SWT resources.
39      */

40     public void dispose() {
41     }
42
43     /*
44      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
45      */

46     public Object JavaDoc[] getChildren(Object JavaDoc element) {
47         if (element instanceof IWorkspace) {
48             // check if closed projects should be shown
49
IProject[] allProjects = ((IWorkspace) element).getRoot()
50                     .getProjects();
51             if (showClosedProjects) {
52                 return allProjects;
53             }
54
55             ArrayList JavaDoc accessibleProjects = new ArrayList JavaDoc();
56             for (int i = 0; i < allProjects.length; i++) {
57                 if (allProjects[i].isOpen()) {
58                     accessibleProjects.add(allProjects[i]);
59                 }
60             }
61             return accessibleProjects.toArray();
62         } else if (element instanceof IContainer) {
63             IContainer container = (IContainer) element;
64             if (container.isAccessible()) {
65                 try {
66                     List JavaDoc children = new ArrayList JavaDoc();
67                     IResource[] members = container.members();
68                     for (int i = 0; i < members.length; i++) {
69                         if (members[i].getType() != IResource.FILE) {
70                             children.add(members[i]);
71                         }
72                     }
73                     return children.toArray();
74                 } catch (CoreException e) {
75                     // this should never happen because we call #isAccessible before invoking #members
76
}
77             }
78         }
79         return new Object JavaDoc[0];
80     }
81
82     /*
83      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
84      */

85     public Object JavaDoc[] getElements(Object JavaDoc element) {
86         return getChildren(element);
87     }
88
89     /*
90      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
91      */

92     public Object JavaDoc getParent(Object JavaDoc element) {
93         if (element instanceof IResource) {
94             return ((IResource) element).getParent();
95         }
96         return null;
97     }
98
99     /*
100      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
101      */

102     public boolean hasChildren(Object JavaDoc element) {
103         return getChildren(element).length > 0;
104     }
105
106     /*
107      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged
108      */

109     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc 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 }
123
Popular Tags