Java Code Examples for org.eclipse.core.resources.IFile#isReadOnly()

The following examples show how to use org.eclipse.core.resources.IFile#isReadOnly() . These examples are extracted from open source projects. 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
/**
* @generated
*/
protected void doValidateState(Object element, Object computationContext) throws CoreException {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		LinkedList<IFile> files2Validate = new LinkedList<IFile>();
		for (Iterator<Resource> it = info.getLoadedResourcesIterator(); it.hasNext();) {
			Resource nextResource = it.next();
			IFile file = WorkspaceSynchronizer.getFile(nextResource);
			if (file != null && file.isReadOnly()) {
				files2Validate.add(file);
			}
		}
		ResourcesPlugin.getWorkspace().validateEdit(
				(IFile[]) files2Validate.toArray(new IFile[files2Validate.size()]), computationContext);
	}

	super.doValidateState(element, computationContext);
}
 
Example 2
/**
* @generated
*/
protected void updateCache(Object element) throws CoreException {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		for (Iterator<Resource> it = info.getLoadedResourcesIterator(); it.hasNext();) {
			Resource nextResource = it.next();
			IFile file = WorkspaceSynchronizer.getFile(nextResource);
			if (file != null && file.isReadOnly()) {
				info.setReadOnly(true);
				info.setModifiable(false);
				return;
			}
		}
		info.setReadOnly(false);
		info.setModifiable(true);
		return;
	}
}
 
Example 3
/**
 * Record a shared persistent property onto a project.
 * Note that it is orthogonal to IResource persistent properties, and client code has to decide
 * which form of storage to use appropriately. Shared properties produce real resource files which
 * can be shared through a VCM onto a server. Persistent properties are not shareable.
 * <p>
 * Shared properties end up in resource files, and thus cannot be modified during
 * delta notifications (a CoreException would then be thrown).
 *
 * @param key String
 * @param value String
 * @see JavaProject#getSharedProperty(String key)
 * @throws CoreException
 */
public void setSharedProperty(String key, String value) throws CoreException {

	IFile rscFile = this.project.getFile(key);
	byte[] bytes = null;
	try {
		bytes = value.getBytes(org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
	} catch (UnsupportedEncodingException e) {
		Util.log(e, "Could not write .classpath with UTF-8 encoding "); //$NON-NLS-1$
		// fallback to default
		bytes = value.getBytes();
	}
	InputStream inputStream = new ByteArrayInputStream(bytes);
	// update the resource content
	if (rscFile.exists()) {
		if (rscFile.isReadOnly()) {
			// provide opportunity to checkout read-only .classpath file (23984)
			ResourcesPlugin.getWorkspace().validateEdit(new IFile[]{rscFile}, IWorkspace.VALIDATE_PROMPT);
		}
		rscFile.setContents(inputStream, IResource.FORCE, null);
	} else {
		rscFile.create(inputStream, IResource.FORCE, null);
	}
}
 
Example 4
/**
* @generated
*/
protected void doValidateState(Object element, Object computationContext) throws CoreException {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		LinkedList<IFile> files2Validate = new LinkedList<IFile>();
		for (Iterator<Resource> it = info.getLoadedResourcesIterator(); it.hasNext();) {
			Resource nextResource = it.next();
			IFile file = WorkspaceSynchronizer.getFile(nextResource);
			if (file != null && file.isReadOnly()) {
				files2Validate.add(file);
			}
		}
		ResourcesPlugin.getWorkspace().validateEdit(
				(IFile[]) files2Validate.toArray(new IFile[files2Validate.size()]), computationContext);
	}

	super.doValidateState(element, computationContext);
}
 
Example 5
/**
* @generated
*/
protected void updateCache(Object element) throws CoreException {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		for (Iterator<Resource> it = info.getLoadedResourcesIterator(); it.hasNext();) {
			Resource nextResource = it.next();
			IFile file = WorkspaceSynchronizer.getFile(nextResource);
			if (file != null && file.isReadOnly()) {
				info.setReadOnly(true);
				info.setModifiable(false);
				return;
			}
		}
		info.setReadOnly(false);
		info.setModifiable(true);
		return;
	}
}
 
Example 6
private void ensureProjectFileWritable() throws CoreException {
  IFile projectFile = mProject.getFile(".project");
  if (projectFile.isReadOnly()) {
    ResourceAttributes attrs = ResourceAttributes.fromFile(projectFile.getFullPath().toFile());
    attrs.setReadOnly(true);
    projectFile.setResourceAttributes(attrs);
  }
}
 
Example 7
/**
 * Store the audit configurations to the persistent state storage.
 */
