Package org.rssowl.contrib.podcast.util

Source Code of org.rssowl.contrib.podcast.util.PulseService$CustomTimeElapser

package org.rssowl.contrib.podcast.util;

/**
* @author <a href="mailto:christophe@kualasoft.com">Christophe Bouhier</a>
* @author <a href="mailto:andreas.schaefer@madplanet.com">Andreas Schaefer</a>
* @version 1.1
*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;

/**
*
* <ul>
* <li>Add Actionlisteners: Triggered every second</li>
* <li>Add ClockListeners: Triggered every 30 seconds</li>
* </ul>
*/
public class PulseService extends TimerTask {

  private Logger mLog = new Logger(getClass().getName());

  public static Timer mTimer;
//  private static final int actionInterval = 30000;

  private List<ActionListener> mActionListeners = new CopyOnWriteArrayList<ActionListener>();
  private List<IPulseListener> mPulseListeners = new CopyOnWriteArrayList<IPulseListener>();

  /**
   * Application elapsed time in miliseconds.
   */
  public static long mTimeElapsed;

  private static PulseService sSelf;

  public static PulseService getInstance() {
    if (sSelf == null) {
      sSelf = new PulseService();
    }
    return sSelf;
  }

  public PulseService() {
    initialize();
  }

  public void initialize() {
    // No operation.
  }

  public void startService(){
    mTimer = new Timer();
    mTimer.schedule(this, 200, 1000);
    mLog.info("Pulse service started");
//    addEverySecondListener(this);
//    addCustomTimeListener(this);   
  }

  public void stopService(){
    if(mTimer != null){
      mTimer.cancel();
      mTimer.purge(); // CB TODO, what does this do ?
      mLog.info("Pulse service started");
    }
  }
 
  // CB TODO Custom time triggers will be handled differently.
//  public void actionPerformed(ActionEvent evt) {
//    mTimeElapsed += 1000;
//    long lModulo = mTimeElapsed % actionInterval;
//    long division = lModulo / (actionInterval - 1000);
//    if (division >= 1) {
//      notifyTimeElapsed();
//    }
//  }

//  private void notifyTimeElapsed() {
//    for (IPulseListener lPulse : mPulseListeners) {
//      lPulse.timeElapsed(new PulseEvent(this));
//    }
//  }

  /**
   * Auto start the pulse service when adding listeners.
   *
   * @param pListener
   */
  public void addEverySecondListener(ActionListener pListener) {
    if(mTimer == null){
      startService();
     
    }
    if (!mActionListeners.contains(pListener)) {
      mActionListeners.add(pListener);
    }
  }

  public void removeEverySecondListener(ActionListener pListener) {
    if (mActionListeners.contains(pListener)) {
      mActionListeners.remove(pListener);
      if(mActionListeners.size() <= 0){
        stopService();
      }
    }
  }

  public void addCustomTimeListener(IPulseListener pListener) {
    if (!mPulseListeners.contains(pListener)) {
      mPulseListeners.add(pListener);
    }
  }

  public void removeCustomTimeListener(IPulseListener pListener) {
    if (mPulseListeners.contains(pListener)) {
      mPulseListeners.remove(pListener);
    }
  }

  public void run() {
    for (ActionListener lAction : mActionListeners) {
      lAction.actionPerformed(new ActionEvent(this, 101, "clock"));
    }
  }

//  public void timeElapsed(PulseEvent pEvent) {
//    mLog.info("Clock Event: " + pEvent.getSource());
//  }
 
  class CustomTimeElapser{
    long mSeconds;
    CustomTimeElapser(long pSeconds){
      mSeconds = pSeconds;
    }
  }
}
TOP

Related Classes of org.rssowl.contrib.podcast.util.PulseService$CustomTimeElapser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.