/*
 * StartupApplet.java
 *
 * Created on 2007 01 04
 */

package lt.ktu.mockus.srgm;

import com.jrefinery.data.*;
 
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.jrefinery.chart.*;
//import com.jrefinery.data.*;

/**
 *
 * @author  Paule Zaksauskiene
 * @author  Arunas Andriulaitis
 * @author  Evaldas Ramonas
 * @autor Indre Zliobaite
 */
public class StartupApplet extends JApplet
{
	
	
	//---------------stock preferences tab----------------
	public static final int MAX_STOCK_COUNT = 1;
	
	private static final double DEFAULT_VOLATOLITY = 0.09d;
	private static final double DEFAULT_DIVIDEND = 0.002d;
		
	public int stockCount = 1;
										
	public ArrayList volatility = null;
	public ArrayList dividend =   null;
	public double yieldOfCd = 0.002d;

	private JPanel stocksPreferencesTabPanel = new JPanel();
	private JComboBox stockCountComboBox = null;
	private JTextField[] volatilityField = null;
	private JTextField[] dividendField = null;
	private JTextField yieldOfCdField = null;	
	
	
	//-----------stock market prices tab--------------
	private static final double STOCK_STARTUP_PRICE = 5.0d;
	private static final int DEFAULT_STOCK_HISTORY_STORAGE = 2000;
									
	public ArrayList stocksVisibility = null;	//[stockNo][firmNo]
	public StockContainer stocksPrices[] = null; //open prices
	public StockContainer stocksPricesT[] = null; //closing prices
	public int stockHistory = DEFAULT_STOCK_HISTORY_STORAGE;
	private int lastRemovedFromHistory = 1;
	
	
	private JTabbedPane stocksMarketTabPanel = new JTabbedPane();
	private JCheckBox[][] customerStockVisibilityCheckBoxes = null;
	
	private BasicTimeSeries[] stocksSeries = null;
	private BasicTimeSeries[][] customerPreditedBuyPricesSeries = null;
	private BasicTimeSeries[][] customerPreditedSellPricesSeries = null;
	private BasicTimeSeries[][] customerPreditedBuy1PricesSeries = null;
	private BasicTimeSeries[][] customerPreditedSell1PricesSeries = null;
	private BasicTimeSeries[][] customerPreditedBuy2PricesSeries = null;
	private BasicTimeSeries[][] customerPreditedSell2PricesSeries = null;
	private TimeSeriesCollection[] stockMarketGraphic = null;
	private JPanel[] stockMarketOneStockPanel = null;
	
	private JPanel stockMarketCustomerProfitPanel = null;
	private TimeSeriesCollection stockMarketCustomerProfitGraphic = null;
	private BasicTimeSeries[] customerProfitSeries = null;
	
	
	//-----------------customer preferences tab----------------
	public static final int MAX_FIRM_COUNT = 5;
	
	public static final int MAX_CAN_HAVE_STOCK = 100;
	public static final int Soros_modifier=10; // kiek kartu uz kitus Sorosas (kuris yra customer #0) gali visko tureti	
	public static final int MAX_CAN_HAVE_CREDIT = -500;
	private static final boolean DEFAULT_MARKET_PRICE_VISIBILITY = true;

	//Indre
	private static final double DEFAULT_BUY_MODIFIER = 0.025d;
	private static final double DEFAULT_SELL_MODIFIER = -0.025d;
	private static final double DEFAULT_BUY1_MODIFIER = 0.05d;
	private static final double DEFAULT_SELL1_MODIFIER = -0.05d;
	private static final double DEFAULT_BUY2_MODIFIER = 0.08d;
	private static final double DEFAULT_SELL2_MODIFIER = -0.08d;

	private static final double CUSTOMER_STARTUP_BUY_PRICE = 4.86d;
	private static final double CUSTOMER_STARTUP_SELL_PRICE = 5.13d;
	private static final double CUSTOMER_STARTUP_BUY1_PRICE = 4.70d;
	private static final double CUSTOMER_STARTUP_SELL1_PRICE = 5.25d;
	private static final double CUSTOMER_STARTUP_BUY2_PRICE = 4.55d;
	private static final double CUSTOMER_STARTUP_SELL2_PRICE = 5.40d;

	private static final double SOROS_STARTUP_BUY_PRICE = 4d;
	private static final double SOROS_STARTUP_SELL_PRICE = 6d;
	private static final double SOROS_STARTUP_BUY1_PRICE = 3d;
	private static final double SOROS_STARTUP_SELL1_PRICE = 7d;
	private static final double SOROS_STARTUP_BUY2_PRICE = 2d;
	private static final double SOROS_STARTUP_SELL2_PRICE = 8d;

	//public ArrayList krepsys = null;
	//private static final double STARTUP_BASKET = 1000d;
	
	public int firmCount = 4;
	
	private final static String[] STRATEGY_METHODS = {"SOROS", "AR1", "AR2", "AR3", "A44", "AR5", "AR6", "AR7", "AR8", "AR9"};
	public ArrayList customers = null;//SRGMCustomer()

	public ArrayList kBuy = null;
	public ArrayList kSell = null;
	public ArrayList kBuy1 = null;
	public ArrayList kSell1 = null;
	public ArrayList kBuy2 = null;
	public ArrayList kSell2 = null;
	
	private JPanel customerTabPanel = new JPanel();
	private JComboBox firmCountComboBox = null;
	private JComboBox[] customerStrategyComboBox = null;
	private JTextField[] customerProfitField = null;
	private JTextField[][] customerHasStockField=null;//[stockNo][firmNo]
	
	private JTextField[] kBuyField = null;
	private JTextField[] kSellField = null;
	private JTextField[] kBuy1Field = null;
	private JTextField[] kSell1Field = null;
	private JTextField[] kBuy2Field = null;
	private JTextField[] kSell2Field = null;
	
	
	//-------------------about tab-------------------
	private JPanel aboutPanel = new JPanel();
	private final static char U_NOSINE = new Character((char)371).charValue();
	private final static char ZH_RAIDE = new Character((char)382).charValue();
	private final static char ZH_DIDZIOJI_RAIDE = new Character((char)381).charValue();
	private final static char CH_RAIDE = new Character((char)269).charValue();
	private final static char SH_DIDZIOJI_RAIDE = new Character((char)352).charValue();
	private final static char SH_RAIDE = new Character((char)353).charValue();
	private final static char E_RAIDE = new Character((char)279).charValue();
	
	
	///----------------main--------------
	private JTabbedPane mainPanel = new JTabbedPane();
	
