/**
   * Returns the link target location entered by the user.
   *
   * @return the link target location entered by the user. null if the user chose not to create a
   *     link.
   */
  public URI getLinkTargetURI() {
    if (!createLink) {
      return null;
    }
    // resolve path variable if we have a relative path
    if (!linkTarget.startsWith("/")) { // $NON-NLS-1$
      IPathVariableManager pathVariableManager =
          RemoteResourcesPlugin.getWorkspace().getPathVariableManager();
      try {

        URI path = new URI(linkTarget.replace(java.io.File.separatorChar, '/'));
        URI resolved = pathVariableManager.resolveURI(path);
        if (path != resolved) {
          // we know this is a path variable, but return unresolved
          // path so resource will be created with variable intact
          return path;
        }
      } catch (URISyntaxException e) {
        // link target is not a valid URI. Fall through to handle this
        // below
      }
    }

    FileSystemConfiguration configuration = getSelectedConfiguration();
    if (configuration == null) {
      return URIUtil.toURI(linkTarget);
    }
    // validate non-local file system location
    return configuration.getContributor().getURI(linkTarget);
  }
  private synchronized void checkFile() {
    if (fActiveEditor == null) return;
    IEditorInput input = fActiveEditor.getEditorInput();
    IPath location = null;

    if (input instanceof IFileEditorInput) {
      IFile file = ((IFileEditorInput) input).getFile();
      location = file.getLocation();
    } else if (input instanceof FileStoreEditorInput) {
      location = URIUtil.toPath(((FileStoreEditorInput) input).getURI());
    } else if (input instanceof CommonSourceNotFoundEditorInput) {
      Object artifact = ((CommonSourceNotFoundEditorInput) input).getArtifact();
      if (artifact instanceof CSourceNotFoundElement) {
        location = new Path(((CSourceNotFoundElement) artifact).getFile());
      }
    }

    if (location != null
        && fExpectedFile != null
        && location.lastSegment().equals(fExpectedFile.lastSegment())) {
      fFileFound = true;

      if (fActiveEditor instanceof ITextEditor) {
        IDocumentProvider docProvider = ((ITextEditor) fActiveEditor).getDocumentProvider();
        fAnnotationModel = docProvider.getAnnotationModel(fActiveEditor.getEditorInput());
        fAnnotationModel.addAnnotationModelListener(this);

        checkAnnotations();
      } else if (fActiveEditor instanceof CommonSourceNotFoundEditor) {
        // No annotation will be painted if source not found.
        fAnnotationFound = true;
      }
    }
  }
  private static boolean equalUris(PackageLibraryManager manager, URI firstUri, URI secondUri) {
    if (firstUri == null) {
      return secondUri == null;
    } else if (secondUri == null) {
      return false;
    }
    String firstScheme = firstUri.getScheme();
    String secondScheme = secondUri.getScheme();
    if (firstScheme == null || firstScheme.equals("file")) {
      if (secondScheme != null && !secondScheme.equals("file")) {
        return false;
      }

      String firstPath = firstUri.getPath();
      String secondPath = secondUri.getPath();

      if (firstPath.equals(secondPath)) {
        return true;
      }

      // handle relative uris
      // TODO(devoncarew): we should look into whether using relative URIs is a good idea

      String currentPath = new File(".").toURI().normalize().getPath();

      if (firstPath.endsWith(secondPath)) {
        if (firstPath.equals(currentPath + secondPath)) {
          return true;
        }
      }

      if (secondPath.endsWith(firstPath)) {
        if (secondPath.equals(currentPath + firstPath)) {
          return true;
        }
      }

      return false;
    } else if (secondScheme == null || !firstScheme.equals(secondScheme)) {
      return false;
    } else if (PackageLibraryManager.isDartUri(firstUri)
        || PackageLibraryManager.isDartUri(secondUri)) {
      return URIUtilities.safelyResolveDartUri(firstUri)
          .equals(URIUtilities.safelyResolveDartUri(secondUri));
    }
    return URIUtil.toPath(firstUri).equals(URIUtil.toPath(secondUri));
  }
  public static ISourceModule resolveSourceModule(FileStoreEditorInput input) {
    final ISourceModule[] modules = new ISourceModule[1];
    final IPath filePath = URIUtil.toPath(input.getURI());
    IScriptModel scriptModel = DLTKCore.create(ResourcesPlugin.getWorkspace().getRoot());
    try {
      scriptModel.accept(
          new IModelElementVisitor() {

            public boolean visit(IModelElement element) {
              boolean shouldDescend = (modules[0] == null);

              if (shouldDescend == true) {
                if (element instanceof ExternalProjectFragment) {
                  ExternalProjectFragment fragment = (ExternalProjectFragment) element;

                  try {
                    if (filePath
                            .removeLastSegments(1)
                            .toFile()
                            .getCanonicalPath()
                            .startsWith(fragment.getPath().toFile().getCanonicalPath())
                        == true) {
                      IPath folderPath =
                          new Path(filePath.removeLastSegments(1).toFile().getCanonicalPath());
                      folderPath =
                          folderPath.removeFirstSegments(
                              new Path(fragment.getPath().toFile().getCanonicalPath())
                                  .segmentCount());
                      IScriptFolder folder = fragment.getScriptFolder(folderPath);
                      if ((folder != null) && (folder.exists() == true)) {
                        ISourceModule module = folder.getSourceModule(filePath.lastSegment());
                        if (module != null) {
                          modules[0] = module;
                        }
                      }
                    }
                  } catch (IOException ixcn) {
                    ixcn.printStackTrace();
                  }

                  shouldDescend = false;
                } else {
                  shouldDescend =
                      ((element instanceof IScriptProject) || (element instanceof IScriptModel));
                }
              }

              return shouldDescend;
            }
          });
    } catch (ModelException mxcn) {
      mxcn.printStackTrace();
    }

    return modules[0];
  }
 protected void doFSSetUp() throws Exception {
   repositoryPath = getRandomLocation();
   URI uri = URIUtil.toURI(repositoryPath); // encoded
   StringBuffer sb = new StringBuffer();
   sb.append(GitFileSystem.SCHEME_GIT);
   sb.append("://test/");
   sb.append(uri.toString());
   sb.append("?/");
   baseStore = EFS.getStore(URI.create(sb.toString()));
   baseStore.mkdir(EFS.NONE, null);
 }
 /**
  * Returns the URI for the specific editor input.
  *
  * @param input the editor input
  * @return the URI, or null if none could be determined
  */
 public static URI getURI(IEditorInput input) {
   if (input instanceof IFileEditorInput) {
     return ((IFileEditorInput) input).getFile().getLocationURI();
   }
   if (input instanceof IURIEditorInput) {
     return ((IURIEditorInput) input).getURI();
   }
   if (input instanceof IPathEditorInput) {
     return URIUtil.toURI(((IPathEditorInput) input).getPath());
   }
   return null;
 }
  public void testVirtualFolderInLinkedFolder() {
    // setup handles
    IFolder topFolder = existingProject.getFolder("topFolder");
    IFolder linkedFolder = topFolder.getFolder("linkedFolder");
    IFolder subFolder = linkedFolder.getFolder("subFolder");
    IFolder virtualFolder = subFolder.getFolder("virtualFolder");

    IPath linkedFolderLocation = getRandomLocation();
    IPath subFolderLocation = linkedFolderLocation.append(subFolder.getName());

    try {
      try {
        // create the structure on disk
        linkedFolderLocation.toFile().mkdir();
        subFolderLocation.toFile().mkdir();

        // create the structure in the workspace
        ensureExistsInWorkspace(topFolder, true);
        linkedFolder.createLink(linkedFolderLocation, IResource.NONE, getMonitor());
        virtualFolder.create(IResource.VIRTUAL, true, getMonitor());
      } catch (CoreException e) {
        fail("1.0", e);
      }

      // assert locations
      assertEquals("2.0", linkedFolderLocation, linkedFolder.getLocation());
      assertEquals(
          "3.0", linkedFolderLocation.append(subFolder.getName()), subFolder.getLocation());
      assertTrue("4.0", virtualFolder.isVirtual());
      assertTrue("5.0", virtualFolder.getLocation() == null);

      // assert URIs
      assertEquals("6.0", URIUtil.toURI(linkedFolderLocation), linkedFolder.getLocationURI());
      assertEquals("7.0", URIUtil.toURI(subFolderLocation), subFolder.getLocationURI());
      // assertTrue("8.0", virtualFolder.getLocationURI() == null);
    } finally {
      Workspace.clear(subFolderLocation.toFile());
      Workspace.clear(linkedFolderLocation.toFile());
    }
  }
 protected IProject getProject() {
   URI root = _index.getRoot();
   IPath containerPath = org.eclipse.core.filesystem.URIUtil.toPath(root);
   if (containerPath == null) {
     return null;
   }
   IContainer container =
       ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(containerPath);
   if (container == null) {
     return null;
   }
   return container.getProject();
 }
 protected void addBindings(final DatabindingSupport db) {
   final IFileStore current = fTool.getWorkspaceData().getWorkspaceDir();
   String dir = ""; // $NON-NLS-1$
   if (current != null) {
     final IPath path = URIUtil.toPath(current.toURI());
     if (path != null) {
       dir = path.toOSString();
     }
   }
   fNewLocationString = new WritableValue(dir, String.class);
   db.getContext()
       .bindValue(
           fLocationGroup.getObservable(),
           fNewLocationString,
           new UpdateValueStrategy().setAfterGetValidator(fLocationGroup.getValidator()),
           null);
 }
  private ISourceContainer getArchiveSourceContainer(String location) throws JavaModelException {
    IWorkspaceRoot root = PDELaunchingPlugin.getWorkspace().getRoot();
    IFile[] containers = root.findFilesForLocationURI(URIUtil.toURI(location));
    for (int i = 0; i < containers.length; i++) {
      IJavaElement element = JavaCore.create(containers[i]);
      if (element instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot archive = (IPackageFragmentRoot) element;
        IPath path = archive.getSourceAttachmentPath();
        if (path == null || path.segmentCount() == 0) continue;

        IPath rootPath = archive.getSourceAttachmentRootPath();
        boolean detectRootPath = rootPath != null && rootPath.segmentCount() > 0;

        IFile archiveFile = root.getFile(path);
        if (archiveFile.exists()) return new ArchiveSourceContainer(archiveFile, detectRootPath);

        File file = path.toFile();
        if (file.exists())
          return new ExternalArchiveSourceContainer(file.getAbsolutePath(), detectRootPath);
      }
    }
    return null;
  }
 /**
  * Returns the resource being edited in the given workbench part.
  *
  * @param part Workbench part to checm.
  * @return Resource being edited.
  */
 protected static IResource getResource(IWorkbenchPart part) {
   IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
   if (part instanceof IEditorPart) {
     IEditorInput editorInput = ((IEditorPart) part).getEditorInput();
     IResource resource = null;
     if (editorInput instanceof IFileEditorInput) {
       resource = ((IFileEditorInput) editorInput).getFile();
     } else if (editorInput instanceof ExternalEditorInput) {
       resource = ((ExternalEditorInput) editorInput).getMarkerResource();
     }
     if (resource != null) return resource;
     /* This file is not in a project, let default case handle it */
     ILocationProvider provider =
         (ILocationProvider) editorInput.getAdapter(ILocationProvider.class);
     if (provider != null) {
       IPath location = provider.getPath(editorInput);
       if (location != null) {
         IFile[] files = root.findFilesForLocationURI(URIUtil.toURI(location));
         if (files.length > 0 && files[0].isAccessible()) return files[0];
       }
     }
   }
   return root;
 }
 /**
  * Returns the current project location URI as entered by the user, or <code>null</code> if a
  * valid project location has not been entered.
  *
  * @return the project location URI, or <code>null</code>
  */
 public URI getLocationURI() {
   String path = getLocationPath().toOSString();
   return URIUtil.toURI(path);
 }
 public static URI getTmpURIPath(final String fileName) {
   return URIUtil.toURI(getTmpPath(fileName).toPortableString());
 }
 /**
  * Sets the project location of the new project or <code>null</code> if the project should be
  * created in the workspace
  *
  * @param uri the new project location
  */
 public void setProjectLocationURI(URI uri) {
   IPath path = uri != null ? URIUtil.toPath(uri) : null;
   fLocationGroup.setLocation(path);
 }
 /**
  * Returns the current project location path as entered by the user, or <code>null</code> if the
  * project should be created in the workspace.
  *
  * @return the project location path or its anticipated initial value.
  */
 public URI getProjectLocationURI() {
   if (fLocationGroup.isLocationInWorkspace()) {
     return null;
   }
   return URIUtil.toURI(fLocationGroup.getLocation());
 }
 /**
  * Returns the current project location path as entered by the user, or <code>null</code> if the
  * project should be created in the workspace. Note that the last segment of the path is the
  * project name.
  *
  * @return the project location path or its anticipated initial value.
  */
 public URI getProjectLocationURI() {
   if (fLocationGroup.isDefaultLocation()) {
     return null;
   }
   return URIUtil.toURI(fLocationGroup.getLocation());
 }