package defaultPackage;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import test.UtilityPanel;

public class Interface extends JApplet {
	
	UtilityPanel p = new UtilityPanel();

	private static final long serialVersionUID = -4910625108222612679L;

	String IType, BankNames[] = new String[9], ShareNames[] = new String[9],
			InsuranceNames[] = new String[9], Method;

	int width = 150, height = 28, Left, Iterations[] = new int[2], Resources,
			It, Ip, ShareLength, ArrayLength, BankLength, InsuranceLength,
			ParamWidth, ParamHeight, fc = 0;

	double Uf[] = new double[9], BankBankrupt[] = new double[9],
			ShareBankrupt[] = new double[9],
			InsuranceIncrease[] = new double[9],
			BankDividends[] = new double[9], ShareMin[] = new double[9],
			ShareAvg[] = new double[9], ShareMax[] = new double[9],
			PyP[] = new double[9], RyP[] = new double[9],
			OyP[] = new double[9], ShareProfit[] = new double[9],
			InsuranceDisaster[] = new double[9], ObjectValue[], AllBankrupt[],
			AllDividends[];

	boolean Can = false;

	Vector wItVector = new Vector(0), wFVector = new Vector(0);

	JButton w1Button, w2Button, w3Button, w4Button;

	JLabel wLabel, wALabel[];

	JTextField w1TextField, w10TextField, w2TextField,
			wUTextField[] = new JTextField[7],
			wBTextField[] = new JTextField[18],
			wTextField[] = new JTextField[18],
			wPTextField[] = new JTextField[9],
			wRTextField[] = new JTextField[9],
			wOTextField[] = new JTextField[9],
			wITextField[] = new JTextField[9],
			wIwTextField[] = new JTextField[9],
			wIdTextField[] = new JTextField[9];

	JComboBox w1ComboBox, w2ComboBox, w3ComboBox, wComboBox;

	JTabbedPane wTabbedPane = new JTabbedPane();

	JPanel wPanel, w1Panel, w2Panel, w3Panel, w4Panel, w5Panel, w1IPanel,
			w6Panel;

	JScrollPane wScrollPane;

	JProgressBar wProgressBar;

	JFrame wIFrame, wGFrame;

	Counter wCounter;

	MeGraphics mGraphics;

	// ------ FocusListener Integer Double ----------------------------
	class CLFocusListener extends FocusAdapter {
		public void focusLost(FocusEvent e) {
			String Str = ((JTextField) e.getSource()).getText();
			if (((JTextField) e.getSource()).getName() == "Double") {
				if (Str.length() == 0) {
					((JTextField) e.getSource()).setText("0.1");
					((JTextField) e.getSource()).setBackground(new Color(255,
							255, 255));
				} else
					try {
						double d = Double.parseDouble(Str);
						if (d < 0)
							d = d * -1;
						if (d > 1)
							d = 1;
						((JTextField) e.getSource())
								.setText(Double.toString(d));
						getAppletContext().showStatus("");
						((JTextField) e.getSource()).setBackground(new Color(
								255, 255, 255));
					} catch (NumberFormatException exc) {
						getAppletContext().showStatus(
								"Error: It's not floating point value!");
						((JTextField) e.getSource()).setBackground(new Color(
								255, 90, 90));
					}
			}
			if (((JTextField) e.getSource()).getName() == "Integer") {
				if (Str.length() == 0) {
					((JTextField) e.getSource()).setText("1");
					((JTextField) e.getSource()).setBackground(new Color(255,
							255, 255));
				} else
					try {
						int n = Integer.parseInt(Str);
						if (n < 0.0)
							n = n * -1;
						if (n == 0)
							n = 1;
						if (n > 1000000)
							n = 1000000;
						((JTextField) e.getSource()).setText(Integer
								.toString(n));
						getAppletContext().showStatus(" ");
						((JTextField) e.getSource()).setBackground(new Color(
								255, 255, 255));
					} catch (NumberFormatException exc) {
						getAppletContext().showStatus(
								"Error: It's not number value or to large!");
						((JTextField) e.getSource()).setBackground(new Color(
								255, 90, 90));
					}
			}
			if (((JTextField) e.getSource()).getName() == "Name")
				if (Str.length() > 40) {
					Str = Str.substring(0, 40);
					((JTextField) e.getSource()).setText(Str);
				}
		}
	}

	CLFocusListener wTLFocusListener = new CLFocusListener();

	// ------ ItemListener ----------------------------------------
	class CItemListener implements ItemListener {
		public void itemStateChanged(ItemEvent e) {
			fc = fc + 1;
			if (fc == 3)
				fc = 1;
			if (fc == 1) {
				String Str = "" + ((JComboBox) e.getSource()).getSelectedItem();
				int n = ((JComboBox) e.getSource()).getY() / 33;
				if (((JComboBox) e.getSource()).getName() == "BanksD")
					BankDividends[n] = Integer.parseInt(Str);

				if (((JComboBox) e.getSource()).getName() == "IType") {
					if (Str.equals("User"))
						wTabbedPane.setEnabledAt(1, true);
					else
						wTabbedPane.setEnabledAt(1, false);
				}
				if (((JComboBox) e.getSource()).getName() == "SharesP")
					ShareProfit[n] = Integer.parseInt(Str);
				if (((JComboBox) e.getSource()).getName() == "SharesMin")
					ShareMin[n] = Integer.parseInt(Str);
				if (((JComboBox) e.getSource()).getName() == "SharesAvg")
					ShareAvg[n] = Integer.parseInt(Str);
				if (((JComboBox) e.getSource()).getName() == "SharesMax")
					ShareMax[n] = Integer.parseInt(Str);
				if (((JComboBox) e.getSource()).getName() == "InsuranceA")
					InsuranceIncrease[n] = Integer.parseInt(Str);
				if (((JComboBox) e.getSource()).getName() == "Method") {
					w1IPanel.removeAll();
					w1TextField = null;
					w10TextField = null;
					// --- Iteration ----------
					Iteration();
					if (((JComboBox) e.getSource()).getSelectedItem() == "Mig") {
					}
					if (((JComboBox) e.getSource()).getSelectedItem() == "LBayes") {
					}
					if (((JComboBox) e.getSource()).getSelectedItem() == "Glopt") {
					}
					if (((JComboBox) e.getSource()).getSelectedItem() == "Bayes") {
						InitialPoints();
					}
					if (((JComboBox) e.getSource()).getSelectedItem() == "Unt") {
						InitialPoints();
					}
					w1IPanel.repaint();
				}
			}
		}
	}

