PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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