protected void changeToExistingLibrary(
      Shell shell, IPath path, boolean isNew, final IJavaProject project) {
    try {
      IClasspathEntry[] entries = project.getRawClasspath();
      int idx = indexOfClasspath(entries, path);
      if (idx == -1) {
        return;
      }
      IClasspathEntry[] res;
      if (isNew) {
        res = BuildPathDialogAccess.chooseContainerEntries(shell, project, entries);
        if (res == null) {
          return;
        }
      } else {
        IClasspathEntry resEntry =
            BuildPathDialogAccess.configureContainerEntry(shell, entries[idx], project, entries);
        if (resEntry == null) {
          return;
        }
        res = new IClasspathEntry[] {resEntry};
      }
      final IClasspathEntry[] newEntries = new IClasspathEntry[entries.length - 1 + res.length];
      System.arraycopy(entries, 0, newEntries, 0, idx);
      System.arraycopy(res, 0, newEntries, idx, res.length);
      System.arraycopy(entries, idx + 1, newEntries, idx + res.length, entries.length - idx - 1);

      IRunnableContext context = JavaPlugin.getActiveWorkbenchWindow();
      if (context == null) {
        context = PlatformUI.getWorkbench().getProgressService();
      }
      context.run(
          true,
          true,
          new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor)
                throws InvocationTargetException, InterruptedException {
              try {
                project.setRawClasspath(newEntries, project.getOutputLocation(), monitor);
              } catch (CoreException e) {
                throw new InvocationTargetException(e);
              }
            }
          });
    } catch (JavaModelException e) {
      String title = NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_title;
      String message =
          NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_creationfailed_message;
      ExceptionHandler.handle(e, shell, title, message);
    } catch (InvocationTargetException e) {
      String title = NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_title;
      String message =
          NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_applyingfailed_message;
      ExceptionHandler.handle(e, shell, title, message);
    } catch (InterruptedException e) {
      // user cancelled
    }
  }
 private IStructuredSelection evaluateCurrentSelection() {
   IWorkbenchWindow window = JavaPlugin.getActiveWorkbenchWindow();
   if (window != null) {
     ISelection selection = window.getSelectionService().getSelection();
     if (selection instanceof IStructuredSelection) {
       return (IStructuredSelection) selection;
     }
   }
   return StructuredSelection.EMPTY;
 }