Java Code Examples for org.eclipse.core.resources.IWorkspaceRoot#getFileForLocation()

The following examples show how to use org.eclipse.core.resources.IWorkspaceRoot#getFileForLocation() . 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: File2Resource.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * get the IResource corresponding to the given file. Given file does not
 * need to exist.
 * 
 * @param file
 * @param isDirectory
 *            if true, an IContainer will be returned, otherwise an IFile
 *            will be returned
 * @return
 */
public static IResource getResource(File file, boolean isDirectory) {
	if (file == null) return null;
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot workspaceRoot = workspace.getRoot();

    IPath pathEclipse = new Path(file.getAbsolutePath());

    IResource resource = null;

    if (isDirectory) {
        resource = workspaceRoot.getContainerForLocation(pathEclipse);
    } else {
        resource = workspaceRoot.getFileForLocation(pathEclipse);
    }
    return resource;
}
 
Example 2
Source File: URIUtils.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return a {@link IFile} for the given absolute file {@link URI} */
static public IFile convertFileUriToPlatformFile(org.eclipse.emf.common.util.URI fileUri) {
	if (fileUri.isFile()) {
		String fileString = fileUri.toFileString();
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IPath iPath = org.eclipse.core.runtime.Path.fromOSString(fileString);
		if (iPath != null) {
			IFile iFile = root.getFileForLocation(iPath);
			return iFile;
		}
	}
	return null;
}
 
Example 3
Source File: OrganizeImportsCommand.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public Object organizeImports(List<Object> arguments) throws CoreException {
	WorkspaceEdit edit = new WorkspaceEdit();
	if (arguments != null && !arguments.isEmpty() && arguments.get(0) instanceof String) {
		final String fileUri = (String) arguments.get(0);
		final IPath rootPath = ResourceUtils.filePathFromURI(fileUri);
		if (rootPath == null) {
			throw new CoreException(new Status(IStatus.ERROR, IConstants.PLUGIN_ID, "URI is not found"));
		}
		final IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
		IResource resource = wsroot.getFileForLocation(rootPath);
		if (resource == null) {
			resource = wsroot.getContainerForLocation(rootPath);
		}
		if (resource != null) {
			final OrganizeImportsCommand command = new OrganizeImportsCommand();
			int type = resource.getType();
			switch (type) {
				case IResource.PROJECT:
					edit = command.organizeImportsInProject(resource.getAdapter(IProject.class));
					break;
				case IResource.FOLDER:
					edit = command.organizeImportsInDirectory(fileUri, resource.getProject());
					break;
				case IResource.FILE:
					edit = command.organizeImportsInFile(fileUri);
					break;
				default://This can only be IResource.ROOT. Which is not relevant to jdt.ls
					// do nothing allow to return the empty WorkspaceEdit.
					break;
			}
		}
	}
	return edit;
}
 
Example 4
Source File: DocumentUtils.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param path
 *            - absolute path to the element
 * @return
 */
public static IFile getWorkspaceFile(IPath path) {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    try {
        return root.getFileForLocation(path);
    } catch (Exception e) {
        return null;
    }
}
 
Example 5
Source File: MergeXliff.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获取相对于项目的相对路径
 * @param absolutePath
 * @return
 */
public String getFullPath(String absolutePath) {
	// UNDO 合并后的文件好像不能转换回原文件,这里还需完善。
	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	IFile iFile = root.getFileForLocation(Path.fromOSString(absolutePath));
	return iFile.getFullPath().toOSString();
}
 
Example 6
Source File: XLFValidator.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean validateXliffFile(String fileLocalPath) {
	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	IFile file = root.getFileForLocation(URIUtil.toPath(new File(fileLocalPath).toURI()));
	if (file == null) {
		Shell shell = Display.getDefault().getActiveShell();
		MessageDialog.openError(shell, Messages.getString("file.XLFValidator.msgTitle"),
				Messages.getString("file.XLFValidator.msg9"));
		return false;
	}
	return validateXliffFile(file);
}
 
