⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 view.java

📁 The WLAN Traffic Visualizer is a platform independent Java program providing accurate measurement of
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package view;

//Copyright (C) 2008 Harald Unander, Wang Wenjuan
//
//    This file is part of WlanTV.
//
//    WlanTV is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    WlanTV is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with WlanTV.  If not, see <http://www.gnu.org/licenses/>.

import icons.MyIcons;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InvalidClassException;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.EventObject;
import java.util.concurrent.Callable;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;
import javax.swing.event.MouseInputAdapter;
import javax.swing.filechooser.FileNameExtensionFilter;

import view.TrainPanel.LocateResult;
import main.Main;
import main.WlanTvProperties;
import model.Availability;
import model.Capture;
import model.Command;
import model.DataSet;
import model.Model;
import model.Packet;
import model.RawPacket;
import model.Statistics;
import model.Command.LowMemException;
import model.Transaction.OneTransaction;

@SuppressWarnings("serial")
public class View extends JFrame {

	public ActionListener buttonHandler;
	public ItemListener radioButtonHandler;
	public ItemListener dropDownHandler;
	public MouseInputAdapter mouseHandler;
	public TrainPanel trainPanel;

	private PacketDetailsPanel packetDetails;
	private PacketPanel packetPanel;
	private ComboBar comboBar;
	private ButtonBar buttonBar;
	private JTabbedPane tabbedPane;
	private JPanel mainPane;
	private JPanel content;
	private SummaryPanel summary;
	private ConversationPanel conversationPanel;
	private PieChart pieChartTime, pieChartCount;
	private InfoBar infoBar;
	private LoadChart loadChart;

	private Capture capture;
	private String selectedLiveIf;

	private String captureFile;
	private Availability availability;
	private Model model;
	private String packetFilter = "";
	private HeapMonitor heapMonitor;

	static public enum MY_EVENT {
		CAPTURE_ABORT, CAPTURE_COMPLETE
	};

	public View(Model model) {
		this.model = model;
	}