private void storeToPersistence(ProjectConfigurationWorkingCopy config)
        throws CheckstylePluginException {

  try {

    Document docu = writeProjectConfig(config);
    byte[] data = XMLUtil.toByteArray(docu);
    InputStream pipeIn = new ByteArrayInputStream(data);

    // create or overwrite the .checkstyle file
    IProject project = config.getProject();
    IFile file = project.getFile(ProjectConfigurationFactory.PROJECT_CONFIGURATION_FILE);
    if (!file.exists()) {
      file.create(pipeIn, true, null);
      file.refreshLocal(IResource.DEPTH_INFINITE, null);
    } else {

      if (file.isReadOnly()) {
        ResourceAttributes attrs = ResourceAttributes.fromFile(file.getFullPath().toFile());
        attrs.setReadOnly(true);
        file.setResourceAttributes(attrs);
      }

      file.setContents(pipeIn, true, true, null);
    }

    config.getLocalCheckConfigWorkingSet().store();
  } catch (Exception e) {
    CheckstylePluginException.rethrow(e,
            NLS.bind(Messages.errorWritingCheckConfigurations, e.getLocalizedMessage()));
  }
}
 
Example 8
/**
 * Ensure that a file is writable. If not currently writable, check it as so
 * that we can edit it.
 *
 * @param file
 *            - file that should be made writable
 * @throws CoreException
 */
private static void ensureReadWrite(IFile file) throws CoreException {
    /*
     * fix for bug 1683264: we should checkout file before writing to it
     */
    if (file.isReadOnly()) {
        IStatus checkOutStatus = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] { file }, null);
        if (!checkOutStatus.isOK()) {
            throw new CoreException(checkOutStatus);
        }
    }
}
 
Example 9
/**
 * Checks the given files that have been changed by validating them with the workspace.
 * 
 * @param files the files to check
 * @param validationContext the context for validating the files.  Should be the value of 
 *      {@link org.eclipse.ltk.core.refactoring.Refactoring#getValidationContext()}.
 * @param refactoringStatus the value to store the detection of problems.
 * @throws CoreException when the validation is canceled
 */
public static void checkFiles(Collection<IFile> files, Object validationContext,
        RefactoringStatus refactoringStatus) throws CoreException {
    List<IFile> readOnly = new ArrayList<IFile>();
    for (IFile file : files) {
        if (file.isReadOnly()) {
            readOnly.add(file);
        }
    }
    if (ResourcesPlugin.getPlugin() == null) {
        //i.e.: in test mode we won't be able to get the workspace
        return;
    }
    if (!readOnly.isEmpty()) {
        IFile[] readOnlyFiles = readOnly.toArray(new IFile[readOnly.size()]);
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IStatus status = workspace.validateEdit(readOnlyFiles, validationContext);
        if (status.getSeverity() == IStatus.CANCEL) {
            throw new OperationCanceledException();
        }
        refactoringStatus.merge(RefactoringStatus.create(status));
        if (refactoringStatus.hasFatalError()) {
            return;
        }
    }
    refactoringStatus.merge(ResourceChangeChecker.checkFilesToBeChanged(
            files.toArray(new IFile[files.size()]), null));
}
 
Example 10
/**
 * Recursively collects existing files in the specified destination path.
 *
 * @param destinationPath
 *            destination path to check for existing files
 * @param copyResources
 *            resources that may exist in the destination
 * @param existing
 *            holds the collected existing files
 */
private void collectExistingReadonlyFiles(IPath destinationPath, IResource[] copyResources,
        ArrayList<IFile> existing) {
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

    for (int i = 0; i < copyResources.length; i++) {
        IResource source = copyResources[i];
        IPath newDestinationPath = destinationPath.append(source.getName());
        IResource newDestination = workspaceRoot.findMember(newDestinationPath);
        IFolder folder;

        if (newDestination == null) {
            continue;
        }
        folder = getFolder(newDestination);
        if (folder != null) {
            IFolder sourceFolder = getFolder(source);

            if (sourceFolder != null) {
                try {
                    collectExistingReadonlyFiles(newDestinationPath, sourceFolder.members(), existing);
                } catch (CoreException exception) {
                    recordError(exception);
                }
            }
        } else {
            IFile file = getFile(newDestination);

            if (file != null) {
                if (file.isReadOnly()) {
                    existing.add(file);
                }
                if (getValidateConflictSource()) {
                    IFile sourceFile = getFile(source);
                    if (sourceFile != null) {
                        existing.add(sourceFile);
                    }
                }
            }
        }
    }
}