package org.kne.cloud.network.klalb.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.border.LineBorder;

import org.jfree.chart.plot.dial.DialTextAnnotation;
import org.jfree.data.general.DefaultValueDataset;
import org.kne.cloud.network.MultiProtocolSocketAddress;
import org.kne.cloud.network.klalb.CONST;
import org.kne.cloud.network.klalb.KLALBUtils;
import org.kne.cloud.network.monitor.SpeedAndTrafficAndDelayMonitorDataImpl;
import org.kne.cloud.network.perf.Kperf;
import org.kne.cloud.network.perf.KperfReports;
import org.kne.ui.XFrame;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.Image;

import javax.swing.JProgressBar;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.imageio.ImageIO;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;

public class KperfGUI extends XFrame{
	private static Timer tmr=new Timer();
	
	private KperfReports reports=new KperfReportsImpl();
	private Kperf kperf;
	private JTextField targetAddressField;
	private JButton startButton;
	private JProgressBar progressBar;
	private DefaultValueDataset upSpeed;
	private DefaultValueDataset upDataSpeed;
	private DialTextAnnotation upSpeedText;
	private DefaultValueDataset downSpeed;
	private DefaultValueDataset downDataSpeed;
	private DialTextAnnotation downSpeedText;
	private JTextField sourceAddressField;

	private SpeedAndTrafficAndDelayMonitorDataImpl monitor;

	private DefaultValueDataset upPPS;

	private DefaultValueDataset upDataPPS;

	private DialTextAnnotation upPPSText;

	private DefaultValueDataset downPPS;

	private DefaultValueDataset downDataPPS;

	private DialTextAnnotation downPPSText;

	private TimerTask tsk;
	private class KperfReportsImpl implements KperfReports{
		@Override
		public void testProcess(double process) {
			progressBar.setValue((int) (process*100.0f));
		}
		@Override
		public void testFinish() {
			if(kperf!=null) {
			kperf.close();
			kperf=null;
			}
			updateButton();
		}
		@Override
		public void updateDial(SpeedAndTrafficAndDelayMonitorDataImpl mdt) {
			monitor=mdt;
		}
		@Override
		public void uploadSpeedFinish(long uploadSpeed) {
			upSpeedLabel.setBandwidth(uploadSpeed);
		}
		@Override
		public void downloadSpeedFinish(long downloadSpeed) {
			downSpeedLabel.setBandwidth(downloadSpeed);
		}
		@Override
		public void uploadPPSFinish(long uploadPPS) {
			upPPSLabel.setText(KLALBUtils.convertUintDefalut(uploadPPS)+"PPS");
		}
		@Override
		public void downloadPPSFinish(long downloadPPS) {
			downPPSLabel.setText(KLALBUtils.convertUintDefalut(downloadPPS)+"PPS");
			
		}
		@Override
		public void uploadDelayFinish(long uploadDelay) {
			// TODO 自动生成的方法存根
			
		}
		@Override
		public void downloadDelayFinish(long downloadDelay) {
			// TODO 自动生成的方法存根
			
		}
		
		
	}
	