	public void start() {
		setTitle("Wlan Traffic Visualizer - v" + Main.class.getPackage().getImplementationVersion());
		setIconImage(MyIcons.getImage(this, "wlantv.png"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize((int) Main.GE.width * 3 / 4, (int) Main.GE.height * 3 / 4);
		content = new JPanel(new BorderLayout());
		this.setContentPane(content);

		// Populate tabbed pane
		tabbedPane = new JTabbedPane();
		tabbedPane.setPreferredSize(new Dimension(300, 0));
		summary = new SummaryPanel();
		tabbedPane.add(summary, "Summary");
		packetPanel = new PacketPanel();
		tabbedPane.add(packetPanel, "Packet");
		conversationPanel = new ConversationPanel();
		tabbedPane.add(conversationPanel, "Conversations");
		packetDetails = new PacketDetailsPanel();
		tabbedPane.add(packetDetails, "Details");
		tabbedPane.setSelectedComponent(packetPanel);
		tabbedPane.setVisible(false);

		// Populate main pane
		trainPanel = new TrainPanel();
		mainPane = new JPanel(new BorderLayout());
		mainPane.add(trainPanel, BorderLayout.CENTER);
		mainPane.add(Main.myLogging, BorderLayout.SOUTH);

		// Populate button bars
		heapMonitor = new HeapMonitor();
		infoBar = new InfoBar();
		buttonBar = new ButtonBar(this, buttonHandler, radioButtonHandler);
		buttonBar.add(infoBar);
		buttonBar.add(Box.createHorizontalGlue());
		buttonBar.add(heapMonitor);
		comboBar = new ComboBar(this, dropDownHandler);
		comboBar.setAdvancedMenu(showFillPackets());		

		content.add(mainPane, BorderLayout.CENTER);
		content.add(tabbedPane, BorderLayout.EAST);
		content.add(buttonBar, BorderLayout.NORTH);
		content.add(comboBar, BorderLayout.SOUTH);

		setVisible(true);

	}

	// Menu functions

	public void startLiveCapture() {
		Thread t = new Thread() {
			@Override
			public void run() {
				if (selectedLiveIf != null) {
					setTitle("WlanTV - " + captureFile);

					runCapture("-S -w" + addQuotesForWindows(captureFile) + " -i" + selectedLiveIf);
				}
				else {
					fireMyEvent(MY_EVENT.CAPTURE_ABORT);
				}
			}
		};

		String logDir = Main.properties.getProperty("livelog.dir",System.getProperty("user.dir"));
		if (!new File(logDir).isDirectory()) {
			logDir = System.getProperty("user.dir");
		}

		String candidateCaptureFile = logDir + File.separatorChar + "wlantv_" + Main.getDate() + ".cap";

		if (selectedLiveIf == null)
			selectedLiveIf = getIface(candidateCaptureFile);

		if (selectedLiveIf != null) {
			captureFile = candidateCaptureFile;
			t.setName("LiveCapture");
			t.start();
		}
		else {
			fireMyEvent(MY_EVENT.CAPTURE_ABORT);
		}
	}

	public void startDeadCapture() {
		Thread t = new Thread() {
			@Override
			public void run() {
				String file = null;
				try {
					file = selectCaptureFile();
				} catch (Exception e) {
					// e.printStackTrace();
					JOptionPane.showMessageDialog(null, e.getMessage());
					fireMyEvent(MY_EVENT.CAPTURE_ABORT);
					return;
				}
				captureFile = file;
				setTitle("WlanTV - " + captureFile + packetFilter);

				if (captureFile.endsWith(".wtvcap"))
					recallCapture(captureFile);
				else
					runCapture("-r " + addQuotesForWindows(captureFile)+packetFilter);
			}
		};
		t.setName("DeadCapture");
		t.start();
	}

	public void stopCapture() {
		if (capture != null)
			capture.abortCapture();
	}

	public void snapshot() {
		int i = 1;
		File picFile;
		do {
			picFile = new File(captureFile + "." + i + ".png");
			i++;
		} while (picFile.exists());
		try {
			Dimension size = trainPanel.getSize();
			BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
			trainPanel.paintSnapshot(image.getGraphics(),
					captureFile + "   " + infoBar.gridLabel.getText() + 
					"   Frame numbers:" + getViewPacketSpan(),
					"WlanTV   " +Main.getDate1() + "   http://wlantv.sourceforge.net/");
			ImageIO.write(image, "png", picFile);
			JOptionPane.showMessageDialog(null, "Saved snapshot to file " + picFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void storeCapture() {
		ArrayList<RawPacket> rp = new ArrayList<RawPacket>();
		for (int j=trainPanel.getViewFirstTransactionNo(); j<=trainPanel.getViewLastTransactionNo(); j++) {
			OneTransaction one = model.getTransaction().list.get(j);
			for (int i=0; i<one.packetList.size(); i++)
				rp.add((RawPacket)one.packetList.get(i));
		}
		String name = captureFile + ".wtvcap";
		Main.save(new File(name), rp);
		JOptionPane.showMessageDialog(null, "Saved capture to file " + name);
	}

	public void makeLoadChart() {
		if (loadChart != null)
			tabbedPane.remove(loadChart);
		loadChart = new LoadChart(availability.getLoad(), this);
		tabbedPane.add(loadChart, "Load Chart");
		tabbedPane.setSelectedComponent(loadChart);
	}

	public void makePieChart() {
		if (availability != null) {
			if (pieChartTime != null)
				tabbedPane.remove(pieChartTime);
			if (pieChartCount != null)
				tabbedPane.remove(pieChartCount);

			// System.out.println("Collecting statistics...");
			Statistics stat = new Statistics(model.getTransaction(), availability);

			// System.out.println("Making pie charts...");

			DataSet[] dsTime = { stat.dsTotalDur, stat.dsFPartDur, stat.dsFTypeDur };
			pieChartTime = new PieChart(dsTime);
			pieChartTime.setLayout(new GridLayout(3, 1));
			tabbedPane.add(pieChartTime, "Pie Chart Time");

			DataSet[] dsCount = { stat.dsModeCount, stat.dsRateCount, stat.dsErrorCount };
			pieChartCount = new PieChart(dsCount);
			pieChartCount.setLayout(new GridLayout(3, 1));
			tabbedPane.add(pieChartCount, "Pie Chart Count");

			tabbedPane.setSelectedComponent(pieChartTime);
		}
	}

	// Mouse functions

	public void showPacketDetails(int x, int y) {
		LocateResult lrFrame = trainPanel.locateFrame(x, y);
		// String s = "";
		if (lrFrame != null && lrFrame.packet != null) {
			// s =
			// r.fType.toString()+" No."+r.frameNumber;//+" ToDs:"+r.wlanToDs
			// +" FromDs:"+r.wlanFromDs;
			if (packetPanel.isVisible()) {
				setPacketText(lrFrame.packet);
				if (lrFrame.one != null)
					setTransactionText(lrFrame.one);
			}
		} else {
			LocateResult lrTrans = trainPanel.locateTransaction(x, y);
			if (lrTrans != null && lrTrans.one != null) {
				// s = one.type.toString() + " transaction";
				if (packetPanel.isVisible()) {
					setTransactionText(lrTrans.one);
					if (lrTrans.packet != null)
						setPacketText(lrTrans.packet);
				}
			}
		}
	}

	public void showPacket(int x, int y) {
		showPacketDetails(x, y);
		tabbedPane.setSelectedComponent(packetPanel);
	}

	public void showPacketDetailsTshark(int x, int y) {
		LocateResult lr = trainPanel.locateFrame(x, y);
		try {
			if (lr != null && lr.packet != null)
				showPacketDetails(lr.packet);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Helper functions used by controller

	public void findAndFilterTransactions() {
		model.findTransactions(captureFile, comboBar.getModeValue(), showFillPackets(), getFillPacketLength());
		model.filterTransactions(comboBar, buttonBar);
		String s = model.getTransaction().conversation.getInfo();
		conversationPanel.setText(WlanTvProperties.replaceMacAddrWithAlias(s, true));
		comboBar.update(model.getTransaction().conversation.getApListStrings(), model.getTransaction().conversation
				.getStaListStrings());
	}

	public void filterTransactions() {
		model.filterTransactions(comboBar, buttonBar);
	}

	public void updateViewSpan() {
		trainPanel.updateViewSpan();
	}

	public void calculateAvailability() {
		availability = new Availability(
				trainPanel.getViewFirstTransactionNo(), trainPanel.getViewLastTransactionNo(),
				model.getTransaction(), comboBar.getFillPayloadLenValue(),
				comboBar.getFillAccessTimeValue(), buttonBar.getAdvancedValue());

		infoBar.busyTimeLabel.setText("Busy:" + availability.getBusyTimePercent() + "%");
		infoBar.accessTimeLabel.setText("Access:" + availability.getAccessTimePercent() + "%");
		infoBar.freeTimeLabel.setText("Free:" + availability.getFreeTimePercent() + "%");

		setSummaryText();

		if (loadChart != null && loadChart.isVisible())
			makeLoadChart();

		if (pieChartTime != null && pieChartTime.isVisible()) {
			makePieChart();
		}

		if (pieChartCount != null && pieChartCount.isVisible()) {
			makePieChart();
		}
	}

	public void setLogging(boolean b) {
		if (Main.myLogging != null) {
			Main.myLogging.setVisible(b);
		}
	}

	// Button functions

	public void setAdvancedMenu(boolean on) {
		comboBar.setAdvancedMenu(on);
	}

	public void redraw() {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -