1 11 package org.eclipse.jdt.ui.wizards; 12 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.Path; 18 19 import org.eclipse.core.resources.IContainer; 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IFolder; 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IWorkspaceRoot; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.widgets.FileDialog; 29 import org.eclipse.swt.widgets.Shell; 30 31 import org.eclipse.jface.window.Window; 32 33 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 34 import org.eclipse.ui.model.WorkbenchContentProvider; 35 import org.eclipse.ui.model.WorkbenchLabelProvider; 36 37 import org.eclipse.ui.views.navigator.ResourceComparator; 38 39 import org.eclipse.jdt.core.IClasspathEntry; 40 import org.eclipse.jdt.core.IJavaProject; 41 42 import org.eclipse.jdt.ui.JavaUI; 43 44 import org.eclipse.jdt.internal.ui.IUIConstants; 45 import org.eclipse.jdt.internal.ui.JavaPlugin; 46 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 47 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator; 48 import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter; 49 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter; 50 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement; 51 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ClasspathContainerWizard; 52 import org.eclipse.jdt.internal.ui.wizards.buildpaths.EditVariableEntryDialog; 53 import org.eclipse.jdt.internal.ui.wizards.buildpaths.JavadocLocationDialog; 54 import org.eclipse.jdt.internal.ui.wizards.buildpaths.MultipleFolderSelectionDialog; 55 import org.eclipse.jdt.internal.ui.wizards.buildpaths.NewVariableEntryDialog; 56 import org.eclipse.jdt.internal.ui.wizards.buildpaths.SourceAttachmentDialog; 57 58 75 public final class BuildPathDialogAccess { 76 77 private BuildPathDialogAccess() { 78 } 80 81 93 public static IClasspathEntry configureSourceAttachment(Shell shell, IClasspathEntry initialEntry) { 94 if (initialEntry == null) { 95 throw new IllegalArgumentException (); 96 } 97 int entryKind= initialEntry.getEntryKind(); 98 if (entryKind != IClasspathEntry.CPE_LIBRARY && entryKind != IClasspathEntry.CPE_VARIABLE) { 99 throw new IllegalArgumentException (); 100 } 101 102 SourceAttachmentDialog dialog= new SourceAttachmentDialog(shell, initialEntry); 103 if (dialog.open() == Window.OK) { 104 return dialog.getResult(); 105 } 106 return null; 107 } 108 109 124 public static URL [] configureJavadocLocation(Shell shell, String libraryName, URL initialURL) { 125 if (libraryName == null) { 126 throw new IllegalArgumentException (); 127 } 128 129 JavadocLocationDialog dialog= new JavadocLocationDialog(shell, libraryName, initialURL); 130 if (dialog.open() == Window.OK) { 131 return new URL [] { dialog.getResult() }; 132 } 133 return null; 134 } 135 136 150 public static IClasspathEntry configureJavadocLocation(Shell shell, IClasspathEntry initialEntry) { 151 if (initialEntry == null) { 152 throw new IllegalArgumentException (); 153 } 154 int entryKind= initialEntry.getEntryKind(); 155 if (entryKind != IClasspathEntry.CPE_LIBRARY && entryKind != IClasspathEntry.CPE_VARIABLE) { 156 throw new IllegalArgumentException (); 157 } 158 159 URL location= JavaUI.getLibraryJavadocLocation(initialEntry); 160 JavadocLocationDialog dialog= new JavadocLocationDialog(shell, initialEntry.getPath().toString(), location); 161 if (dialog.open() == Window.OK) { 162 CPListElement element= CPListElement.createFromExisting(initialEntry, null); 163 URL res= dialog.getResult(); 164 element.setAttribute(CPListElement.JAVADOC, res != null ? res.toExternalForm() : null); 165 return element.getClasspathEntry(); 166 } 167 return null; 168 } 169 170 184 public static IPath configureVariableEntry(Shell shell, IPath initialEntryPath, IPath[] existingPaths) { 185 if (existingPaths == null) { 186 throw new IllegalArgumentException (); 187 } 188 189 EditVariableEntryDialog dialog= new EditVariableEntryDialog(shell, initialEntryPath, existingPaths); 190 if (dialog.open() == Window.OK) { 191 return dialog.getPath(); 192 } 193 return null; 194 } 195 196 208 public static IPath[] chooseVariableEntries(Shell shell, IPath[] existingPaths) { 209 if (existingPaths == null) { 210 throw new IllegalArgumentException (); 211 } 212 NewVariableEntryDialog dialog= new NewVariableEntryDialog(shell); 213 if (dialog.open() == Window.OK) { 214 return dialog.getResult(); 215 } 216 return null; 217 } 218 219 235 public static IClasspathEntry configureContainerEntry(Shell shell, IClasspathEntry initialEntry, IJavaProject project, IClasspathEntry[] currentClasspath) { 236 if (initialEntry == null || currentClasspath == null) { 237 throw new IllegalArgumentException (); 238 } 239 240 ClasspathContainerWizard wizard= new ClasspathContainerWizard(initialEntry, project, currentClasspath); 241 if (ClasspathContainerWizard.openWizard(shell, wizard) == Window.OK) { 242 IClasspathEntry[] created= wizard.getNewEntries(); 243 if (created != null && created.length == 1) { 244 return created[0]; 245 } 246 } 247 return null; 248 } 249 250 266 public static IClasspathEntry[] chooseContainerEntries(Shell shell, IJavaProject project, IClasspathEntry[] currentClasspath) { 267 if (currentClasspath == null) { 268 throw new IllegalArgumentException (); 269 } 270 271 ClasspathContainerWizard wizard= new ClasspathContainerWizard((IClasspathEntry) null, project, currentClasspath); 272 if (ClasspathContainerWizard.openWizard(shell, wizard) == Window.OK) { 273 return wizard.getNewEntries(); 274 } 275 return null; 276 } 277 278 279 291 public static IPath configureJAREntry(Shell shell, IPath initialEntry, IPath[] usedEntries) { 292 if (initialEntry == null || usedEntries == null) { 293 throw new IllegalArgumentException (); 294 } 295 296 Class [] acceptedClasses= new Class [] { IFile.class }; 297 TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, false); 298 299 ArrayList usedJars= new ArrayList (usedEntries.length); 300 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 301 for (int i= 0; i < usedEntries.length; i++) { 302 IPath curr= usedEntries[i]; 303 if (!curr.equals(initialEntry)) { 304 IResource resource= root.findMember(usedEntries[i]); 305 if (resource instanceof IFile) { 306 usedJars.add(resource); 307 } 308 } 309 } 310 311 IResource existing= root.findMember(initialEntry); 312 313 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider()); 314 dialog.setValidator(validator); 315 dialog.setTitle(NewWizardMessages.BuildPathDialogAccess_JARArchiveDialog_edit_title); 316 dialog.setMessage(NewWizardMessages.BuildPathDialogAccess_JARArchiveDialog_edit_description); 317 dialog.addFilter(new ArchiveFileFilter(usedJars, true)); 318 dialog.setInput(root); 319 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME)); 320 dialog.setInitialSelection(existing); 321 322 if (dialog.open() == Window.OK) { 323 IResource element= (IResource) dialog.getFirstResult(); 324 return element.getFullPath(); 325 } 326 return null; 327 } 328 329 341 public static IPath[] chooseJAREntries(Shell shell, IPath initialSelection, IPath[] usedEntries) { 342 if (usedEntries == null) { 343 throw new IllegalArgumentException (); 344 } 345 346 Class [] acceptedClasses= new Class [] { IFile.class }; 347 TypedElementSelectionValidator validator= new TypedElementSelectionValidator(acceptedClasses, true); 348 ArrayList usedJars= new ArrayList (usedEntries.length); 349 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 350 for (int i= 0; i < usedEntries.length; i++) { 351 IResource resource= root.findMember(usedEntries[i]); 352 if (resource instanceof IFile) { 353 usedJars.add(resource); 354 } 355 } 356 IResource focus= initialSelection != null ? root.findMember(initialSelection) : null; 357 358 ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider()); 359 dialog.setHelpAvailable(false); 360 dialog.setValidator(validator); 361 dialog.setTitle(NewWizardMessages.BuildPathDialogAccess_JARArchiveDialog_new_title); 362 dialog.setMessage(NewWizardMessages.BuildPathDialogAccess_JARArchiveDialog_new_description); 363 dialog.addFilter(new ArchiveFileFilter(usedJars, true)); 364 dialog.setInput(root); 365 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME)); 366 dialog.setInitialSelection(focus); 367 368 if (dialog.open() == Window.OK) { 369 Object [] elements= dialog.getResult(); 370 IPath[] res= new IPath[elements.length]; 371 for (int i= 0; i < res.length; i++) { 372 IResource elem= (IResource)elements[i]; 373 res[i]= elem.getFullPath(); 374 } 375 return res; 376 } 377 return null; 378 } 379 380 390 public static IPath configureExternalJAREntry(Shell shell, IPath initialEntry) { 391 if (initialEntry == null) { 392 throw new IllegalArgumentException (); 393 } 394 395 String lastUsedPath= initialEntry.removeLastSegments(1).toOSString(); 396 397 FileDialog dialog= new FileDialog(shell, SWT.SINGLE); 398 dialog.setText(NewWizardMessages.BuildPathDialogAccess_ExtJARArchiveDialog_edit_title); 399 dialog.setFilterExtensions(ArchiveFileFilter.FILTER_EXTENSIONS); 400 dialog.setFilterPath(lastUsedPath); 401 dialog.setFileName(initialEntry.lastSegment()); 402 403 String res= dialog.open(); 404 if (res == null) { 405 return null; 406 } 407 JavaPlugin.getDefault().getDialogSettings().put(IUIConstants.DIALOGSTORE_LASTEXTJAR, dialog.getFilterPath()); 408 409 return Path.fromOSString(res).makeAbsolute(); 410 } 411 412 421 public static IPath[] chooseExternalJAREntries(Shell shell) { 422 String lastUsedPath= JavaPlugin.getDefault().getDialogSettings().get(IUIConstants.DIALOGSTORE_LASTEXTJAR); 423 if (lastUsedPath == null) { 424 lastUsedPath= ""; } 426 FileDialog dialog= new FileDialog(shell, SWT.MULTI); 427 dialog.setText(NewWizardMessages.BuildPathDialogAccess_ExtJARArchiveDialog_new_title); 428 dialog.setFilterExtensions(ArchiveFileFilter.FILTER_EXTENSIONS); 429 dialog.setFilterPath(lastUsedPath); 430 431 String res= dialog.open(); 432 if (res == null) { 433 return null; 434 } 435 String [] fileNames= dialog.getFileNames(); 436 int nChosen= fileNames.length; 437 438 IPath filterPath= Path.fromOSString(dialog.getFilterPath()); 439 IPath[] elems= new IPath[nChosen]; 440 for (int i= 0; i < nChosen; i++) { 441 elems[i]= filterPath.append(fileNames[i]).makeAbsolute(); 442 } 443 JavaPlugin.getDefault().getDialogSettings().put(IUIConstants.DIALOGSTORE_LASTEXTJAR, dialog.getFilterPath()); 444 445 return elems; 446 } 447 448 460 public static IPath[] chooseClassFolderEntries(Shell shell, IPath initialSelection, IPath[] usedEntries) { 461 if (usedEntries == null) { 462 throw new IllegalArgumentException (); 463 } 464 String title= NewWizardMessages.BuildPathDialogAccess_ExistingClassFolderDialog_new_title; 465 String message= NewWizardMessages.BuildPathDialogAccess_ExistingClassFolderDialog_new_description; 466 return internalChooseFolderEntry(shell, initialSelection, usedEntries, title, message); 467 } 468 469 481 public static IPath[] chooseSourceFolderEntries(Shell shell, IPath initialSelection, IPath[] usedEntries) { 482 if (usedEntries == null) { 483 throw new IllegalArgumentException (); 484 } 485 String title= NewWizardMessages.BuildPathDialogAccess_ExistingSourceFolderDialog_new_title; 486 String message= NewWizardMessages.BuildPathDialogAccess_ExistingSourceFolderDialog_new_description; 487 return internalChooseFolderEntry(shell, initialSelection, usedEntries, title, message); 488 } 489 490 491 private static IPath[] internalChooseFolderEntry(Shell shell, IPath initialSelection, IPath[] usedEntries, String title, String message) { 492 Class [] acceptedClasses= new Class [] { IProject.class, IFolder.class }; 493 494 ArrayList usedContainers= new ArrayList (usedEntries.length); 495 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 496 for (int i= 0; i < usedEntries.length; i++) { 497 IResource resource= root.findMember(usedEntries[i]); 498 if (resource instanceof IContainer) { 499 usedContainers.add(resource); 500 } 501 } 502 503 IResource focus= initialSelection != null ? root.findMember(initialSelection) : null; 504 Object [] used= usedContainers.toArray(); 505 506 MultipleFolderSelectionDialog dialog= new MultipleFolderSelectionDialog(shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider()); 507 dialog.setExisting(used); 508 dialog.setTitle(title); 509 dialog.setMessage(message); 510 dialog.setHelpAvailable(false); 511 dialog.addFilter(new TypedViewerFilter(acceptedClasses, used)); 512 dialog.setInput(root); 513 dialog.setInitialFocus(focus); 514 515 if (dialog.open() == Window.OK) { 516 Object [] elements= dialog.getResult(); 517 IPath[] res= new IPath[elements.length]; 518 for (int i= 0; i < res.length; i++) { 519 IResource elem= (IResource) elements[i]; 520 res[i]= elem.getFullPath(); 521 } 522 return res; 523 } 524 return null; 525 } 526 } 527 | Popular Tags |