import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.json.JSONObject;
public class TrayUpdate implements Runnable {
public static String code; // 股票代码
public static ArrayList<StockBean> list; // 获取的股票数据
TrayIcon trayIcon1, trayIcon2;
// 停市控制器
public static boolean isStop = false;
public TrayUpdate(TrayIcon trayIcon1, TrayIcon trayIcon2) throws Exception {
this.trayIcon1 = trayIcon1;
this.trayIcon2 = trayIcon2;
// 先从配置文件取股票代码,没有启用默认代码
String r = FileUtil.readTxtFile();
// 获取配置文件参数
JSONObject jsonObject;
if (r == null || r.equals("")) {
jsonObject = new JSONObject();
jsonObject.put("code", "sz000838");
FileUtil.contentToTxt(jsonObject.toString());
code = "sz000838";
} else {
jsonObject = new JSONObject(r);
code = jsonObject.getString("code").toString();
}
}
@Override
public void run() {
StringToImage formatImg = new StringToImage();
// 循环请求网络数据
while (true) {
try {
if (!isStop) {
// 返回的股票数据
list = HttpUtil.StockHttp(code);
// System.out.println(list.get(0).getCurPrice());
// 是否有触发预警
checkLimit(list.get(0).getCurPrice(), list.get(0).getTime());
// 当前价格转成Image
String currentPrice = currentPrice(list.get(0).getCurPrice());
String[] prices = currentPrice.split("\\.");
Image img1 = formatImg.StrToImg(prices[1].substring(0, 2),
setColor(list));
Image img2 = formatImg.StrToImg(prices[0], setColor(list));
// 设置图片
trayIcon1.setImage(img1);
// 设置悬浮提示
trayIcon1.setToolTip(getToolTips(list));
trayIcon2.setImage(img2);
trayIcon2.setToolTip(getToolTips(list));
// 停市
if (list.get(0).getTime().startsWith("15:")) {
isStop = true;
}
}
Thread.sleep(2000);
} catch (Exception e) {
// 异常时退出程序
System.exit(0);
}
}
}
/**
* 判断是否需要预警
*
* @param curPrice
* @param time
*/
public static void checkLimit(String curPrice, String time) {
try {
float p = Float.parseFloat(curPrice);
String r = FileUtil.readTxtFile();
JSONObject jsonObject;
if (r == null || r.equals("")) {
return;
} else {
jsonObject = new JSONObject(r);
}
String max = jsonObject.getString("max");
String min = jsonObject.getString("min");
int maxW = jsonObject.getInt("maxW");
int minW = jsonObject.getInt("minW");
if ("".equals(max)) {
return;
} else {
// 超出上限
if (maxW != 1 && p >= Float.parseFloat(max)) {
showLimitDialog(1, time);
jsonObject.put("maxW", 1);
FileUtil.contentToTxt(jsonObject.toString());
}
// 低于下限
if (minW != 1 && p <= Float.parseFloat(min)) {
showLimitDialog(0, time);
jsonObject.put("minW", 1);
FileUtil.contentToTxt(jsonObject.toString());
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 预警弹窗
*
* @param i
*/
private static void showLimitDialog(int i, String time) {
final TipWindow tw = new TipWindow(200, 100);
// tw.setTitle("预警提示");
JPanel headPan = new JPanel();
JPanel btnPan = new JPanel();
JButton update = new JButton("CLOSE");
JLabel head = new JLabel();
if (i == 0) {
head.setForeground(Color.green);
head.setText("<html>警告! 已到价格下限<br>" + time + "</html>");
} else if (i == 1) {
head.setForeground(Color.red);
head.setText("<html>恭喜! 已到价格上限<br>" + time + "</html>");
}
headPan.add(head);
btnPan.add(update);
update.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
tw.close();
}
});
tw.add(headPan, BorderLayout.CENTER);
tw.add(btnPan, BorderLayout.SOUTH);
tw.setAlwaysOnTop(true);
tw.setResizable(false);
tw.setVisible(true);
tw.run();
}
/**
* 取当前价
* @param price
* @return
*/
public static String currentPrice(String price){
float f1 = Float.parseFloat(list.get(0).getCurPrice());
// 开市前的判断
if (f1 == 0) {
if (list.get(0).getTime().startsWith("09:1")
|| list.get(0).getTime().startsWith("09:2")) {
return list.get(0).getBuyPrice(); // 竞买价
}
}
return list.get(0).getCurPrice();
}
/**
* 涨跌判断
*
* @param list
* @return
*/
public static int setColor(ArrayList<StockBean> list) {
float f1 = Float.parseFloat(list.get(0).getCurPrice());
float f2 = Float.parseFloat(list.get(0).getYtdPrice());
// 开市前的判断
if (f1 == 0) {
if (list.get(0).getTime().startsWith("09:1")
|| list.get(0).getTime().startsWith("09:2")) {
f1 = Float.parseFloat(list.get(0).getBuyPrice()); // 竞买价
} else {
return 0;
}
}
if (f1 - f2 > 0) {
return 1;
} else if (f1 - f2 < 0) {
return -1;
} else {
return 0;
}
}
/**
* 获取悬浮提示内容
*
* @param list
* @return
*/
public static String getToolTips(ArrayList<StockBean> list) {
float f1 = Float.parseFloat(list.get(0).getCurPrice());
float f2 = Float.parseFloat(list.get(0).getYtdPrice());
// 开市前的判断
if (f1 == 0) {
if (list.get(0).getTime().startsWith("09:1")
|| list.get(0).getTime().startsWith("09:2")) {
f1 = Float.parseFloat(list.get(0).getBuyPrice()); // 竞买价
} else {
return list.get(0).getName() + " " + list.get(0).getCode()
+ " 0% 0 ";
}
}
float[] f = new float[2];
f[0] = f1 - f2;
f[1] = f[0] / f2 * 100;
return list.get(0).getName() + " " + list.get(0).getCode() + " "
+ getTwo(f[0]) + " " + getTwo(f[1]) + "% "
+ list.get(0).getTime() + " ";
}
/**
* 取小数点后两位
*
* @param f
* @return
*/
public static String getTwo(float f) {
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
return df.format(f);
}
}