public void testIsDirty() throws IOException, CoreException { IStructuredModel model = getTestModel(); try { assertFalse("model should not be dirty", model.isDirty()); } finally { if (model != null) { model.releaseFromEdit(); } } }
/** * 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; }