1 11 package org.eclipse.jdt.internal.debug.ui.sourcelookup; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IWorkspaceRoot; 18 import org.eclipse.core.resources.ResourcesPlugin; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.MultiStatus; 21 import org.eclipse.debug.core.sourcelookup.ISourceContainer; 22 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector; 23 import org.eclipse.debug.ui.sourcelookup.AbstractSourceContainerBrowser; 24 import org.eclipse.jdt.core.IClasspathContainer; 25 import org.eclipse.jdt.core.IClasspathEntry; 26 import org.eclipse.jdt.core.IJavaModel; 27 import org.eclipse.jdt.core.IJavaProject; 28 import org.eclipse.jdt.core.IPackageFragmentRoot; 29 import org.eclipse.jdt.core.JavaCore; 30 import org.eclipse.jdt.core.JavaModelException; 31 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 32 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 33 import org.eclipse.jdt.internal.debug.ui.actions.ProjectSelectionDialog; 34 import org.eclipse.jdt.launching.sourcelookup.containers.ClasspathContainerSourceContainer; 35 import org.eclipse.jdt.launching.sourcelookup.containers.ClasspathVariableSourceContainer; 36 import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer; 37 import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer; 38 import org.eclipse.jdt.ui.JavaElementLabelProvider; 39 import org.eclipse.jface.viewers.ILabelProvider; 40 import org.eclipse.jface.viewers.IStructuredContentProvider; 41 import org.eclipse.jface.viewers.Viewer; 42 import org.eclipse.jface.window.Window; 43 import org.eclipse.swt.widgets.Shell; 44 45 50 public class JavaProjectSourceContainerBrowser extends AbstractSourceContainerBrowser { 51 52 class ContentProvider implements IStructuredContentProvider { 53 54 private List fProjects; 55 56 public ContentProvider(List projects) { 57 fProjects = projects; 58 } 59 60 63 public Object [] getElements(Object inputElement) { 64 return fProjects.toArray(); 65 } 66 67 70 public void dispose() { 71 } 72 73 76 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 77 } 78 79 } 80 83 public ISourceContainer[] addSourceContainers(Shell shell, ISourceLookupDirector director) { 84 List projects = getPossibleAdditions(director); 85 86 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 87 IStructuredContentProvider content = new ContentProvider(projects); 88 ProjectSelectionDialog dialog= new ProjectSelectionDialog(shell, projects, content, labelProvider, SourceLookupMessages.JavaProjectSourceContainerBrowser_0); 89 dialog.setTitle(SourceLookupMessages.JavaProjectSourceContainerBrowser_1); 90 MultiStatus status = new MultiStatus(JDIDebugUIPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, SourceLookupMessages.JavaProjectSourceContainerBrowser_2, null); 91 92 List sourceContainers = new ArrayList (); 93 if (dialog.open() == Window.OK) { 94 Object [] selections = dialog.getResult(); 95 List additions = new ArrayList (selections.length); 96 try { 97 for (int i = 0; i < selections.length; i++) { 98 IJavaProject jp = (IJavaProject)selections[i]; 99 if (dialog.isAddRequiredProjects()) { 100 collectRequiredProjects(jp, additions); 101 } else { 102 additions.add(jp); 103 } 104 } 105 } catch (JavaModelException e) { 106 status.add(e.getStatus()); 107 } 108 109 Iterator iter = additions.iterator(); 110 while (iter.hasNext()) { 111 IJavaProject jp = (IJavaProject)iter.next(); 112 sourceContainers.add(new JavaProjectSourceContainer(jp)); 113 if (dialog.isAddExportedEntries()) { 114 try { 115 collectExportedEntries(jp, sourceContainers); 116 } catch (CoreException e) { 117 status.add(e.getStatus()); 118 } 119 } 120 } 121 } 122 123 content.dispose(); 124 labelProvider.dispose(); 125 126 if (!status.isOK()) { 127 JDIDebugUIPlugin.statusDialog(status); 128 } 129 return (ISourceContainer[])sourceContainers.toArray(new ISourceContainer[sourceContainers.size()]); 130 } 131 132 137 protected List getPossibleAdditions(ISourceLookupDirector director) { 138 IJavaProject[] projects; 139 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 140 try { 141 projects= JavaCore.create(root).getJavaProjects(); 142 } catch (JavaModelException e) { 143 JDIDebugUIPlugin.log(e); 144 projects= new IJavaProject[0]; 145 } 146 List remaining = new ArrayList (); 147 for (int i = 0; i < projects.length; i++) { 148 remaining.add(projects[i]); 149 } 150 List alreadySelected = new ArrayList (); 151 ISourceContainer[] containers = director.getSourceContainers(); 152 for (int i = 0; i < containers.length; i++) { 153 ISourceContainer container = containers[i]; 154 if (container.getType().getId().equals(JavaProjectSourceContainer.TYPE_ID)) { 155 alreadySelected.add(((JavaProjectSourceContainer)container).getJavaProject()); 156 } 157 } 158 remaining.removeAll(alreadySelected); 159 return remaining; 160 } 161 162 170 protected void collectRequiredProjects(IJavaProject proj, List res) throws JavaModelException { 171 if (!res.contains(proj)) { 172 res.add(proj); 173 174 IJavaModel model= proj.getJavaModel(); 175 176 IClasspathEntry[] entries= proj.getRawClasspath(); 177 for (int i= 0; i < entries.length; i++) { 178 IClasspathEntry curr= entries[i]; 179 if (curr.getEntryKind() == IClasspathEntry.CPE_PROJECT) { 180 IJavaProject ref= model.getJavaProject(curr.getPath().segment(0)); 181 if (ref.exists()) { 182 collectRequiredProjects(ref, res); 183 } 184 } 185 } 186 } 187 } 188 189 197 protected void collectExportedEntries(IJavaProject proj, List list) throws CoreException { 198 IClasspathEntry[] entries = proj.getRawClasspath(); 199 for (int i = 0; i < entries.length; i++) { 200 IClasspathEntry entry = entries[i]; 201 ISourceContainer sourceContainer = null; 202 if (entry.isExported()) { 203 switch (entry.getEntryKind()) { 204 case IClasspathEntry.CPE_CONTAINER: 205 IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), proj); 206 sourceContainer = new ClasspathContainerSourceContainer(container.getPath()); 207 break; 208 case IClasspathEntry.CPE_LIBRARY: 209 IPackageFragmentRoot[] roots = proj.findPackageFragmentRoots(entry); 210 if (roots != null && roots.length > 0) { 211 sourceContainer = new PackageFragmentRootSourceContainer(roots[0]); 212 } 213 break; 214 case IClasspathEntry.CPE_PROJECT: 215 String name = entry.getPath().segment(0); 216 IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(name); 217 if (p.exists()) { 218 IJavaProject jp = JavaCore.create(p); 219 if (jp.exists()) { 220 sourceContainer = new JavaProjectSourceContainer(jp); 221 } 222 } 223 break; 224 case IClasspathEntry.CPE_VARIABLE: 225 sourceContainer = new ClasspathVariableSourceContainer(entry.getPath()); 226 break; 227 default: 228 break; 229 } 230 if (sourceContainer != null) { 231 if (!list.contains(sourceContainer)) { 232 list.add(sourceContainer); 233 } 234 } 235 } 236 } 237 } 238 } 239 | Popular Tags |