	private static final String CONTROL_BUTTON_TRACE = "Trace";
	private JButton traceButton = new JButton( CONTROL_BUTTON_TRACE );
	
	private static final String CONTROL_BUTTON_RUN_WITH_DELAY = "Run with delay";
	private JButton runWithDelayButton = new JButton( CONTROL_BUTTON_RUN_WITH_DELAY );
	
	private static final String CONTROL_BUTTON_RUN = "Run";
	private JButton runWithoutDelayButton = new JButton( CONTROL_BUTTON_RUN );
	
	private static final String CONTROL_BUTTON_STOP = "Stop";
	private JButton stopButton = new JButton( CONTROL_BUTTON_STOP );
	
	
	
	//----------------------------------------------------------------
	public boolean readingData = false;
	public static final int CALCULATION_THREAD_DELAY = 500;
	public static final int CALCULATION_THREAD_WITHOUT_DELAY = 10;	//time needed to repaint GUI
	
	//calculation thread
	StockTradeThread calculationThread = null;
	
	
	public Calendar startupTime = null;
	
	YieldFocusListener yieldFocusListener = null;
	StockPreferencesFocusListener stockPreferencesFocusListener = null;
	StockCountChangeListener stockCountChangeListener = null;
	FirmCountChangeListener firmCountChangeListener = null;
	CustomerStrategyChangeListener customerStrategyChangeListener = null;
	CustomerPreferencesFocusListener customerPreferencesFocusListener = null;
	CustomerPricesVisibilityChangeListener customerPricesVisibilityChangeListener = null;
	ControlButtonsListener controlButtonsListener = null;
	
	
	
	
	public void reinitStockPreferencesData()
	{
			
		if ( volatility == null )
			volatility = new ArrayList();
		for (int i = volatility.size(); i < stockCount; i++)
			volatility.add( new Double(DEFAULT_VOLATOLITY));
			
		if ( dividend == null )
			dividend = new ArrayList();
		for (int i = dividend.size(); i < stockCount; i++)
			dividend.add( new Double(DEFAULT_DIVIDEND));
	}
	
	private JPanel getControlButtonsPanel()
	{
		JPanel panel3 = new JPanel();
		panel3.setLayout( new FlowLayout() ); //GridLayout(1, 4, 10, 0) );
		panel3.add( traceButton );
		traceButton.addActionListener( controlButtonsListener );
		panel3.add( runWithDelayButton );
		runWithDelayButton.addActionListener( controlButtonsListener );
		panel3.add( runWithoutDelayButton );
		runWithoutDelayButton.addActionListener( controlButtonsListener );
		panel3.add( stopButton );
		stopButton.addActionListener( controlButtonsListener );
		stopButton.setEnabled( false );
		
		return panel3;
	}
	
	public void reinitStockPreferencesTabPanel()
	{
		stocksPreferencesTabPanel.removeAll();
		stocksPreferencesTabPanel.setLayout( new BorderLayout() );
		
		//Number of different stocks in the market& Yield of CD panel
		JPanel panel1 = new JPanel();
		panel1.setLayout( new FlowLayout() );
		panel1.add( new JLabel("Number of different stocks in the market:") );
				
		String[] stockCounts = new String[MAX_STOCK_COUNT - 1];
		for ( int i = 1; i < MAX_STOCK_COUNT; i++ )
			stockCounts[i - 1] = "" + i;
		stockCountComboBox = new JComboBox( stockCounts );
		stockCountComboBox.setSelectedItem( "" + stockCount );
		stockCountComboBox.addItemListener( stockCountChangeListener );
		panel1.add( stockCountComboBox );
		
		panel1.add( new JLabel("   Yield of CD:") );
		yieldOfCdField = new JTextField( "" + yieldOfCd, 5 );
		yieldOfCdField.addFocusListener( yieldFocusListener );
		panel1.add( yieldOfCdField );
		panel1.setBorder( BorderFactory.createEtchedBorder() );
		
		//stock preferences panels
		JPanel panel2 = new JPanel();
		panel2.setLayout( new FlowLayout() );//GridLayout(1, stockCount, 5, 5) );
		
		volatilityField = new JTextField[stockCount];
		dividendField = new JTextField[stockCount];

		for ( int i = 0; i < stockCount; i++ )
		{
			JPanel stockParamsPanel = new JPanel(new GridLayout(0, 2, 2, 2));
		
			stockParamsPanel.add(new JLabel("Volatility:"));
			volatilityField[i] = new JTextField( volatility.get(i).toString() );
			volatilityField[i].addFocusListener( stockPreferencesFocusListener );
			stockParamsPanel.add(volatilityField[i]);
			//volatilityField[stockNo].addFocusListener(focusListener);
			
			stockParamsPanel.add(new JLabel("Dividend:"));
			dividendField[i] = new JTextField( dividend.get(i).toString() );
			dividendField[i].addFocusListener( stockPreferencesFocusListener );
			stockParamsPanel.add(dividendField[i]);
			//dividendField[stockNo].addFocusListener(focusListener);
	
			JPanel totalPanel = new JPanel(new BorderLayout());
			totalPanel.setBorder(BorderFactory.createEtchedBorder());
			
			JLabel label = new JLabel("Stock #" + (i + 1) + " options:");
			Font labelFont = label.getFont();
			label.setFont(new Font(labelFont.getName(), labelFont.getStyle(), labelFont.getSize() + 4));
			
			totalPanel.add(label, BorderLayout.NORTH);
			totalPanel.add(stockParamsPanel, BorderLayout.SOUTH);
			
			
			panel2.add( totalPanel );
		}

		stocksPreferencesTabPanel.add(panel1, BorderLayout.NORTH);
		stocksPreferencesTabPanel.add(panel2, BorderLayout.CENTER);  
	}
	
