1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.OperationCanceledException; 24 import org.eclipse.core.runtime.SubProgressMonitor; 25 26 import org.eclipse.core.resources.IContainer; 27 import org.eclipse.core.resources.IFolder; 28 import org.eclipse.core.resources.IProject; 29 import org.eclipse.core.resources.IResource; 30 import org.eclipse.core.resources.IWorkspaceRoot; 31 32 import org.eclipse.swt.widgets.Shell; 33 34 import org.eclipse.jface.dialogs.IDialogConstants; 35 import org.eclipse.jface.dialogs.MessageDialog; 36 import org.eclipse.jface.operation.IRunnableContext; 37 import org.eclipse.jface.operation.IRunnableWithProgress; 38 import org.eclipse.jface.viewers.IStructuredSelection; 39 import org.eclipse.jface.viewers.StructuredSelection; 40 41 import org.eclipse.ui.IWorkbenchSite; 42 import org.eclipse.ui.PlatformUI; 43 import org.eclipse.ui.part.ISetSelectionTarget; 44 45 import org.eclipse.jdt.core.IClasspathEntry; 46 import org.eclipse.jdt.core.IJavaElement; 47 import org.eclipse.jdt.core.IJavaProject; 48 import org.eclipse.jdt.core.IPackageFragment; 49 import org.eclipse.jdt.core.IPackageFragmentRoot; 50 import org.eclipse.jdt.core.JavaCore; 51 52 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; 53 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; 54 import org.eclipse.jdt.internal.corext.util.Messages; 55 56 import org.eclipse.jdt.internal.ui.JavaPlugin; 57 import org.eclipse.jdt.internal.ui.JavaPluginImages; 58 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 59 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 60 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage; 61 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathsBlock; 62 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 63 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.OutputFolderQuery; 64 65 public class AddFolderToBuildpathAction extends BuildpathModifierAction { 67 68 private final IRunnableContext fContext; 69 70 public AddFolderToBuildpathAction(IWorkbenchSite site) { 71 this(site, null, PlatformUI.getWorkbench().getProgressService()); 72 } 73 74 public AddFolderToBuildpathAction(IRunnableContext context, ISetSelectionTarget selectionTarget) { 75 this(null, selectionTarget, context); 76 } 77 78 private AddFolderToBuildpathAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) { 79 super(site, selectionTarget, BuildpathModifierAction.ADD_SEL_SF_TO_BP); 80 81 fContext= context; 82 83 setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddSelSFToCP_label); 84 setImageDescriptor(JavaPluginImages.DESC_ELCL_ADD_AS_SOURCE_FOLDER); 85 setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddSelSFToCP_tooltip); 86 } 87 88 91 public String getDetailedDescription() { 92 if (!isEnabled()) 93 return null; 94 95 if (getSelectedElements().size() != 1) 96 return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_toBuildpath; 97 98 Object obj= getSelectedElements().get(0); 99 if (obj instanceof IJavaProject) { 100 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ProjectToBuildpath, ((IJavaProject)obj).getElementName()); 101 } else if (obj instanceof IPackageFragment) { 102 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_PackageToBuildpath, ((IPackageFragment)obj).getElementName()); 103 } else if (obj instanceof IResource) { 104 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_FolderToBuildpath, ((IResource)obj).getName()); 105 } 106 107 return null; 108 } 109 110 113 public void run() { 114 115 try { 116 final IJavaProject project; 117 Object object= getSelectedElements().get(0); 118 if (object instanceof IJavaProject) { 119 project= (IJavaProject)object; 120 } else if (object instanceof IPackageFragment) { 121 project= ((IPackageFragment)object).getJavaProject(); 122 } else { 123 IFolder folder= (IFolder)object; 124 project= JavaCore.create(folder.getProject()); 125 if (project == null) 126 return; 127 } 128 129 final Shell shell= getShell(); 130 131 final boolean removeProjectFromClasspath; 132 IPath outputLocation= project.getOutputLocation(); 133 final IPath defaultOutputLocation= outputLocation.makeRelative(); 134 final IPath newDefaultOutputLocation; 135 final boolean removeOldClassFiles; 136 IPath projPath= project.getProject().getFullPath(); 137 if (!(getSelectedElements().size() == 1 && getSelectedElements().get(0) instanceof IJavaProject) && (outputLocation.equals(projPath) || defaultOutputLocation.segmentCount() == 1)) { 139 140 141 final OutputFolderQuery outputFolderQuery= ClasspathModifierQueries.getDefaultFolderQuery(shell, defaultOutputLocation); 142 if (outputFolderQuery.doQuery(true, ClasspathModifier.getValidator(getSelectedElements(), project), project)) { 143 newDefaultOutputLocation= outputFolderQuery.getOutputLocation(); 144 removeProjectFromClasspath= outputFolderQuery.removeProjectFromClasspath(); 145 146 if (BuildPathsBlock.hasClassfiles(project.getProject()) && outputLocation.equals(projPath)) { 147 String title= NewWizardMessages.BuildPathsBlock_RemoveBinariesDialog_title; 148 String message= Messages.format(NewWizardMessages.BuildPathsBlock_RemoveBinariesDialog_description, projPath.toString()); 149 MessageDialog dialog= new MessageDialog(shell, title, null, message, MessageDialog.QUESTION, new String [] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }, 0); 150 int answer= dialog.open(); 151 if (answer == 0) { 152 removeOldClassFiles= true; 153 } else if (answer == 1) { 154 removeOldClassFiles= false; 155 } else { 156 return; 157 } 158 } else { 159 removeOldClassFiles= false; 160 } 161 } else { 162 return; 163 } 164 } else { 165 removeProjectFromClasspath= false; 166 removeOldClassFiles= false; 167 newDefaultOutputLocation= defaultOutputLocation; 168 } 169 170 try { 171 final IRunnableWithProgress runnable= new IRunnableWithProgress() { 172 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 173 try { 174 List result= addToClasspath(getSelectedElements(), project, newDefaultOutputLocation.makeAbsolute(), removeProjectFromClasspath, removeOldClassFiles, monitor); 175 selectAndReveal(new StructuredSelection(result)); 176 } catch (CoreException e) { 177 throw new InvocationTargetException (e); 178 } 179 } 180 }; 181 fContext.run(false, false, runnable); 182 } catch (final InvocationTargetException e) { 183 if (e.getCause() instanceof CoreException) { 184 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.AddSourceFolderToBuildpathAction_ErrorTitle); 185 } else { 186 JavaPlugin.log(e); 187 } 188 } catch (final InterruptedException e) { 189 } 190 } catch (CoreException e) { 191 showExceptionDialog(e, NewWizardMessages.AddSourceFolderToBuildpathAction_ErrorTitle); 192 } 193 } 194 195 private List addToClasspath(List elements, IJavaProject project, IPath outputLocation, boolean removeProjectFromClasspath, boolean removeOldClassFiles, IProgressMonitor monitor) throws OperationCanceledException, CoreException { 196 if (!project.getProject().hasNature(JavaCore.NATURE_ID)) { 197 StatusInfo rootStatus= new StatusInfo(); 198 rootStatus.setError(NewWizardMessages.ClasspathModifier_Error_NoNatures); 199 throw new CoreException(rootStatus); 200 } 201 202 try { 203 monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_AddToBuildpath, elements.size() + 4); 204 IWorkspaceRoot workspaceRoot= JavaPlugin.getWorkspace().getRoot(); 205 206 if (removeOldClassFiles) { 207 IResource res= workspaceRoot.findMember(project.getOutputLocation()); 208 if (res instanceof IContainer && BuildPathsBlock.hasClassfiles(res)) { 209 BuildPathsBlock.removeOldClassfiles(res); 210 } 211 } 212 213 BuildpathDelta delta= new BuildpathDelta(getToolTipText()); 214 215 if (!project.getOutputLocation().equals(outputLocation)) { 216 project.setOutputLocation(outputLocation, new SubProgressMonitor(monitor, 1)); 217 delta.setDefaultOutputLocation(outputLocation); 218 } else { 219 monitor.worked(1); 220 } 221 222 List existingEntries= ClasspathModifier.getExistingEntries(project); 223 if (removeProjectFromClasspath) { 224 ClasspathModifier.removeFromClasspath(project, existingEntries, new SubProgressMonitor(monitor, 1)); 225 } else { 226 monitor.worked(1); 227 } 228 229 List newEntries= new ArrayList (); 230 for (int i= 0; i < elements.size(); i++) { 231 Object element= elements.get(i); 232 CPListElement entry; 233 if (element instanceof IResource) 234 entry= ClasspathModifier.addToClasspath((IResource) element, existingEntries, newEntries, project, new SubProgressMonitor(monitor, 1)); 235 else 236 entry= ClasspathModifier.addToClasspath((IJavaElement) element, existingEntries, newEntries, project, new SubProgressMonitor(monitor, 1)); 237 newEntries.add(entry); 238 } 239 240 Set modifiedSourceEntries= new HashSet (); 241 BuildPathBasePage.fixNestingConflicts((CPListElement[])newEntries.toArray(new CPListElement[newEntries.size()]), (CPListElement[])existingEntries.toArray(new CPListElement[existingEntries.size()]), modifiedSourceEntries); 242 243 ClasspathModifier.setNewEntry(existingEntries, newEntries, project, new SubProgressMonitor(monitor, 1)); 244 245 ClasspathModifier.commitClassPath(existingEntries, project, new SubProgressMonitor(monitor, 1)); 246 247 delta.setNewEntries((CPListElement[])existingEntries.toArray(new CPListElement[existingEntries.size()])); 248 informListeners(delta); 249 250 List result= new ArrayList (); 251 for (int i= 0; i < newEntries.size(); i++) { 252 IClasspathEntry entry= ((CPListElement) newEntries.get(i)).getClasspathEntry(); 253 IJavaElement root; 254 if (entry.getPath().equals(project.getPath())) 255 root= project; 256 else 257 root= project.findPackageFragmentRoot(entry.getPath()); 258 if (root != null) { 259 result.add(root); 260 } 261 } 262 263 return result; 264 } finally { 265 monitor.done(); 266 } 267 } 268 269 protected boolean canHandle(IStructuredSelection elements) { 270 if (elements.size() == 0) 271 return false; 272 try { 273 for (Iterator iter= elements.iterator(); iter.hasNext();) { 274 Object element= iter.next(); 275 if (element instanceof IJavaProject) { 276 if (ClasspathModifier.isSourceFolder((IJavaProject)element)) 277 return false; 278 } else if (element instanceof IPackageFragment) { 279 IPackageFragment fragment= (IPackageFragment)element; 280 if (ClasspathModifier.isDefaultFragment(fragment)) 281 return false; 282 283 if (((IPackageFragmentRoot)fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT)).isArchive()) 284 return false; 285 } else if (element instanceof IFolder) { 286 IProject project= ((IFolder)element).getProject(); 287 IJavaProject javaProject= JavaCore.create(project); 288 if (javaProject == null || !javaProject.exists()) 289 return false; 290 } else { 291 return false; 292 } 293 } 294 return true; 295 } catch (CoreException e) { 296 } 297 return false; 298 } 299 } 300 | Popular Tags |