0

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

Seng Zhe
  • 613
  • 1
  • 11
  • 21

2 Answers2

1

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.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Hi @Rüdiger, Thanks for the suggestion. The message digest would not work because the content varies because there are a few parameter changes whenever user modifies them. So even if the file content did not change, the comparison will fail because generated content might be different. However, I do not understand how modification stamp will help in this case? How do I know if the files are modified with no comparison to creation stamp ? – Seng Zhe Nov 07 '14 at 02:02
  • @SengZhe Please see my amended answer. – Rüdiger Herrmann Nov 07 '14 at 10:39
  • Thanks for the suggestion ! I was not aware about the setPersistentProperty method for IResource . Really useful knowledge. MY next implementation will be using the hash content solution. For now, the filestate solves my problem but thanks for the tips ! – Seng Zhe Nov 11 '14 at 02:01
0

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);
Seng Zhe
  • 613
  • 1
  • 11
  • 21
  • The local resource history is not meant to track resource changes. It is meant to be under the users control who specifies if at all and how long the history for a resource is kept. Moreover, clearing the history will break certain features like 'Compare with History' etc. – Rüdiger Herrmann Nov 07 '14 at 10:48