Search in sources :

Example 51 with IContainer

use of org.eclipse.core.resources.IContainer in project sling by apache.

the class ProjectAdapter method ensureDirectoryExists.

/**
     * Ensures that the specified directory exists
     * 
     * @param path the path where the directory should exist
     * @return the created or existing directory
     * @throws CoreException
     */
public IContainer ensureDirectoryExists(IPath path) throws CoreException {
    IContainer current = project;
    for (int i = 0; i < path.segmentCount(); i++) {
        String currentSegment = path.segment(i);
        IResource container = current.findMember(currentSegment);
        if (container != null) {
            if (container.getType() != IContainer.FOLDER) {
                throw new IllegalArgumentException("Resource " + container + " exists and is not a folder; unable to create file at path " + path);
            }
            current = (IContainer) container;
        } else {
            IFolder newFolder = ((IContainer) current).getFolder(Path.fromPortableString(currentSegment));
            newFolder.create(true, true, new NullProgressMonitor());
            current = newFolder;
        }
    }
    return current;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 52 with IContainer

use of org.eclipse.core.resources.IContainer in project sling by apache.

the class ProjectAdapter method createOrUpdateFile.

/**
     * Creates or updates an existing file
     * 
     * @param fileLocation the path where the resource will be created or updated
     * @param contents the contents to write to the file. This stream will be closed after being used
     * @throws CoreException
     */
public void createOrUpdateFile(IPath fileLocation, InputStream contents) throws CoreException {
    if (contents == null) {
        throw new IllegalArgumentException("resourceAsStream may not be null");
    }
    try {
        IContainer holder = ensureDirectoryExists(fileLocation.removeLastSegments(1));
        IPath fileName = Path.fromPortableString(fileLocation.lastSegment());
        IFile file = holder.getFile(fileName);
        if (file.exists()) {
            file.setContents(contents, true, true, new NullProgressMonitor());
        } else {
            file.create(contents, true, new NullProgressMonitor());
        }
    } finally {
        IOUtils.closeQuietly(contents);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IContainer(org.eclipse.core.resources.IContainer)

Example 53 with IContainer

use of org.eclipse.core.resources.IContainer in project bndtools by bndtools.

the class OpenExtConfigsContributionItem method getContributionItems.

@Override
protected IContributionItem[] getContributionItems() {
    try {
        IFile buildFile = Central.getWorkspaceBuildFile();
        if (buildFile == null)
            return EMPTY;
        IContainer cnfDir = buildFile.getParent();
        IFolder extDir = cnfDir.getFolder(new Path("ext"));
        if (extDir == null || !extDir.exists())
            return EMPTY;
        IResource[] extFiles = extDir.members();
        List<IContributionItem> result = new ArrayList<IContributionItem>(extFiles.length);
        for (final IResource extFile : extFiles) {
            if (extFile.getType() == IResource.FILE && "bnd".equalsIgnoreCase(extFile.getFileExtension())) {
                Action action = new Action() {

                    @Override
                    public void run() {
                        try {
                            FileEditorInput input = new FileEditorInput((IFile) extFile);
                            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                            page.openEditor(input, "bndtools.bndWorkspaceConfigEditor", true);
                        } catch (PartInitException e) {
                            ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", "Unable to open editor", e.getStatus());
                        }
                    }
                };
                action.setText("Open " + extFile.getProjectRelativePath());
                action.setImageDescriptor(extFileImg);
                result.add(new ActionContributionItem(action));
            }
        }
        return result.toArray(new IContributionItem[0]);
    } catch (Exception e) {
        logger.logError("Unable to find default config files", e);
        return EMPTY;
    }
}
Also used : Path(org.eclipse.core.runtime.Path) Action(org.eclipse.jface.action.Action) IFile(org.eclipse.core.resources.IFile) IContributionItem(org.eclipse.jface.action.IContributionItem) ArrayList(java.util.ArrayList) PartInitException(org.eclipse.ui.PartInitException) ActionContributionItem(org.eclipse.jface.action.ActionContributionItem) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 54 with IContainer

use of org.eclipse.core.resources.IContainer in project applause by applause.

the class ApplauseEclipseResourceFileSystemAccess2 method getFile.

protected IFile getFile(String fileName, String outputName) {
    OutputConfiguration configuration = getOutputConfig(outputName);
    IContainer container = getContainer(configuration);
    IFile result = container.getFile(new Path(fileName));
    syncIfNecessary(result);
    return result;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) OutputConfiguration(org.eclipse.xtext.generator.OutputConfiguration) IContainer(org.eclipse.core.resources.IContainer)

Example 55 with IContainer

use of org.eclipse.core.resources.IContainer in project bndtools by bndtools.

the class FileUtils method mkdirs.

public static void mkdirs(IContainer container, IProgressMonitor monitor) throws CoreException {
    SubMonitor progress = SubMonitor.convert(monitor, 2);
    if (container.exists())
        return;
    IContainer parent = container.getParent();
    if (parent != null)
        mkdirs(parent, progress.newChild(1));
    if (container.getType() == IResource.FOLDER) {
        IFolder folder = (IFolder) container;
        folder.create(false, true, progress.newChild(1));
    } else {
        throw new CoreException(new Status(IStatus.ERROR, "bndtools.utils", 0, "Can only create plain Folder parent containers.", null));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) SubMonitor(org.eclipse.core.runtime.SubMonitor) IContainer(org.eclipse.core.resources.IContainer) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

IContainer (org.eclipse.core.resources.IContainer)199 IResource (org.eclipse.core.resources.IResource)89 IFile (org.eclipse.core.resources.IFile)71 IPath (org.eclipse.core.runtime.IPath)47 CoreException (org.eclipse.core.runtime.CoreException)46 IFolder (org.eclipse.core.resources.IFolder)45 Path (org.eclipse.core.runtime.Path)43 IProject (org.eclipse.core.resources.IProject)28 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)26 ArrayList (java.util.ArrayList)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 IOException (java.io.IOException)13 IStatus (org.eclipse.core.runtime.IStatus)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 File (java.io.File)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)12 Status (org.eclipse.core.runtime.Status)10 PartInitException (org.eclipse.ui.PartInitException)10 InputStream (java.io.InputStream)9