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.IPath; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 22 import org.eclipse.swt.widgets.Shell; 23 24 import org.eclipse.jface.action.Action; 25 import org.eclipse.jface.operation.IRunnableWithProgress; 26 import org.eclipse.jface.resource.ImageDescriptor; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.IStructuredSelection; 29 import org.eclipse.jface.viewers.StructuredSelection; 30 31 import org.eclipse.ui.IWorkbenchPage; 32 import org.eclipse.ui.IWorkbenchPart; 33 import org.eclipse.ui.IWorkbenchPartReference; 34 import org.eclipse.ui.IWorkbenchSite; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.part.ISetSelectionTarget; 37 import org.eclipse.ui.texteditor.IUpdate; 38 39 import org.eclipse.jdt.core.IJavaProject; 40 import org.eclipse.jdt.core.JavaModelException; 41 42 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier; 43 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifierOperation; 44 import org.eclipse.jdt.internal.corext.buildpath.EditOutputFolderOperation; 45 import org.eclipse.jdt.internal.corext.buildpath.IClasspathInformationProvider; 46 import org.eclipse.jdt.internal.corext.util.Messages; 47 48 import org.eclipse.jdt.internal.ui.JavaPlugin; 49 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 50 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 51 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IAddArchivesQuery; 52 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IAddLibrariesQuery; 53 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IFolderCreationQuery; 54 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IInclusionExclusionQuery; 55 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.ILinkToQuery; 56 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IOutputLocationQuery; 57 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.IRemoveLinkedFolderQuery; 58 import org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage.ClasspathModifierQueries.OutputFolderQuery; 59 60 65 public class BuildPathAction extends Action implements IClasspathInformationProvider, IUpdate { 66 private ClasspathModifierOperation fOperation; 67 private IJavaProject fJavaProject; 68 private final BuildActionSelectionContext fContext; 69 private final IWorkbenchSite fSite; 70 private IStructuredSelection fCurrentSelection; 71 72 77 public BuildPathAction(IWorkbenchSite site, BuildActionSelectionContext context) { 78 super(); 79 fSite= site; 80 fContext= context; 81 fCurrentSelection= StructuredSelection.EMPTY; 82 } 83 84 private Shell getShell() { 85 return fSite.getShell(); 86 } 87 88 96 106 public void initialize(ClasspathModifierOperation operation, ImageDescriptor imageDescriptor, ImageDescriptor disabledImageDescriptor, String text, String tooltip) { 107 fOperation= operation; 108 setImageDescriptor(imageDescriptor); 109 setDisabledImageDescriptor(disabledImageDescriptor); 110 setText(text); 111 setToolTipText(tooltip); 112 } 113 114 117 public void run() { 118 try { 119 IRunnableWithProgress runnable= new IRunnableWithProgress() { 120 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 121 fOperation.run(monitor); 122 } 123 }; 124 PlatformUI.getWorkbench().getProgressService().run(true, false, runnable); 125 } catch (InvocationTargetException e) { 126 ExceptionHandler.handle(e, getShell(), Messages.format(NewWizardMessages.HintTextGroup_Exception_Title, 127 fOperation.getName()), e.getMessage()); 128 } catch (InterruptedException e) { 129 } 131 } 132 133 public void update() { 134 IStructuredSelection structSelection; 135 ISelection selection= fSite.getSelectionProvider().getSelection(); 136 if (selection instanceof IStructuredSelection) { 137 structSelection= (IStructuredSelection) selection; 138 } else { 139 structSelection= StructuredSelection.EMPTY; 140 } 141 142 fCurrentSelection= structSelection; 143 try { 144 fContext.init(structSelection); 145 146 fJavaProject= fContext.getJavaProject(); 147 setEnabled(fJavaProject != null && fOperation.isValid(fContext.getElements(), fContext.getTypes())); 148 149 } catch (JavaModelException e) { 150 JavaPlugin.log(e); 151 setEnabled(false); 152 } 153 } 154 155 158 public IStructuredSelection getSelection() { 159 return fCurrentSelection; 160 } 161 162 167 public IJavaProject getJavaProject() { 168 return fJavaProject; 169 } 170 171 179 public OutputFolderQuery getOutputFolderQuery() throws JavaModelException { 180 return ClasspathModifierQueries.getDefaultFolderQuery(getShell(), fJavaProject.getOutputLocation().makeRelative()); 181 } 182 183 189 public IInclusionExclusionQuery getInclusionExclusionQuery() { 190 return ClasspathModifierQueries.getDefaultInclusionExclusionQuery(getShell()); 191 } 192 193 200 public IOutputLocationQuery getOutputLocationQuery() throws JavaModelException { 201 List classpathEntries= ClasspathModifier.getExistingEntries(fJavaProject); 202 return ClasspathModifierQueries.getDefaultOutputLocationQuery(getShell(), fJavaProject.getOutputLocation().makeRelative(), classpathEntries); 203 } 204 205 211 public IFolderCreationQuery getFolderCreationQuery() { 212 IStructuredSelection selection= getSelection(); 213 return ClasspathModifierQueries.getDefaultFolderCreationQuery(getShell(), selection.getFirstElement()); 214 } 215 216 222 public ILinkToQuery getLinkFolderQuery() throws JavaModelException { 223 return ClasspathModifierQueries.getDefaultLinkQuery(getShell(), fJavaProject, fJavaProject.getOutputLocation().makeRelative()); 224 } 225 226 232 public IRemoveLinkedFolderQuery getRemoveLinkedFolderQuery() throws JavaModelException { 233 return ClasspathModifierQueries.getDefaultRemoveLinkedFolderQuery(getShell()); 234 } 235 236 242 public IAddArchivesQuery getExternalArchivesQuery() throws JavaModelException { 243 return ClasspathModifierQueries.getDefaultArchivesQuery(getShell()); 244 } 245 246 252 public IAddLibrariesQuery getLibrariesQuery() throws JavaModelException { 253 return ClasspathModifierQueries.getDefaultLibrariesQuery(getShell()); 254 } 255 256 262 public void deleteCreatedResources() { 263 } 265 266 270 public void handleResult(List resultElements, CoreException exception, int operationType) { 271 if (exception != null) { 272 ExceptionHandler.handle(exception, getShell(), Messages.format(NewWizardMessages.HintTextGroup_Exception_Title, 273 fOperation.getName()), exception.getMessage()); 274 return; 275 } 276 if (resultElements.size() == 0) 277 return; 279 IStructuredSelection selection; 280 if (fOperation instanceof EditOutputFolderOperation) 281 selection= (StructuredSelection) getSelection(); 282 else 283 selection= new StructuredSelection(resultElements); 284 selectAndReveal(selection); 285 } 286 287 293 private void selectAndReveal(final ISelection selection) { 294 IWorkbenchPage page= fSite.getPage(); 296 if (page == null) 297 return; 298 299 List parts= new ArrayList (); 301 IWorkbenchPartReference refs[]= page.getViewReferences(); 302 for (int i= 0; i < refs.length; i++) { 303 IWorkbenchPart part= refs[i].getPart(false); 304 if (part != null) 305 parts.add(part); 306 } 307 refs= page.getEditorReferences(); 308 for (int i= 0; i < refs.length; i++) { 309 if (refs[i].getPart(false) != null) 310 parts.add(refs[i].getPart(false)); 311 } 312 313 Iterator itr= parts.iterator(); 314 while (itr.hasNext()) { 315 IWorkbenchPart part= (IWorkbenchPart) itr.next(); 316 317 ISetSelectionTarget target= null; 319 if (part instanceof ISetSelectionTarget) 320 target= (ISetSelectionTarget) part; 321 else 322 target= (ISetSelectionTarget) part.getAdapter(ISetSelectionTarget.class); 323 324 if (target != null) { 325 final ISetSelectionTarget finalTarget= target; 327 page.getWorkbenchWindow().getShell().getDisplay().asyncExec(new Runnable () { 328 public void run() { 329 finalTarget.selectReveal(selection); 330 } 331 }); 332 } 333 } 334 } 335 336 337 } 338 | Popular Tags |