Skip to content
Snippets Groups Projects
JFrameCompareSimulationTraces.java 12.8 KiB
Newer Older
Maysam Zoor's avatar
Maysam Zoor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
package ui.interactivesimulation;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.Timer;
import javax.swing.border.LineBorder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import avatartranslator.AvatarSpecification;
import myutil.GraphicLib;
import myutil.TraceManager;
import ui.MainGUI;
import ui.SimulationTrace;
import ui.util.IconManager;

public class JFrameCompareSimulationTraces extends JFrame implements ActionListener, WindowListener {

	private MainGUI mgui;
	private JButton browse, parse, difference, latencyDetails;
	private JButton openButton, saveButton;
	private JTextArea log;
	private JFileChooser fc;
	private File file;
	private SimulationTrace selectedST;
	private Vector<SimulationTransaction> transFile1;
	private Vector<SimulationTransaction> transFile2;
	private SimulationTransaction st = new SimulationTransaction();
	private JTextField file2 = new JTextField();
	private boolean panelAdded = false;
	private static JPanelCompareXmlGraph newContentPane;
	private JComboBox<Object> devicesDropDownCombo1 = new JComboBox<Object>();
	private JComboBox<Object> devicesDropDownCombo2 = new JComboBox<Object>();
	private Vector<Object> transacationsDropDown1, transacationsDropDown2;
	private JComboBox<Object> tracesCombo1, tracesCombo2;

