/** * Validates the destination file if it is read-only and additionally the source file if both are * read-only. Returns true if both files could be made writeable. * * @param source source file * @param destination destination file * @param shell ui context for the validation * @return boolean <code>true</code> both files could be made writeable. <code>false</code> either * one or both files were not made writeable */ boolean validateEdit(IFile source, IFile destination, Shell shell) { if (destination.isReadOnly()) { IWorkspace workspace = ModelerCore.getWorkspace(); IStatus status; if (source.isReadOnly()) status = workspace.validateEdit(new IFile[] {source, destination}, shell); else status = workspace.validateEdit(new IFile[] {destination}, shell); return status.isOK(); } return true; }
@Override protected void setInput(final IEditorInput input) { if (input instanceof FileEditorInput) { final IFile file = ((FileEditorInput) input).getFile(); setPartName(file.getProject().getName() + "/" + input.getName()); editorInput = new RedProjectEditorInput( Optional.of(file), !file.isReadOnly(), new RedEclipseProjectConfigReader().readConfigurationWithLines(file)); installResourceListener(); } else { final IStorage storage = (IStorage) input.getAdapter(IStorage.class); if (storage != null) { setPartName(storage.getName() + " [" + storage.getFullPath() + "]"); try (InputStream stream = storage.getContents()) { editorInput = new RedProjectEditorInput( Optional.<IFile>absent(), !storage.isReadOnly(), new RobotProjectConfigReader().readConfigurationWithLines(stream)); } catch (final CoreException | IOException e) { throw new IllegalProjectConfigurationEditorInputException( "Unable to open editor: unrecognized input of class: " + input.getClass().getName(), e); } } else { throw new IllegalProjectConfigurationEditorInputException( "Unable to open editor: unrecognized input of class: " + input.getClass().getName()); } } super.setInput(input); }
/** * @param IFile theFile that will be written out * @param Writer the String Writer that will be writen out * @modelguid {32B1885D-A8E1-4267-B66E-4D3ABA83DC69} */ private void saveDocumentAsResource(IFile theFile, OutputStream writer) { if (theFile == null) { return; } InputStream input = null; try { // read the bytes in the outputStreamWriter input = new ByteArrayInputStream(((ByteArrayOutputStream) writer).toByteArray()); if (theFile.exists()) { if (theFile.isReadOnly()) { // Check out the file - ME TODO if (true) { theFile.setContents(input, true, true, null); } } else { theFile.setContents(input, true, true, null); } } else { theFile.create(input, false, null); } } catch (Exception e) { } finally { try { input.close(); } catch (Exception e) { } } }
/** @generated */ protected void updateCache(Object element) throws CoreException { ResourceSetInfo info = getResourceSetInfo(element); if (info != null) { for (Iterator it = info.getResourceSet().getResources().iterator(); it.hasNext(); ) { Resource nextResource = (Resource) 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; } }
/** * Method validateEdit. * * @param file org.eclipse.core.resources.IFile * @param context org.eclipse.swt.widgets.Shell * @return IStatus */ private static IStatus validateEdit(IFile file, Shell context) { if (file == null || !file.exists()) return STATUS_ERROR; if (!(file.isReadOnly())) return STATUS_OK; IPath location = file.getLocation(); final long beforeModifiedFromJavaIO = (location != null) ? location.toFile().lastModified() : IResource.NULL_STAMP; final long beforeModifiedFromIFile = file.getModificationStamp(); IStatus status = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] {file}, context); if (!status.isOK()) return status; final long afterModifiedFromJavaIO = (location != null) ? location.toFile().lastModified() : IResource.NULL_STAMP; final long afterModifiedFromIFile = file.getModificationStamp(); if (beforeModifiedFromJavaIO != afterModifiedFromJavaIO || beforeModifiedFromIFile != afterModifiedFromIFile) { IModelManager manager = StructuredModelManager.getModelManager(); IStructuredModel model = manager.getExistingModelForRead(file); if (model != null) { if (!model.isDirty()) { try { file.refreshLocal(IResource.DEPTH_ONE, null); } catch (CoreException e) { return STATUS_ERROR; } finally { model.releaseFromRead(); } } else { model.releaseFromRead(); } } } if ((beforeModifiedFromJavaIO != afterModifiedFromJavaIO) || (beforeModifiedFromIFile != afterModifiedFromIFile)) { // the file is replaced. Modification cannot be // applied. return STATUS_ERROR; } return STATUS_OK; }
/** @generated */ protected void doValidateState(Object element, Object computationContext) throws CoreException { ResourceSetInfo info = getResourceSetInfo(element); if (info != null) { Collection files2Validate = new ArrayList(); for (Iterator it = info.getResourceSet().getResources().iterator(); it.hasNext(); ) { Resource nextResource = (Resource) 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); }
/** * 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 static void setSharedProperty(IProject p, String key, String value) throws CoreException { IFile rscFile = p.getFile(key); byte[] bytes = null; try { bytes = value.getBytes(UTF_8); // .classpath always encoded with UTF-8 } catch (UnsupportedEncodingException e) { MindIdeCore.log(e, "Could not write " + key + " 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}, null); } rscFile.setContents(inputStream, IResource.FORCE, null); } else { rscFile.create(inputStream, IResource.FORCE, null); } }
/** * Copies/moves a compilation unit with the name <code>newCUName</code> to the destination * package.<br> * The package statement in the compilation unit is updated if necessary. The main type of the * compilation unit is renamed if necessary. * * @exception JavaModelException if the operation is unable to complete */ private void processCompilationUnitResource(ICompilationUnit source, PackageFragment dest) throws JavaModelException { String newCUName = getNewNameFor(source); String destName = (newCUName != null) ? newCUName : source.getElementName(); TextEdit edit = updateContent(source, dest, newCUName); // null if unchanged // TODO (frederic) remove when bug 67606 will be fixed (bug 67823) // store encoding (fix bug 66898) IFile sourceResource = (IFile) source.getResource(); String sourceEncoding = null; try { sourceEncoding = sourceResource.getCharset(false); } catch (CoreException ce) { // no problem, use default encoding } // end todo // copy resource IContainer destFolder = (IContainer) dest.getResource(); // can be an IFolder or an IProject IFile destFile = destFolder.getFile(new Path(destName)); org.eclipse.jdt.internal.core.CompilationUnit destCU = new org.eclipse.jdt.internal.core.CompilationUnit( dest, destName, DefaultWorkingCopyOwner.PRIMARY); if (!destFile.equals(sourceResource)) { try { if (!destCU.isWorkingCopy()) { if (destFile.exists()) { if (this.force) { // we can remove it deleteResource(destFile, IResource.KEEP_HISTORY); destCU.close(); // ensure the in-memory buffer for the dest CU is closed } else { // abort throw new JavaModelException( new JavaModelStatus( IJavaModelStatusConstants.NAME_COLLISION, Messages.bind( Messages.status_nameCollision, destFile.getFullPath().toString()))); } } int flags = this.force ? IResource.FORCE : IResource.NONE; if (isMove()) { flags |= IResource.KEEP_HISTORY; sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1)); } else { if (edit != null) flags |= IResource.KEEP_HISTORY; sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1)); } setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); } else { destCU.getBuffer().setContents(source.getBuffer().getContents()); } } catch (JavaModelException e) { throw e; } catch (CoreException e) { throw new JavaModelException(e); } // update new resource content if (edit != null) { boolean wasReadOnly = destFile.isReadOnly(); try { saveContent(dest, destName, edit, sourceEncoding, destFile); } catch (CoreException e) { if (e instanceof JavaModelException) throw (JavaModelException) e; throw new JavaModelException(e); } finally { Util.setReadOnly(destFile, wasReadOnly); } } // register the correct change deltas prepareDeltas(source, destCU, isMove()); if (newCUName != null) { // the main type has been renamed String oldName = Util.getNameWithoutJavaLikeExtension(source.getElementName()); String newName = Util.getNameWithoutJavaLikeExtension(newCUName); prepareDeltas(source.getType(oldName), destCU.getType(newName), isMove()); } } else { if (!this.force) { throw new JavaModelException( new JavaModelStatus( IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(Messages.status_nameCollision, destFile.getFullPath().toString()))); } // update new resource content // in case we do a saveas on the same resource we have to simply update the contents // see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351 if (edit != null) { saveContent(dest, destName, edit, sourceEncoding, destFile); } } }