	public void reinitMarketData()
	{
		if ( stocksVisibility == null )
			stocksVisibility = new ArrayList();
		
		stocksPrices = new StockContainer[stockCount];
		stocksPricesT = new StockContainer[stockCount];
		
		for (int i = 0; i < stockCount; i++)
		{
			stocksPrices[i] = new StockContainer();
			stocksPrices[i].add(0, STOCK_STARTUP_PRICE );
			stocksPricesT[i] = new StockContainer();
			stocksPricesT[i].add(0, STOCK_STARTUP_PRICE );
			
			ArrayList stockVisibility = null;
			if ( stockCount > stocksVisibility.size() )
			{
				stockVisibility = new ArrayList();
				stocksVisibility.add( stockVisibility );
			}
			else
			{
				stockVisibility = (ArrayList)stocksVisibility.get(i);
			}
			
			for (int j = stockVisibility.size(); j < firmCount; j++ )
				stockVisibility.add( new Boolean(DEFAULT_MARKET_PRICE_VISIBILITY) );
		}
	}
	
	private void reconstructMarketOneStockPanel(int stockNr)
	{
		stockMarketOneStockPanel[stockNr].removeAll();

		JPanel panel3 = new JPanel();
		panel3.setLayout( new FlowLayout() );//GridLayout(1, firmCount*2 ) );
		stockMarketGraphic[stockNr] = new TimeSeriesCollection();
		
		stockMarketGraphic[ stockNr ].addSeries( stocksSeries[ stockNr ] );
		for (int j = 0; j < firmCount; j++ )
		{
			ArrayList stockVisibility = (ArrayList)stocksVisibility.get( stockNr );
			boolean visible = ((Boolean)stockVisibility.get(j)).booleanValue();
			if ( visible == true )
			{
				System.out.println(visible);
				stockMarketGraphic[stockNr].addSeries( customerPreditedBuyPricesSeries[stockNr][j] );
				stockMarketGraphic[stockNr].addSeries( customerPreditedSellPricesSeries[stockNr][j] );
				stockMarketGraphic[stockNr].addSeries( customerPreditedBuy1PricesSeries[stockNr][j] );
				stockMarketGraphic[stockNr].addSeries( customerPreditedSell1PricesSeries[stockNr][j] );
				stockMarketGraphic[stockNr].addSeries( customerPreditedBuy2PricesSeries[stockNr][j] );
				stockMarketGraphic[stockNr].addSeries( customerPreditedSell2PricesSeries[stockNr][j] );
			}
			 
			customerStockVisibilityCheckBoxes[stockNr][j] = new JCheckBox("Customer #" + j, visible);
			customerStockVisibilityCheckBoxes[stockNr][j].addActionListener( customerPricesVisibilityChangeListener );
			panel3.add( customerStockVisibilityCheckBoxes[stockNr][j] );
		}
		JFreeChart chart = ChartFactory.createTimeSeriesChart("Stock #" + (stockNr + 1) + " market", "Date", "Price", stockMarketGraphic[ stockNr ], true);
		ChartPanel panel1 = new ChartPanel( chart );
		stockMarketOneStockPanel[ stockNr ].add( panel1, BorderLayout.CENTER );
		stockMarketOneStockPanel[ stockNr ].add( panel3, BorderLayout.SOUTH );
	}
	
	private void reconstructMarketCustomerProfitPanel()
	{
		for (int j = 0; j < firmCount; j++ )
		{
			stockMarketCustomerProfitGraphic.addSeries( customerProfitSeries[j] );
		}
		JFreeChart chart = ChartFactory.createTimeSeriesChart("Customers profit", "Date", "Profit", stockMarketCustomerProfitGraphic, true);
		ChartPanel panel = new ChartPanel( chart );
		
		stockMarketCustomerProfitPanel = new JPanel();
		stockMarketCustomerProfitPanel.setLayout( new BorderLayout() );
		stockMarketCustomerProfitPanel.add( panel, BorderLayout.CENTER );
	}
	
