1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.IWorkspaceRunnable; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IAdaptable; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.OperationCanceledException; 25 import org.eclipse.core.runtime.SubProgressMonitor; 26 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 29 import org.eclipse.ui.IWorkbenchSite; 30 import org.eclipse.ui.PlatformUI; 31 32 import org.eclipse.jdt.core.IClasspathEntry; 33 import org.eclipse.jdt.core.IJavaProject; 34 import org.eclipse.jdt.core.JavaCore; 35 import org.eclipse.jdt.core.JavaModelException; 36 37 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 39 import org.eclipse.jdt.internal.ui.JavaPlugin; 40 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 41 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; 42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 43 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter; 44 45 55 public class AddToClasspathAction extends SelectionDispatchAction { 56 57 64 public AddToClasspathAction(IWorkbenchSite site) { 65 super(site); 66 setText(ActionMessages.AddToClasspathAction_label); 67 setToolTipText(ActionMessages.AddToClasspathAction_toolTip); 68 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ADD_TO_CLASSPATH_ACTION); 69 } 70 71 74 public void selectionChanged(IStructuredSelection selection) { 75 try { 76 setEnabled(checkEnabled(selection)); 77 } catch (JavaModelException e) { 78 if (JavaModelUtil.isExceptionToBeLogged(e)) 80 JavaPlugin.log(e); 81 setEnabled(false); 82 } 83 } 84 85 private static boolean checkEnabled(IStructuredSelection selection) throws JavaModelException { 86 if (selection.isEmpty()) 87 return false; 88 for (Iterator iter= selection.iterator(); iter.hasNext();) { 89 if (! canBeAddedToBuildPath(iter.next())) 90 return false; 91 } 92 return true; 93 } 94 95 private static boolean canBeAddedToBuildPath(Object element) throws JavaModelException{ 96 return (element instanceof IAdaptable) && getCandidate((IAdaptable) element) != null; 97 } 98 99 private static IFile getCandidate(IAdaptable element) throws JavaModelException { 100 IResource resource= (IResource)element.getAdapter(IResource.class); 101 if (! (resource instanceof IFile) || ! ArchiveFileFilter.isArchivePath(resource.getFullPath())) 102 return null; 103 104 IJavaProject project= JavaCore.create(resource.getProject()); 105 if (project != null && project.exists() && (project.findPackageFragmentRoot(resource.getFullPath()) == null)) 106 return (IFile) resource; 107 return null; 108 } 109 110 113 public void run(IStructuredSelection selection) { 114 try { 115 final IFile[] files= getJARFiles(selection); 116 117 IWorkspaceRunnable operation= new IWorkspaceRunnable() { 118 public void run(IProgressMonitor monitor) throws CoreException { 119 monitor.beginTask(ActionMessages.AddToClasspathAction_progressMessage, files.length); 120 for (int i= 0; i < files.length; i++) { 121 monitor.subTask(files[i].getFullPath().toString()); 122 IJavaProject project= JavaCore.create(files[i].getProject()); 123 addToClassPath(project, files[i].getFullPath(), new SubProgressMonitor(monitor, 1)); 124 } 125 } 126 127 private void addToClassPath(IJavaProject project, IPath jarPath, IProgressMonitor monitor) throws JavaModelException { 128 if (monitor.isCanceled()) 129 throw new OperationCanceledException(); 130 IClasspathEntry[] entries= project.getRawClasspath(); 131 IClasspathEntry[] newEntries= new IClasspathEntry[entries.length + 1]; 132 System.arraycopy(entries, 0, newEntries, 0, entries.length); 133 newEntries[entries.length]= JavaCore.newLibraryEntry(jarPath, null, null, false); 134 project.setRawClasspath(newEntries, monitor); 135 } 136 }; 137 138 PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, new WorkbenchRunnableAdapter(operation)); 139 } catch (InvocationTargetException e) { 140 ExceptionHandler.handle(e, getShell(), 141 ActionMessages.AddToClasspathAction_error_title, 142 ActionMessages.AddToClasspathAction_error_message); 143 } catch (JavaModelException e) { 144 ExceptionHandler.handle(e, getShell(), 145 ActionMessages.AddToClasspathAction_error_title, 146 ActionMessages.AddToClasspathAction_error_message); 147 } catch (InterruptedException e) { 148 } 150 151 } 152 153 private static IFile[] getJARFiles(IStructuredSelection selection) throws JavaModelException { 154 ArrayList list= new ArrayList (); 155 for (Iterator iter= selection.iterator(); iter.hasNext();) { 156 Object element= iter.next(); 157 if (element instanceof IAdaptable) { 158 IFile file= getCandidate((IAdaptable) element); 159 if (file != null) { 160 list.add(file); 161 } 162 } 163 } 164 return (IFile[]) list.toArray(new IFile[list.size()]); 165 } 166 } 167 168 | Popular Tags |