/**
   * Create or open the project in the given directory (or the directory containing the given file
   * if the file is not a directory).
   *
   * @param file the file or directory indicating which directory should be the root of the project
   * @param monitor the progress monitor used to provide feedback to the user, or <code>null</code>
   *     if no feedback is desired
   * @return the resource associated with the file that was passed in
   * @throws CoreException if the project cannot be opened or created
   */
  public static IResource createOrOpenProject(File file, IProgressMonitor monitor)
      throws CoreException {
    IResource[] existingResources = ResourceUtil.getResources(file);
    if (existingResources.length == 1) {
      return existingResources[0];
    } else if (existingResources.length > 1) {
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              DartCore.PLUGIN_ID,
              "Too many files representing " + file.getAbsolutePath()));
    }
    final File projectDirectory;
    if (file.isDirectory()) {
      projectDirectory = file;
    } else {
      projectDirectory = file.getParentFile();
    }
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    workspace.run(
        new IWorkspaceRunnable() {
          @Override
          public void run(IProgressMonitor monitor) throws CoreException {
            String projectName = projectDirectory.getName();
            IProject project = getProject(workspace, projectName);
            IProjectDescription description =
                createProjectDescription(project, projectDirectory.toURI());

            monitor.beginTask("Create project " + projectName, 300); // $NON-NLS-1$
            project.create(description, new SubProgressMonitor(monitor, 100));
            if (monitor.isCanceled()) {
              throw new OperationCanceledException();
            }
            project.open(IResource.NONE, new SubProgressMonitor(monitor, 100));
            if (monitor.isCanceled()) {
              throw new OperationCanceledException();
            }
            DartProjectNature nature = new DartProjectNature();
            nature.setProject(project);
            nature.configure();
            monitor.done();
          }
        },
        monitor);
    IResource[] newResources = ResourceUtil.getResources(file);
    if (newResources.length == 1) {
      return newResources[0];
    } else if (newResources.length == 0) {
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              DartCore.PLUGIN_ID,
              "No files representing " + file.getAbsolutePath()));
    }
    throw new CoreException(
        new Status(
            IStatus.ERROR,
            DartCore.PLUGIN_ID,
            "Too many files representing " + file.getAbsolutePath()));
  }