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.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 22 import org.eclipse.core.resources.IFile; 23 import org.eclipse.core.resources.IResource; 24 25 import org.eclipse.jface.operation.IRunnableContext; 26 import org.eclipse.jface.operation.IRunnableWithProgress; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 30 import org.eclipse.ui.IWorkbenchSite; 31 import org.eclipse.ui.PlatformUI; 32 import org.eclipse.ui.part.ISetSelectionTarget; 33 34 import org.eclipse.jdt.core.IClasspathEntry; 35 import org.eclipse.jdt.core.IJavaElement; 36 import org.eclipse.jdt.core.IJavaProject; 37 import org.eclipse.jdt.core.JavaCore; 38 import org.eclipse.jdt.core.JavaModelException; 39 40 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta; 41 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; 42 import org.eclipse.jdt.internal.corext.util.Messages; 43 44 import org.eclipse.jdt.internal.ui.JavaPlugin; 45 import org.eclipse.jdt.internal.ui.JavaPluginImages; 46 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 47 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 48 49 public class AddSelectedLibraryToBuildpathAction extends BuildpathModifierAction { 51 52 private final IRunnableContext fContext; 53 54 public AddSelectedLibraryToBuildpathAction(IWorkbenchSite site) { 55 this(site, null, PlatformUI.getWorkbench().getProgressService()); 56 } 57 58 public AddSelectedLibraryToBuildpathAction(IRunnableContext context, ISetSelectionTarget selectionTarget) { 59 this(null, selectionTarget, context); 60 } 61 62 private AddSelectedLibraryToBuildpathAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) { 63 super(site, selectionTarget, BuildpathModifierAction.ADD_SEL_LIB_TO_BP); 64 65 fContext= context; 66 67 setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddSelLibToCP_label); 68 setImageDescriptor(JavaPluginImages.DESC_OBJS_EXTJAR); 69 setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddSelLibToCP_tooltip); 70 } 71 72 75 public String getDetailedDescription() { 76 if (!isEnabled()) 77 return null; 78 79 IFile file= (IFile)getSelectedElements().get(0); 80 IJavaProject project= JavaCore.create(file.getProject()); 81 82 try { 83 if (ClasspathModifier.isArchive(file, project)) { 84 String name= ClasspathModifier.escapeSpecialChars(file.getName()); 85 return Messages.format(NewWizardMessages.PackageExplorerActionGroup_FormText_ArchiveToBuildpath, name); 86 } 87 } catch (JavaModelException e) { 88 JavaPlugin.log(e); 89 } 90 91 return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_toBuildpath; 92 } 93 94 97 public void run() { 98 try { 99 final IFile[] files= (IFile[])getSelectedElements().toArray(new IFile[getSelectedElements().size()]); 100 101 final IRunnableWithProgress runnable= new IRunnableWithProgress() { 102 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 103 try { 104 IJavaProject project= JavaCore.create(files[0].getProject()); 105 List result= addLibraryEntries(files, project, monitor); 106 selectAndReveal(new StructuredSelection(result)); 107 } catch (CoreException e) { 108 throw new InvocationTargetException (e); 109 } 110 } 111 }; 112 fContext.run(false, false, runnable); 113 } catch (final InvocationTargetException e) { 114 if (e.getCause() instanceof CoreException) { 115 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.AddSelectedLibraryToBuildpathAction_ErrorTitle); 116 } else { 117 JavaPlugin.log(e); 118 } 119 } catch (final InterruptedException e) { 120 } 121 } 122 123 private List addLibraryEntries(IFile[] resources, IJavaProject project, IProgressMonitor monitor) throws CoreException { 124 List addedEntries= new ArrayList (); 125 try { 126 monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_AddToBuildpath, 4); 127 for (int i= 0; i < resources.length; i++) { 128 IResource res= resources[i]; 129 addedEntries.add(new CPListElement(project, IClasspathEntry.CPE_LIBRARY, res.getFullPath(), res)); 130 } 131 monitor.worked(1); 132 133 List existingEntries= ClasspathModifier.getExistingEntries(project); 134 ClasspathModifier.setNewEntry(existingEntries, addedEntries, project, new SubProgressMonitor(monitor, 1)); 135 ClasspathModifier.commitClassPath(existingEntries, project, new SubProgressMonitor(monitor, 1)); 136 137 BuildpathDelta delta= new BuildpathDelta(getToolTipText()); 138 delta.setNewEntries((CPListElement[])existingEntries.toArray(new CPListElement[existingEntries.size()])); 139 informListeners(delta); 140 141 List result= new ArrayList (addedEntries.size()); 142 for (int i= 0; i < resources.length; i++) { 143 IResource res= resources[i]; 144 IJavaElement elem= project.getPackageFragmentRoot(res); 145 if (elem != null) { 146 result.add(elem); 147 } 148 } 149 150 monitor.worked(1); 151 return result; 152 } finally { 153 monitor.done(); 154 } 155 } 156 157 protected boolean canHandle(IStructuredSelection elements) { 158 if (elements.size() == 0) 159 return false; 160 161 try { 162 for (Iterator iter= elements.iterator(); iter.hasNext();) { 163 Object element= iter.next(); 164 if (element instanceof IFile) { 165 IFile file= (IFile)element; 166 IJavaProject project= JavaCore.create(file.getProject()); 167 if (project == null) 168 return false; 169 170 if (!ClasspathModifier.isArchive(file, project)) 171 return false; 172 } else { 173 return false; 174 } 175 } 176 } catch (CoreException e) { 177 return false; 178 } 179 return true; 180 } 181 } 182 | Popular Tags |