KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > browsing > ProjectAndSourceFolderContentProvider


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.jdt.internal.ui.browsing;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.jface.viewers.IStructuredSelection;
20
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IPackageFragmentRoot;
23 import org.eclipse.jdt.core.JavaModelException;
24
25 class ProjectAndSourceFolderContentProvider extends JavaBrowsingContentProvider {
26
27     ProjectAndSourceFolderContentProvider(JavaBrowsingPart browsingPart) {
28         super(false, browsingPart);
29     }
30
31     /* (non-Javadoc)
32      * Method declared on ITreeContentProvider.
33      */

34     public Object JavaDoc[] getChildren(Object JavaDoc element) {
35         if (!exists(element))
36             return NO_CHILDREN;
37
38         try {
39             startReadInDisplayThread();
40             if (element instanceof IStructuredSelection) {
41                 Assert.isLegal(false);
42                 Object JavaDoc[] result= new Object JavaDoc[0];
43                 Class JavaDoc clazz= null;
44                 Iterator JavaDoc iter= ((IStructuredSelection)element).iterator();
45                 while (iter.hasNext()) {
46                     Object JavaDoc item= iter.next();
47                     if (clazz == null)
48                         clazz= item.getClass();
49                     if (clazz == item.getClass())
50                         result= concatenate(result, getChildren(item));
51                     else
52                         return NO_CHILDREN;
53                 }
54                 return result;
55             }
56             if (element instanceof IStructuredSelection) {
57                 Assert.isLegal(false);
58                 Object JavaDoc[] result= new Object JavaDoc[0];
59                 Iterator JavaDoc iter= ((IStructuredSelection)element).iterator();
60                 while (iter.hasNext())
61                     result= concatenate(result, getChildren(iter.next()));
62                 return result;
63             }
64             if (element instanceof IJavaProject)
65                 return getPackageFragmentRoots((IJavaProject)element);
66             if (element instanceof IPackageFragmentRoot)
67                 return NO_CHILDREN;
68
69             return super.getChildren(element);
70
71         } catch (JavaModelException e) {
72             return NO_CHILDREN;
73         } finally {
74             finishedReadInDisplayThread();
75         }
76     }
77
78     protected Object JavaDoc[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
79         if (!project.getProject().isOpen())
80             return NO_CHILDREN;
81
82         IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
83         List JavaDoc list= new ArrayList JavaDoc(roots.length);
84         // filter out package fragments that correspond to projects and
85
// replace them with the package fragments directly
86
for (int i= 0; i < roots.length; i++) {
87             IPackageFragmentRoot root= roots[i];
88             if (!isProjectPackageFragmentRoot(root))
89                 list.add(root);
90         }
91         return list.toArray();
92     }
93
94     /*
95      *
96      * @see ITreeContentProvider
97      */

98     public boolean hasChildren(Object JavaDoc element) {
99         return element instanceof IJavaProject && super.hasChildren(element);
100     }
101 }
102
Popular Tags