	public void reinitMarketTabPanel()
	{
		stocksMarketTabPanel.removeAll();
		
		stockMarketOneStockPanel = new JPanel[stockCount];
		customerStockVisibilityCheckBoxes = new JCheckBox[stockCount][firmCount];
		stocksSeries = new BasicTimeSeries[stockCount];
		customerPreditedBuyPricesSeries = new BasicTimeSeries[stockCount][firmCount];
		customerPreditedSellPricesSeries = new BasicTimeSeries[stockCount][firmCount];
		customerPreditedBuy1PricesSeries = new BasicTimeSeries[stockCount][firmCount];
		customerPreditedSell1PricesSeries = new BasicTimeSeries[stockCount][firmCount];
		customerPreditedBuy2PricesSeries = new BasicTimeSeries[stockCount][firmCount];
		customerPreditedSell2PricesSeries = new BasicTimeSeries[stockCount][firmCount];
		stockMarketGraphic = new TimeSeriesCollection[stockCount];
		customerProfitSeries = new BasicTimeSeries[firmCount];
		stockMarketCustomerProfitGraphic = new TimeSeriesCollection();
		
		for (int i = 0; i < stockCount; i++)
		{
			//JPanel panel = new JPanel();
			//panel.setLayout( new BorderLayout() );
			
			stocksSeries[i] = new BasicTimeSeries("Stock price", Day.class);
			//stockMarketGraphic[i] = new TimeSeriesCollection();
			
			for (int j = 0; j < firmCount; j++ )
			{
				customerPreditedBuyPricesSeries[i][j] = new BasicTimeSeries("zBuy of user#" + j);
				customerPreditedSellPricesSeries[i][j] = new BasicTimeSeries("zSell of user#" + j);
				customerPreditedBuy1PricesSeries[i][j] = new BasicTimeSeries("zBuy1 of user#" + j);
				customerPreditedSell1PricesSeries[i][j] = new BasicTimeSeries("zSell1 of user#" + j);
				customerPreditedBuy2PricesSeries[i][j] = new BasicTimeSeries("zBuy2 of user#" + j);
				customerPreditedSell2PricesSeries[i][j] = new BasicTimeSeries("zSell2 of user#" + j);
				customerProfitSeries[j] = new BasicTimeSeries("Customer #" + j);
			}
			stockMarketOneStockPanel[i] = new JPanel();
			stockMarketOneStockPanel[i].setLayout( new BorderLayout() );
			reconstructMarketOneStockPanel( i );
			stocksMarketTabPanel.addTab( "Stock #" + (i + 1) + " market", stockMarketOneStockPanel[i] );
			
		}
		
		reconstructMarketCustomerProfitPanel();
		stocksMarketTabPanel.addTab( "Customer profit", stockMarketCustomerProfitPanel );
		
	}
	
	
	private void reinitCustomerData()
	{
		if ( customers == null )
			customers = new ArrayList();
		//Indre
		if ( kBuy == null )
			kBuy = new ArrayList();
		//for (int i = kBuy.size(); i < stockCount; i++)
		//	kBuy.add( new Double(DEFAULT_BUY_MODIFIER));
			
		if ( kBuy1 == null )
			kBuy1 = new ArrayList();
		//for (int i = kBuy1.size(); i < stockCount; i++)
		//	kBuy1.add( new Double(DEFAULT_BUY1_MODIFIER));
		
		if ( kSell1 == null )
			kSell1 = new ArrayList();
		//for (int i = kSell1.size(); i < stockCount; i++)
		//	kSell1.add( new Double(DEFAULT_SELL1_MODIFIER));
			
		if ( kBuy2 == null )
			kBuy2 = new ArrayList();
		//for (int j = kBuy2.size(); j < firmCount; j++)
		//	kBuy2.add( new Double(DEFAULT_BUY2_MODIFIER));
		
		if ( kSell2 == null )
			kSell2 = new ArrayList();
		//for (int i = kSell2.size(); i < stockCount; i++)
		//	kSell2.add( new Double(DEFAULT_SELL2_MODIFIER));
		
		if ( kSell == null )
			kSell = new ArrayList();
		//for (int i = kSell.size(); i < stockCount; i++)
		//	kSell.add( new Double(DEFAULT_SELL_MODIFIER));
			

		for (int j = 0; j < firmCount; j++)
		{
			if ( firmCount > customers.size() )
			{
				SRGMCustomer customer = new SRGMCustomer();

				//Indre
				kBuy.add( new Double(DEFAULT_BUY_MODIFIER));
				kBuy1.add( new Double(DEFAULT_BUY1_MODIFIER));
				kBuy2.add( new Double(DEFAULT_BUY2_MODIFIER));
				kSell2.add( new Double(DEFAULT_SELL2_MODIFIER));				
				kSell1.add( new Double(DEFAULT_SELL1_MODIFIER));
				kSell.add( new Double(DEFAULT_SELL_MODIFIER));

				//customer.prepareStorage( stockCount );
				customer.prepareStorage( firmCount );
				customer.iStrategy = 0;
				for ( int i = 0; i < stockCount; i++ )
				{
					double dZbuy = CUSTOMER_STARTUP_BUY_PRICE;
					double dZsell = CUSTOMER_STARTUP_SELL_PRICE;
					double dZbuy1 = CUSTOMER_STARTUP_BUY1_PRICE;
					double dZsell1 = CUSTOMER_STARTUP_SELL1_PRICE;
					double dZbuy2 = CUSTOMER_STARTUP_BUY2_PRICE;
					double dZsell2 = CUSTOMER_STARTUP_SELL2_PRICE;
					double dZpred = 0.0;
					double Delta = 0.0;
					int iN = 100;
					if (j==0) {iN = 1000;};
					double dProfit = -iN*STOCK_STARTUP_PRICE;
					double dProfAll = -iN*STOCK_STARTUP_PRICE;
					int iIterN = 0;
					int stockNo = i;
					customer.setInitialData(dZbuy, dZsell, dZbuy1, dZsell1, dZbuy2, dZsell2, dZpred, dProfit, dProfAll, iN, iIterN, stockNo, Delta);
				}
				
				customers.add( customer );
			}
			else
			{
				SRGMCustomer customer = (SRGMCustomer)customers.get(j);
				//customer.prepareStorage( stockCount );
				customer.prepareStorage( firmCount );
			
				for ( int i = 0; customer.iN.size() < stockCount; i++ )
				{
					double dZbuy = CUSTOMER_STARTUP_BUY_PRICE;
					double dZsell = CUSTOMER_STARTUP_SELL_PRICE;
					double dZbuy1 = CUSTOMER_STARTUP_BUY1_PRICE;
					double dZsell1 = CUSTOMER_STARTUP_SELL1_PRICE;
					double dZbuy2 = CUSTOMER_STARTUP_BUY2_PRICE;
					double dZsell2 = CUSTOMER_STARTUP_SELL2_PRICE;
					double dZpred = 0.0;
					double Delta = 0.0;
					int iN = 100;
					if (j==0) {iN = 1000;};
					double dProfit = -iN*STOCK_STARTUP_PRICE;
					double dProfAll = -iN*STOCK_STARTUP_PRICE;
					int iIterN = 0;
					int stockNo = i;
					customer.setInitialData(dZbuy, dZsell, dZbuy1, dZsell1, dZbuy2, dZsell2, dZpred, dProfit, dProfAll, iN, iIterN, stockNo, Delta);
				}
			}
					
		}
	}	


	private void reinitCustomerTabPanel()
	{
		customerTabPanel.removeAll();
		
		customerStrategyComboBox = new JComboBox[firmCount];
		
		customerHasStockField = new JTextField[stockCount][firmCount];
		kBuyField = new JTextField[firmCount];
		kSellField = new JTextField[firmCount];
		kBuy1Field = new JTextField[firmCount];
		kSell1Field = new JTextField[firmCount];
		kBuy2Field = new JTextField[firmCount];
		kSell2Field = new JTextField[firmCount];
	
		JPanel panel = new JPanel();
		panel.setLayout( new FlowLayout() );//GridLayout(1, firmCount) );
		
		for (int j = 0; j < firmCount; j++ )
		{
						
			JPanel customerPanel = new JPanel();
			customerPanel.setLayout( new GridLayout(stockCount + 7, 2) );
			
			customerPanel.add( new JLabel("Method:") );
			
			SRGMCustomer customer = (SRGMCustomer)customers.get( j );
			customerStrategyComboBox[j] = new JComboBox(STRATEGY_METHODS);
			customerStrategyComboBox[j].setSelectedIndex( customer.iStrategy );
			customerStrategyComboBox[j].addItemListener( customerStrategyChangeListener );
						
			customerPanel.add( customerStrategyComboBox[j] );
			
			for (int i = 0; i < stockCount; i++)
			{
				int hasStock = ((Integer)customer.iN.get(i)).intValue();
				customerHasStockField[i][j] = new JTextField("" + hasStock ,5 );
				customerHasStockField[i][j].addFocusListener( customerPreferencesFocusListener );
				customerPanel.add( new JLabel("Has stock #" + (i + 1) + ":") );
				customerPanel.add( customerHasStockField[i][j] );
			}
			

			//Indre

			customerPanel.add(new JLabel("Kbuy2:"));
			kBuy2Field[j] = new JTextField( kBuy2.get(j).toString());
			kBuy2Field[j].addFocusListener( customerPreferencesFocusListener );
			customerPanel.add(kBuy2Field[j]);

			customerPanel.add(new JLabel("Kbuy1:"));
			kBuy1Field[j] = new JTextField( kBuy1.get(j).toString() );
			kBuy1Field[j].addFocusListener( customerPreferencesFocusListener );
			customerPanel.add(kBuy1Field[j]);
				
			customerPanel.add(new JLabel("Kbuy:"));
			kBuyField[j] = new JTextField( kBuy.get(j).toString() );
			kBuyField[j].addFocusListener( customerPreferencesFocusListener );
			customerPanel.add(kBuyField[j]);
			
			customerPanel.add(new JLabel("Ksell:"));
			kSellField[j] = new JTextField( kSell.get(j).toString() );
			kSellField[j].addFocusListener( customerPreferencesFocusListener);
			customerPanel.add(kSellField[j]);
			
			customerPanel.add(new JLabel("Ksell1:"));
			kSell1Field[j] = new JTextField( kSell1.get(j).toString() );
			kSell1Field[j].addFocusListener( customerPreferencesFocusListener);
			customerPanel.add(kSell1Field[j]);
			
			customerPanel.add(new JLabel("Ksell2:"));
			kSell2Field[j] = new JTextField( kSell2.get(j).toString() );
			kSell2Field[j].addFocusListener( customerPreferencesFocusListener);
			customerPanel.add(kSell2Field[j]);
						
			
			JPanel fullCustomerPanel = new JPanel();
			fullCustomerPanel.setLayout( new BorderLayout() );
			if (j==0) {fullCustomerPanel.add( new JLabel("Customer #" + j), BorderLayout.NORTH );}
			else {fullCustomerPanel.add( new JLabel("Customer #" + j), BorderLayout.NORTH );};
			fullCustomerPanel.add( customerPanel, BorderLayout.CENTER );
			fullCustomerPanel.setBorder( BorderFactory.createEtchedBorder() );
			
			panel.add( fullCustomerPanel );
		}
		
		JPanel firmCountPanel = new JPanel();
		firmCountPanel.setLayout( new FlowLayout() );
		firmCountPanel.add( new JLabel("FirmCount:") );
		String[] firmCounts = new String[MAX_FIRM_COUNT - 1];
		for ( int j = 4; j < MAX_FIRM_COUNT; j++ )
			firmCounts[j - 1] = "" + j;
		firmCountComboBox = new JComboBox( firmCounts );
		firmCountComboBox.addItemListener( firmCountChangeListener );
		firmCountComboBox.setSelectedItem( "" + firmCount );
		firmCountPanel.add( firmCountComboBox );
		firmCountPanel.setBorder( BorderFactory.createEtchedBorder() );
		
		customerTabPanel.setLayout( new BorderLayout() );
		customerTabPanel.add( firmCountPanel, BorderLayout.NORTH );
		customerTabPanel.add( panel, BorderLayout.CENTER );
		
	}
	
	




	
	public StartupApplet()
	{
		System.out.println("tikrinam applet 1");
		startupTime = Calendar.getInstance();
		//System.out.println("tikrinam applet 2");
		
		yieldFocusListener = new YieldFocusListener(this);
		//System.out.println("tikrinam applet 3");
		stockPreferencesFocusListener = new StockPreferencesFocusListener(this);
		//System.out.println("tikrinam applet 4");
		stockCountChangeListener = new StockCountChangeListener(this);
		//System.out.println("tikrinam applet 5");
		firmCountChangeListener = new FirmCountChangeListener(this);
		//System.out.println("tikrinam applet 6");
		customerStrategyChangeListener = new CustomerStrategyChangeListener(this);
		//System.out.println("tikrinam applet 7");
		customerPreferencesFocusListener = new CustomerPreferencesFocusListener(this);
		//System.out.println("tikrinam applet 8");
		customerPricesVisibilityChangeListener = new CustomerPricesVisibilityChangeListener(this);
		//System.out.println("tikrinam applet 9");
		controlButtonsListener = new ControlButtonsListener(this);
		//System.out.println("tikrinam applet 10");
		
		reinitStockPreferencesData();
		//System.out.println("tikrinam applet 11");
		reinitStockPreferencesTabPanel();
		//System.out.println("tikrinam applet 12");
		
		reinitMarketData();
		//System.out.println("tikrinam applet 13");
		reinitMarketTabPanel();
		//System.out.println("tikrinam applet 14");
		
		reinitCustomerData();
		//System.out.println("tikrinam applet 15");
		reinitCustomerTabPanel();
		//System.out.println("tikrinam applet 16");
		
		constructAboutPanel();
		//System.out.println("tikrinam applet 17");
		
		mainPanel.addTab( "Stock Preferences", stocksPreferencesTabPanel );
		//System.out.println("tikrinam applet 18");
		mainPanel.addTab( "Market", stocksMarketTabPanel );
		//System.out.println("tikrinam applet 19");
		mainPanel.addTab( "Customer Preferences", customerTabPanel );
		//System.out.println("tikrinam applet 20");
		mainPanel.addTab( "About", aboutPanel );
		//System.out.println("tikrinam applet 21");
		
		getContentPane().add( mainPanel, BorderLayout.CENTER );
		//System.out.println("tikrinam applet 22");
		getContentPane().add( getControlButtonsPanel(), BorderLayout.SOUTH );
		System.out.println("tikrinam applet 23");
		//getContentPane().validate();
		//focusListener.setParent(this);
		//initComponents();
	}
	
	
	public synchronized void updateGUI()
	{
		System.out.println(" GUI update 1");
		readingData = true;
		//System.out.println(" GUI update 2");
		
		int iterNr = stocksPrices[0].getIterNr();
		//System.out.println(" GUI update 3");
		
		//calculate new day on graphic
		Calendar now = (Calendar)startupTime.clone();
		//System.out.println(" GUI update 4");
		//System.out.println("Calendar"+ now +" Iternr"+ iterNr +" DAY_OF_MONTH"+ Calendar.DAY_OF_MONTH );
		now.add(Calendar.DAY_OF_MONTH, iterNr);
		//System.out.println(" GUI update 5");
		Day day = new Day( now.getTime() );
		//System.out.println(" GUI update 6");
		
		//update market info
		for ( int i = 0; i < stockCount; i++ )
		{
			//System.out.println(i +" GUI update 7" );
			//update stock 'i' price
			stocksSeries[i].add( day, stocksPrices[i].getLastValue() );
			
			//update customers buy & sell prices
			for ( int j = 0; j < firmCount; j++ )
			{
				//System.out.println(i + " "+j+" GUI update 8" );
				SRGMCustomer customer = (SRGMCustomer)customers.get( j );
				//System.out.println(i + " "+ j+" GUI update 9" );
				customerPreditedBuyPricesSeries[i][j].add( day, customer.dZbuy[i].getLastValue() );
				//System.out.println(i + " " +j+" GUI update 10" );
				customerPreditedSellPricesSeries[i][j].add( day, customer.dZsell[i].getLastValue() );
				//System.out.println(i + " " +j+" GUI update 11" );
				customerPreditedBuy1PricesSeries[i][j].add( day, customer.dZbuy1[i].getLastValue() );
				//System.out.println(i + " " +j+" GUI update 12" );
				customerPreditedSell1PricesSeries[i][j].add( day, customer.dZsell1[i].getLastValue() );
				//System.out.println(i + " " +j+" GUI update 13" );
				customerPreditedBuy2PricesSeries[i][j].add( day, customer.dZbuy2[i].getLastValue() );
				//System.out.println(i + " " +j+" GUI update 14" );
				customerPreditedSell2PricesSeries[i][j].add( day, customer.dZsell2[i].getLastValue() );
				//System.out.println(i + " " +j+" GUI update 15" );
			}
		}
		
		//remove obsolete market history
		if ( iterNr - lastRemovedFromHistory > stockHistory )
		{
			//System.out.println(" GUI update 16");
			now = (Calendar)startupTime.clone();
			//System.out.println(" GUI update 17");
			now.add(Calendar.DAY_OF_MONTH, lastRemovedFromHistory);
			//System.out.println(" GUI update 18");
			Day removeDay = new Day( now.getTime() );
			//System.out.println(" GUI update 19");
			
			for ( int i = 0; i < stockCount; i++ )
			{
				//System.out.println(i + " GUI update 20");
				//remove stock 'i' price
				stocksSeries[i].delete( removeDay );
				//System.out.println(i + " GUI update 21");
				//remove customers buy & sell prices
				for ( int j = 0; j < firmCount; j++ )
				{
					//System.out.println(i + " " + j + " GUI update 22");
					customerPreditedBuyPricesSeries[i][j].delete( removeDay );
					customerPreditedSellPricesSeries[i][j].delete( removeDay );
					customerPreditedBuy1PricesSeries[i][j].delete( removeDay );
					customerPreditedSell1PricesSeries[i][j].delete( removeDay );
					customerPreditedBuy2PricesSeries[i][j].delete( removeDay );
					//System.out.println(" GUI update 23");
					customerPreditedSell2PricesSeries[i][j].delete( removeDay );
				}
			}
			
			lastRemovedFromHistory++;
		}
				
		
		//update customer profit
		for ( int j = 0; j < firmCount; j++ )
		{
			SRGMCustomer customer = (SRGMCustomer)customers.get( j );
			double profit = 0;
			for ( int i = 0; i < stockCount; i++ )
			{
				//sum all profit
				profit += customer.dProfAll[i];
								
				Integer hasStock = (Integer)customer.iN.get( i );
				
				//update 'has stock' field
				customerHasStockField[i][j].setText( hasStock.toString() );
			}
			
			//update customer 'j' profit graphic
			customerProfitSeries[j].add( day, profit );
		}
		
		readingData = false;
	}
	
	
	private double parseText(double initialValue, JTextField field)
	{
		try
		{
			return Double.parseDouble(field.getText());
		}
		catch (NumberFormatException nfe)
		{
			Toolkit.getDefaultToolkit().beep();
			field.setText("" + initialValue);
			return initialValue;
		}
	}

	private int parseText(int initialValue, JTextField field, int minValue, int maxValue)
	{
		try
		{
			int value = Integer.parseInt(field.getText());
			if ((value > maxValue) || (value < minValue))
			{
				throw new NumberFormatException();
			}
			return value;
		}
		catch (NumberFormatException nfe)
		{
			Toolkit.getDefaultToolkit().beep();
			field.setText("" + initialValue);
			return initialValue;
		}
	}
	

	private void constructAboutPanel()
	{
		JPanel tmpPanel = new JPanel();
		tmpPanel.setLayout(new BoxLayout(tmpPanel, BoxLayout.Y_AXIS));
		tmpPanel.add(new JLabel("Stock rate game model."));
		tmpPanel.add(new JLabel(" "));
		tmpPanel.add(new JLabel("Theory: prof. Jonas Mockus"));
		tmpPanel.add(new JLabel("Author: Vytautas Vaitkevi" + CH_RAIDE + "ius"));
		tmpPanel.add(new JLabel("Modified by[1]: Mindaugas Baltutis"));
		tmpPanel.add(new JLabel("Modified by[2]: Paul" + E_RAIDE + " " + ZH_DIDZIOJI_RAIDE + "ak" + SH_RAIDE + "auskien" + E_RAIDE));
		tmpPanel.add(new JLabel("Modified by[3]: Arunas Andriulaitis") );
		tmpPanel.add(new JLabel("Modified by[4]: Vytautas Le"+SH_RAIDE+CH_RAIDE+"iauskas") );
		tmpPanel.add(new JLabel("Modified by[5]: Donatas "+SH_DIDZIOJI_RAIDE+"idla") );
		tmpPanel.add(new JLabel("Modified by[6]: Evaldas Ramonas") );
		tmpPanel.add(new JLabel("Modified by[7]: Indre Zliobaite") );
		aboutPanel.add(tmpPanel, BorderLayout.CENTER);
		
	}

	// Apleto informacija
	public String getAppletInfo()
	{
		return "Stock Rate Game Model Test.\n Modified by Indre Zliobaite. \n Modified by Evaldas Ramonas.\n Modified by Arunas Andriulaitis. \n Modified by Paule Zaksauskiene.\n" +
			"Modified by Mindaugas Baltutis,\n Created by Vytautas Vaitkevicius.";
	}

	public static void main(String[] args)
	{
		//System.out.println("main 1");
		StartupApplet sa = new StartupApplet();
		//System.out.println("main 2");
		sa.setVisible( true );
		//System.out.println("main 3");
	}
	
	public void stockCountUpdated( )
	{
		int _stockCount = Integer.parseInt( (String)stockCountComboBox.getSelectedItem() );
		
		if ( stockCount == _stockCount )
			return;
		
		stockCount = _stockCount;
		
		//System.out.println("reinit stocks" + stockCount);
		
		reinitStockPreferencesData();
		reinitStockPreferencesTabPanel();
		
		reinitMarketData();
		reinitMarketTabPanel();
		
		reinitCustomerData();
		reinitCustomerTabPanel();
	}
	
	public void firmCountUpdated()
	{
		int _firmCount = Integer.parseInt( (String)firmCountComboBox.getSelectedItem() );
		
		if ( firmCount == _firmCount )
			return;
		
		firmCount = _firmCount;
		
		reinitCustomerData();
		reinitCustomerTabPanel();
		
		reinitMarketData();
		reinitMarketTabPanel();
	}
	
	public void customerStrategyUpdated( JComboBox combo )
	{
		for (int j = 0; j < firmCount; j++)
		{
			if ( combo == customerStrategyComboBox[j] )
			{
				int itemIndex = combo.getSelectedIndex();
				SRGMCustomer customer = (SRGMCustomer)customers.get( j );
				customer.iStrategy = itemIndex;
				customers.set(j, customer);
				break;
			}
		}
	}
	
	public void yieldFieldUpdated()
	{
		yieldOfCd = parseText(yieldOfCd, yieldOfCdField);
	}
	
	public void stockPreferencesUpdated(JTextField textField)
	{
		for (int i = 0; i < stockCount; i++)
		{
			if ( volatilityField[i] == textField )
			{
				double _volatility = ((Double)volatility.get( i )).doubleValue();
				_volatility = parseText(_volatility, textField);
				volatility.set(i, new Double(_volatility) );
				break;
			}
			else if ( dividendField[i] == textField )
			{
				double _dividend = ((Double)dividend.get( i )).doubleValue();
				_dividend = parseText(_dividend, textField);
				dividend.set(i, new Double(_dividend) );
				break;
			}

		}
	}


	public void customerPreferencesUpdated( JTextField textField )
	{
		int mSTOCK=MAX_CAN_HAVE_STOCK;
		for (int j = 0; j < firmCount; j++)
			{
					//System.out.println(" bega bite 1j" +j);
					//Indre 7				
					if ( kBuyField[j] == textField )
					{	
						//System.out.println(" bega bite 2j" +j);
						double _kBuy = ((Double)kBuy.get( j )).doubleValue();
						_kBuy = parseText(_kBuy, textField);
						kBuy.set(j, new Double(_kBuy) );
						//System.out.println(" bega bite 3j" +j+" kbuyj" + j + " " + kBuy.get(j));
						break;
					}
					else if ( kSellField[j] == textField )
					{
						double _kSell = ((Double)kSell.get( j )).doubleValue();
						_kSell = parseText(_kSell, textField);
						kSell.set(j, new Double(_kSell) );
						//System.out.println(" bega bite 3j" +j+" ksellj" + j + " " + kSell.get(j));
						break;
					}
					else if ( kBuy1Field[j] == textField )
						{
						double _kBuy1 = ((Double)kBuy1.get( j )).doubleValue();
						_kBuy1 = parseText(_kBuy1, textField);
						kBuy1.set(j, new Double(_kBuy1) );
						break;
					}
					else if ( kSell1Field[j] == textField )
					{
						double _kSell1 = ((Double)kSell1.get( j )).doubleValue();
						_kSell1 = parseText(_kSell1, textField);
						kSell1.set(j, new Double(_kSell1) );
						break;
					}
					else if ( kBuy2Field[j] == textField )
					{
						double _kBuy2 = ((Double)kBuy2.get( j )).doubleValue();
						_kBuy2 = parseText(_kBuy2, textField);
						kBuy2.set(j, new Double(_kBuy2) );
						break;
					}
					else if ( kSell2Field[j] == textField )
					{	
						double _kSell2 = ((Double)kSell2.get( j )).doubleValue();
						_kSell2 = parseText(_kSell2, textField);
						kSell2.set(j, new Double(_kSell2) );
						break;
					};

					for (int i = 0; i < stockCount; i++)
						{
							if (j==0) 					
							{mSTOCK=MAX_CAN_HAVE_STOCK*Soros_modifier;} 
							else 	
							{mSTOCK=MAX_CAN_HAVE_STOCK;};
							if ( customerHasStockField[i][j] == textField )
								{
									SRGMCustomer customer = (SRGMCustomer)customers.get( j );
									int hasStock = ((Integer)customer.iN.get(i)).intValue();
									int hadStock=hasStock;
									hasStock = parseText(hasStock, textField, 0, mSTOCK);
									System.out.println(hasStock + " bite 1");
									if (hadStock > hasStock) 
									{
										System.out.println(" eina bite 2");
										calculationThread.doShopping(this, i, true, false, j, hadStock-hasStock);
										System.out.println(" eina bite 3");
									}
									if (hadStock < hasStock) 
									{
										System.out.println(" eina bite 4");
										calculationThread.doShopping(this, i, false, true, j, hasStock-hadStock);
										System.out.println(" eina bite 5");
									}
									//customer.iN.set( i, new Integer(hasStock) );
									//customers.set( j, customer );
									break;
								}

						}
		
		
			
				
				
			}

				
	}	

			
	public void customerPricesVisibilityChanged( JCheckBox checkbox )
	{
		for (int i = 0; i < stockCount; i++)
			for (int j = 0; j < firmCount; j++)
				if ( customerStockVisibilityCheckBoxes[i][j] == checkbox )
				{
					ArrayList stockVisibility = (ArrayList)stocksVisibility.get( i );
					
					Boolean visibility = (Boolean)stockVisibility.get( j );
					visibility = new Boolean( !visibility.booleanValue() );
					
					//System.out.println( !visibility.booleanValue() );
					
					stockVisibility.set( j, visibility );
					stocksVisibility.set( i, stockVisibility );
					
					reconstructMarketOneStockPanel(i);
					stocksMarketTabPanel.validate();
					
					break;
				}
	}
			
	private void setEnabledActions( boolean enable )
	{
		//stock preferences panel
		stockCountComboBox.setEnabled( enable );
		yieldOfCdField.setEnabled( enable );
		
		for ( int i = 0; i < stockCount; i++ )
		{
			volatilityField[i].setEnabled( enable );
			dividendField[i].setEnabled( enable );
		}
		
		//market panel
		for ( int i = 0; i < stockCount; i++ )
			for ( int j = 0; j < firmCount; j++ )
				customerStockVisibilityCheckBoxes[i][j].setEnabled( enable );
				
		//customer/firm preferences
		firmCountComboBox.setEnabled( enable );
		for ( int j = 0; j < firmCount; j++ )
		{
			customerStrategyComboBox[j].setEnabled( enable );
			//customerProfitField[j].setEnabled( enable );
			//Indre 9
			kBuyField[j].setEnabled( enable );
			kSellField[j].setEnabled( enable );
			kBuy1Field[j].setEnabled( enable );
			kSell1Field[j].setEnabled( enable );
			kBuy2Field[j].setEnabled( enable );
			kSell2Field[j].setEnabled( enable );
		}
		
		for ( int i = 0; i < stockCount; i++ )
			for ( int j = 0; j < firmCount; j++ )
				customerHasStockField[i][j].setEnabled( enable );
		
		//control buttons
		traceButton.setEnabled( enable );
		runWithDelayButton.setEnabled( enable );
		runWithoutDelayButton.setEnabled( enable );
		stopButton.setEnabled( !enable );
		
	}
	
	private void startCalculation( int delay )
	{
		calculationThread = new StockTradeThread( this );
		calculationThread.setDelay( delay );
		calculationThread.start();
	}
	
	private void stopCalculation()
	{
		calculationThread.requestStop();
		//while ( calculationThread.running )
		{
			try { Thread.sleep( 300 ); }
			catch ( Exception ex ) { ex.printStackTrace(); }
		}
	}
	
	private void traceCalculation()
	{
		//System.out.println("traceCalculation 1");
		StockTradeThread.shop( this );
		//System.out.println("traceCalculation 2");
		updateGUI();
		//System.out.println("traceCalculation 3");
	}
	
	public void controlButtonsPressed( JButton button )
	{
		String action = button.getText();
		
		if ( action.equals( CONTROL_BUTTON_TRACE ) )
		{
			setEnabledActions( false );
			traceCalculation();
			setEnabledActions( true );
		}
		else if ( action.equals( CONTROL_BUTTON_RUN_WITH_DELAY ) )
		{
			setEnabledActions( false );
			startCalculation( CALCULATION_THREAD_DELAY );
		}
		else if ( action.equals( CONTROL_BUTTON_RUN ) )
		{
			setEnabledActions( false );
			startCalculation( CALCULATION_THREAD_WITHOUT_DELAY );
		}
		else if ( action.equals( CONTROL_BUTTON_STOP ) )
		{
			stopCalculation();
			setEnabledActions( true );
		}
		
		//System.out.println( button.getText() );
	}
	
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

class YieldFocusListener implements FocusListener
{
	StartupApplet applet;
	
	public YieldFocusListener(StartupApplet applet)
	{
		this.applet = applet;
	}
	
	public void focusLost(FocusEvent e)
	{
		JTextField textField = (JTextField)e.getComponent();
		applet.yieldFieldUpdated();
		System.out.println ("focus lost" + e.getComponent());
	}

	public void focusGained(FocusEvent e)
	{
		System.out.println ("focus gained" + e.getComponent());
	}
}

class StockPreferencesFocusListener implements FocusListener
{
	StartupApplet applet;
	
	public StockPreferencesFocusListener(StartupApplet applet)
	{
		this.applet = applet;
	}
	
	public void focusLost(FocusEvent e)
	{
		JTextField textField = (JTextField)e.getComponent();
		System.out.println("focus lost:"+textField.getText()+ "  per stock preferences");
		applet.stockPreferencesUpdated( textField );
	}

	public void focusGained(FocusEvent e) {}
}


class CustomerPreferencesFocusListener implements FocusListener
{
	StartupApplet applet;
	
	public CustomerPreferencesFocusListener(StartupApplet applet)
	{
		this.applet = applet;
	}
	
	public void focusLost(FocusEvent e)
	{
		JTextField textField = (JTextField)e.getComponent();
		System.out.println("focus lost:"+textField.getText()+  "  isejo per customer");
		applet.customerPreferencesUpdated( textField );
	}

	public void focusGained(FocusEvent e) {}
}


class StockCountChangeListener implements ItemListener
{
	StartupApplet applet;
	
	public StockCountChangeListener(StartupApplet applet)
	{
		this.applet = applet;
	}
	
	public void itemStateChanged(ItemEvent e)
	{
		applet.stockCountUpdated( );
		System.out.println("Irasytas naujas stock");
	}

	public void focusGained(FocusEvent e) {}
}


class FirmCountChangeListener implements ItemListener
{
	StartupApplet applet;
	
	public FirmCountChangeListener(StartupApplet applet)
	{
		this.applet = applet;
	}
	
	public void itemStateChanged(ItemEvent e)
	{
		applet.firmCountUpdated( );
	}

	public void focusGained(FocusEvent e) {}
}

class CustomerStrategyChangeListener implements ItemListener
{
	StartupApplet applet;
	
	public CustomerStrategyChangeListener(StartupApplet applet)
	{
		this.applet = applet;
	}
	
	public void itemStateChanged(ItemEvent e)
	{
		JComboBox combo = (JComboBox)e.getItemSelectable();
		applet.customerStrategyUpdated( combo );
	}

	public void focusGained(FocusEvent e) {}
}

class CustomerPricesVisibilityChangeListener implements ActionListener
{
	StartupApplet applet;
	
	public CustomerPricesVisibilityChangeListener(StartupApplet applet)
	{
		this.applet = applet;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		JCheckBox checkbox = (JCheckBox)e.getSource();
		applet.customerPricesVisibilityChanged( checkbox );
	}
}

class ControlButtonsListener implements ActionListener
{
	StartupApplet applet;
	
	public ControlButtonsListener(StartupApplet applet)
	{
		this.applet = applet;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton)e.getSource();
		applet.controlButtonsPressed( button );
	}
}
