Search in sources :

Example 76 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project che by eclipse.

the class JavaProject method canonicalizedPath.

/**
     * Returns a canonicalized path from the given external path.
     * Note that the return path contains the same number of segments
     * and it contains a device only if the given path contained one.
     * @param externalPath IPath
     * @see java.io.File for the definition of a canonicalized path
     * @return IPath
     */
public static IPath canonicalizedPath(IPath externalPath) {
    if (externalPath == null)
        return null;
    if (IS_CASE_SENSITIVE) {
        return externalPath;
    }
    // if not external path, return original path
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    // protection during shutdown (30487)
    if (workspace == null)
        return externalPath;
    if (workspace.getRoot().findMember(externalPath) != null) {
        return externalPath;
    }
    IPath canonicalPath = null;
    try {
        canonicalPath = new Path(new File(externalPath.toOSString()).getCanonicalPath());
    } catch (IOException e) {
        // default to original path
        return externalPath;
    }
    IPath result;
    int canonicalLength = canonicalPath.segmentCount();
    if (canonicalLength == 0) {
        // the java.io.File canonicalization failed
        return externalPath;
    } else if (externalPath.isAbsolute()) {
        result = canonicalPath;
    } else {
        // if path is relative, remove the first segments that were added by the java.io.File canonicalization
        // e.g. 'lib/classes.zip' was converted to 'd:/myfolder/lib/classes.zip'
        int externalLength = externalPath.segmentCount();
        if (canonicalLength >= externalLength) {
            result = canonicalPath.removeFirstSegments(canonicalLength - externalLength);
        } else {
            return externalPath;
        }
    }
    // keep device only if it was specified (this is because File.getCanonicalPath() converts '/lib/classes.zip' to 'd:/lib/classes/zip')
    if (externalPath.getDevice() == null) {
        result = result.setDevice(null);
    }
    // keep trailing separator only if it was specified (this is because File.getCanonicalPath() converts 'd:/lib/classes/' to 'd:/lib/classes')
    if (externalPath.hasTrailingSeparator()) {
        result = result.addTrailingSeparator();
    }
    return result;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IWorkspace(org.eclipse.core.resources.IWorkspace) IOException(java.io.IOException) JavaModelManager.isFile(org.eclipse.jdt.internal.core.JavaModelManager.isFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 77 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project che by eclipse.

the class JavaModelOperation method deleteResources.

/**
	 * Convenience method to delete resources
	 */
protected void deleteResources(IResource[] resources, boolean forceFlag) throws JavaModelException {
    if (resources == null || resources.length == 0)
        return;
    IProgressMonitor subProgressMonitor = getSubProgressMonitor(resources.length);
    IWorkspace workspace = resources[0].getWorkspace();
    try {
        workspace.delete(resources, forceFlag ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY, subProgressMonitor);
        setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
    } catch (CoreException e) {
        throw new JavaModelException(e);
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace)

Example 78 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project che by eclipse.

the class ImportOperation method importFolder.

/**
     * Imports the specified file system container object into the workspace.
     * If the import fails, adds a status object to the list to be returned by
     * <code>getResult</code>.
     *
     * @param folderObject the file system container object to be imported
     * @param policy determines how the folder object and children are imported
     * @return the policy to use to import the folder's children
     * @throws CoreException
     */
int importFolder(Object folderObject, int policy) throws CoreException {
    IContainer containerResource;
    try {
        containerResource = getDestinationContainerFor(folderObject);
    } catch (CoreException e) {
        errorTable.add(e.getStatus());
        return policy;
    }
    if (containerResource == null) {
        return policy;
    }
    monitor.subTask(provider.getFullPath(folderObject));
    IWorkspace workspace = destinationContainer.getWorkspace();
    IPath containerPath = containerResource.getFullPath();
    IPath resourcePath = containerPath.append(provider.getLabel(folderObject));
    // when importing from a zip file.
    if (resourcePath.equals(containerPath)) {
        return policy;
    }
    if (workspace.getRoot().exists(resourcePath)) {
        if (rejectedFiles.contains(resourcePath)) {
            return POLICY_SKIP_CHILDREN;
        }
        IFolder folder = workspace.getRoot().getFolder(resourcePath);
        if (createVirtualFolder || createLinks || folder.isVirtual() || folder.isLinked()) {
            folder.delete(true, null);
        } else
            return POLICY_FORCE_OVERWRITE;
    }
    try {
        if (createVirtualFolder)
            workspace.getRoot().getFolder(resourcePath).create(IResource.VIRTUAL, true, null);
        else if (createLinks) {
            IFolder newFolder = workspace.getRoot().getFolder(resourcePath);
            newFolder.createLink(createRelativePath(new Path(provider.getFullPath(folderObject)), newFolder), 0, null);
            policy = POLICY_SKIP_CHILDREN;
        } else
            workspace.getRoot().getFolder(resourcePath).create(false, true, null);
    } catch (CoreException e) {
        errorTable.add(e.getStatus());
    }
    return policy;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) CoreException(org.eclipse.core.runtime.CoreException) IPath(org.eclipse.core.runtime.IPath) IWorkspace(org.eclipse.core.resources.IWorkspace) IContainer(org.eclipse.core.resources.IContainer) IFolder(org.eclipse.core.resources.IFolder)

Example 79 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project translationstudio8 by heartsome.

the class RenameResourceAndCloseEditorAction method saveChangesAndDispose.

/**
	 * Save the changes and dispose of the text widget.
	 * 
	 * @param resource -
	 *            the resource to move.
	 */
private void saveChangesAndDispose(IResource resource) {
    if (saving == true) {
        return;
    }
    saving = true;
    // Cache the resource to avoid selection loss since a selection of
    // another item can trigger this method
    inlinedResource = resource;
    final String newName = textEditor.getText();
    // Run this in an async to make sure that the operation that triggered
    // this action is completed. Otherwise this leads to problems when the
    // icon of the item being renamed is clicked (i.e., which causes the
    // rename
    // text widget to lose focus and trigger this method).
    Runnable query = new Runnable() {

        public void run() {
            try {
                if (!newName.equals(inlinedResource.getName())) {
                    IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
                    IStatus status = workspace.validateName(newName, inlinedResource.getType());
                    if (!status.isOK()) {
                        displayError(status.getMessage());
                    } else {
                        // 验证资源名称是否合法	robert	2013-07-01
                        String validResult = CommonFunction.validResourceName(newName);
                        if (validResult != null) {
                            displayError(validResult);
                        } else {
                            IPath newPath = inlinedResource.getFullPath().removeLastSegments(1).append(newName);
                            runWithNewPath(newPath, inlinedResource);
                        }
                    }
                }
                inlinedResource = null;
                // Dispose the text widget regardless
                disposeTextWidget();
                // text widget previously had focus.
                if (navigatorTree != null && !navigatorTree.isDisposed()) {
                    navigatorTree.setFocus();
                }
            } finally {
                saving = false;
            }
        }
    };
    getTree().getShell().getDisplay().asyncExec(query);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) IWorkspace(org.eclipse.core.resources.IWorkspace)

Example 80 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project translationstudio8 by heartsome.

the class ImportProjectWizardPage method createExistingProject.

/**
	 * Create the project described in record. If it is successful return true.
	 * 
	 * @param record
	 * @return boolean <code>true</code> if successful
	 * @throws InterruptedException
	 */
private boolean createExistingProject(final ProjectRecord record, IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
    String projectName = record.getProjectName();
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final IProject project = workspace.getRoot().getProject(projectName);
    createdProjects.add(project);
    if (record.description == null) {
        // error case
        record.description = workspace.newProjectDescription(projectName);
        IPath locationPath = new Path(record.projectSystemFile.getAbsolutePath());
        // If it is under the root use the default location
        if (Platform.getLocation().isPrefixOf(locationPath)) {
            record.description.setLocation(null);
        } else {
            record.description.setLocation(locationPath);
        }
    } else {
        record.description.setName(projectName);
    }
    if (record.projectArchiveFile != null) {
        // import from archive
        List fileSystemObjects = structureProvider.getChildren(record.parent);
        structureProvider.setStrip(record.level);
        ImportOperation operation = new ImportOperation(project.getFullPath(), structureProvider.getRoot(), structureProvider, this, fileSystemObjects);
        operation.setContext(getShell());
        operation.run(monitor);
        return true;
    }
    // import from file system
    File importSource = null;
    if (copyFiles) {
        // import project from location copying files - use default project
        // location for this workspace
        URI locationURI = record.description.getLocationURI();
        // some error condition occured.
        if (locationURI != null) {
            // validate the location of the project being copied
            IStatus result = ResourcesPlugin.getWorkspace().validateProjectLocationURI(project, locationURI);
            if (!result.isOK())
                throw new InvocationTargetException(new CoreException(result));
            importSource = new File(locationURI);
            IProjectDescription desc = workspace.newProjectDescription(projectName);
            desc.setBuildSpec(record.description.getBuildSpec());
            desc.setComment(record.description.getComment());
            desc.setDynamicReferences(record.description.getDynamicReferences());
            desc.setNatureIds(record.description.getNatureIds());
            desc.setReferencedProjects(record.description.getReferencedProjects());
            record.description = desc;
        }
    }
    try {
        monitor.beginTask(DataTransferMessages.WizardProjectsImportPage_CreateProjectsTask, 100);
        project.create(record.description, new SubProgressMonitor(monitor, 30));
        project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(monitor, 70));
    } catch (CoreException e) {
        throw new InvocationTargetException(e);
    } finally {
        monitor.done();
    }
    // import operation to import project files if copy checkbox is selected
    if (copyFiles && importSource != null) {
        List filesToImport = FileSystemStructureProvider.INSTANCE.getChildren(importSource);
        ImportOperation operation = new ImportOperation(project.getFullPath(), importSource, FileSystemStructureProvider.INSTANCE, this, filesToImport);
        operation.setContext(getShell());
        // need to overwrite
        operation.setOverwriteResources(true);
        // .project, .classpath
        // files
        operation.setCreateContainerStructure(false);
        operation.run(monitor);
    }
    return true;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) ImportOperation(org.eclipse.ui.wizards.datatransfer.ImportOperation) URI(java.net.URI) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IProjectDescription(org.eclipse.core.resources.IProjectDescription) List(java.util.List) ArrayList(java.util.ArrayList) ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) File(java.io.File)

Aggregations

IWorkspace (org.eclipse.core.resources.IWorkspace)118 IProject (org.eclipse.core.resources.IProject)55 CoreException (org.eclipse.core.runtime.CoreException)54 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)37 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)36 IPath (org.eclipse.core.runtime.IPath)35 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)29 IStatus (org.eclipse.core.runtime.IStatus)27 IFile (org.eclipse.core.resources.IFile)24 PersistenceException (org.talend.commons.exception.PersistenceException)23 IProjectDescription (org.eclipse.core.resources.IProjectDescription)21 File (java.io.File)19 InvocationTargetException (java.lang.reflect.InvocationTargetException)19 Path (org.eclipse.core.runtime.Path)19 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)19 IResource (org.eclipse.core.resources.IResource)18 Status (org.eclipse.core.runtime.Status)14 IOException (java.io.IOException)13 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)13 ArrayList (java.util.ArrayList)12