IEditorPart editorPart = getSite().getWorkbenchWindow().getActivePage().getActiveEditor(); if(editorPart != null) { IFileEditorInput input = (IFileEditorInput)editorPart.getEditorInput() ; IFile file = input.getFile(); IProject activeProject = file.getProject(); String activeProjectName = activeProject.getName(); //... use activeProjectName }
IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class); if (file == null) throw new FileNotFoundException(); String content = IOUtils.toString(file.getContents(), file.getCharset());
private static IContainer getAsContainer(IResource resDest) { if (resDest instanceof IContainer) { return (IContainer) resDest; } if (resDest instanceof IFile) { return ((IFile) resDest).getParent(); } return null; }
private void saveManifest() throws CoreException, IOException { ByteArrayOutputStream manifestOutput= new ByteArrayOutputStream(); Manifest manifest= fJarPackage.getManifestProvider().create(fJarPackage); manifest.write(manifestOutput); ByteArrayInputStream fileInput= new ByteArrayInputStream(manifestOutput.toByteArray()); IFile manifestFile= fJarPackage.getManifestFile(); if (manifestFile.isAccessible()) { if (fJarPackage.allowOverwrite() || JarPackagerUtil.askForOverwritePermission(fParentShell, manifestFile.getFullPath().toString())) manifestFile.setContents(fileInput, true, true, null); } else manifestFile.create(fileInput, true, null); }
private InputStream createInputStream(IFile propertiesFile) throws CoreException { ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); if (manager != null) { ITextFileBuffer buffer= manager.getTextFileBuffer(propertiesFile.getFullPath(), LocationKind.IFILE); if (buffer != null) { return new ByteArrayInputStream(buffer.getDocument().get().getBytes()); } } return propertiesFile.getContents(); }
private void writeCommandFile ( final IProgressMonitor monitor ) throws Exception { final String data = "# These commands must be executed on the target instance" + NL + this.commandBuilder; try ( final ByteArrayInputStream input = new ByteArrayInputStream ( data.getBytes () ) ) { this.commandFile.create ( input, IResource.KEEP_HISTORY, monitor ); } }
public void createXMLDocument(IFile newFile, String xmlFileName) throws Exception { String charset = newFile.getCharset(); ByteArrayOutputStream outputStream = createXMLDocument(xmlFileName, charset); ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); newFile.setContents(inputStream, true, true, null); inputStream.close(); }
private void checkPropertiesFile(IFile file) { if (!file.exists()) { String propertiesFileComment = ExternalizeStringsOperation.getPropertiesFileComment(file); try (ByteArrayInputStream pStream = new ByteArrayInputStream(propertiesFileComment.getBytes())) { IContainer container = file.getParent(); if (!container.exists()) // project will exists, therefore we can assume if !IContainer.exist(), the object is an IFolder CoreUtility.createFolder((IFolder) container); file.create(pStream, true, new NullProgressMonitor()); } catch (CoreException | IOException e1) { } } } }
private static void createNewFile(IFile file, String newContent, IProgressMonitor monitor) throws CoreException { ensureExists(file.getParent(), monitor); try { file.create(new ByteArrayInputStream(newContent.getBytes("UTF-8")), false, monitor); //$NON-NLS-1$ } catch (UnsupportedEncodingException e) { throw new CoreException(new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, e.getMessage(), e)); } }
private void createPackageHtml(IPackageFragmentRoot root, IProgressMonitor monitor) throws CoreException { IWorkspace workspace= ResourcesPlugin.getWorkspace(); IFolder createdPackage= workspace.getRoot().getFolder(fCreatedPackageFragment.getPath()); IFile packageHtml= createdPackage.getFile(PACKAGE_HTML_FILENAME); String charset= packageHtml.getCharset(); String content= buildPackageHtmlContent(root, charset); try { packageHtml.create(new ByteArrayInputStream(content.getBytes(charset)), false, monitor); } catch (UnsupportedEncodingException e) { String message= "charset " + charset + " not supported by platform"; //$NON-NLS-1$ //$NON-NLS-2$ throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, message, e)); } }
private void addExtraLibrary(IPath path, IPluginModelBase model, ArrayList<IClasspathEntry> entries) { if (path.segmentCount() > 1) { IPath srcPath = null; if (model != null) { IPath shortPath = path.removeFirstSegments(path.matchingFirstSegments(new Path(model.getInstallLocation()))); srcPath = ClasspathUtilCore.getSourceAnnotation(model, shortPath.toString()); } else { String filename = ClasspathUtilCore.getSourceZipName(path.lastSegment()); IPath candidate = path.removeLastSegments(1).append(filename); if (PDECore.getWorkspace().getRoot().getFile(candidate).exists()) srcPath = candidate; } IClasspathEntry clsEntry = JavaCore.newLibraryEntry(path, srcPath, null); if (!entries.contains(clsEntry)) entries.add(clsEntry); } } }
private void copyFile(String fileName, InputStream input, IContainer dst, boolean binary, IProgressMonitor monitor) throws CoreException { monitor.subTask(fileName); IFile dstFile = dst.getFile(new Path(fileName)); try (InputStream stream = getProcessedStream(fileName, input, binary)) { if (dstFile.exists()) { dstFile.setContents(stream, true, true, monitor); } else { dstFile.create(stream, true, monitor); } } catch (IOException e) { } }
protected void okPressed() { IFile file = wsSelectedContainer.getFile(new Path( wsFilenameText.getText())); if (file != null) wsPathText.setText(file.getFullPath().toString()); validatePage(); super.okPressed(); }
/** * @since 2.3 */ public @Nullable IStorage getTraceFile(IStorage storage) { if (storage instanceof IFile) { IFile file = (IFile) storage; String originLastSegment = file.getFullPath().lastSegment(); IFile traceFile = file.getParent().getFile(new Path(traceFileNameProvider.getTraceFromJava(originLastSegment))); return traceFile; } return null; }
public static String getEncoding(String path) { try { return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path)).getCharset(); } catch (CoreException exception) { return null; } }
protected FileListener(AFileManager<T> fileManager) { this.fileManager = fileManager; IPath absolutePath2 = new org.eclipse.core.runtime.Path(fileManager.getAbsolutePath()); final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); final IPath rootLocation = root.getLocation(); if (absolutePath2.matchingFirstSegments(rootLocation) != rootLocation.segmentCount()) { try { final IFile[] filesOfLocation = root.findFilesForLocationURI(URI.create("file:/" + absolutePath2.toString().replace(" ", "%20"))); absolutePath2 = filesOfLocation[0].getFullPath().makeRelativeTo(rootLocation); } catch (final IndexOutOfBoundsException e) { Logger.logError(e); eclipseFile = null; return; } } this.eclipseFile = absolutePath2.makeRelativeTo(rootLocation); }
public String getLocalFile() { String file = reader.getFile(fileid); if (file.startsWith("file:/")) { IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); IFile iFile = workspaceRoot.getFileForLocation(new Path(file.substring(6))); file = iFile.getFullPath().toString(); } return file; }
private String getOutputFileName(IFile file) { String fileName = file.getName(); int dot = fileName.lastIndexOf('.'); String pageName = fileName.substring(0, dot) + ".html"; //$NON-NLS-1$ String mangledPluginId = getMangledPluginId(file); if (mangledPluginId != null) pageName = mangledPluginId + "_" + pageName; //$NON-NLS-1$ IPath path = file.getProject().getFullPath().append(getDocLocation(file)).append(pageName); return path.toString(); }
protected File getEditedFile() { IWorkbenchPage page = AntUIPlugin.getActivePage(); if (page == null) { return null; } IEditorPart editor = page.getActiveEditor(); if (editor == null) { return null; } FileEditorInput input = (FileEditorInput) editor.getEditorInput(); String projectPath = input.getFile().getProject().getLocation().toFile().getAbsolutePath(); String projectRelativeFilePath = input.getFile().getFullPath().removeFirstSegments(1).makeRelative().toString(); return new File(projectPath + File.separator + projectRelativeFilePath); }
public static IFile getWorkspaceFile(File file, String expectedProjectName) { IWorkspaceRoot root = CoreUtil.getWorkspaceRoot(); IPath path = new Path(file.getAbsolutePath()); File f = path.toFile(); IFile[] files = root.findFilesForLocationURI(f.toURI()); if (files.length == 0) { return null; } for (IFile wsFile : files) { IProject project = wsFile.getProject(); String projectName = project.getName(); if (projectName.equals(expectedProjectName)) { return wsFile; } } return null; }