1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IWorkspaceRoot; 21 import org.eclipse.core.resources.ResourcesPlugin; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.MultiStatus; 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.JavaCore; 29 import org.eclipse.jdt.core.JavaModelException; 30 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 31 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 32 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer; 33 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 34 import org.eclipse.jdt.launching.JavaRuntime; 35 import org.eclipse.jdt.ui.JavaElementLabelProvider; 36 import org.eclipse.jface.viewers.ILabelProvider; 37 import org.eclipse.jface.viewers.IStructuredContentProvider; 38 import org.eclipse.jface.viewers.IStructuredSelection; 39 import org.eclipse.jface.viewers.Viewer; 40 import org.eclipse.jface.window.Window; 41 42 45 public class AddProjectAction extends RuntimeClasspathAction { 46 47 class ContentProvider implements IStructuredContentProvider { 48 49 private List fProjects; 50 51 public ContentProvider(List projects) { 52 fProjects = projects; 53 } 54 55 58 public Object [] getElements(Object inputElement) { 59 return fProjects.toArray(); 60 } 61 62 65 public void dispose() { 66 } 67 68 71 public void inputChanged( 72 Viewer viewer, 73 Object oldInput, 74 Object newInput) { 75 } 76 77 } 78 79 public AddProjectAction(IClasspathViewer viewer) { 80 super(ActionMessages.AddProjectAction_Add_Project_1, viewer); 81 } 82 83 88 public void run() { 89 List projects = getPossibleAdditions(); 90 91 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 92 IStructuredContentProvider content = new ContentProvider(projects); 93 ProjectSelectionDialog dialog= new ProjectSelectionDialog(getShell(),projects, content, labelProvider, ActionMessages.AddProjectAction_Choose__project_s__to_add__3); 94 dialog.setTitle(ActionMessages.AddProjectAction_Project_Selection_2); 95 MultiStatus status = new MultiStatus(JDIDebugUIPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, ActionMessages.AddProjectAction_One_or_more_exceptions_occurred_while_adding_projects__1, null); 96 97 if (dialog.open() == Window.OK) { 98 Object [] selections = dialog.getResult(); 99 100 List additions = new ArrayList (selections.length); 101 try { 102 for (int i = 0; i < selections.length; i++) { 103 IJavaProject jp = (IJavaProject)selections[i]; 104 if (dialog.isAddRequiredProjects()) { 105 collectRequiredProjects(jp, additions); 106 } else { 107 additions.add(jp); 108 } 109 } 110 } catch (JavaModelException e) { 111 status.add(e.getStatus()); 112 } 113 114 List runtimeEntries = new ArrayList (additions.size()); 115 Iterator iter = additions.iterator(); 116 while (iter.hasNext()) { 117 IJavaProject jp = (IJavaProject)iter.next(); 118 runtimeEntries.add(JavaRuntime.newProjectRuntimeClasspathEntry(jp)); 119 if (dialog.isAddExportedEntries()) { 120 try { 121 collectExportedEntries(jp, runtimeEntries); 122 } catch (CoreException e) { 123 status.add(e.getStatus()); 124 } 125 } 126 } 127 IRuntimeClasspathEntry[] entries = (IRuntimeClasspathEntry[])runtimeEntries.toArray(new IRuntimeClasspathEntry[runtimeEntries.size()]); 128 getViewer().addEntries(entries); 129 } 130 131 content.dispose(); 132 labelProvider.dispose(); 133 134 if (!status.isOK()) { 135 JDIDebugUIPlugin.statusDialog(status); 136 } 137 } 138 139 142 protected boolean updateSelection(IStructuredSelection selection) { 143 return getViewer().updateSelection(getActionType(), selection) && !getPossibleAdditions().isEmpty(); 144 } 145 146 protected int getActionType() { 147 return ADD; 148 } 149 150 153 protected List getPossibleAdditions() { 154 IJavaProject[] projects; 155 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 156 try { 157 projects= JavaCore.create(root).getJavaProjects(); 158 } catch (JavaModelException e) { 159 JDIDebugUIPlugin.log(e); 160 projects= new IJavaProject[0]; 161 } 162 List remaining = new ArrayList (); 163 for (int i = 0; i < projects.length; i++) { 164 remaining.add(projects[i]); 165 } 166 List alreadySelected = new ArrayList (); 167 IRuntimeClasspathEntry[] entries = getViewer().getEntries(); 168 for (int i = 0; i < entries.length; i++) { 169 if (entries[i].getType() == IRuntimeClasspathEntry.PROJECT) { 170 IResource res = root.findMember(entries[i].getPath()); 171 IJavaProject jp = (IJavaProject)JavaCore.create(res); 172 alreadySelected.add(jp); 173 } 174 } 175 remaining.removeAll(alreadySelected); 176 return remaining; 177 } 178 179 187 protected void collectRequiredProjects(IJavaProject proj, List res) throws JavaModelException { 188 if (!res.contains(proj)) { 189 res.add(proj); 190 191 IJavaModel model= proj.getJavaModel(); 192 193 IClasspathEntry[] entries= proj.getRawClasspath(); 194 for (int i= 0; i < entries.length; i++) { 195 IClasspathEntry curr= entries[i]; 196 if (curr.getEntryKind() == IClasspathEntry.CPE_PROJECT) { 197 IJavaProject ref= model.getJavaProject(curr.getPath().segment(0)); 198 if (ref.exists()) { 199 collectRequiredProjects(ref, res); 200 } 201 } 202 } 203 } 204 } 205 206 214 protected void collectExportedEntries(IJavaProject proj, List runtimeEntries) throws CoreException { 215 IClasspathEntry[] entries = proj.getRawClasspath(); 216 for (int i = 0; i < entries.length; i++) { 217 IClasspathEntry entry = entries[i]; 218 if (entry.isExported()) { 219 IRuntimeClasspathEntry rte = null; 220 switch (entry.getEntryKind()) { 221 case IClasspathEntry.CPE_CONTAINER: 222 IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), proj); 223 int kind = 0; 224 switch (container.getKind()) { 225 case IClasspathContainer.K_APPLICATION: 226 kind = IRuntimeClasspathEntry.USER_CLASSES; 227 break; 228 case IClasspathContainer.K_SYSTEM: 229 kind = IRuntimeClasspathEntry.BOOTSTRAP_CLASSES; 230 break; 231 case IClasspathContainer.K_DEFAULT_SYSTEM: 232 kind = IRuntimeClasspathEntry.STANDARD_CLASSES; 233 break; 234 } 235 rte = JavaRuntime.newRuntimeContainerClasspathEntry(entry.getPath(), kind, proj); 236 break; 237 case IClasspathEntry.CPE_LIBRARY: 238 rte = JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath()); 239 rte.setSourceAttachmentPath(entry.getSourceAttachmentPath()); 240 rte.setSourceAttachmentRootPath(entry.getSourceAttachmentRootPath()); 241 break; 242 case IClasspathEntry.CPE_PROJECT: 243 String name = entry.getPath().segment(0); 244 IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(name); 245 if (p.exists()) { 246 IJavaProject jp = JavaCore.create(p); 247 if (jp.exists()) { 248 rte = JavaRuntime.newProjectRuntimeClasspathEntry(jp); 249 } 250 } 251 break; 252 case IClasspathEntry.CPE_VARIABLE: 253 rte = JavaRuntime.newVariableRuntimeClasspathEntry(entry.getPath()); 254 break; 255 default: 256 break; 257 } 258 if (rte != null) { 259 if (!runtimeEntries.contains(rte)) { 260 runtimeEntries.add(rte); 261 } 262 } 263 } 264 } 265 } 266 } 267 | Popular Tags |