package ckelling.baukasten;

import java.awt.*;
import java.net.URL;

class Komponenten_RAM_Control extends Frame
{
	private Panel	otherPanel;
	private Panel	buttonPanel;
	private Menu	menu;

	private CheckboxGroup	actionChoice;
	public	Checkbox		actionRead;
	public  Checkbox		actionWrite;

	private symantec.itools.awt.ImageButton directionPlay;
	private symantec.itools.awt.ImageButton directionReverse;

	private Komponenten_RAM parent;


	public Komponenten_RAM_Control (Komponenten_RAM parent)
	{
		this.parent = parent;

		setFont(parent.SMALLFONT);
		setBackground(parent.BACKGROUND);

		setLayout(new BorderLayout());

		otherPanel = new Panel();
		otherPanel.setLayout(new GridLayout(0, 3, 0, 10));
		add("Center", otherPanel);

		buttonPanel = new Panel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,20,0));
		add("North", buttonPanel);

		//buttonPanel.add (new Button("Einmal"));
		//buttonPanel.add (new Button("Permanent"));
		//buttonPanel.add (new Button("Aus"));

		//buttonPanel.add (new Button("<"));
		//buttonPanel.add (new Button(">"));

		directionReverse = new symantec.itools.awt.ImageButton();
		directionReverse.reshape(38,5,30,32);
		buttonPanel.add(directionReverse);
		try {
                        directionReverse.setImageURL(new URL(parent.getDocumentBase(), "Bilder/pfeile/links.gif"));
		} catch (java.net.MalformedURLException error) {
		}
		directionReverse.setCenterMode(false);
		directionPlay = new symantec.itools.awt.ImageButton();
		directionPlay.reshape(73,5,30,32);
		buttonPanel.add(directionPlay);
		try {
                        directionPlay.setImageURL(new URL(parent.getDocumentBase(), "Bilder/pfeile/rechts.gif"));
		} catch (java.net.MalformedURLException error) {
		}
		directionPlay.setCenterMode(false);

		buttonPanel.add (new Button("Reset auslösen"));
		//buttonPanel.add (new Button("Simulation neu starten"));
		if (parent.manualRedraw == true)
			buttonPanel.add (new Button("Neuzeichnen"));

		Checkbox cb = new Checkbox("Werte zufällig");
		cb.setState(parent.useRandomNumbers);
		otherPanel.add (cb);

		actionChoice = new CheckboxGroup();
		actionRead	= new Checkbox("Lesen",     actionChoice, (parent.c_state == parent.READ) );
		actionWrite = new Checkbox("Schreiben", actionChoice, (parent.c_state == parent.WRITE));
		if (parent.c_state == parent.READ)
			actionRead.setState(true);
		else if (parent.c_state == parent.WRITE)
			actionWrite.setState(true);
		else
		{
			actionRead.setState(false);
			actionWrite.setState(false);
		}

		otherPanel.add(actionRead);
		otherPanel.add(actionWrite);

		MenuBar menubar = new MenuBar();
		menu = new Menu("Einstellungen");
		CheckboxMenuItem cbmi;
		//cbmi = new CheckboxMenuItem("Zweifarbig");
		//cbmi.setState(parent.blackAndWhite);
		//menu.add(cbmi);
		cbmi = new CheckboxMenuItem("Erweitern auf vier/fünf Stellen");
		cbmi.setState(parent.expandNumbers);
		menu.add(cbmi);
		cbmi = new CheckboxMenuItem("Dezimalzahlen");
		if (parent.NUMBERBASE == 10)
			cbmi.setState(true);
		else
			cbmi.setState(false);
		menu.add(cbmi);
		//cbmi = new CheckboxMenuItem("Hilfetext im Blocksatz");
		//if (parent.helpText.isBlockMode())
		//	cbmi.setState(true);
		//else
		//	cbmi.setState(false);
		//menu.add(cbmi);
		menu.add(new MenuItem("-"));
		menu.add(new MenuItem("Standardwerte benutzen"));
		menubar.add(menu);
		setMenuBar(menubar);

		hide();
	} /* end Komponenten_RAM_Control */


	public Insets insets()	//	leave some space around the edges
	{
		Insets i = super.insets();
		return new Insets (i.top + 10, i.left + 10, i.bottom + 10, i.right + 10);
		//return new Insets (60, 20, 30, 20);
		//return new Insets (42, 5, 5, 5);
	} /* end Insets */


	public boolean handleEvent(Event e)
	{
		if (e.id == Event.ACTION_EVENT)
			mouseDown(e);
		else if (e.id == Event.WINDOW_DESTROY)
			return true;

	return super.handleEvent(e);

	} /* end handleEvent */


	private void mouseDown (Event e)
	{
		if (e.target instanceof symantec.itools.awt.ImageButton)
		{
			if (e.target.equals(directionReverse))  // "<"
			{
				parent.demonstrateBack();
			}
			else if (e.target.equals(directionPlay))  // ">"
			{
				if (parent.firstTime == true)
				{
					parent.firstTime = false;
					parent.demonstrate(false);
					parent.demonstrate(true);
				}
				else
				{
					parent.demonstrate();
				}
				//parent.updateTextFields();

				parent.paintActivated(parent.onScreenGC);
			}
		}
		else if (e.target instanceof Button)
		{
			String label = ((Button)(e.target)).getLabel ();

			if (label.equals("Einmal"))
			{
				if (parent.scrollThread != (TimerThread) null)
					parent.scrollThread.stop();
				parent.scrollAll();
			}
			else if (label.equals("Permanent"))
			{
				if ((parent.scrollThread == (TimerThread) null) || (parent.scrollThread.isAlive() == false))
				{
					parent.scrollThread = new TimerThread(100, "scrolltimer", parent);
					parent.scrollThread.start();
				}
			}
			else if (label.equals("Aus"))
			{
				if (parent.scrollThread != (TimerThread) null)
					parent.scrollThread.stop();
			}
			else if (label.equals("Reset auslösen"))
			{
				if (actionRead.getState() == true)
					parent.c_state = parent.READ;
				else if (actionWrite.getState() == true)
					parent.c_state = parent.WRITE;
				else
					parent.c_state = parent.n_state;
				actionRead.setCheckboxGroup(null);
				actionWrite.setCheckboxGroup(null);
				actionRead.setState(false);
				actionWrite.setState(false);
				actionRead.setCheckboxGroup(actionChoice);
				actionWrite.setCheckboxGroup(actionChoice);

				parent.setComponentsEditableOrNot();

				// bloeder Hack
				parent.downdateAll();

				parent.demonstrationReady = true;
				parent.n_state = parent.RESET;
				parent.demonstrate(false);
				parent.demonstrate(true);
			}
			else if (label.equals("Simulation neu starten"))
			{
				parent.stopSimulation();
			}
			else if (label.equals("Neuzeichnen"))
			{
				parent.paint(parent.onScreenGC);
			}
		}
		else if (e.target instanceof Checkbox)
		{
			Checkbox cb = (Checkbox)(e.target);
			String label = cb.getLabel();

			if (label.equals("DECODE-Phase zeigen"))
				parent.showDecodeCycle = cb.getState();
			else if (label.equals("Werte zufällig"))
			{
				boolean state = cb.getState();
				parent.useRandomNumbers = state;
				parent.setComponentsEditableOrNot();
			}
			else if ( (label.equals("Lesen")) || (label.equals("Schreiben")) )
			{
				if (actionRead.getState() == true)
					parent.n_state = parent.READ;
				else
					parent.n_state = parent.WRITE;
			}

			if (parent.getDemonstrationStep() == 1)
			{
				//debug System.out.println("-- n_state geändert, singleStep() aufgerufen");
				parent.setComputer();
				// letztes singleStep() rueckgaengig machen

				parent.firstTime = false;

				parent.demonstrationReady = true;
				parent.demonstrate(false);
				// bewirkt nur (demonstrationReady = false) und
				// (demonstrationStep = 1)
			}
		}
		else if (e.target instanceof CheckboxMenuItem)
		{
			CheckboxMenuItem cbmi = (CheckboxMenuItem)(e.target);
			String label = cbmi.getLabel();

			if (label.equals("Zweifarbig"))
			{
				parent.blackAndWhite = cbmi.getState();
				if (parent.blackAndWhite == false)
				{
					if (parent.blinkThread != (TimerThread) null)
						parent.blinkThread.stop();
				}
				else
				{
					if ((parent.blinkThread == (TimerThread) null) || (parent.blinkThread.isAlive() == false))
					{
						parent.blinkThread = new TimerThread(500, "blinktimer", parent);
						parent.blinkThread.start();
					}
				}

				parent.chooseColors();
				parent.invalidateAllImageCaches();
				java.lang.System.gc(); // den Garbage Collector aufrufen
				parent.paint(parent.onScreenGC);
			}
			else if (label.equals("Erweitern auf vier/fünf Stellen"))
			{
				parent.expandNumbers = cbmi.getState();
				parent.updateAll();
				parent.paint(parent.onScreenGC);
			}
			else if (label.equals("Dezimalzahlen"))
			{
				if (cbmi.getState() == true)
					parent.NUMBERBASE = 10;
				else
					parent.NUMBERBASE = 16;
				parent.updateAll();
				parent.paint(parent.onScreenGC);
			}
			else if (label.equals("Hilfetext im Blocksatz"))
			{
				parent.helpText.setBlockMode(cbmi.getState());
			}
		}
		else if (e.target instanceof MenuItem)
		{
			MenuItem mi = (MenuItem)(e.target);
			String label = mi.getLabel();

			if (label.equals("Standardwerte benutzen"))
			{
				parent.blackAndWhite = false;
				if (parent.blinkThread != (TimerThread) null)
					parent.blinkThread.stop();
				parent.chooseColors();
				parent.expandNumbers = true;
				parent.NUMBERBASE = 16;
				parent.helpText.setBlockMode(true);
				CheckboxMenuItem cbmi;
				//cbmi = (CheckboxMenuItem) menu.getItem(0);  // zweifarbig!
				//cbmi.setState(parent.blackAndWhite);
				cbmi = (CheckboxMenuItem) menu.getItem(0);  // zweifarbig!
				cbmi.setState(parent.expandNumbers);
				cbmi = (CheckboxMenuItem) menu.getItem(1);  // zweifarbig!
				if (parent.NUMBERBASE == 10)
					cbmi.setState(true);
				else
					cbmi.setState(false);
				cbmi = (CheckboxMenuItem) menu.getItem(3);
				cbmi.setState(parent.helpText.isBlockMode());

				parent.updateAll();
				parent.paint(parent.onScreenGC);
			}
		}

	} /* end mouseDown */

} /* end Komponenten_RAM_Control */

