1. "By the time..." something happens [ie James retires] must relate to an event that will have already happened or will be in place at the specified time, ie "I will be fifty-nine".
3. "By the time I leave here, I will do..." means that the demonstration will not yet have been done, so the phrase is incorrect. However, if it was expressed as "....I will have done a demonstration in the office", that would be correct.
"When", on the other hand, can mean things what have already happened and things that will happen in the future, so 2. and 4. are both correct.
Student or Learner