package org.hwo.pulscounter; import java.io.FileNotFoundException; import java.util.LinkedList; import java.util.List; import java.util.Vector; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; import org.hwo.configuration.ConfigurableObjects; import org.hwo.io.SerialPort; import org.hwo.io.NewSerialPort.NewSerialPort; import org.hwo.servicelink.ServiceLink; import org.hwo.servicelink.ServiceLinkListener; import org.hwo.tasklet.TaskletManager; import org.hwo.pulscounter.SnapshotManager.Notification; import org.hwo.pulscounter.ui.AppSettingsListener; import org.hwo.scheduler.Scheduler; public class PulsCounter2Application implements ServiceLinkListener{ static PulsCounter2Application _application; public static PulsCounter2Application getApplication(){ if (_application == null) _application = new PulsCounter2Application(); return _application; } private NewSerialPort serialPort; private ServiceLink serviceLink; private List appSettingsListeners; private List applicationListeners; private boolean snapshotLock; private Vector unseenMessages; private SnapshotManager snapshotManager; private List exportSettings; private Scheduler scheduler; public PulsCounter2Application() { appSettingsListeners = new LinkedList(); applicationListeners = new LinkedList(); unseenMessages = new Vector(); exportSettings = new LinkedList(); scheduler = new Scheduler(); loadPrefs(); try { snapshotManager = new SnapshotManager(); snapshotManager.notify(Notification.INITIALIZE); } catch (FileNotFoundException e){ e.printStackTrace(); } } public void addAppSettingsListener(AppSettingsListener listener){ appSettingsListeners.add(listener); } public void removeAppSettingsListener(AppSettingsListener listener){ appSettingsListeners.remove(listener); } public void addPulsCounterApplicationListener(PulsCounterApplicationListener listener){ applicationListeners.add(listener); } public void removePulsCounterApplicationListener(PulsCounterApplicationListener listener){ applicationListeners.remove(listener); } public void fireServiceLinkChanged(){ for (AppSettingsListener l: appSettingsListeners){ l.ServiceLinkChanged(serviceLink); } } public void fireSerialPortChanged(){ for (PulsCounterApplicationListener listener: applicationListeners){ listener.serialPortChanged(); } } public void fireConnectionStateChanged(){ fireConnectionStateChanged(getServiceLink().isOpen()); } public void fireConnectionStateChanged(Boolean connected){ for (PulsCounterApplicationListener listener: applicationListeners){ listener.connectionStateChanged(connected); } } public void message(String message){ if (applicationListeners.size() == 0){ unseenMessages.addElement(message); } else { while (!unseenMessages.isEmpty()){ String msg = unseenMessages.remove(0); for (PulsCounterApplicationListener listener: applicationListeners){ listener.messageArrived(msg); } } for (PulsCounterApplicationListener listener: applicationListeners){ listener.messageArrived(message); } }; } public synchronized NewSerialPort getSerialPort() { if (serialPort == null){ serialPort = new NewSerialPort("COM1:"); } return serialPort; } public synchronized void setSerialPort(NewSerialPort serialPort) { if (serviceLink != null){ serviceLink.close(); serviceLink = null; } this.serialPort = serialPort; getServiceLink(); fireServiceLinkChanged(); fireSerialPortChanged(); } public synchronized ServiceLink getServiceLink() { if (serviceLink == null){ serviceLink = new ServiceLink(getSerialPort()); serviceLink.getSerialPort().setTimeOut(200); serviceLink.addServiceLinkListener(this); } return serviceLink; } public synchronized void setServiceLink(ServiceLink serviceLink) { if (serviceLink != null){ serviceLink.close(); } this.serviceLink = serviceLink; fireServiceLinkChanged(); } private Preferences getPreferencesNode(){ return Preferences.userNodeForPackage(getClass()); } public void savePrefs(){ Preferences prefs = getPreferencesNode(); if (serialPort != null) prefs.put("io.port", serialPort.getPortName()); System.out.println(String.format("savePrefs(): %d exportSettings werden gesichert.", exportSettings.size())); if (exportSettings.size()>0) { for (int n=0;n getExportSettings() { return exportSettings; } public void shutdown(){ System.err.println("Shutting down..."); this.scheduler.shutdown(); this.snapshotManager.doShutdown(); TaskletManager.instance().shutdown(); this.getServiceLink().close(); } }