/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
- package uk.ac.cam.ch.ami.amievents;
- import com.thoughtworks.xstream.XStream;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOUtils;
- import uk.ac.cam.ch.ami.Experiment;
- import uk.ac.cam.ch.ami.amievents.events.AmiEvent;
- import uk.ac.cam.ch.ami.amievents.events.ChemicalAmiEvent;
- import uk.ac.cam.ch.ami.amievents.log.AmiEventLog;
- import uk.ac.cam.ch.ami.events.UpdateStatusBarEvent;
- import uk.ac.cam.ch.ami.listeners.ResetLogoutTimerListener;
- import uk.ac.cam.ch.ami.listeners.UpdateStatusBarListener;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- /**
- * Central handler which listens for all AmiEvents and processes them accordingly.
- * <p>
- * Created by IntelliJ IDEA.
- * User: alt36
- * Date: 02-Sep-2010
- * Time: 19:26:08
- */
- public class AmiEventHandler implements AmiEventListener {
- AmiEventLog amiEventLog = null;
- protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList();
- Experiment experiment;
- /**
- * Process an incoming AmiEvent. The event is appended to the log, the LogoutTimer (which logs the user out after a period
- * of inactivity) in HomeScreen is reset to zero, and the status bar of ExperimentScreen is updated to provide
- * feedback that the event has occurred.
- * @param event the event to process
- */
- @Override
- public void processAmiEvent(AmiEvent event) {
- amiEventLog.appendEvent(event);
- fireResetLogoutTimerEvent();
- if(!event.isToBeDeleted()) {
- String msg = event.getAmiEventType() + ": " + event.getTimelineText();
- fireUpdateStatusBarEvent(new UpdateStatusBarEvent(this, msg));
- experiment.updateDateLastModified();
- }
- }
- public AmiEventHandler(Experiment experiment) {
- amiEventLog = new AmiEventLog(experiment.getAmiEventLogPath());
- this.experiment = experiment;
- loadLogFile();
- }
- private void loadLogFile(){
- File amiEventLogFile = amiEventLog.getLogFile();
- if (amiEventLogFile.exists()) {
- InputStream is = null;
- try {
- XStream xstream = new XStream();
- xstream.processAnnotations(AmiEventLog.class);
- xstream.processAnnotations(ChemicalAmiEvent.class);
- is = FileUtils.openInputStream(amiEventLogFile);
- amiEventLog = (AmiEventLog) xstream.fromXML(is);
- } catch (IOException e) {
- e.printStackTrace();
- System.err.println("File "+ amiEventLogFile.toString()+ " not found");
- } finally{
- IOUtils.closeQuietly(is);
- }
- }
- }
- public void fireResetLogoutTimerEvent() {
- Object[] listeners = listenerList.getListenerList();
- // Each listener occupies two elements - the first is the listener class
- // and the second is the listener instance
- for (int i = 0; i < listeners.length; i += 2) {
- if (listeners[i] == ResetLogoutTimerListener.class) {
- ((ResetLogoutTimerListener) listeners[i + 1]).processResetLogoutTimer();
- }
- }
- }
- public void addResetLogoutTimerListener(ResetLogoutTimerListener listener) {
- listenerList.add(ResetLogoutTimerListener.class, listener);
- }
- public void removeResetLogoutTimerListener(ResetLogoutTimerListener listener) {
- listenerList.remove(ResetLogoutTimerListener.class, listener);
- }
- public void fireUpdateStatusBarEvent(UpdateStatusBarEvent event) {
- Object[] listeners = listenerList.getListenerList();
- // Each listener occupies two elements - the first is the listener class
- // and the second is the listener instance
- for (int i = 0; i < listeners.length; i += 2) {
- if (listeners[i] == UpdateStatusBarListener.class) {
- ((UpdateStatusBarListener) listeners[i + 1]).processUpdateStatusBar(event);
- }
- }
- }
- public void addUpdateStatusBarListener(UpdateStatusBarListener listener) {
- listenerList.add(UpdateStatusBarListener.class, listener);
- }
- public void removeUpdateStatusBarListener(UpdateStatusBarListener listener) {
- listenerList.remove(UpdateStatusBarListener.class, listener);
- }
- public AmiEventLog getAmiEventLog() {
- return amiEventLog;
- }
- }