	public KperfGUI() {
		this(null);
	}
	public KperfGUI(Component comp) {
		// setResizable(false);
		Image img=getIcon3();
			if(img!=null)
				setIconImage(img);
				// setTitleColor(new Color(255, 255, 255, 250));
				setTitleColor(UIEnv.getDefaultTitleColor());
				getContentPane().setBackground(UIEnv.getDefaultBackgroundColor());
				
				JPanel panel = new JPanel();
				panel.setLayout(new BorderLayout(0, 0));
				
				targetAddressField = new JTextField();
				panel.add(targetAddressField);
				targetAddressField.setColumns(10);
				
				startButton = new JButton(UIEnv.getRsb().getString("starttest"));
				startButton.setForeground(Color.GREEN);
				startButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if(kperf==null) {
							try {
							String tar=targetAddressField.getText();
							MultiProtocolSocketAddress msa=new MultiProtocolSocketAddress(tar);
							String sour=sourceAddressField.getText();
							MultiProtocolSocketAddress msas=new MultiProtocolSocketAddress(sour);
							kperf=new Kperf(msa,msas);
							kperf.setReports(reports);
							kperf.startPerfing();
							}catch(RuntimeException ex) {
								ex.printStackTrace();
								JOptionPane.showMessageDialog(KperfGUI.this,UIEnv.getRsb().getString("invaildinput") ,UIEnv.getRsb().getString("warning"), JOptionPane.WARNING_MESSAGE);
							}
						}else {
							kperf.close();
							kperf=null;
						}
							updateButton();
					}
				});
				panel.add(startButton, BorderLayout.EAST);
				
				JPanel panel_1 = new JPanel();
				panel_1.setOpaque(false);
				getContentPane().add(panel_1, BorderLayout.CENTER);
				panel_1.setLayout(new BorderLayout(0, 0));

				JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
				panel_1.add(tabbedPane, BorderLayout.CENTER);
				
				JPanel panel_2 = new JPanel();
				panel_2.setOpaque(false);
				tabbedPane.addTab(UIEnv.getRsb().getString("bandwidth"), null, panel_2, null);
				panel_2.setLayout(new GridLayout(4, 1, 0, 0));
				
				JPanel panel_3 = new JPanel();
				panel_2.add(panel_3);
				panel_3.setLayout(new BorderLayout(0, 0));
				
				upSpeedLabel = new DoubleBandwidthLabel("? ? ?");
				upSpeedLabel.setLabelForeground(Color.RED);
				upSpeedLabel.setFont(new Font("微软雅黑", Font.PLAIN, 23));
				panel_3.add(upSpeedLabel, BorderLayout.EAST);
				
				JPanel panel_4 = new JPanel();
				panel_2.add(panel_4);
				panel_4.setLayout(new BorderLayout(0, 0));
				
				downSpeedLabel = new DoubleBandwidthLabel("? ? ?");
				downSpeedLabel.setLabelForeground(Color.GREEN);
				downSpeedLabel.setFont(new Font("微软雅黑", Font.PLAIN, 23));
				panel_4.add(downSpeedLabel, BorderLayout.EAST);
				
				JPanel panel_5 = new JPanel();
				panel_2.add(panel_5);
				panel_5.setLayout(new BorderLayout(0, 0));
				
				upPPSLabel = new JLabel("? ? ?PPS");
				upPPSLabel.setPreferredSize(new Dimension(140, 50));
				upPPSLabel.setHorizontalAlignment(SwingConstants.CENTER);
				upPPSLabel.setForeground(Color.RED);
				upPPSLabel.setFont(new Font("微软雅黑", Font.PLAIN, 23));
				panel_5.add(upPPSLabel, BorderLayout.EAST);
				
				JPanel panel_6 = new JPanel();
				panel_2.add(panel_6);
				panel_6.setLayout(new BorderLayout(0, 0));
				
				downPPSLabel = new JLabel("? ? ?PPS");
				downPPSLabel.setPreferredSize(new Dimension(140, 50));
				downPPSLabel.setHorizontalAlignment(SwingConstants.CENTER);
				downPPSLabel.setForeground(Color.GREEN);
				downPPSLabel.setFont(new Font("微软雅黑", Font.PLAIN, 23));
				panel_6.add(downPPSLabel, BorderLayout.EAST);
				
				
				
				/*
				{
					upSpeed = new DefaultValueDataset(0);
					upDataSpeed = new DefaultValueDataset(0);
					DialPlot dpup = new DialPlot();
					dpup.setDataset(1, upSpeed);
					dpup.setDataset(0, upDataSpeed);
					StandardDialFrame sdfs = new StandardDialFrame();
					sdfs.setVisible(false);
					dpup.setDialFrame(sdfs);
					StandardDialScale sds = new StandardDialScale(0, 100, -120, -300, 10, 5);
					sds.setTickRadius(0.9);
					sds.setTickLabelsVisible(false);
					dpup.addScale(0, sds);

					StandardDialRange sdrr = new StandardDialRange(0, 70, Color.GREEN);
					sdrr.setInnerRadius(0.92);
					sdrr.setOuterRadius(0.93);
					dpup.addLayer(sdrr);

					StandardDialRange sdrr2 = new StandardDialRange(70, 90, Color.YELLOW);
					sdrr2.setInnerRadius(0.92);
					sdrr2.setOuterRadius(0.93);
					dpup.addLayer(sdrr2);

					StandardDialRange sdrr3 = new StandardDialRange(90, 100, Color.RED);
					sdrr3.setInnerRadius(0.92);
					sdrr3.setOuterRadius(0.93);
					dpup.addLayer(sdrr3);

					DialPointer.Pointer dpd = new DialPointer.Pointer();
					dpd.setRadius(0.8);
					dpd.setFillPaint(new Color(127, 0, 0, 0));
					dpd.setOutlinePaint(new Color(127, 0, 0));
					dpd.setDatasetIndex(0);
					//dpup.addLayer(dpd);

					DialPointer.Pointer dp = new DialPointer.Pointer();
					dp.setRadius(0.8);
					dp.setFillPaint(Color.RED);
					dp.setOutlinePaint(Color.RED);
					dp.setDatasetIndex(1);
					dpup.addLayer(dp);

					upSpeedText = new DialTextAnnotation("0%");
					upSpeedText.setFont(UIEnv.getFont().deriveFont(20.0f));
					dpup.addLayer(upSpeedText);

					JFreeChart jfup = new JFreeChart(dpup);
					jfup.setBorderPaint(Color.WHITE);
					jfup.setTitle(UIEnv.getRsb().getString("uploadspeed"));
					jfup.getTitle().setFont(UIEnv.getFont().deriveFont(22.0f));
					jfup.getTitle().setPosition(RectangleEdge.BOTTOM);
					dpup.setView(0, 0, 1, 1);
					ChartPanel cpup = new ChartPanel(jfup);
					cpup.setPreferredSize(dashSize);
					cpup.setSize(dashSize);
					cpup.setMinimumSize(dashSize);
					cpup.setBackground(Color.WHITE);
					cpup.setBorder(new LineBorder(Color.LIGHT_GRAY));
					panel_3.add(cpup, BorderLayout.WEST);

					downSpeed = new DefaultValueDataset(0);
					downDataSpeed = new DefaultValueDataset(0);
					DialPlot dpup1 = new DialPlot();
					dpup1.setDataset(1, downSpeed);
					dpup1.setDataset(0, downDataSpeed);
					StandardDialFrame sdfs1 = new StandardDialFrame();
					sdfs1.setVisible(false);
					dpup1.setDialFrame(sdfs1);
					StandardDialScale sds1 = new StandardDialScale(0, 100, -120, -300, 10, 5);
					sds1.setTickRadius(0.9);
					sds1.setTickLabelsVisible(false);
					dpup1.addScale(0, sds1);

					StandardDialRange sdrr1 = new StandardDialRange(0, 70, Color.GREEN);
					sdrr1.setInnerRadius(0.92);
					sdrr1.setOuterRadius(0.93);
					dpup1.addLayer(sdrr1);

					StandardDialRange sdrr21 = new StandardDialRange(70, 90, Color.YELLOW);
					sdrr21.setInnerRadius(0.92);
					sdrr21.setOuterRadius(0.93);
					dpup1.addLayer(sdrr21);

					StandardDialRange sdrr31 = new StandardDialRange(90, 100, Color.RED);
					sdrr31.setInnerRadius(0.92);
					sdrr31.setOuterRadius(0.93);
					dpup1.addLayer(sdrr31);

					DialPointer.Pointer dp11 = new DialPointer.Pointer();
					dp11.setRadius(0.8);
					dp11.setFillPaint(new Color(0, 127, 0, 0));
					dp11.setOutlinePaint(new Color(0, 127, 0));
					dp11.setDatasetIndex(0);
					//dpup1.addLayer(dp11);

					DialPointer.Pointer dp1 = new DialPointer.Pointer();
					dp1.setRadius(0.8);
					dp1.setFillPaint(Color.GREEN);
					dp1.setOutlinePaint(Color.GREEN);
					dp1.setDatasetIndex(1);
					dpup1.addLayer(dp1);

					downSpeedText = new DialTextAnnotation("0%");
					downSpeedText.setFont(UIEnv.getFont().deriveFont(20.0f));
					dpup1.addLayer(downSpeedText);

					JFreeChart jfup1 = new JFreeChart(dpup1);
					jfup1.setBorderPaint(Color.WHITE);
					jfup1.setTitle(UIEnv.getRsb().getString("downloadspeed"));
					jfup1.getTitle().setFont(UIEnv.getFont().deriveFont(22.0f));
					jfup1.getTitle().setPosition(RectangleEdge.BOTTOM);
					dpup1.setView(0, 0, 1, 1);
					ChartPanel cpup1 = new ChartPanel(jfup1);
					cpup1.setPreferredSize(dashSize);
					cpup1.setSize(dashSize);
					cpup1.setMinimumSize(dashSize);
					cpup1.setBackground(Color.WHITE);
					cpup1.setBorder(new LineBorder(Color.LIGHT_GRAY));
					panel_4.add(cpup1, BorderLayout.WEST);

				}
				{
					upPPS = new DefaultValueDataset(0);
					upDataPPS = new DefaultValueDataset(0);
					DialPlot dpup = new DialPlot();
					dpup.setDataset(1, upPPS);
					dpup.setDataset(0, upDataPPS);
					StandardDialFrame sdfs = new StandardDialFrame();
					sdfs.setVisible(false);
					dpup.setDialFrame(sdfs);
					StandardDialScale sds = new StandardDialScale(0, 100, -120, -300, 10, 5);
					sds.setTickRadius(0.9);
					sds.setTickLabelsVisible(false);
					dpup.addScale(0, sds);

					StandardDialRange sdrr = new StandardDialRange(0, 70, Color.GREEN);
					sdrr.setInnerRadius(0.92);
					sdrr.setOuterRadius(0.93);
					dpup.addLayer(sdrr);

					StandardDialRange sdrr2 = new StandardDialRange(70, 90, Color.YELLOW);
					sdrr2.setInnerRadius(0.92);
					sdrr2.setOuterRadius(0.93);
					dpup.addLayer(sdrr2);

					StandardDialRange sdrr3 = new StandardDialRange(90, 100, Color.RED);
					sdrr3.setInnerRadius(0.92);
					sdrr3.setOuterRadius(0.93);
					dpup.addLayer(sdrr3);

					DialPointer.Pointer dp1 = new DialPointer.Pointer();
					dp1.setRadius(0.8);
					dp1.setFillPaint(new Color(127, 0, 0, 0));
					dp1.setOutlinePaint(new Color(127, 0, 0));
					dp1.setDatasetIndex(0);
					//dpup.addLayer(dp1);

					DialPointer.Pointer dp = new DialPointer.Pointer();
					dp.setRadius(0.8);
					dp.setFillPaint(Color.RED);
					dp.setOutlinePaint(Color.RED);
					dp.setDatasetIndex(1);
					dpup.addLayer(dp);

					upPPSText = new DialTextAnnotation("0PPS");
					upPPSText.setFont(UIEnv.getFont().deriveFont(20.0f));
					dpup.addLayer(upPPSText);

					JFreeChart jfup = new JFreeChart(dpup);
					jfup.setBorderPaint(Color.WHITE);
					jfup.setTitle(UIEnv.getRsb().getString("uploadpps"));
					jfup.getTitle().setFont(UIEnv.getFont().deriveFont(22.0f));
					jfup.getTitle().setPosition(RectangleEdge.BOTTOM);
					dpup.setView(0, 0, 1, 1);
					ChartPanel cpup = new ChartPanel(jfup);
					cpup.setPreferredSize(dashSize);
					cpup.setSize(cpup.getPreferredSize());
					cpup.setBackground(Color.WHITE);
					cpup.setBorder(new LineBorder(Color.LIGHT_GRAY));
					panel_5.add(cpup, BorderLayout.WEST);

					downPPS = new DefaultValueDataset(0);
					downDataPPS = new DefaultValueDataset(0);
					DialPlot dpup1 = new DialPlot();
					dpup1.setDataset(1, downPPS);
					dpup1.setDataset(0, downDataPPS);
					StandardDialFrame sdfs1 = new StandardDialFrame();
					sdfs1.setVisible(false);
					dpup1.setDialFrame(sdfs1);
					StandardDialScale sds1 = new StandardDialScale(0, 100, -120, -300, 10, 5);
					sds1.setTickRadius(0.9);
					sds1.setTickLabelsVisible(false);
					dpup1.addScale(0, sds1);

					StandardDialRange sdrr1 = new StandardDialRange(0, 70, Color.GREEN);
					sdrr1.setInnerRadius(0.92);
					sdrr1.setOuterRadius(0.93);
					dpup1.addLayer(sdrr1);

					StandardDialRange sdrr21 = new StandardDialRange(70, 90, Color.YELLOW);
					sdrr21.setInnerRadius(0.92);
					sdrr21.setOuterRadius(0.93);
					dpup1.addLayer(sdrr21);

					StandardDialRange sdrr31 = new StandardDialRange(90, 100, Color.RED);
					sdrr31.setInnerRadius(0.92);
					sdrr31.setOuterRadius(0.93);
					dpup1.addLayer(sdrr31);

					DialPointer.Pointer dp111 = new DialPointer.Pointer();
					dp111.setRadius(0.8);
					dp111.setFillPaint(new Color(0, 127, 0, 0));
					dp111.setOutlinePaint(new Color(0, 127, 0));
					dp111.setDatasetIndex(0);
					///dpup1.addLayer(dp111);
					DialPointer.Pointer dp11 = new DialPointer.Pointer();
					dp11.setRadius(0.8);
					dp11.setFillPaint(Color.GREEN);
					dp11.setOutlinePaint(Color.GREEN);
					dp11.setDatasetIndex(1);
					dpup1.addLayer(dp11);

					downPPSText = new DialTextAnnotation("0PPS");
					downPPSText.setFont(UIEnv.getFont().deriveFont(20.0f));
					dpup1.addLayer(downPPSText);

					JFreeChart jfup1 = new JFreeChart(dpup1);
					jfup1.setBorderPaint(Color.WHITE);
					jfup1.setTitle(UIEnv.getRsb().getString("downloadpps"));
					jfup1.getTitle().setFont(UIEnv.getFont().deriveFont(22.0f));
					jfup1.getTitle().setPosition(RectangleEdge.BOTTOM);
					dpup1.setView(0, 0, 1, 1);
					ChartPanel cpup1 = new ChartPanel(jfup1);
					cpup1.setPreferredSize(dashSize);
					cpup1.setSize(cpup1.getPreferredSize());
					cpup1.setBackground(Color.WHITE);
					cpup1.setBorder(new LineBorder(Color.LIGHT_GRAY));
					panel_6.add(cpup1, BorderLayout.WEST);
				}*/
				
				
				// 上行速度仪表盘
		        upSpeed = new DefaultValueDataset(0);
		        upDataSpeed = new DefaultValueDataset(0);
		        DialPanel dialup=new DialPanel(CONST.dialwidth, UIEnv.getRsb().getString("uploadspeed"), upSpeed, upDataSpeed, Color.RED, new Color(127, 0, 0));
		        dialup.addGradientRanges(UIEnv.RANGE_1);
		        upSpeedText =dialup.getTextAnnotation();

		       
		        panel_3.add(dialup, BorderLayout.WEST);
		        

		        // 下行速度仪表盘
		        downSpeed = new DefaultValueDataset(0);
		        downDataSpeed = new DefaultValueDataset(0);
		        DialPanel dialdown=new DialPanel(CONST.dialwidth, UIEnv.getRsb().getString("downloadspeed"), downSpeed, downDataSpeed, Color.GREEN,  new Color(0, 127, 0));
		        dialdown.addGradientRanges(UIEnv.RANGE_1);
		        downSpeedText =dialdown.getTextAnnotation();
		        
		        panel_4.add(dialdown, BorderLayout.WEST);
				
		        
		        // 上行包速率仪表盘
		        upPPS = new DefaultValueDataset(0);
		        upDataPPS = new DefaultValueDataset(0);
		        DialPanel dialupPPS=new DialPanel(CONST.dialwidth, UIEnv.getRsb().getString("uploadpps"), upPPS, upDataPPS, Color.RED, new Color(127, 0, 0));
		        dialupPPS.addGradientRanges(UIEnv.RANGE_1);
		        upPPSText = dialupPPS.getTextAnnotation();

		        panel_5.add(dialupPPS, BorderLayout.WEST);

		        // 下行包速率仪表盘
		        downPPS = new DefaultValueDataset(0);
		        downDataPPS = new DefaultValueDataset(0);
		        DialPanel dialdownPPS=new DialPanel(CONST.dialwidth, UIEnv.getRsb().getString("downloadpps"), downPPS, downDataPPS, Color.GREEN,  new Color(0, 127, 0));
		        dialdownPPS.addGradientRanges(UIEnv.RANGE_1);
		        downPPSText = dialdownPPS.getTextAnnotation();

		        panel_6.add(dialdownPPS, BorderLayout.WEST);
		        
				JPanel panel_top = new JPanel();
				panel_1.add(panel_top, BorderLayout.NORTH);
				panel_top.setLayout(new GridLayout(0, 2, 0, 0));
				JPanel panel_7 = new JPanel();
				panel_7.setOpaque(false);
				panel_top.add(panel_7);
				panel_7.setLayout(new BorderLayout(0, 0));
				
				sourceAddressField = new JTextField();
				panel_7.add(sourceAddressField);
				sourceAddressField.setColumns(10);
				
				JLabel lblNewLabel_4 = new JLabel("");
				Image img2= getIcon2();
				if(img2!=null)
				lblNewLabel_4.setIcon(new ImageIcon(img2));
				lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
				panel_7.add(lblNewLabel_4, BorderLayout.EAST);
				panel_top.add(panel);
				
			
				
				progressBar = new JProgressBar();
				progressBar.setPreferredSize(new Dimension(100, 5));
				getContentPane().add(progressBar, BorderLayout.NORTH);
				getTitlelabel().setFont(UIEnv.getFont().deriveFont(17.0f));
				getTitlepanel().setBorder(new LineBorder(Color.LIGHT_GRAY));
				getTitlelabel().setForeground(Color.WHITE);
				
				setTitle(UIEnv.getRsb().getString("klalbperf"));
				setSize(CONST.windowwidth,CONST.windowheight );
				setResizable(false);
				setLocationRelativeTo(comp);
				tsk=new TimerTask() {
					
					@Override
					public void run() {
						if(monitor!=null) {
						double outload = monitor.getOutSpeed() * 100.0 / monitor.getOutSpeedMax();
						if (Double.isFinite(outload)) {
							upSpeedText.setLabel(KLALBUtils.convertIBUint(monitor.getOutSpeed()) + "/s");
							upSpeed.setValue(Math.min(upSpeed.getValue().doubleValue() * 0.98 + outload * 0.02, 101.0));
						}

						double inload = monitor.getInSpeed() * 100.0 / monitor.getInSpeedMax();
						if (Double.isFinite(inload)) {
							downSpeedText.setLabel(KLALBUtils.convertIBUint(monitor.getInSpeed()) + "/s");
							downSpeed.setValue(Math.min(downSpeed.getValue().doubleValue() * 0.98 + inload * 0.02, 101.0));
						}

						double outPPSload = monitor.getOutPPS() * 100.0 / monitor.getOutPPSMax();
						if (Double.isFinite(outPPSload)) {
							upPPSText.setLabel(KLALBUtils.convertUintDefalut(monitor.getOutPPS()) + "PPS");
							upPPS.setValue(Math.min(upPPS.getValue().doubleValue() * 0.98 + outPPSload * 0.02, 101.0));
						}

						double inPPSload = monitor.getInPPS() * 100.0 / monitor.getInPPSMax();
						if (Double.isFinite(inPPSload)) {
							downPPSText.setLabel(KLALBUtils.convertUintDefalut(monitor.getInPPS()) + "PPS");
							downPPS.setValue(Math.min(downPPS.getValue().doubleValue() * 0.98 + inPPSload * 0.02, 101.0));
						}
						}
					}
				};
				tmr.scheduleAtFixedRate(tsk, 200, 100);
				addWindowListener(new WindowListener() {
					
					@Override
					public void windowOpened(WindowEvent e) {
						// TODO 自动生成的方法存根
						
					}
					
					@Override
					public void windowIconified(WindowEvent e) {
						// TODO 自动生成的方法存根
						
					}
					
					@Override
					public void windowDeiconified(WindowEvent e) {
						// TODO 自动生成的方法存根
						
					}
					
					@Override
					public void windowDeactivated(WindowEvent e) {
						// TODO 自动生成的方法存根
						
					}
					
					@Override
					public void windowClosing(WindowEvent e) {
						if(tsk!=null)
						tsk.cancel();
					}
					
					@Override
					public void windowClosed(WindowEvent e) {
						// TODO 自动生成的方法存根
						
					}
					
					@Override
					public void windowActivated(WindowEvent e) {
						// TODO 自动生成的方法存根
						
					}
				});
	}
	
	
	
	private Image icon2;
	private Image getIcon2() {
		if(icon2==null)
			try {
				icon2=ImageIO.read(KperfGUI.class.getResourceAsStream("/assets/speedtest2.png")).getScaledInstance(20, 20, Image.SCALE_SMOOTH);
			} catch (IOException e) {
				e.printStackTrace();
			}
		return icon2;
	}
	
	private Image icon3;

	private DoubleBandwidthLabel upSpeedLabel;

	private DoubleBandwidthLabel downSpeedLabel;

	private JLabel upPPSLabel;

	private JLabel downPPSLabel;
	private Image getIcon3() {
		if(icon3==null)
			try {
				icon3=ImageIO.read(KperfGUI.class.getResourceAsStream("/assets/speedtest3.png")).getScaledInstance(20, 20, Image.SCALE_SMOOTH);
			} catch (IOException e) {
				e.printStackTrace();
			}
		return icon3;
	}
	private void updateButton() {
		if(kperf==null) {
			startButton.setForeground(Color.GREEN);
			startButton.setText(UIEnv.getRsb().getString("starttest"));
		}else {
			startButton.setForeground(Color.RED);
			startButton.setText(UIEnv.getRsb().getString("stoptest"));
		}
	}
	public void setTarget(MultiProtocolSocketAddress target) {
		targetAddressField.setText(target.toString()); 
	}
	public void setTarget(MultiProtocolSocketAddress target, MultiProtocolSocketAddress source) {
		targetAddressField.setText(target.toString()); 
		sourceAddressField.setText(source.toString()); 
	}
}
