/**
 * Copyright (C) 2016, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano

 * This program 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 2 of the License, or
 * (at your option) any later version.

 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

package jmt.gui.jwat;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

import jmt.engine.jwat.JwatSession;
import jmt.engine.jwat.ProgressStatusListener;
import jmt.engine.jwat.fitting.FittingSession;
import jmt.engine.jwat.input.EventFinishAbort;
import jmt.engine.jwat.input.EventSessionLoaded;
import jmt.engine.jwat.input.EventStatus;
import jmt.engine.jwat.input.Loader;
import jmt.engine.jwat.input.ProgressMonitorShow;
import jmt.engine.jwat.trafficAnalysis.TrafficAnalysisSession;
import jmt.engine.jwat.workloadAnalysis.WorkloadAnalysisSession;
import jmt.framework.gui.help.HoverHelp;
import jmt.framework.gui.wizard.WizardPanel;
import jmt.gui.common.CommonConstants;
import jmt.gui.common.Defaults;
import jmt.gui.common.JMTImageLoader;
import jmt.gui.jwat.fitting.panels.FittingPanel;
import jmt.gui.jwat.fitting.panels.LoadDemoFittingPanel;
import jmt.gui.jwat.trafficAnalysis.panels.EpochPanel;
import jmt.gui.jwat.trafficAnalysis.panels.GraphArrivalPanel;
import jmt.gui.jwat.trafficAnalysis.panels.GraphPanel;
import jmt.gui.jwat.trafficAnalysis.panels.TextualPanel;
import jmt.gui.jwat.workloadAnalysis.panels.ClusterPanel;
import jmt.gui.jwat.workloadAnalysis.panels.ClusteringInfoPanel;
import jmt.gui.jwat.workloadAnalysis.panels.InputPanel;
import jmt.gui.jwat.workloadAnalysis.panels.LoadDemoPanel;
import jmt.gui.jwat.workloadAnalysis.panels.StatsPanel;
import jmt.manual.ManualBookmarkers;
import jmt.manual.PDFViewerBuffer;

public class MainJwatWizard extends JWatWizard {

	private static final long serialVersionUID = 1L;
	//JWAT tool icons
	private String IMG_JWATICON = "JWATIcon";
	//private JToolBar toolBar = null;
	private static final String TITLE = "JWAT";
	private static final String WORK_LOAD_TITLE = "Workload Analysis";
	private static final String BURSTINESS_TITLE = "Traffic Analysis  - Burstiness";
	private static final String FITTING_TITLE = "Fitting Workload Data";
	private JPanel menus = null;

	//private JWatModel model = null;
	private JwatSession session = null;

	//Last panel visited, used to control correct next step
	private int lastPanel = 0;
	private int currentPanel = 0;

	private HoverHelp help = null;
	// List of panels create for Workload Analysis tool
	private ArrayList<WizardPanel> JWatPanels = new ArrayList<WizardPanel>();
	// First panel
	private JWatMainPanel mainPanel = null;

	/**
	 * Constructor.
	 */
	public MainJwatWizard() {
		initGUI();
	}

	private JFileChooser fileSaveF = new JFileChooser(Defaults.getWorkingPath()) {

		private static final long serialVersionUID = 1L;

		{
			setApproveButtonText("Save");
			setFileSelectionMode(JFileChooser.FILES_ONLY);
		}

	};

	private JFileChooser fileOpenF = new JFileChooser(Defaults.getWorkingPath()) {

		private static final long serialVersionUID = 1L;

		{
			setApproveButtonText("Open");
			setFileSelectionMode(JFileChooser.FILES_ONLY);
		}

	};

	/**
	 * Initializes JWAT start screen GUI
	 */
	private void initGUI() {
		this.setIconImage(JMTImageLoader.loadImage(IMG_JWATICON).getImage());
		//this.setResizable(false);
		this.setTitle(TITLE);
		this.setSize(CommonConstants.MAX_GUI_WIDTH_JWAT, CommonConstants.MAX_GUI_HEIGHT_JWAT);
		centerWindow();
		menus = new JPanel(new BorderLayout());
		help = this.getHelp();
		getContentPane().add(menus, BorderLayout.NORTH);
		//Aggiunta del pannello principale dell'applicazione
		mainPanel = new JWatMainPanel(this);
		this.addPanel(mainPanel);
		setEnableButton("Solve", false);
	}

	public void setFittingEnv(String mode) {
		this.setTitle(TITLE + " - " + FITTING_TITLE);
		session = new FittingSession();

		//Creates and adds all necessary panels to JWAT main screen
		WizardPanel p;

		if (mode.equals("load")) {
			p = new jmt.gui.jwat.fitting.panels.InputPanel(this);
		} else {
			p = new LoadDemoFittingPanel(this);
		}

		JWatPanels.add(p);
		this.addPanel(p);

		p = new FittingPanel(this, FittingPanel.PARETO);
		JWatPanels.add(p);
		this.addPanel(p);

		p = new FittingPanel(this, FittingPanel.EXPO);
		JWatPanels.add(p);
		this.addPanel(p);

		getFittingToolbar(mode);
		getFittingMenubar(mode);
		setEnableButton("Next >", false);
		setEnableButton("Solve", false);
		//Shows next panel, the first of traffic analysis wizard
		showNextPanel();
	}

	// Set correct environment for traffic analysis
	public void setTrafficEnv() {
		this.setTitle(TITLE + " - " + BURSTINESS_TITLE);
		session = new TrafficAnalysisSession();

		//Creates and adds all necessary panels to JWAT main screen
		WizardPanel p = new jmt.gui.jwat.trafficAnalysis.panels.InputPanel(this);
		JWatPanels.add(p);
		this.addPanel(p);

		p = new EpochPanel(this);
		JWatPanels.add(p);
		this.addPanel(p);

		p = new TextualPanel(this);
		JWatPanels.add(p);
		this.addPanel(p);

		p = new GraphPanel(this);
		JWatPanels.add(p);
		this.addPanel(p);

		p = new GraphArrivalPanel(this);
		JWatPanels.add(p);
		this.addPanel(p);

		getTrafficToolbar();
		getTrafficMenubar();
		setEnableButton("Next >", false);
		setEnableButton("Solve", false);
		//Shows next panel, the first of traffic analysis wizard
		showNextPanel();
	}

	//Adds all necessary panes concerning with Workload analysis
	public void setWorkloadEnv(String mode) {
		this.setTitle(TITLE + " - " + WORK_LOAD_TITLE);
		session = new WorkloadAnalysisSession();
		WizardPanel p;
		if (mode.equals("load")) {
			p = new InputPanel(this);
		} else {
			p = new LoadDemoPanel(this);
		}
		JWatPanels.add(p);
		this.addPanel(p);
		p = new StatsPanel(this);
		JWatPanels.add(p);
		this.addPanel(p);
		p = new ClusterPanel(this);
		JWatPanels.add(p);
		this.addPanel(p);
		p = new ClusteringInfoPanel(this);
		JWatPanels.add(p);
		this.addPanel(p);
		//Set Workload ToolBar
		getWorkloadToolbar(mode);
		//Set Workload MenuBar
		getWorkloadMenubar(mode);
		setEnableButton("Next >", false);
		setEnableButton("Solve", false);
		lastPanel = 1;
		showNextPanel();
	}

	/**
	 * Main method.
	 * @param args no args.
	 */
	public static void main(String[] args) {
		new MainJwatWizard().setVisible(true);
	}

	public void setToolBar(JToolBar bar) {
		if (toolBar != null) {
			menus.remove(toolBar);
		}
		menus.add(bar, BorderLayout.SOUTH);
		toolBar = bar;
	}

	public void setMenuBar(JMenuBar bar) {
		if (mMenuBar != null) {
			menus.remove(mMenuBar);
		}
		menus.add(bar, BorderLayout.NORTH);
		mMenuBar = bar;
	}

	// Riseleziona il pannello da cui si e' arrivati
	public void setLastPanel() {
		tabbedPane.setSelectedIndex(lastPanel);
	}

	// Salva il pannello che da cui si e' arrivati
	public void setLastPanel(int panel) {
		lastPanel = panel;
	}

	public void setCurrentPanel(int panel) {
		currentPanel = panel;
	}

	public JWatModel getModel() {
		JWatModel mode = null;
		if (session != null) {
			mode = session.getDataModel();
		}
		return mode;
	}

	public JwatSession getSession() {
		return session;
	}

	protected AbstractAction WL_EXIT_ACTION = new AbstractAction("Exit") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Exits Application");
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_Q));
		}

		public void actionPerformed(ActionEvent e) {
			close();
		}

	};

	private AbstractAction WL_HELP_SHOWHELP = new AbstractAction("JWAT Help") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Show JWAT help");
			putValue(Action.SMALL_ICON, JMTImageLoader.loadImage("Help"));
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_H));
		}

		public void actionPerformed(ActionEvent e) {
			Runnable r = new Runnable() {
				public void run() {
					try {
						new PDFViewerBuffer("JWAT manual", ManualBookmarkers.JWAT);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			};
			EventQueue.invokeLater(r);
		}

	};

	private AbstractAction WL_HELP_CREDITS = new AbstractAction("About JWAT") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "About JWAT");
		}

		public void actionPerformed(ActionEvent e) {
			workloadShowAbout();
		}

	};

	private AbstractAction WL_FILE_NEW = new AbstractAction("New") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Create session");
			putValue(Action.SMALL_ICON, JMTImageLoader.loadImage("New"));
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_N));
		}

		public void actionPerformed(ActionEvent e) {
			if (JOptionPane.showConfirmDialog(MainJwatWizard.this, "This operation will reset data. Continue?", "Warning", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
				//Reset model and set first panel
				session.resetSession();
				tabbedPane.setSelectedIndex(JWATConstants.WORKLOAD_INPUT_PANEL);
				((InputPanel) tabbedPane.getComponentAt(JWATConstants.WORKLOAD_INPUT_PANEL)).resetOnNew();
			}
		}

	};

	private AbstractAction WL_FILE_OPEN = new AbstractAction("Open") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Open session");
			putValue(Action.SMALL_ICON, JMTImageLoader.loadImage("Open"));
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
		}

		public void actionPerformed(ActionEvent e) {
			if (fileOpenF.showOpenDialog(MainJwatWizard.this) == JFileChooser.APPROVE_OPTION) {
				if (currentPanel != JWATConstants.WORKLOAD_INPUT_PANEL) {
					tabbedPane.setSelectedIndex(JWATConstants.WORKLOAD_INPUT_PANEL);
				}
				File fFile = fileOpenF.getSelectedFile();
				String fileName = fFile.getAbsolutePath();
				Loader.loadSession(fileName, new ProgressMonitorShow(tabbedPane.getComponentAt(currentPanel), "Loading Session...", 1000),
						new SessionStatusListener(), session);
			}
		}

	};

	private AbstractAction WL_FILE_SAVE = new AbstractAction("Save") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Save session");
			putValue(Action.SMALL_ICON, JMTImageLoader.loadImage("Save"));
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
		}

		public void actionPerformed(ActionEvent e) {
			if (fileSaveF.showOpenDialog(MainJwatWizard.this) == JFileChooser.APPROVE_OPTION) {
				File fFile = fileSaveF.getSelectedFile();
				String fileName = fFile.getAbsolutePath();
				MainJwatWizard.this.session.saveSession(fileName.substring(0, fileName.lastIndexOf("\\")) + "\\", fileName.substring(fileName
						.lastIndexOf("\\") + 1), JwatSession.WORKLOAD_SAVE);
			}
		}

	};

	private class SessionStatusListener implements ProgressStatusListener {

		public void statusEvent(EventStatus e) {
			switch (e.getType()) {
			case EventStatus.ABORT_EVENT:
				abortEvent((EventFinishAbort) e);
				break;
			case EventStatus.DONE_EVENT:
				finishedEvent((EventSessionLoaded) e);
				break;
			}
		}

		//Abort caricamento file input
		private void abortEvent(EventFinishAbort e) {
			JWatWizard wizard = (JWatWizard) ((WizardPanel) tabbedPane.getComponentAt(currentPanel)).getParentWizard();
			JOptionPane.showMessageDialog(tabbedPane.getComponentAt(currentPanel), e.getMessage(), "LOADING ABORTED!!", JOptionPane.WARNING_MESSAGE);
			((InputPanel) tabbedPane.getComponentAt(JWATConstants.WORKLOAD_INPUT_PANEL)).setCanGoForward(false);
			wizard.setEnableButton("Next >", false);
			wizard.setEnableButton("Solve", false);
		}

		//dati caricati
		private void finishedEvent(final EventSessionLoaded e) {
			JButton[] optBtn = new JButton[2];
			JOptionPane pane;
			((InputPanel) tabbedPane.getComponentAt(JWATConstants.WORKLOAD_INPUT_PANEL)).setCanGoForward(false);
			JWatWizard wizard = (JWatWizard) ((WizardPanel) tabbedPane.getComponentAt(currentPanel)).getParentWizard();

			optBtn[0] = new JButton("Continue");
			optBtn[1] = new JButton("Cancel");

			pane = new JOptionPane("Load session done.", JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, optBtn, null);
			final JDialog dialog = pane.createDialog(wizard, "Loading Complete");
			pane.selectInitialValue();

			optBtn[0].addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent ev) {
					JwatSession newSession = e.getSession();
					dialog.dispose();
					JWatWizard wizard = (JWatWizard) ((WizardPanel) tabbedPane.getComponentAt(currentPanel)).getParentWizard();
					session.copySession(newSession);
					wizard.setEnableButton("Next >", true);
					wizard.setEnableButton("Solve", false);
					((InputPanel) tabbedPane.getComponentAt(JWATConstants.WORKLOAD_INPUT_PANEL)).setCanGoForward(true);
					wizard.showNextPanel();
				}

			});

			optBtn[1].addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent ev) {
					JWatWizard wizard = (JWatWizard) ((WizardPanel) tabbedPane.getComponentAt(currentPanel)).getParentWizard();
					dialog.dispose();
					((InputPanel) tabbedPane.getComponentAt(JWATConstants.WORKLOAD_INPUT_PANEL)).setCanGoForward(true);
					System.gc();
					wizard.setEnableButton("Next >", false);
					wizard.setEnableButton("Solve", false);
				}

			});

			dialog.show();
		}

	}

	private AbstractAction WL_ACTION_SOLVE = new AbstractAction("Clusterize") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Clusterize");
			putValue(Action.SMALL_ICON, JMTImageLoader.loadImage("Sim"));
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_L));
		}

		public void actionPerformed(ActionEvent e) {
		}

	};

	/**
	 * @return the toolbar for the exact wizard. Shamelessly uses icon from the main jmt frame
	 */
	protected void getWorkloadToolbar(String mode) {
		JToolBar workloadToolbar = new JToolBar();
		workloadToolbar.setRollover(true);
		workloadToolbar.setOrientation(SwingConstants.HORIZONTAL);
		workloadToolbar.setFloatable(false);
		//null values add a gap between toolbar icons
		Action[] actions = { WL_FILE_NEW, WL_FILE_OPEN, WL_FILE_SAVE, null, WL_ACTION_SOLVE, null, WL_HELP_SHOWHELP };
		String[] icons = { "New", "Open", "Save", "Sim", "Help" };
		String[] htext = { "Creates a new session", "Opens a saved session", "Saves the current session", "Clusterize", "Show help" };
		JButton button;
		workloadToolbar.setBorderPainted(true);
		//i index scans actions' array which includes null values, while j scans other arrays.
		//so j must be decremented when a null value is found in action array.
		for (int i = 0, j = 0; i < actions.length; i++, j++) {
			if (actions[i] == null) {
				j--;
				workloadToolbar.addSeparator(new Dimension(20, 2));
			} else {
				button = new JButton(actions[i]);
				button.setText("");
				button.setIcon(JMTImageLoader.loadImage(icons[j]));
				button.setRolloverIcon(JMTImageLoader.loadImage(icons[j] + "RO"));
				button.setPressedIcon(JMTImageLoader.loadImage(icons[j] + "P"));
				button.setFocusPainted(false);
				button.setContentAreaFilled(false);
				button.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
				workloadToolbar.add(button);
				help.addHelp(button, htext[j]);
				if ((j == 0 && mode.equals("demo")) || j == 1 || j == 2|| j == 3) {
					button.setEnabled(false);
				}
			}
		}
		setToolBar(workloadToolbar);
	}

	private void getWorkloadMenubar(String mode) {
		JMenuBar workloadMenubar = new JMenuBar();
		JMenuItem[][] menuItems = {
				{ new JMenuItem(WL_FILE_NEW), new JMenuItem(WL_FILE_OPEN), new JMenuItem(WL_FILE_SAVE), null, new JMenuItem(WL_EXIT_ACTION) },
				{ new JMenuItem(WL_ACTION_SOLVE) }, { new JMenuItem(WL_HELP_SHOWHELP), null, new JMenuItem(WL_HELP_CREDITS) } };
		String[] menuTitles = { "File", "Action", "Help" };
		char[] chars = { 'F', 'A', 'e' };
		for (int i = 0; i < menuItems.length; i++) {
			JMenu menu = new JMenu(menuTitles[i]);
			menu.setMnemonic(chars[i]);
			for (int j = 0; j < menuItems[i].length; j++) {
				if (menuItems[i][j] == null) {
					menu.addSeparator();
				} else {
					menu.add(menuItems[i][j]);
					if ((i == 0 && ((j == 0 && mode.equals("demo")) || j == 1 || j == 2)) || (i == 1 && j == 0)) {
						menuItems[i][j].setEnabled(false);
					}
				}
			}
			workloadMenubar.add(menu);
		}
		setMenuBar(workloadMenubar);
	}

	public void resetScreen() {
		for (int i = 0; i < JWatPanels.size(); i++) {
			tabbedPane.remove(JWatPanels.get(i));
		}
		JWatPanels.clear();
		mainPanel.makeMenubar();
		mainPanel.makeToolbar();
		this.validate();
	}

	private void getTrafficMenubar() {
		JMenuBar jmb = new JMenuBar();
		JMenuItem[][] menuItems = { { new JMenuItem(TR_FILE_NEW), null, new JMenuItem(TR_FILE_EXIT) },
				{ new JMenuItem(TR_HELP), null, new JMenuItem(TR_ABOUT) } };
		String[] menuTitles = { "File", "Help" };
		char[] chars = { 'F', 'e' };
		for (int i = 0; i < menuItems.length; i++) {
			JMenu menu = new JMenu(menuTitles[i]);
			menu.setMnemonic(chars[i]);
			for (int j = 0; j < menuItems[i].length; j++) {
				if (menuItems[i][j] == null) {
					menu.addSeparator();
				} else {
					menu.add(menuItems[i][j]);
				}
			}
			jmb.add(menu);
		}
		setMenuBar(jmb);
	}

	/**
	 * @return the toolbar for the jaba wizard. Shamelessly uses icon from the main jmt frame
	 */
	protected void getTrafficToolbar() {
		JToolBar tb = new JToolBar();
		tb.setRollover(true);
		tb.setOrientation(SwingConstants.HORIZONTAL);
		tb.setFloatable(false);
		tb.setBorderPainted(true);

		Action[] actions = { TR_FILE_NEW, null, TR_HELP };
		String[] icons = { "New", "Help" };
		JButton button;

		for (int i = 0, j = 0; i < actions.length; i++, j++) {
			if (actions[i] == null) {
				j--;
				tb.addSeparator(new Dimension(20, 2));
			} else {
				button = new JButton(actions[i]);
				button.setText("");
				button.setIcon(JMTImageLoader.loadImage(icons[j]));
				button.setRolloverIcon(JMTImageLoader.loadImage(icons[j] + "RO"));
				button.setPressedIcon(JMTImageLoader.loadImage(icons[j] + "P"));
				button.setFocusPainted(false);
				button.setContentAreaFilled(false);
				button.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
				tb.add(button);
			}
		}
		setToolBar(tb);
	}

	private void getFittingMenubar(String mode) {
		JMenuBar jmb = new JMenuBar();
		JMenuItem[][] menuItems = { { new JMenuItem(FI_FILE_NEW), null, new JMenuItem(FI_FILE_EXIT) },
				{ new JMenuItem(FI_HELP), null, new JMenuItem(FI_ABOUT) } };
		String[] menuTitles = { "File", "Help" };
		char[] chars = { 'F', 'e' };
		for (int i = 0; i < menuItems.length; i++) {
			JMenu menu = new JMenu(menuTitles[i]);
			menu.setMnemonic(chars[i]);
			for (int j = 0; j < menuItems[i].length; j++) {
				if (menuItems[i][j] == null) {
					menu.addSeparator();
				} else {
					menu.add(menuItems[i][j]);
					if (i == 0 && (j == 0 && mode.equals("demo"))) {
						menuItems[i][j].setEnabled(false);
					}
				}
			}
			jmb.add(menu);
		}
		setMenuBar(jmb);
	}

	/**
	 * @return the toolbar for the jaba wizard. Shamelessly uses icon from the main jmt frame
	 */
	protected void getFittingToolbar(String mode) {
		JToolBar tb = new JToolBar();
		tb.setRollover(true);
		tb.setOrientation(SwingConstants.HORIZONTAL);
		tb.setFloatable(false);
		tb.setBorderPainted(true);

		Action[] actions = { FI_FILE_NEW, null, FI_HELP };
		String[] icons = { "New", "Help" };
		JButton button;

		for (int i = 0, j = 0; i < actions.length; i++, j++) {
			if (actions[i] == null) {
				j--;
				tb.addSeparator(new Dimension(20, 2));
			} else {
				button = new JButton(actions[i]);
				button.setText("");
				button.setIcon(JMTImageLoader.loadImage(icons[j]));
				button.setRolloverIcon(JMTImageLoader.loadImage(icons[j] + "RO"));
				button.setPressedIcon(JMTImageLoader.loadImage(icons[j] + "P"));
				button.setFocusPainted(false);
				button.setContentAreaFilled(false);
				button.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
				tb.add(button);
				if (i == 0 && mode.equals("demo")) {
					button.setEnabled(false);
				}
			}
		}
		setToolBar(tb);
	}

	private AbstractAction TR_FILE_NEW = new AbstractAction("New...") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Create New Model");
			putValue(Action.SMALL_ICON, JMTImageLoader.loadImage("New"));
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_N));
		}

		public void actionPerformed(ActionEvent e) {
			trafficNewModel();
		}

	};

	private AbstractAction FI_FILE_NEW = new AbstractAction("New...") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Create New Model");
			putValue(Action.SMALL_ICON, JMTImageLoader.loadImage("New"));
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_N));
		}

		public void actionPerformed(ActionEvent e) {
			fittingNewModel();
		}

	};

	private AbstractAction TR_FILE_EXIT = new AbstractAction("Exit") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Exits Application");
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_Q));
		}

		public void actionPerformed(ActionEvent e) {
			close();
		}

	};

	private AbstractAction FI_FILE_EXIT = new AbstractAction("Exit") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Exits Application");
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_Q));
		}

		public void actionPerformed(ActionEvent e) {
			close();
		}

	};

	private AbstractAction TR_HELP = new AbstractAction("Burstiness Help") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Show Burstiness help");
			putValue(Action.SMALL_ICON, JMTImageLoader.loadImage("Help"));
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_H));
		}

		public void actionPerformed(ActionEvent e) {
			Runnable r = new Runnable() {
				public void run() {
					try {
						new PDFViewerBuffer("JWAT manual", ManualBookmarkers.JWAT);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			};
			EventQueue.invokeLater(r);
		}

	};

	private AbstractAction FI_HELP = new AbstractAction("Fitting Help") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "Show Fitting help");
			putValue(Action.SMALL_ICON, JMTImageLoader.loadImage("Help"));
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.CTRL_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_H));
		}

		public void actionPerformed(ActionEvent e) {
			Runnable r = new Runnable() {
				public void run() {
					try {
						new PDFViewerBuffer("JWAT manual", ManualBookmarkers.JWAT);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			};
			EventQueue.invokeLater(r);
		}

	};

	private AbstractAction TR_ABOUT = new AbstractAction("About Burstiness") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "About Burstiness");
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_H));
		}

		public void actionPerformed(ActionEvent e) {
			trafficShowAbout();
		}

	};

	private AbstractAction FI_ABOUT = new AbstractAction("About Fitting") {

		private static final long serialVersionUID = 1L;

		{
			putValue(Action.SHORT_DESCRIPTION, "About Fitting");
			putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, ActionEvent.ALT_MASK));
			putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_H));
		}

		public void actionPerformed(ActionEvent e) {
			fittingShowAbout();
		}

	};

	private void trafficNewModel() {
		if (JOptionPane.showConfirmDialog(MainJwatWizard.this, "This operation will reset data. Continue?", "Warning", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
			//Reset model and set first panel
			session.resetSession();
			tabbedPane.setSelectedIndex(JWATConstants.WORKLOAD_INPUT_PANEL);
			((jmt.gui.jwat.trafficAnalysis.panels.InputPanel) tabbedPane.getComponentAt(JWATConstants.WORKLOAD_INPUT_PANEL)).resetOnNew();
		}
	}

	private void fittingNewModel() {
		if (JOptionPane.showConfirmDialog(MainJwatWizard.this, "This operation will reset data. Continue?", "Warning", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
			//Reset model and set first panel
			session.resetSession();
			tabbedPane.setSelectedIndex(JWATConstants.WORKLOAD_INPUT_PANEL);
			((jmt.gui.jwat.fitting.panels.InputPanel) tabbedPane.getComponentAt(JWATConstants.WORKLOAD_INPUT_PANEL)).resetOnNew();
		}
	}

	private void workloadShowAbout() {
		JOptionPane.showMessageDialog(this, "Sorry, this is not available", "About JWAT not found", JOptionPane.ERROR_MESSAGE);
	}

	private void trafficShowAbout() {
		JOptionPane.showMessageDialog(this, "Sorry, this is not available", "About Burstiness not found", JOptionPane.ERROR_MESSAGE);
	}

	private void fittingShowAbout() {
		JOptionPane.showMessageDialog(this, "Sorry, this is not available", "About Fitting not found", JOptionPane.ERROR_MESSAGE);
	}

}