	public JFrameCompareSimulationTraces(MainGUI _mgui, String _title, SimulationTrace sST) {

		super(_title);

		System.out.println("in hereee");
		this.selectedST = sST;
		GridLayout myLayout = new GridLayout(3, 1);

		this.setBackground(Color.RED);
		this.setLayout(myLayout);

		// f = _f;
		mgui = _mgui;
		addWindowListener(this);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		// setIconImage(IconManager.img5100);
		// setBackground(Color.WHITE);
		System.out.println("in main");
		fc = new JFileChooser();

		JPanel buttonPanel = new JPanel(new GridBagLayout()); // use FlowLayout
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.NORTHWEST;

		JTextField file1 = new JTextField();

		JLabel lab1 = new JLabel("First Simulation Traces File ", JLabel.LEFT);
		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 1;
		c.weighty = 1;
		buttonPanel.add(lab1, c);

		JLabel lab2 = new JLabel("Secound Simulation Traces File ", JLabel.LEFT);

		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 0;
		c.gridy = 1;
		c.weightx = 1;
		c.weighty = 1;
		buttonPanel.add(lab2, c);

		file1.setEditable(false);
		file1.setBorder(new LineBorder(Color.BLACK));
		file1.setText(selectedST.getFullPath());

		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 1;
		c.gridy = 0;
		c.weightx = 1;
		c.weighty = 1;
		buttonPanel.add(file1, c);

		file2.setEditable(false);
		file2.setText("file 2 name");
		file2.setBorder(new LineBorder(Color.BLACK));
		// file2.setSize(1, 1);
		// file2.setPreferredSize(file2.getSize());

		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 1;
		c.gridy = 1;
		c.weightx = 1;
		c.weighty = 1;
		buttonPanel.add(file2, c);

		browse = new JButton("Browse");
		browse.addActionListener(this);
		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 1;
		c.gridy = 2;
		c.weightx = 1;
		c.weighty = 1;
		buttonPanel.add(browse, c);

		parse = new JButton("parse");
		parse.addActionListener(this);
		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 3;
		c.gridy = 2;
		c.weightx = 1;
		c.weighty = 1;
		buttonPanel.add(parse, c);

		difference = new JButton("difference");
		difference.addActionListener(this);
		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 3;
		c.gridy = 3;
		c.weightx = 1;
		c.weighty = 1;
		buttonPanel.add(difference, c);

		this.add(buttonPanel);

		/*
		 * // For layout purposes, put the buttons in a separate panel
		 * 
		 * buttonPanel.setBackground(Color.yellow);
		 * 
		 * buttonPanel.add(lab1); buttonPanel.add(file1); buttonPanel.add(new
		 * Label("")); buttonPanel.add(lab2); buttonPanel.add(file2);
		 * buttonPanel.add(browse);
		 * 
		 * buttonPanel.add(new Label("")); buttonPanel.add(new Label(""));
		 * 
		 * buttonPanel.add(parse);
		 * 
		 * 
		 * 
		 * this.add(buttonPanel);
		 */
		// JPanelCompareXmlGraph graphPanel = new JPanelCompareXmlGraph();
		// JPanelCompareXmlXYChart xychart = new JPanelCompareXmlXYChart();
		// For layout purposes, put the g in a separate panel
		/// JPanel graphPanel = new JPanel(); // use FlowLayout

		// this.add(graphPanel);

		// Sets the specified boolean to indicate whether or not
		// this textfield should be editable.

		this.pack();
		this.setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		System.out.println(e.getActionCommand());
		if (e.getSource() == browse) {
			// Handle open button action.

			int returnVal = fc.showOpenDialog(JFrameCompareSimulationTraces.this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				file = fc.getSelectedFile();
				file2.setText(file.getPath());

			}
		} else if (e.getSource() == parse) {
			// Handle open button action.

			System.out.println("Time to parse");
			System.out.println(selectedST.getFullPath());
			System.out.println(file.getPath());
			try {
				int x = parseXML(selectedST.getFullPath(), file.getPath());

			} catch (SAXException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		} else if (e.getSource() == latencyDetails) {
			System.out.println("Time for latency analysis");

		} else if (e.getSource() == difference) {
			// Handle open button action.

			System.out.println("Time to show the difference");

			newContentPane.showDifference();
			// this.add(newContentPane);
			this.pack();
			this.setVisible(true);
		} else if (e.getSource() == devicesDropDownCombo1) {
			Vector<Object> transacationsDropDown1 = newContentPane
					.loadTransacationsDropDown(devicesDropDownCombo1.getSelectedItem());

			System.out.println("Time to show the difference" + transacationsDropDown1.size());

			System.out.println("Time to show the difference" + tracesCombo1.getSelectedIndex());

			final DefaultComboBoxModel<Object> model = new DefaultComboBoxModel<Object>(transacationsDropDown1);

			tracesCombo1.setModel(model);

		} else if (e.getSource() == devicesDropDownCombo2) {
			Vector<Object> transacationsDropDown2 = newContentPane
					.loadTransacationsDropDown(devicesDropDownCombo2.getSelectedItem());

			System.out.println("Time to show the difference" + transacationsDropDown2.size());

			System.out.println("Time to show the difference" + tracesCombo2.getSelectedIndex());

			final DefaultComboBoxModel<Object> model = new DefaultComboBoxModel<Object>(transacationsDropDown2);

			tracesCombo2.setModel(model);

		}
	}

	// Returns the currentY position
	private int parseXML(String file1Path, String file2Path)
			throws SAXException, IOException, ParserConfigurationException {

		if (file1Path.length() == 0 || file2Path.length() == 0)
			throw new RuntimeException("The name of the XML file is required!");

		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		SAXParser saxParser = saxParserFactory.newSAXParser();
		SAXParserHandler handler = new SAXParserHandler();

		saxParser.parse(new File(file1Path), handler);
		transFile1 = handler.getStList();

		handler = new SAXParserHandler();

		saxParser.parse(new File(file2Path), handler);
		transFile2 = handler.getStList();
		System.out.println("transFile1 :" + transFile1.size());
		System.out.println("transFile2 :" + transFile2.size());
		DrawSimulationResults(transFile1, transFile2);

		/*
		 * // Print all employees. for (SimulationTransaction st1 : transFile2) {
		 * System.out.println(st1.toString());
		 * 
		 * }
		 */

		// this.pack();
		// this.setVisible(true);

		return 1;

	}

	private void DrawSimulationResults(Vector<SimulationTransaction> transFile1,
			Vector<SimulationTransaction> transFile2) {

		if (panelAdded == true) {

			newContentPane = new JPanelCompareXmlGraph(transFile1, transFile2);
			newContentPane.setOpaque(true); // content panes must be opaque

			newContentPane.updateTable();
			// newContentPane.revalidate();
			// newContentPane.repaint();

			// this.revalidate();
			// this.repaint();

			// this.add(newContentPane);
			System.out.println(" rewrite table");

		} else {

			newContentPane = new JPanelCompareXmlGraph(transFile1, transFile2);
			newContentPane.setOpaque(true); // content panes must be opaque

			newContentPane.drawTable();

			System.out.println(" New table");
			this.add(newContentPane);

			DrawLatencyPanel();

			// this.setWidgetTopBottom(newContentPane, 32, Unit.PX, 0, Unit.PX);

			panelAdded = true;
		}

		this.pack();
		this.setVisible(true);
	}

	private void DrawLatencyPanel() {

		JPanel latencyPanel = new JPanel(new GridBagLayout()); // use FlowLayout
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.NORTHWEST;

		JTextField file1 = new JTextField();

		JLabel lab1 = new JLabel("Latency for Simulation Traces File ", JLabel.LEFT);
		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 0;
		c.gridy = 0;
		// c.weightx = 0;
		// c.weighty = 0;
		latencyPanel.add(lab1, c);

		Vector<Object> devicesDropDown1 = newContentPane.loadDevicesDropDown();

		devicesDropDownCombo1 = new JComboBox<Object>(devicesDropDown1);

		Vector<Object> transacationsDropDown1 = newContentPane
				.loadTransacationsDropDown(devicesDropDownCombo1.getSelectedItem());
		tracesCombo1 = new JComboBox<Object>(transacationsDropDown1);

		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 0;
		c.gridy = 1;
		// c.weightx = 0;
		// c.weighty = 1;
		latencyPanel.add(devicesDropDownCombo1, c);

		// String[] choices2 = { "CHOICE 1","CHOICE 2", "CHOICE 3","CHOICE 4","CHOICE
		// 5","CHOICE 6"};

		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 0;
		c.gridy = 2;
		// c.weightx = 0;
		// c.weighty =2;
		latencyPanel.add(tracesCombo1, c);
		this.add(latencyPanel);

		devicesDropDownCombo1.addActionListener(this);

		Vector<Object> devicesDropDown2 = newContentPane.loadDevicesDropDown();

		devicesDropDownCombo2 = new JComboBox<Object>(devicesDropDown2);

		Vector<Object> transacationsDropDown2 = newContentPane
				.loadTransacationsDropDown(devicesDropDownCombo2.getSelectedItem());
		tracesCombo2 = new JComboBox<Object>(transacationsDropDown2);

		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 1;
		c.gridy = 1;
		// c.weightx = 1;
		// c.weighty = 1;
		latencyPanel.add(devicesDropDownCombo2, c);

		// String[] choices2 = { "CHOICE 1","CHOICE 2", "CHOICE 3","CHOICE 4","CHOICE
		// 5","CHOICE 6"};

		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 1;
		c.gridy = 2;
		// c.weightx = 0;
		// c.weighty =2;
		latencyPanel.add(tracesCombo2, c);
		this.add(latencyPanel);

		devicesDropDownCombo2.addActionListener(this);

		latencyDetails = new JButton("latency Details");
		latencyDetails.addActionListener(this);
		c.fill = GridBagConstraints.NORTHWEST;
		c.gridx = 2;
		c.gridy = 2;
		c.weightx = 1;
		c.weighty = 1;
		latencyPanel.add(latencyDetails, c);
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		TraceManager.addDev("Windows closed!");
		close();

	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void close() {

		dispose();
		setVisible(false);

	}

}