Seng Zhe
Seng Zhe

Reputation: 623

How to check generated file has been modified in Eclipse plugin development?

Currently the plugin will generate a series of files in an IProject, I need to check whether the generated file has been modified by user before. If the generated artifact has been modified by user, I will need to handle the regeneration differently.

What I can think of is by checking Creation Date == Modified Date . The fact that I will delete the old file and create it again when user has not touched the file before to make sure the Creation Date always equals Modified Date. However I did not see how to retrieve these 2 properties from IFile. Anyone can help me regarding this?

I am quite new to Eclipse plugin development, can anyone suggest another way around this ?

*** Generated files cannot be locked as those are source codes

Upvotes: 0

Views: 465

Answers (2)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

The modification stamp of an IFile or more generally an IResource can be obtained with getModificationStamp(). The return value is not strictly a time stamp but should serve your needs, see the JavaDoc for details.

If, however, you would like to track whether the content of a file was changed I would rather compute a hash of the content, for example with a MessageDigest. You can then compare the two hashes to decide whether the file was changed.

This latter approach would regard a file as unchanged if it was changed - saved - changes reverted - saved again. The modification stamp on the other hand would declare the file as changed even though its content is the same again.

Whichever approach you choose, you can store the modification stamp (or content hash) at generation time by using IResource#setPersistentProperty() and later compare it with the current modification stamp. Persistent properties are stored on disk with the platform metadata and maintained across platform shutdown and restart.

Upvotes: 1

Seng Zhe
Seng Zhe

Reputation: 623

I found the answer:

private boolean isModified(IFile existingFile) throws CoreException {
  IFileState[] history = existingFile.getHistory(NullProgessMonitor);
  return history.length > 0;
}

This feature is maintained by eclipse IDE so it will survive the restarting of eclipse. If the file has been created without modification , the history state is zero.

You can clear local history by doing:

existingFile.clearHistory(NullProgessMonitor);

Upvotes: 0

Related Questions