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;
}
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);
}
}
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;
}
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);
}
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;
}
Aggregations