<commit_before>package shakey.internal

import javax.swing._
import java.applet.Applet
import shakey.services.{LoggerSupport, Stock}
import java.awt._
import java.awt.event._
import java.awt.image.BufferedImage
import shakey.app.ShakeyApp
import java.awt.TrayIcon.MessageType
import java.util.concurrent.PriorityBlockingQueue
import shakey.internal.MessageNotifier.StockWithTime
import org.joda.time.DateTime

/**
 * 消息通知
 */
object MessageNotifier {
  val queue = new PriorityBlockingQueue[StockWithTime](15)

  class StockWithTime(val stock: Stock, val time: Long) extends Comparable[StockWithTime] {
    override def compareTo(o: StockWithTime): Int = {
      o.time.compareTo(time)
    }

    override def equals(obj: scala.Any): Boolean = {
      obj.asInstanceOf[StockWithTime].stock.symbol == stock.symbol
    }

    override def toString: String = {
      new DateTime(time).toString("HH:mm") + " " + stock.symbol
    }
  }

}

class MessageNotifier(name: String) extends LoggerSupport {
  private val codebase = getClass.getResource("/ding.wav")
  private val ding = Applet.newAudioClip(codebase);
  private var trayIcon: TrayIcon = _
  //private val image:Image= loadTrayIcon("/ico.png","Shakey").getImage
  //private val image2:Image= loadTrayIcon("/ico2.png","Shakey").getImage
  private val image: Image = iconToImage(new BevelArrowIcon(BevelArrowIcon.UP, false, false))
  private val image2: Image = iconToImage(new BevelArrowIcon(BevelArrowIcon.DOWN, false, false))
  private var timer: Timer = _
  private val tray = SystemTray.getSystemTray
  @volatile
  private var runFlag = false

  initTray

  def play {
    ding.play()
    runFlag = true
  }

  private def showStockInfo {
    //TODO 打开一个panel能够展示提醒的股票信息
    val builder = new StringBuilder
    val it = MessageNotifier.queue.iterator()
    while (it.hasNext) {
      builder.append(it.next()).append("\n")
    }
    trayIcon.displayMessage("Shakey", builder.toString(), MessageType.INFO)
    runFlag = false
  }

  def initTray {
    val mouseListener = new MouseAdapter() {
      override def mouseClicked(e: MouseEvent): Unit = {
        //双击图标不再闪动
        if (e.getClickCount == 2) {
          showStockInfo
        }
      }
    };

    val exitListener = new ActionListener() {
      override def actionPerformed(e: ActionEvent): Unit = {
        tray.remove(trayIcon)
        logger.debug("exiting ...")
        ShakeyApp.stopServer()
        System.exit(0);
      }
    };
    val aboutListener = new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = {
        trayIcon.displayMessage("Shakey", "powered by you and me!", MessageType.INFO)
      }
    }<commit_msg>FIXES #24 修正报警信息未能按照时间进行排序<commit_after>package shakey.internal

import javax.swing._
import java.applet.Applet
import shakey.services.{LoggerSupport, Stock}
import java.awt._
import java.awt.event._
import java.awt.image.BufferedImage
import shakey.app.ShakeyApp
import java.awt.TrayIcon.MessageType
import java.util.concurrent.{TimeUnit, PriorityBlockingQueue}
import shakey.internal.MessageNotifier.StockWithTime
import org.joda.time.DateTime

/**
 * 消息通知
 */
object MessageNotifier {
  val queue = new PriorityBlockingQueue[StockWithTime](15)

  class StockWithTime(val stock: Stock, val time: Long) extends Comparable[StockWithTime] {
    override def compareTo(o: StockWithTime): Int = {
      o.time.compareTo(time)
    }

    override def equals(obj: scala.Any): Boolean = {
      obj.asInstanceOf[StockWithTime].stock.symbol == stock.symbol
    }

    override def toString: String = {
      new DateTime(time).toString("HH:mm") + " " + stock.symbol
    }
  }

}

class MessageNotifier(name: String) extends LoggerSupport {
  private val codebase = getClass.getResource("/ding.wav")
  private val ding = Applet.newAudioClip(codebase);
  private var trayIcon: TrayIcon = _
  //private val image:Image= loadTrayIcon("/ico.png","Shakey").getImage
  //private val image2:Image= loadTrayIcon("/ico2.png","Shakey").getImage
  private val image: Image = iconToImage(new BevelArrowIcon(BevelArrowIcon.UP, false, false))
  private val image2: Image = iconToImage(new BevelArrowIcon(BevelArrowIcon.DOWN, false, false))
  private var timer: Timer = _
  private val tray = SystemTray.getSystemTray
  @volatile
  private var runFlag = false

  initTray

  def play {
    ding.play()
    runFlag = true
  }

  private def showStockInfo {
    //TODO 打开一个panel能够展示提醒的股票信息
    val builder = new StringBuilder
    val queue = MessageNotifier.queue
    0 until queue.size() foreach {
      case i =>
        val stock = queue.poll(1, TimeUnit.MILLISECONDS)
        if (stock != null)
          builder.append(stock)
    }
    trayIcon.displayMessage("Shakey", builder.toString(), MessageType.INFO)
    runFlag = false
  }

  def initTray {
    val mouseListener = new MouseAdapter() {
      override def mouseClicked(e: MouseEvent): Unit = {
        //双击图标不再闪动
        if (e.getClickCount == 2) {
          showStockInfo
        }
      }
    };

    val exitListener = new ActionListener() {
      override def actionPerformed(e: ActionEvent): Unit = {
        tray.remove(trayIcon)
        logger.debug("exiting ...")
        ShakeyApp.stopServer()
        System.exit(0);
      }
    };
    val aboutListener = new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = {
        trayIcon.displayMessage("Shakey", "powered by you and me!", MessageType.INFO)
      }
    }
{'fraction_non_alphanumeric': 0.10777126099706745, 'fraction_numerical': 0.0032991202346041057, 'mean_word_length': 3.358626198083067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4259285', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1751, 'n_words': 386}