KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javadocexport > JavadocProjectContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.javadocexport;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.resources.IWorkspaceRoot;
16 import org.eclipse.core.resources.ResourcesPlugin;
17
18 import org.eclipse.jface.viewers.ITreeContentProvider;
19 import org.eclipse.jface.viewers.Viewer;
20
21 import org.eclipse.jdt.core.IJavaElement;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IPackageFragment;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.JavaModelException;
27
28 import org.eclipse.jdt.internal.ui.JavaPlugin;
29
30 public class JavadocProjectContentProvider implements ITreeContentProvider {
31
32     /*
33      * @see ITreeContentProvider#getChildren(Object)
34      */

35     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
36         try {
37             if (parentElement instanceof IJavaProject) {
38                 IJavaProject project= (IJavaProject) parentElement;
39                 return getPackageFragmentRoots(project);
40             } else if (parentElement instanceof IPackageFragmentRoot) {
41                 return getPackageFragments((IPackageFragmentRoot) parentElement);
42             }
43         } catch (JavaModelException e) {
44             JavaPlugin.log(e);
45         }
46         return new Object JavaDoc[0];
47     }
48     /*
49      * @see IStructuredContentProvider#getElements(Object)
50      */

51     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
52         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
53         try {
54             return JavaCore.create(root).getJavaProjects();
55         } catch (JavaModelException e) {
56             JavaPlugin.log(e);
57         }
58         return new Object JavaDoc[0];
59     }
60
61     /*
62      * @see ITreeContentProvider#getParent(Object)
63      */

64     public Object JavaDoc getParent(Object JavaDoc element) {
65         
66         IJavaElement parent= ((IJavaElement)element).getParent();
67         if (parent instanceof IPackageFragmentRoot) {
68             IPackageFragmentRoot root= (IPackageFragmentRoot) parent;
69             if (root.getPath().equals(root.getJavaProject().getProject().getFullPath())) {
70                 return root.getJavaProject();
71             }
72         }
73         return parent;
74     }
75
76     /*
77      * @see ITreeContentProvider#hasChildren(Object)
78      */

79     public boolean hasChildren(Object JavaDoc element) {
80         return (getChildren(element).length > 0);
81     }
82
83     /*
84      * @see IContentProvider#dispose()
85      */

86     public void dispose() {
87     }
88
89     /*
90      * @see IContentProvider#inputChanged(Viewer, Object, Object)
91      */

92     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
93     }
94
95     private Object JavaDoc[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
96         ArrayList JavaDoc result= new ArrayList JavaDoc();
97
98         IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
99         for (int i= 0; i < roots.length; i++) {
100             IPackageFragmentRoot root= roots[i];
101             if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
102                 if (root.getPath().equals(root.getJavaProject().getPath())) {
103                     Object JavaDoc[] packageFragments= getPackageFragments(root);
104                     for (int k= 0; k < packageFragments.length; k++) {
105                         result.add(packageFragments[k]);
106                     }
107                 } else {
108                     result.add(root);
109                 }
110             }
111         }
112         return result.toArray();
113     }
114
115     private Object JavaDoc[] getPackageFragments(IPackageFragmentRoot root) throws JavaModelException {
116         ArrayList JavaDoc packageFragments= new ArrayList JavaDoc();
117
118         IJavaElement[] children= root.getChildren();
119         for (int i= 0; i < children.length; i++) {
120             if (((IPackageFragment) children[i]).containsJavaResources())
121                 packageFragments.add(children[i]);
122         }
123         return packageFragments.toArray();
124     }
125 }
126
Popular Tags