	CItemListener wItemListener = new CItemListener();

	void Iteration() {
		wLabel = new JLabel("Iterations: ");
		wLabel.setBounds(25, 5, width, height);
		w1IPanel.add(wLabel);

		w1TextField = new JTextField("1000");
		w1TextField.setBounds(Left, 5, width, height);
		w1TextField.addFocusListener(wTLFocusListener);
		w1TextField.setToolTipText("Min: 1; Max: 1000000"); // Integer.MAX_VALUE
		w1TextField.setName("Integer");
		w1IPanel.add(w1TextField);
	}

	void InitialPoints() {
		wLabel = new JLabel("Initial points: ");
		wLabel.setBounds(25, 33 + 5, width, height);
		w1IPanel.add(wLabel);

		w10TextField = new JTextField("10");
		w10TextField.setBounds(Left, 33 + 5, width, height);
		w10TextField.addFocusListener(wTLFocusListener);
		w10TextField.setToolTipText("Min: 1; Max: 1000000"); // Integer.MAX_VALUE
		w10TextField.setName("Integer");
		w1IPanel.add(w10TextField);
	}

	// ------ ActionListener ----------------------------------------
	class CActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Object wObject = e.getSource();
			if (wObject == w1Button) {
				if (w1Button.getText() == "Start") {
					w1Button.setEnabled(false);
					w3Button.setEnabled(false);
					Can = true;
					WriteOneArray();
					WriteToScroll();
					try {
						int InType = w2ComboBox.getSelectedIndex(), Resours = Integer
								.parseInt(w2TextField.getText());
						IType = "" + w2ComboBox.getSelectedItem();
						IType = IType.toLowerCase();
						Method = "" + w1ComboBox.getSelectedItem();
						if (Method.equals("Glopt"))
							Method = "Globt";
						wCounter = new Counter(Iterations, InType, Resours,
								AllBankrupt, AllDividends, wProgressBar,
								wALabel, wTabbedPane, w1Button, w2Button,
								w3Button, BankLength, ShareLength,
								InsuranceLength, ObjectValue, Method,
								mGraphics, Uf);
						wCounter.setPriority(Counter.MIN_PRIORITY);
						wItVector.clear();
						wFVector.clear();
						wCounter.Data(wItVector, wFVector);
						wCounter.start();
					} catch (Exception ex) {
						getAppletContext().showStatus(
								"Error: Can't create thread!");
						w1Button.setEnabled(true);
						w3Button.setEnabled(true);
						Can = false;
					}
				} else if (w1Button.getText() == "Pause") {
					wCounter.SuspendCounter();
				} else if (w1Button.getText() == "Continue") {
					wCounter.ResumeCounter();
				}
			}
			if (wObject == w2Button) {
				wCounter.FinishedCounter();
			}
			if (wObject == w3Button) {
				if (wCounter.isAlive())
					wCounter.FrameD();
				int X = 30, Y = 30, Xx = 550, Yy = 300;
				if (wGFrame != null) {
					X = wGFrame.getX();
					Y = wGFrame.getY();
					Xx = wGFrame.getWidth();
					Yy = wGFrame.getHeight();
					wGFrame.dispose();
				}
				wGFrame = new JFrame("Convergence graphic");
				int IT;
				if (Method.equals("Globt"))
					IT = (Iterations[0] * 3) + 1;
				else
					IT = Iterations[0];
				mGraphics = new MeGraphics(IT, wItVector, wFVector);
				wGFrame.getContentPane().add(mGraphics);
				wGFrame.setLocation(X, Y);
				wGFrame.setSize(Xx, Yy);
				if (wCounter.isAlive())
					wCounter.FrameC(mGraphics);
				wGFrame.setVisible(true);
			}
			if (wObject == w4Button)
				CheckValues();
		}
	}

	CActionListener wBActionListener = new CActionListener();

	// ------ ChangeListener ----------------------------------------
	class CChangeListener implements ChangeListener {
		public void stateChanged(ChangeEvent e) {
			if (((JTabbedPane) e.getSource()).getSelectedIndex() == 5) {
				WriteArray();
				InNames();
				CheckValues();
				w1Button.setText("Start");
			}
		}
	}

	CChangeListener wChangeListener = new CChangeListener();

	// ---------------------------------------------------------------
	// -- METODS -----------------------------------------------------
	// ---------------------------------------------------------------

	void CheckValues() // Check utility values
	{
		getAppletContext().showStatus("");
		for (int i = 1; i < Uf.length; ++i) {
			double val1 = Uf[i - 1];
			double val2 = Uf[i];
			if (val1 >= val2) {			
				getAppletContext().showStatus("Utility function must be monotonic");
			}
		}
		
		/*
		for (int i = 0; i < 7; i++) {
			try {
				Double.parseDouble(wUTextField[i].getText());
				getAppletContext().showStatus("");
				wUTextField[i].setBackground(new Color(255, 255, 255));
			} catch (NumberFormatException exc) {
				getAppletContext().showStatus(
						"Error: It's not floating point value!");
				wUTextField[i].setBackground(new Color(255, 90, 90));
				return;
			}
		}
		for (int i = 0; i < 6; i++) {
			double d = Double.parseDouble(wUTextField[i].getText());
			double dd = Double.parseDouble(wUTextField[i + 1].getText());
			if (d > dd) {
				getAppletContext().showStatus(
						"Error: " + (i + 1)
								+ " value must be less or equal then "
								+ (i + 2) + " value!");
				wUTextField[i].setBackground(new Color(255, 90, 90));
				wUTextField[i + 1].setBackground(new Color(255, 90, 90));
				return;
			}
		}*/
	}

	void WriteOneArray() // Write same data in one array
	{
		Iterations[0] = It;
		if (w10TextField != null)
			Iterations[1] = Ip;
		else
			Iterations[1] = -1;
		AllBankrupt = new double[ArrayLength];
		AllDividends = new double[ArrayLength];
		int Month = 12 / Integer.parseInt("" + w3ComboBox.getSelectedItem());
		int Count = 0;
		for (int i = 1; i < 9; i++) {
			if (BankNames[i] != null) {
				AllBankrupt[Count] = (double) Math
						.round((1 - BankBankrupt[i]) * 100000) / 100000;
				AllDividends[Count] = BankDividends[i] / (Month * 100) + 1;
				++Count;
			}
		}
		for (int i = 1; i < 9; i++) // Shares Probability
		{
			if (ShareNames[i] != null) {
				double Sum = PyP[i] + RyP[i] + OyP[i];
				if (Sum > 1) {
					PyP[i] = PyP[i] / Sum;
					RyP[i] = RyP[i] / Sum;
					OyP[i] = OyP[i] / Sum;
				}
				AllBankrupt[Count] = (double) Math
						.round((1 - ShareBankrupt[i]) * 100000) / 100000;
				AllDividends[Count] = 1
						+ (ShareProfit[i] / (Month * 100))
						+ (PyP[i] * (ShareMin[i] / (Month * 100)) + RyP[i]
								* (ShareAvg[i] / (Month * 100)) + OyP[i]
								* (ShareMax[i] / (Month * 100)));
				++Count;
			}
		}
		for (int i = 1; i < 9; i++) {
			if (InsuranceNames[i] != null) {
				AllBankrupt[Count] = InsuranceDisaster[i]; // (double)Math.round((1-InsuranceDisaster[i])*100000)/100000;
				AllDividends[Count] = InsuranceIncrease[i];
				++Count;
			}
		}
	}

	// -----------------------------------------------------------
	void WriteArray() // Wriite all data in arrays
	{
		int count = 0, Icount = 0;
		BankLength = 0;
		ShareLength = 0;
		InsuranceLength = 0;
		for (int i = 1; i < 9; i++)
			if (wITextField[i].getText().length() != 0)
				++Icount;
		ObjectValue = new double[Icount];

		String Strr;
		Strr = w1TextField.getText(); // Iterations
		try {
			int n = Integer.parseInt(Strr);
			It = n;
		} catch (NumberFormatException exc) {
			It = -1;
		}

		if (w10TextField != null) {
			Strr = w10TextField.getText(); // Initial point
			try {
				int n = Integer.parseInt(Strr);
				Ip = n;
			} catch (NumberFormatException exc) {
				Ip = -1;
			}
		}

		Strr = w2TextField.getText(); // Resources
		try {
			int n = Integer.parseInt(Strr);
			Resources = n;
		} catch (NumberFormatException exc) {
			Resources = -1;
		}

		
		/*
		for (int i = 0; i < 7; i++) {
			String Str = wUTextField[i].getText();
			try {
				double n = Double.parseDouble(Str);
				Uf[i + 1] = n;
			} catch (NumberFormatException exc) {
				Uf[i + 1] = -1;
			}
		}
		Uf[8] = 1;*/
		double[] values = new double[11];
		values[0] = 0;
		values[10] = 1;
		for (int i = 1; i < 10; i++) {
			String str = p.getPanelTextFields()[i - 1].getText();
			try {
				double y = Double.parseDouble(str);
				values[i] = y;
			} catch (NumberFormatException exc) {
				values[i] = -1;
			}
		}
		for (int i = 0; i < 9; ++i) {
			double pos = 1.0 * i / 8 * 10;
			int posL = (int) Math.floor(pos);
			int posH = (int) Math.ceil(pos);
			if (posL == posH) {
				Uf[i] = values[posL];
			} else {
				double lWeight = posH - pos;
				double hWeight = pos - posL;
				Uf[i] = lWeight * values[posL] + hWeight * values[posH];
			}
		}

		

		for (int i = 1; i < 9; i++) {
			String Str = wBTextField[i].getText(), Str1 = wBTextField[i + 9]
					.getText(), Str2 = wTextField[i].getText(), Str4 = wTextField[i + 9]
					.getText(), Str5 = wPTextField[i].getText(), Str6 = wRTextField[i]
					.getText(), Str7 = wOTextField[i].getText(), Str8 = wITextField[i]
					.getText(), Str9 = wIdTextField[i].getText(), Str10 = wIwTextField[i]
					.getText();
			if (Str.length() != 0) {
				if (Str.length() > 40)
					Str = Str.substring(0, 0);
				BankNames[i] = Str;
				++count;
				++BankLength;
				try {
					double n = Double.parseDouble(Str1);
					BankBankrupt[i] = n;
				} catch (NumberFormatException exc) {
					BankBankrupt[i] = -1;
				}
			} else {
				BankNames[i] = null;
			}

			if (Str2.length() != 0) {
				if (Str2.length() > 40)
					Str2 = Str2.substring(0, 40);
				ShareNames[i] = Str2;
				++count;
				++ShareLength;
				try {
					double n = Double.parseDouble(Str4);
					ShareBankrupt[i] = n;
					n = Double.parseDouble(Str5);
					PyP[i] = n;
					n = Double.parseDouble(Str6);
					RyP[i] = n;
					n = Double.parseDouble(Str7);
					OyP[i] = n;
				} catch (NumberFormatException exc) {
					ShareBankrupt[i] = -1;
				}
			} else {
				ShareNames[i] = null;
			}

			if (Str8.length() != 0) {
				if (Str8.length() > 40)
					Str8 = Str8.substring(0, 40);
				InsuranceNames[i] = Str8;
				++count;
				++InsuranceLength;
				try {
					double n = Double.parseDouble(Str9);
					InsuranceDisaster[i] = n;
					int nn = Integer.parseInt(Str10);
					ObjectValue[InsuranceLength - 1] = (double) nn / Resources;
				} catch (NumberFormatException exc) {
					InsuranceDisaster[i] = -1;
				}
			} else {
				InsuranceNames[i] = null;
			}
		}

		ArrayLength = count;
	}

	// -----------------------------------------------------------
	void InNames() // Errors in imput
	{
		boolean full = false;
		int i = 0;
		while ((ShareNames[i] == null) && (i < 9 - 1) && (BankNames[i] == null)) {
			i++;
			if ((ShareNames[i] != null) || (BankNames[i] != null)
					|| (InsuranceNames[i] != null))
				full = true;
		}

		for (i = 0; i < 9; i++)
			if (Uf[i] == -1)
				full = false;
		for (i = 0; i < 8; i++)
			if (Uf[i] > Uf[i + 1])
				full = false;

		for (i = 1; i < 9; i++) {
			if ((ShareNames[i] != null) && (ShareBankrupt[i] == -1))
				full = false;
			if ((BankNames[i] != null) && (BankBankrupt[i] == -1))
				full = false;
			if ((InsuranceNames[i] != null) && (InsuranceDisaster[i] == -1))
				full = false;
		}

		if (It == -1)
			full = false;
		if (Ip == -1)
			full = false;
		if (Resources == -1)
			full = false;

		Full(full);
	}

	// -----------------------------------------------------------
	void WriteToScroll() // Write to ScrollPane
	{
		wPanel = new JPanel();
		JPanel wFlowPanel, wWestPanel = new JPanel(), wEastPanel = new JPanel(), wNorthPanel = new JPanel();

		wWestPanel.setLayout(new BoxLayout(wWestPanel, BoxLayout.Y_AXIS));
		wEastPanel.setLayout(new BoxLayout(wEastPanel, BoxLayout.Y_AXIS));
		int Count = 0;
		wPanel.removeAll();

		wALabel = new JLabel[ArrayLength + 3];
		int OptionsCount = 6;

		for (int i = 1; i <= OptionsCount; i++) // West Options
		{
			wFlowPanel = new JPanel();
			wFlowPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
			if (i == 1)
				wLabel = new JLabel("  Iterations");
			if (i == 2)
				wLabel = new JLabel("  U(x)");
			if (i == 3)
				wLabel = new JLabel("  Investment type");
			if (i == 4)
				wLabel = new JLabel("  Period (month)");
			if (i == 5)
				wLabel = new JLabel("  Resources");
			if (i == 6) {
				wLabel = new JLabel("  Returned wealth");
				wLabel.setForeground(new Color(255, 90, 90));
			}
			wFlowPanel.add(wLabel);
			wWestPanel.add(wFlowPanel);
		}

		for (int i = 1; i < 9; i++) // West BankNames
		{
			if (BankNames[i] != null) {
				Count++;
				wFlowPanel = new JPanel();
				wFlowPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
				wLabel = new JLabel("  " + Count + ". B ");
				wFlowPanel.add(wLabel);
				wLabel = new JLabel("" + BankNames[i]);
				wLabel.setForeground(Color.black);
				wFlowPanel.add(wLabel);
				wWestPanel.add(wFlowPanel);
			}
		}

		for (int i = 1; i < 9; i++) // West ShareNames
		{
			if (ShareNames[i] != null) {
				Count++;
				wFlowPanel = new JPanel();
				wFlowPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
				wLabel = new JLabel("  " + Count + ". S ");
				wFlowPanel.add(wLabel);
				wLabel = new JLabel("" + ShareNames[i]);
				wLabel.setForeground(Color.black);
				wFlowPanel.add(wLabel);
				wWestPanel.add(wFlowPanel);
			}
		}

		for (int i = 1; i < 9; i++) // West InsuranceNames
		{
			if (InsuranceNames[i] != null) {
				Count++;
				wFlowPanel = new JPanel();
				wFlowPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
				wLabel = new JLabel("  " + Count + ". I ");
				wFlowPanel.add(wLabel);
				wLabel = new JLabel("" + InsuranceNames[i]);
				wLabel.setForeground(Color.black);
				wFlowPanel.add(wLabel);
				wWestPanel.add(wFlowPanel);
			}
		}

		for (int i = 0; i < OptionsCount; i++) // East
		{
			wFlowPanel = new JPanel();
			wFlowPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
			if (i == 0)
				wALabel[0] = new JLabel(" "); // Iterations
			if (i == 1)
				wALabel[ArrayLength + 2] = new JLabel(" "); // U(x)
			if (i == 2)
				wLabel = new JLabel("" + w2ComboBox.getSelectedItem()); // Investment
																		// type
			if (i == 3)
				wLabel = new JLabel("" + w3ComboBox.getSelectedItem()); // Period
																		// (month)
			if (i == 4)
				wLabel = new JLabel("" + w2TextField.getText()); // Resources
			if (i == 5) {
				wALabel[ArrayLength + 1] = new JLabel(" "); // Returned wealth
				wALabel[ArrayLength + 1].setForeground(new Color(255, 90, 90));
			}

			if (i == 0)
				wFlowPanel.add(wALabel[0]);
			else if (i == 1)
				wFlowPanel.add(wALabel[ArrayLength + 2]);
			else if (i == 5)
				wFlowPanel.add(wALabel[ArrayLength + 1]);
			else
				wFlowPanel.add(wLabel);

			wLabel = new JLabel("          ");
			wFlowPanel.add(wLabel);
			wEastPanel.add(wFlowPanel);
		}

		for (int i = 1; i <= Count; i++) // East Count side
		{
			wFlowPanel = new JPanel();
			wFlowPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
			wALabel[i] = new JLabel(" ");
			wALabel[i].setForeground(Color.black);
			wFlowPanel.add(wALabel[i]);
			wLabel = new JLabel("          ");
			wFlowPanel.add(wLabel);
			wEastPanel.add(wFlowPanel);
		}

		wPanel.setLayout(new BorderLayout());
		wPanel.add(wWestPanel, "West");
		wPanel.add(wEastPanel, "East");
		wNorthPanel.setLayout(new BorderLayout());
		wNorthPanel.add(wPanel, "North");
		wScrollPane.setViewportView(wNorthPanel);
	}

	void Full(boolean full) // On off buttons
	{
		if (full) {
			w1Button.setEnabled(true);
			w1Button.setToolTipText("Start investment count");
		} else {
			w1Button.setEnabled(false);
			w1Button.setToolTipText(null);
		}
	}

	// **************************************************************/
	// **************************************************************/

	public void init() {
		// Force SwingApplet to come up in the System L&F
		String laf = UIManager.getCrossPlatformLookAndFeelClassName();
		// String laf = UIManager.getSystemLookAndFeelClassName();
		try {
			System.out.println("");
			System.out.println("Started");
			UIManager.setLookAndFeel(laf);
			System.out.println("Found LookAndFeel: " + laf);
			System.out.println("");
		} catch (UnsupportedLookAndFeelException exc) {
			System.out.println("Warning: UnsupportedLookAndFeel: " + laf);
		} catch (Exception exc) {
			System.out.println("Error loading " + laf + ": " + exc);
		}
		// --------------------------------------------------------------

		getContentPane().setLayout(new BorderLayout());
		try {
			String Str = getParameter("width"); // From HTML get width parameter
			if (Str != null)
				ParamWidth = Integer.parseInt(Str);
			Str = getParameter("height");
			if (Str != null)
				ParamHeight = Integer.parseInt(Str);
			if (ParamWidth < 770)
				ParamWidth = 770;
			if (ParamHeight < 350)
				ParamHeight = 350;
			setSize(ParamWidth, ParamHeight);
		} catch (NumberFormatException exc) {
			setSize(770, 350);
			ParamWidth = 770;
			ParamHeight = 350;
		}

		// --------------- OPTIONS --------------------------

		w1Panel = new JPanel();
		w1Panel.setLayout(null);

		for (int i = 0; i < 9; i++) {
			Uf[i] = 0.1;

			BankNames[i] = null;
			BankDividends[i] = 1;
			BankBankrupt[i] = 0.1;

			ShareNames[i] = null;
			ShareProfit[i] = 0;
			ShareMin[i] = 0;
			ShareAvg[i] = 0;
			ShareMax[i] = 0;

			ShareBankrupt[i] = 0.1;
			PyP[i] = 0.1;
			RyP[i] = 0.1;
			OyP[i] = 0.1;

			InsuranceNames[i] = null;
			InsuranceDisaster[i] = 0.1;
			InsuranceIncrease[i] = 1;
		}

		// ----- OPTIONS Title --------
		wLabel = new JLabel("  Property ");
		wLabel.setBounds(15, 5, ParamWidth - 200, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		w1Panel.add(wLabel);

		Left = ParamWidth - 170;
		wLabel = new JLabel("  Value ");
		wLabel.setBounds(Left, 5, width, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		w1Panel.add(wLabel);

		// ------- OPTIONS Property ------
		int l = 5;

		wLabel = new JLabel("Investment type: ");
		wLabel.setBounds(25, (33 * 1) + l, width, height);
		w1Panel.add(wLabel);

		wLabel = new JLabel("Period (month): ");
		wLabel.setBounds(25, (33 * 2) + l, width, height);
		w1Panel.add(wLabel);

		wLabel = new JLabel("Resources: ");
		wLabel.setBounds(25, (33 * 3) + l, width, height);
		w1Panel.add(wLabel);

		// ------- OPTION Value ------

		w2ComboBox = new JComboBox();
		w2ComboBox.setBounds(Left, (33 * 1) + l, width, height);
		w2ComboBox.addItem("Real");
		w2ComboBox.addItem("Prudent");
		w2ComboBox.addItem("Risky");
		w2ComboBox.addItem("Rich");
		w2ComboBox.addItem("User");
		w2ComboBox.addItemListener(wItemListener);
		w2ComboBox.setName("IType");
		w1Panel.add(w2ComboBox);

		w3ComboBox = new JComboBox();
		w3ComboBox.setBounds(Left, (33 * 2) + l, width, height);
		w3ComboBox.addItem("3");
		w3ComboBox.addItem("6");
		w3ComboBox.addItem("12");
		w3ComboBox.setSelectedItem("12");
		w3ComboBox.setName("w3ComboBox");
		w3ComboBox.setToolTipText("Min: 3; Max: 12");
		w1Panel.add(w3ComboBox);

		w2TextField = new JTextField("100");
		w2TextField.setBounds(Left, (33 * 3) + l, width, height);
		w2TextField.addFocusListener(wTLFocusListener);
		w2TextField.setToolTipText("Min: 1; Max: 1000000");
		w2TextField.setName("Integer");
		w1Panel.add(w2TextField);

		// --- METHOD Metod ----------
		wLabel = new JLabel("Metod : ");
		wLabel.setBounds(25, 33 * 4 + l, width, height);
		w1Panel.add(wLabel);

		w1ComboBox = new JComboBox();
		w1ComboBox.setBounds(Left, (33 * 4) + l, width, height);
		w1ComboBox.addItem("Mig1");
		// w1ComboBox.addItem("LBayes");
		w1ComboBox.addItem("Glopt");
		w1ComboBox.addItem("Bayes");
		// w1ComboBox.addItem("Unt");
		w1ComboBox.addItemListener(wItemListener);
		w1ComboBox.setName("Method");
		w1Panel.add(w1ComboBox);

		// ------- --------------------------
		w1IPanel = new JPanel();
		w1IPanel.setLayout(null);
		w1IPanel.setBounds(0, 33 * 5, ParamWidth, ParamHeight - (33 * 5));
		w1Panel.add(w1IPanel);
		Iteration();

		System.out.println(" Added Options");

		// --------------- UTILITY FUNCTION ----------------
		w6Panel = p.getPanel();
		

		System.out.println(" Added Profit f");

		// --------------- BANK ---------------------------
		w2Panel = new JPanel();
		w2Panel.setLayout(null);

		// --- BNAK Title ----------
		wLabel = new JLabel("  Bank name ");
		wLabel.setBounds(15, 5, ParamWidth - 180, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		w2Panel.add(wLabel);

		wLabel = new JLabel("  Interest R");
		wLabel.setBounds(Left + 20, 5, width - 85, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Interest rates (%)");
		w2Panel.add(wLabel);

		wLabel = new JLabel("  BP ");
		wLabel.setBounds(Left + 100, 5, width - 100, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Bankruptcy probability");
		w2Panel.add(wLabel);

		// ------- BANK name -------
		for (int i = 1; i < 9; i++) {
			wLabel = new JLabel("" + i + ".");
			wLabel.setBounds(20, (33 * i) + l, 15, height);
			w2Panel.add(wLabel);

			wBTextField[i] = new JTextField();
			wBTextField[i]
					.setBounds(35, (33 * i) + l, ParamWidth - 200, height);
			wBTextField[i].addFocusListener(wTLFocusListener);
			wBTextField[i].setToolTipText("Min length: 0; Max length: 40");
			wBTextField[i].setName("Name");
			w2Panel.add(wBTextField[i]);

			// ------- BANK Divident -------
			wComboBox = new JComboBox();
			wComboBox.setBounds(Left + 20, (33 * i) + l, width - 85, height);
			for (int j = 1; j <= 100; j++) {
				wComboBox.addItem("" + j);
			}
			wComboBox.addItemListener(wItemListener);
			wComboBox.setToolTipText("Min: 1; Max: 100");
			wComboBox.setName("BanksD");
			w2Panel.add(wComboBox);

			// ------- BANK Bankrupt -------
			wBTextField[i + 9] = new JTextField("0.1");
			wBTextField[i + 9].setBounds(Left + 100, (33 * i) + l, width - 100,
					height);
			wBTextField[i + 9].addFocusListener(wTLFocusListener);
			wBTextField[i + 9].setName("Double");
			wBTextField[i + 9].setToolTipText("Bankrupt = 1; Min: 0; Max: 1");
			w2Panel.add(wBTextField[i + 9]);
		}

		System.out.println(" Added Banks");

		// --------------- SHARES ---------------------------

		w3Panel = new JPanel();
		w3Panel.setLayout(null);

		// --- Chares Title ----------
		wLabel = new JLabel("  Stock name ");
		wLabel.setBounds(15, 5, ParamWidth - 515, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		w3Panel.add(wLabel);

		wLabel = new JLabel("  Min SR");
		wLabel.setBounds(ParamWidth - 486, 5, width - 98, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Min shares rate");
		w3Panel.add(wLabel);

		wLabel = new JLabel("  PyP");
		wLabel.setBounds(ParamWidth - 420, 5, width - 110, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Pessimstic year probability");
		w3Panel.add(wLabel);

		wLabel = new JLabel("  Real SR");
		wLabel.setBounds(ParamWidth - 366, 5, width - 98, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Real shares rate");
		w3Panel.add(wLabel);

		wLabel = new JLabel("  RyP");
		wLabel.setBounds(ParamWidth - 300, 5, width - 110, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Realistic year probability");
		w3Panel.add(wLabel);

		wLabel = new JLabel("  Max SR");
		wLabel.setBounds(ParamWidth - 245, 5, width - 100, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Max shares rate");
		w3Panel.add(wLabel);

		wLabel = new JLabel("  OyP");
		wLabel.setBounds(ParamWidth - 180, 5, width - 110, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Optimistic year probability");
		w3Panel.add(wLabel);

		wLabel = new JLabel(" Dividend");
		wLabel.setBounds(Left + 44, 5, width - 99, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Share dividend"); // "Dividends (%) +
													// (Predicted SR - Now
													// SR)*100/Predicted SR (%)"
		w3Panel.add(wLabel);

		wLabel = new JLabel("  BP ");
		wLabel.setBounds(Left + 110, 5, width - 110, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Bankruptcy probability");
		w3Panel.add(wLabel);

		// ---- SHARES Name ----------
		for (int i = 1; i < 9; i++) {
			wLabel = new JLabel("" + i + ".");
			wLabel.setBounds(20, (33 * i) + l, 15, height);
			w3Panel.add(wLabel);

			wTextField[i] = new JTextField();
			wTextField[i].setBounds(35, (33 * i) + l, ParamWidth - 535, height);
			wTextField[i].setToolTipText("Min length: 0; Max length: 40");
			wTextField[i].addFocusListener(wTLFocusListener);
			wTextField[i].setName("Name");
			w3Panel.add(wTextField[i]);

			// ---- SHARES Min ---------
			wComboBox = new JComboBox();
			wComboBox.setBounds(Left - 316, (33 * i) + l, width - 97, height);
			for (int j = -100; j <= 100; j++) {
				wComboBox.addItem("" + j);
			}
			wComboBox.addItemListener(wItemListener);
			wComboBox.setToolTipText("Min: -100; Max: 100");
			wComboBox.setName("SharesMin");
			wComboBox.setSelectedItem("0");
			w3Panel.add(wComboBox);

			// ---- SHARES Pessimistic ---------
			wPTextField[i] = new JTextField("0.1");
			wPTextField[i].setBounds(Left - 250, (33 * i) + l, width - 110,
					height);
			wPTextField[i].setToolTipText("Pessimistic = 1; Min: 0; Max: 1");
			wPTextField[i].setName("Double");
			wPTextField[i].addFocusListener(wTLFocusListener);
			w3Panel.add(wPTextField[i]);

			// ---- SHARES Nezinau ----------

			wComboBox = new JComboBox();
			wComboBox.setBounds(Left - 196, (33 * i) + l, width - 97, height);
			for (int j = -100; j <= 100; j++) {
				wComboBox.addItem("" + j);
			}
			wComboBox.addItemListener(wItemListener);
			wComboBox.setToolTipText("Min: -100; Max: 100");
			wComboBox.setName("SharesAvg");
			wComboBox.setSelectedItem("0");
			w3Panel.add(wComboBox);

			// ---- SHARES Realistic -----------
			wRTextField[i] = new JTextField("0.1");
			wRTextField[i].setBounds(Left - 130, (33 * i) + l, width - 110,
					height);
			wRTextField[i].setToolTipText("Realistic = 1; Min: 0; Max: 1");
			wRTextField[i].setName("Double");
			wRTextField[i].addFocusListener(wTLFocusListener);
			w3Panel.add(wRTextField[i]);

			// ---- SHARES Max ----------

			wComboBox = new JComboBox();
			wComboBox.setBounds(Left - 75, (33 * i) + l, width - 100, height);
			for (int j = 0; j <= 100; j++) {
				wComboBox.addItem("" + j);
			}
			wComboBox.addItemListener(wItemListener);
			wComboBox.setToolTipText("Min: 0; Max: 100");
			wComboBox.setName("SharesMax");
			w3Panel.add(wComboBox);

			// ---- SHARES Optimistic ----------
			wOTextField[i] = new JTextField("0.1");
			wOTextField[i].setBounds(Left - 10, (33 * i) + l, width - 110,
					height);
			wOTextField[i].setToolTipText("Optimistic = 1; Min: 0; Max: 1");
			wOTextField[i].setName("Double");
			wOTextField[i].addFocusListener(wTLFocusListener);
			w3Panel.add(wOTextField[i]);

			// ------- SHARES Dividend -------
			wComboBox = new JComboBox();
			wComboBox.setBounds(Left + 44, (33 * i) + l, width - 99, height);
			for (int j = 0; j <= 100; j++) {
				wComboBox.addItem("" + j);
			}
			wComboBox.addItemListener(wItemListener);
			wComboBox.setToolTipText("Min: 0; Max: 100");
			wComboBox.setName("SharesP");
			w3Panel.add(wComboBox);

			// ---- SHARES Bankrupt ----------
			wTextField[i + 9] = new JTextField("0.1");
			wTextField[i + 9].setBounds(Left + 110, (33 * i) + l, width - 110,
					height);
			wTextField[i + 9].setToolTipText("Bankrupt = 1; Min: 0; Max: 1");
			wTextField[i + 9].setName("Double");
			wTextField[i + 9].addFocusListener(wTLFocusListener);
			w3Panel.add(wTextField[i + 9]);
		}

		System.out.println(" Added Shares");

		// --------------- INSURANSE ------------------------

		w4Panel = new JPanel();
		w4Panel.setLayout(null);

		// --- INSURANSE Title ----------
		wLabel = new JLabel("  Object name");
		wLabel.setBounds(15, 5, ParamWidth - 275, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		w4Panel.add(wLabel);

		wLabel = new JLabel("  Object value");
		wLabel.setBounds(ParamWidth - 245, 5, width - 70, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Object value");
		w4Panel.add(wLabel);

		wLabel = new JLabel("  IS/P");
		wLabel.setBounds(Left + 20, 5, width - 85, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Insurance sum/payment");// Dividends (%) +
														// (Predicted SR - Now
														// SR)*100/Predicted SR
														// (%)
		w4Panel.add(wLabel);

		wLabel = new JLabel("  DP ");
		wLabel.setBounds(Left + 100, 5, width - 100, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		wLabel.setToolTipText("Deprivation probability");
		w4Panel.add(wLabel);

		// ---------------------
		for (int i = 1; i < 9; i++) {
			wLabel = new JLabel("" + i + ".");
			wLabel.setBounds(20, (33 * i) + l, 15, height);
			w4Panel.add(wLabel);
			// ------ INSURANSE Names ------------------
			wITextField[i] = new JTextField();
			wITextField[i]
					.setBounds(35, (33 * i) + l, ParamWidth - 295, height);
			wITextField[i].setToolTipText("Min length: 0; Max length: 40");
			wITextField[i].addFocusListener(wTLFocusListener);
			wITextField[i].setName("Name");
			w4Panel.add(wITextField[i]);
			// ------INSURANSE Object value ------------
			wIwTextField[i] = new JTextField("1000");
			wIwTextField[i].setBounds(ParamWidth - 245, (33 * i) + l,
					width - 70, height);
			wIwTextField[i].addFocusListener(wTLFocusListener);
			wIwTextField[i].setToolTipText("Min: 1; Max: 1000000");
			wIwTextField[i].setName("Integer");
			w4Panel.add(wIwTextField[i]);
			// ------INSURANSE
			wComboBox = new JComboBox();
			wComboBox.setBounds(Left + 20, (33 * i) + l, width - 85, height);
			for (int j = 1; j <= 100; j++) {
				wComboBox.addItem("" + j);
			}
			wComboBox.addItemListener(wItemListener);
			wComboBox.setToolTipText("Min: 1; Max: 100");
			wComboBox.setName("InsuranceA");
			w4Panel.add(wComboBox);
			// ------ INSURANSE Disaster -----------------
			wIdTextField[i] = new JTextField("0.1");
			wIdTextField[i].setBounds(Left + 100, (33 * i) + l, width - 100,
					height);
			wIdTextField[i].setToolTipText("Deprivation = 1; Min: 0; Max: 1");
			wIdTextField[i].setName("Double");
			wIdTextField[i].addFocusListener(wTLFocusListener);
			w4Panel.add(wIdTextField[i]);
		}

		System.out.println(" Added Insuranse");

		// --------------- COUNT ----------------------------

		w5Panel = new JPanel();
		w5Panel.setLayout(null);

		// --- Count Title ----------
		wLabel = new JLabel("  Name");
		wLabel.setBounds(15, 5, ParamWidth - 200, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		w5Panel.add(wLabel);

		wLabel = new JLabel("  Value");
		wLabel.setBounds(Left, 5, width, height);
		wLabel.setOpaque(true);
		wLabel.setBackground(new Color(161, 161, 161));
		wLabel.setForeground(new Color(66, 75, 145));
		w5Panel.add(wLabel);

		// --- Count Butons ----------
		int LeftWidth = (ParamWidth - 45) / 3;
		w1Button = new JButton("Start");
		w1Button.setBounds(25, ParamHeight - 60, LeftWidth - 10, height);
		w1Button.addActionListener(wBActionListener);
		w1Button.setEnabled(false);
		w5Panel.add(w1Button);

		w2Button = new JButton("Stop");
		w2Button.setBounds(25 + LeftWidth, ParamHeight - 60, LeftWidth - 10,
				height);
		w2Button.addActionListener(wBActionListener);
		w2Button.setEnabled(false);
		w5Panel.add(w2Button);

		w3Button = new JButton("Graphic");
		w3Button.setBounds(25 + LeftWidth * 2, ParamHeight - 60,
				LeftWidth - 10, height);
		w3Button.setEnabled(false);
		w3Button.addActionListener(wBActionListener);
		w5Panel.add(w3Button);

		// --- COUNT ScrollPane -------------------------------------------
		wScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		wScrollPane.setBounds(15, 33 + l, ParamWidth - 35, ParamHeight - 110);
		wScrollPane.setViewportView(wPanel);
		w5Panel.add(wScrollPane);
		// --- COUNT ProgressBar -------------------------------------------
		wProgressBar = new JProgressBar();
		wProgressBar.setBounds(15, ParamHeight - 70, ParamWidth - 35, 5);
		wProgressBar.setValue(0);
		w5Panel.add(wProgressBar);

		System.out.println(" Added Count");

		// ----------------------------------------------------------------

		wTabbedPane.addTab("Options", w1Panel); // Options
		wTabbedPane.addTab("Utility F", w6Panel); // Options
		wTabbedPane.addTab("Banks", w2Panel); // Banks
		wTabbedPane.addTab("Shares", w3Panel); // Shares
		wTabbedPane.addTab("Insurance", w4Panel); // Insuranse
		wTabbedPane.addTab("Count", w5Panel); // Count
		wTabbedPane.setEnabledAt(1, false);
		wTabbedPane.addChangeListener(wChangeListener);

		getContentPane().add(wTabbedPane);

		System.out.println("");
		System.out.println("Finished");

	}

}