Java Code Examples for org.eclipse.core.resources.IContainer#getLocationURI()

The following examples show how to use org.eclipse.core.resources.IContainer#getLocationURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JDTUtilsTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testIsFolder() throws Exception {
	IProject project = WorkspaceHelper.getProject(ProjectsManager.DEFAULT_PROJECT_NAME);

	// Creating test folders and file using 'java.io.*' API (from the outside of the workspace)
	File dir = new File(project.getLocation().toString(), "/src/org/eclipse/testIsFolder");
	dir.mkdirs();
	File file = new File(dir, "Test.java");
	file.createNewFile();

	// Accessing test folders and file using 'org.eclipse.core.resources.*' API (from inside of the workspace)
	IContainer iFolder = project.getFolder("/src/org/eclipse/testIsFolder");
	URI uriFolder = iFolder.getLocationURI();
	IFile iFile = project.getFile("/src/org/eclipse/testIsFolder/Test.java");
	URI uriFile = iFile.getLocationURI();
	assertTrue(JDTUtils.isFolder(uriFolder.toString()));
	assertEquals(JDTUtils.getFileOrFolder(uriFolder.toString()).getType(), IResource.FOLDER);
	assertEquals(JDTUtils.getFileOrFolder(uriFile.toString()).getType(), IResource.FILE);
	assertFalse(JDTUtils.isFolder(uriFile.toString()));
	assertNotNull(JDTUtils.findFile(uriFile.toString()));
	assertNotNull(JDTUtils.findFolder(uriFolder.toString()));
}
 
Example 2
Source File: JarWriter3.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates the directory entries for the given path and writes it to the
 * current archive.
 *
 * @param resource
 *            the resource for which the parent directories are to be added
 * @param destinationPath
 *            the path to add
 *
 * @throws IOException
 *             if an I/O error has occurred
 * @throws CoreException
 *             if accessing the resource failes
 */
protected void addDirectories(IResource resource, IPath destinationPath) throws IOException, CoreException {
	IContainer parent= null;
	String path= destinationPath.toString().replace(File.separatorChar, '/');
	int lastSlash= path.lastIndexOf('/');
	List<JarEntry> directories= new ArrayList<JarEntry>(2);
	while (lastSlash != -1) {
		path= path.substring(0, lastSlash + 1);
		if (!fDirectories.add(path))
			break;

		parent= resource.getParent();
		long timeStamp= System.currentTimeMillis();
		URI location= parent.getLocationURI();
		if (location != null) {
			IFileInfo info= EFS.getStore(location).fetchInfo();
			if (info.exists())
				timeStamp= info.getLastModified();
		}

		JarEntry newEntry= new JarEntry(path);
		newEntry.setMethod(ZipEntry.STORED);
		newEntry.setSize(0);
		newEntry.setCrc(0);
		newEntry.setTime(timeStamp);
		directories.add(newEntry);

		lastSlash= path.lastIndexOf('/', lastSlash - 1);
	}

	for (int i= directories.size() - 1; i >= 0; --i) {
		fJarOutputStream.putNextEntry(directories.get(i));
	}
}
 
Example 3
Source File: AddSourceFolderWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static StatusInfo validatePathName(String str, IContainer parent) {
	StatusInfo result= new StatusInfo();
	result.setOK();

	IPath parentPath= parent.getFullPath();

	if (str.length() == 0) {
		result.setError(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_error_EnterRootName, BasicElementLabels.getPathLabel(parentPath, false)));
		return result;
	}

	IPath path= parentPath.append(str);

	IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
	IStatus validate= workspaceRoot.getWorkspace().validatePath(path.toString(), IResource.FOLDER);
	if (validate.matches(IStatus.ERROR)) {
		result.setError(Messages.format(NewWizardMessages.NewSourceFolderWizardPage_error_InvalidRootName, validate.getMessage()));
		return result;
	}

	IResource res= workspaceRoot.findMember(path);
	if (res != null) {
		if (res.getType() != IResource.FOLDER) {
			result.setError(NewWizardMessages.NewSourceFolderWizardPage_error_NotAFolder);
			return result;
		}
	} else {

		URI parentLocation= parent.getLocationURI();
		if (parentLocation != null) {
			try {
				IFileStore store= EFS.getStore(parentLocation).getChild(str);
				if (store.fetchInfo().exists()) {
					result.setError(NewWizardMessages.NewSourceFolderWizardPage_error_AlreadyExistingDifferentCase);
					return result;
				}
			} catch (CoreException e) {
				// we couldn't create the file store. Ignore the exception
				// since we can't check if the file exist. Pretend that it
				// doesn't.
			}
		}
	}

	return result;
}
 
Example 4
Source File: CopyFilesAndFoldersOperation.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Checks whether the destination is valid for copying the source resources.
 * <p>
 * Note this method is for internal use only. It is not API.
 * </p>
 *
 * @param destination
 *            the destination container
 * @param sourceResources
 *            the source resources
 * @return an error message, or <code>null</code> if the path is valid
 */
public String validateDestination(IContainer destination, IResource[] sourceResources) {
    if (!isAccessible(destination)) {
        return IDEWorkbenchMessages.CopyFilesAndFoldersOperation_destinationAccessError;
    }
    IContainer firstParent = null;
    URI destinationLocation = destination.getLocationURI();
    for (int i = 0; i < sourceResources.length; i++) {
        IResource sourceResource = sourceResources[i];
        if (firstParent == null) {
            firstParent = sourceResource.getParent();
        } else if (firstParent.equals(sourceResource.getParent()) == false) {
            // Resources must have common parent. Fixes bug 33398.
            return IDEWorkbenchMessages.CopyFilesAndFoldersOperation_parentNotEqual;
        }

        URI sourceLocation = sourceResource.getLocationURI();
        if (sourceLocation == null) {
            if (sourceResource.isLinked()) {
                // Don't allow copying linked resources with undefined path
                // variables. See bug 28754.
                return NLS.bind(IDEWorkbenchMessages.CopyFilesAndFoldersOperation_missingPathVariable,
                        sourceResource.getName());
            }
            return NLS.bind(IDEWorkbenchMessages.CopyFilesAndFoldersOperation_resourceDeleted,
                    sourceResource.getName());

        }
        if (sourceLocation.equals(destinationLocation)) {
            return NLS.bind(IDEWorkbenchMessages.CopyFilesAndFoldersOperation_sameSourceAndDest,
                    sourceResource.getName());
        }
        // is the source a parent of the destination?
        if (new Path(sourceLocation.toString()).isPrefixOf(new Path(destinationLocation.toString()))) {
            return IDEWorkbenchMessages.CopyFilesAndFoldersOperation_destinationDescendentError;
        }

        String linkedResourceMessage = validateLinkedResource(destination, sourceResource);
        if (linkedResourceMessage != null) {
            return linkedResourceMessage;
        }
    }
    return null;
}