Example 7
Source File: XLFValidator.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean validateXliffFile(File file) {
	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	IFile ifile = root.getFileForLocation(URIUtil.toPath(file.toURI()));
	if (ifile == null) {
		Shell shell = Display.getDefault().getActiveShell();
		MessageDialog.openError(shell, Messages.getString("file.XLFValidator.msgTitle"),
				Messages.getString("file.XLFValidator.msg9"));
		return false;
	}
	return validateXliffFile(ifile);
}
 
Example 8
Source File: MergeXliff.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获取相对于项目的相对路径
 * @param absolutePath
 * @return
 */
public String getFullPath(String absolutePath) {
	// UNDO 合并后的文件好像不能转换回原文件,这里还需完善。
	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	IFile iFile = root.getFileForLocation(Path.fromOSString(absolutePath));
	return iFile.getFullPath().toOSString();
}
 
Example 9
Source File: XLFValidator.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean validateXliffFile(String fileLocalPath) {
	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	IFile file = root.getFileForLocation(URIUtil.toPath(new File(fileLocalPath).toURI()));
	if (file == null) {
		Shell shell = Display.getDefault().getActiveShell();
		MessageDialog.openError(shell, Messages.getString("file.XLFValidator.msgTitle"),
				Messages.getString("file.XLFValidator.msg9"));
		return false;
	}
	return validateXliffFile(file);
}
 
Example 10
Source File: XLFValidator.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean validateXliffFile(File file) {
	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	IFile ifile = root.getFileForLocation(URIUtil.toPath(file.toURI()));
	if (ifile == null) {
		Shell shell = Display.getDefault().getActiveShell();
		MessageDialog.openError(shell, Messages.getString("file.XLFValidator.msgTitle"),
				Messages.getString("file.XLFValidator.msg9"));
		return false;
	}
	return validateXliffFile(ifile);
}
 
Example 11
Source File: UserLibraryPreferencePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private CPListElement[] doOpenExternalJarFileDialog(CPListElement existing, Object parent) {
	String lastUsedPath;
	if (existing != null) {
		lastUsedPath= existing.getPath().removeLastSegments(1).toOSString();
	} else {
		lastUsedPath= fDialogSettings.get(IUIConstants.DIALOGSTORE_LASTEXTJAR);
		if (lastUsedPath == null) {
			lastUsedPath= ""; //$NON-NLS-1$
		}
	}
	String title= (existing == null) ? PreferencesMessages.UserLibraryPreferencePage_browsejar_new_title : PreferencesMessages.UserLibraryPreferencePage_browsejar_edit_title;

	FileDialog dialog= new FileDialog(getShell(), existing == null ? SWT.MULTI : SWT.SINGLE);
	dialog.setText(title);
	dialog.setFilterExtensions(ArchiveFileFilter.ALL_ARCHIVES_FILTER_EXTENSIONS);
	dialog.setFilterPath(lastUsedPath);
	if (existing != null) {
		dialog.setFileName(existing.getPath().lastSegment());
	}

	String res= dialog.open();
	if (res == null) {
		return null;
	}
	String[] fileNames= dialog.getFileNames();
	int nChosen= fileNames.length;

	IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();

	IPath filterPath= Path.fromOSString(dialog.getFilterPath());
	CPListElement[] elems= new CPListElement[nChosen];
	for (int i= 0; i < nChosen; i++) {
		IPath path= filterPath.append(fileNames[i]).makeAbsolute();

		IFile file= root.getFileForLocation(path); // support internal JARs: bug 133191
		if (file != null) {
			path= file.getFullPath();
		}

		CPListElement curr= new CPListElement(parent, null, IClasspathEntry.CPE_LIBRARY, path, file);
		curr.setAttribute(CPListElement.SOURCEATTACHMENT, BuildPathSupport.guessSourceAttachment(curr));
		curr.setAttribute(CPListElement.JAVADOC, BuildPathSupport.guessJavadocLocation(curr));
		elems[i]= curr;
	}
	fDialogSettings.put(IUIConstants.DIALOGSTORE_LASTEXTJAR, dialog.getFilterPath());

	return elems;
}