/src/main/java/uk/ac/cam/ch/ami/amievents/AmiEventHandler.java

https://bitbucket.org/jat45/ami · Java · 118 lines · 83 code · 17 blank · 18 comment · 8 complexity · 6ded958a484ca6990c928bec4fafb7e3 MD5 · raw file

  1. package uk.ac.cam.ch.ami.amievents;
  2. import com.thoughtworks.xstream.XStream;
  3. import org.apache.commons.io.FileUtils;
  4. import org.apache.commons.io.IOUtils;
  5. import uk.ac.cam.ch.ami.Experiment;
  6. import uk.ac.cam.ch.ami.amievents.events.AmiEvent;
  7. import uk.ac.cam.ch.ami.amievents.events.ChemicalAmiEvent;
  8. import uk.ac.cam.ch.ami.amievents.log.AmiEventLog;
  9. import uk.ac.cam.ch.ami.events.UpdateStatusBarEvent;
  10. import uk.ac.cam.ch.ami.listeners.ResetLogoutTimerListener;
  11. import uk.ac.cam.ch.ami.listeners.UpdateStatusBarListener;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. /**
  16. * Central handler which listens for all AmiEvents and processes them accordingly.
  17. * <p>
  18. * Created by IntelliJ IDEA.
  19. * User: alt36
  20. * Date: 02-Sep-2010
  21. * Time: 19:26:08
  22. */
  23. public class AmiEventHandler implements AmiEventListener {
  24. AmiEventLog amiEventLog = null;
  25. protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList();
  26. Experiment experiment;
  27. /**
  28. * Process an incoming AmiEvent. The event is appended to the log, the LogoutTimer (which logs the user out after a period
  29. * of inactivity) in HomeScreen is reset to zero, and the status bar of ExperimentScreen is updated to provide
  30. * feedback that the event has occurred.
  31. * @param event the event to process
  32. */
  33. @Override
  34. public void processAmiEvent(AmiEvent event) {
  35. amiEventLog.appendEvent(event);
  36. fireResetLogoutTimerEvent();
  37. if(!event.isToBeDeleted()) {
  38. String msg = event.getAmiEventType() + ": " + event.getTimelineText();
  39. fireUpdateStatusBarEvent(new UpdateStatusBarEvent(this, msg));
  40. experiment.updateDateLastModified();
  41. }
  42. }
  43. public AmiEventHandler(Experiment experiment) {
  44. amiEventLog = new AmiEventLog(experiment.getAmiEventLogPath());
  45. this.experiment = experiment;
  46. loadLogFile();
  47. }
  48. private void loadLogFile(){
  49. File amiEventLogFile = amiEventLog.getLogFile();
  50. if (amiEventLogFile.exists()) {
  51. InputStream is = null;
  52. try {
  53. XStream xstream = new XStream();
  54. xstream.processAnnotations(AmiEventLog.class);
  55. xstream.processAnnotations(ChemicalAmiEvent.class);
  56. is = FileUtils.openInputStream(amiEventLogFile);
  57. amiEventLog = (AmiEventLog) xstream.fromXML(is);
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. System.err.println("File "+ amiEventLogFile.toString()+ " not found");
  61. } finally{
  62. IOUtils.closeQuietly(is);
  63. }
  64. }
  65. }
  66. public void fireResetLogoutTimerEvent() {
  67. Object[] listeners = listenerList.getListenerList();
  68. // Each listener occupies two elements - the first is the listener class
  69. // and the second is the listener instance
  70. for (int i = 0; i < listeners.length; i += 2) {
  71. if (listeners[i] == ResetLogoutTimerListener.class) {
  72. ((ResetLogoutTimerListener) listeners[i + 1]).processResetLogoutTimer();
  73. }
  74. }
  75. }
  76. public void addResetLogoutTimerListener(ResetLogoutTimerListener listener) {
  77. listenerList.add(ResetLogoutTimerListener.class, listener);
  78. }
  79. public void removeResetLogoutTimerListener(ResetLogoutTimerListener listener) {
  80. listenerList.remove(ResetLogoutTimerListener.class, listener);
  81. }
  82. public void fireUpdateStatusBarEvent(UpdateStatusBarEvent event) {
  83. Object[] listeners = listenerList.getListenerList();
  84. // Each listener occupies two elements - the first is the listener class
  85. // and the second is the listener instance
  86. for (int i = 0; i < listeners.length; i += 2) {
  87. if (listeners[i] == UpdateStatusBarListener.class) {
  88. ((UpdateStatusBarListener) listeners[i + 1]).processUpdateStatusBar(event);
  89. }
  90. }
  91. }
  92. public void addUpdateStatusBarListener(UpdateStatusBarListener listener) {
  93. listenerList.add(UpdateStatusBarListener.class, listener);
  94. }
  95. public void removeUpdateStatusBarListener(UpdateStatusBarListener listener) {
  96. listenerList.remove(UpdateStatusBarListener.class, listener);
  97. }
  98. public AmiEventLog getAmiEventLog() {
  99. return amiEventLog;
  100. }
  101. }