package ckelling.baukasten;

import java.awt.*;
import java.io.*;

/**
 *	EditableMemory_ContextMenu.java
 *
 *	Wie der Name schon sagt:
 *	Kontextmenü, das bei Klick mit der rechten Maustaste
 *  auf ein EditableMemory erscheint
 *
 *	@author		Carsten Kelling
 *	@version	0.8.0, 29.06.97
 */
public class EditableMemory_ContextMenu extends Frame {
	private EditableMemory	parent;
	
	private int		startX;
	private int		startY;
	private File	programFile;

	void saveProgram_Action(Event event) {
		if (! parent.parent.fileDialogIsSupported())
			return;
			
		hide();
		//dispose();
		java.awt.FileDialog saveDialog = new java.awt.FileDialog(this, "Programm speichern",FileDialog.SAVE);
		saveDialog.setFile("*.prg");
		//Rectangle b = bounds();
		//Rectangle fdb = saveDialog.bounds();
		//saveDialog.move(Math.max(b.x + (b.width - fdb.width)/2, 0), Math.max(b.y + (b.height - fdb.height)/2, 0));
		saveDialog.show();
		String answer = saveDialog.getDirectory() + saveDialog.getFile();
		//debug Rechner.out("Sie haben als Datei gewählt: " + answer);
		if ((saveDialog.getDirectory() != null) && (saveDialog.getFile() != null))
		{
			String name = saveDialog.getFile();
			if (name.substring(name.length() - 4, name.length()).equals(".*.*"))
				name = name.substring(0, name.length() - 4);
			programFile = new java.io.File(saveDialog.getDirectory(), name);
			
		}
		//if (programFile.isFile())
		if (true)
		{
			FileOutputStream fos = null;
			try
			{
				fos = new FileOutputStream(programFile);
			}
			catch (java.io.IOException error)
			{
				Rechner.out("Fehler beim Anlegen der Datei!");
			}
			
			if (fos != null)
			{
				String prg = EditableMemory_ProgramChange.commentProgram(parent.getAllValues(), parent);
				byte[] contents = new byte[prg.length()];
				prg.getBytes(0, prg.length(), contents, 0);
				try
				{
					fos.write(contents);
				}
				catch (java.io.IOException error)
				{
					Rechner.out("Fehler beim Schreiben in die Datei!");
				}
				
				try
				{
					fos.close();
				}
				catch (java.io.IOException error)
				{
					Rechner.out("Fehler beim Schließen der Datei!");
				}
			}
		}
		saveDialog.hide();
		saveDialog.dispose();

   		dispose();
	}

	void loadProgram_Action(Event event) {
		if (! parent.parent.fileDialogIsSupported())
			return;
			
		hide();
		//dispose();
		java.awt.FileDialog loadDialog = new java.awt.FileDialog(this, "Programm laden",FileDialog.LOAD);
		loadDialog.setFile("*.prg");
		//Rectangle b = bounds();
		//Rectangle fdb = loadDialog.bounds();
		//loadDialog.move(Math.max(b.x + (b.width - fdb.width)/2, 0), Math.max(b.y + (b.height - fdb.height)/2, 0));
		loadDialog.show();
		String answer = loadDialog.getDirectory() + loadDialog.getFile();
		//debug Rechner.out("Sie haben als Datei gewählt: " + answer);
		if ((loadDialog.getDirectory() != null) && (loadDialog.getFile() != null))
		{
			programFile = new java.io.File(loadDialog.getDirectory(), loadDialog.getFile());
		}
		if (programFile.isFile())
		{
			FileInputStream fis = null;
			try
			{
				fis = new FileInputStream(programFile);
			}
			catch (java.io.FileNotFoundException error)
			{
				Rechner.out("Ich kann die Datei nicht finden!");
			}
			
			if (fis != null)
			{
				byte[] contents = new byte[65536];
				int programLength = 0;
				try
				{
					programLength = fis.available();
					fis.read(contents, 0, 65536);
				}
				catch (java.io.IOException error)
				{
					Rechner.out("Fehler beim Lesen aus der Datei!");
				}
				
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < programLength; i++)
					if (contents[i] != 13)  // CR unterdrücken, LF allein reicht
						sb.append((char) contents[i]);
						
				//debug Rechner.out("Anfang der Datei: " + sb.toString());
	    		parent.initRam(sb.toString());
	    		parent.paint(parent.parent.onScreenGC);
	
				try
				{
					fis.close();
				}
				catch (java.io.IOException error)
				{
					Rechner.out("Fehler beim Schließen der Datei!");
				}

				}
		}
		
		loadDialog.hide();
		loadDialog.dispose();

   		dispose();
	}

	void pasteProgram_Action(Event event) {
		//{{CONNECTION
		// Create and show the Frame
		EditableMemory_ProgramChange pc = new EditableMemory_ProgramChange(parent);
		Rectangle b = bounds();
		Rectangle pcb = pc.bounds();
		pc.move(Math.max(b.x + (b.width - pcb.width)/2, 0), Math.max(b.y + (b.height - pcb.height)/2, 0));
		hide();
		dispose();
		//}}
	}

	public boolean action(Event event, Object arg) {
		if (event.target instanceof MenuItem) {
			String label = (String) arg;
			if (event.target == saveProgram) {
				saveProgram_Action(event);
				return true;
			} else
			if (event.target == loadProgram) {
				loadProgram_Action(event);
				return true;
			} else
				if (event.target == pasteProgram) {
					pasteProgram_Action(event);
					return true;
				}
		}
		return super.action(event, arg);
	}


	public EditableMemory_ContextMenu(int x, int y, EditableMemory parent) {

		this.parent = parent;
		startX = x + parent.startX - 20;
		startY = y + parent.startY + 10;

		//{{INIT_CONTROLS
		setLayout(null);
		addNotify();
		resize(insets().left + insets().right + 159,insets().top + insets().bottom + 23);
		setBackground(new Color(12632256));
		setTitle("EditableMemory");
		//}}

		//{{INIT_MENUS
		menuBar = new java.awt.MenuBar();

		programMenu = new java.awt.Menu("Programm", true);
		pasteProgram = new java.awt.MenuItem("Eingeben...");
		programMenu.add(pasteProgram);
		loadProgram = new java.awt.MenuItem("Laden...");
		programMenu.add(loadProgram);
		saveProgram = new java.awt.MenuItem("Speichern unter...");
		programMenu.add(saveProgram);
		menuBar.add(programMenu);
		setMenuBar(menuBar);
		//$$ menuBar.move(12,0);
		//}}

		if (! parent.parent.fileDialogIsSupported())
		{
			loadProgram.disable();
			saveProgram.disable();
		}

		show();
	}

	public EditableMemory_ContextMenu(String title, int x, int y, EditableMemory parent) {
	    this(x, y, parent);
	    setTitle(title);
	}

    public synchronized void show() {
		move (startX, startY);
    	super.show();
    }

	public boolean handleEvent(Event event) {
    	if (event.id == Event.WINDOW_DESTROY) {
            hide();         // hide the Frame
            dispose();
            return true;
    	}
		return super.handleEvent(event);
	}

	//{{DECLARE_CONTROLS
	//}}

	//{{DECLARE_MENUS
	java.awt.MenuBar menuBar;
	java.awt.Menu programMenu;
	java.awt.MenuItem pasteProgram;
	java.awt.MenuItem loadProgram;
	java.awt.MenuItem saveProgram;
	//}}
}
