1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 18 import org.eclipse.jdt.core.IClasspathEntry; 19 import org.eclipse.jdt.core.IJavaProject; 20 import org.eclipse.jdt.core.JavaModelException; 21 22 import org.eclipse.swt.widgets.Shell; 23 24 import org.eclipse.jface.action.IAction; 25 import org.eclipse.jface.operation.IRunnableContext; 26 import org.eclipse.jface.operation.IRunnableWithProgress; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.IStructuredSelection; 29 30 import org.eclipse.ui.IObjectActionDelegate; 31 import org.eclipse.ui.IWorkbenchPart; 32 import org.eclipse.ui.PlatformUI; 33 34 import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer; 35 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 36 37 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess; 38 39 43 public class ConfigureContainerAction implements IObjectActionDelegate { 44 45 private ISelection fCurrentSelection; 46 private IWorkbenchPart fPart; 47 48 51 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 52 fPart= targetPart; 53 } 54 55 58 public void run(IAction action) { 59 if (fCurrentSelection instanceof IStructuredSelection) { 60 ClassPathContainer container= (ClassPathContainer) ((IStructuredSelection) fCurrentSelection).getFirstElement(); 61 openWizard(container.getClasspathEntry(), container.getLabel(), container.getJavaProject()); 62 } 63 } 64 65 private void openWizard(IClasspathEntry entry, String label, final IJavaProject project) { 66 Shell shell= fPart.getSite().getShell(); 67 try { 68 IClasspathEntry[] entries= project.getRawClasspath(); 69 70 IClasspathEntry result= BuildPathDialogAccess.configureContainerEntry(shell, entry, project, entries); 71 if (result == null || result.equals(entry)) { 72 return; } 74 75 int idx= indexInClasspath(entries, entry); 76 if (idx == -1) { 77 return; 78 } 79 80 final IClasspathEntry[] newEntries= new IClasspathEntry[entries.length]; 81 System.arraycopy(entries, 0, newEntries, 0, entries.length); 82 newEntries[idx]= result; 83 84 IRunnableContext context= fPart.getSite().getWorkbenchWindow(); 85 if (context == null) { 86 context= PlatformUI.getWorkbench().getProgressService(); 87 } 88 context.run(true, true, new IRunnableWithProgress() { 89 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 90 try { 91 project.setRawClasspath(newEntries, project.getOutputLocation(), monitor); 92 } catch (CoreException e) { 93 throw new InvocationTargetException (e); 94 } 95 } 96 }); 97 } catch (JavaModelException e) { 98 String title= ActionMessages.ConfigureContainerAction_error_title; 99 String message= ActionMessages.ConfigureContainerAction_error_creationfailed_message; 100 ExceptionHandler.handle(e, shell, title, message); 101 } catch (InvocationTargetException e) { 102 String title= ActionMessages.ConfigureContainerAction_error_title; 103 String message= ActionMessages.ConfigureContainerAction_error_applyingfailed_message; 104 ExceptionHandler.handle(e, shell, title, message); 105 } catch (InterruptedException e) { 106 } 108 } 109 110 protected static int indexInClasspath(IClasspathEntry[] entries, IClasspathEntry entry) { 111 for (int i= 0; i < entries.length; i++) { 112 if (entries[i] == entry) { 113 return i; 114 } 115 } 116 return -1; 117 } 118 119 122 public void selectionChanged(IAction action, ISelection selection) { 123 fCurrentSelection= selection; 124 } 125 126 } 127 | Popular Tags |