import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;

/**
* File handler handles all the request from the file menu
* extends Handler object so it can be loaded in JPE object
*/
public class EditHandler extends Handler {


	// Strings for the pulldown
	String CutString="Cut";
	String CopyString="Copy";
	String PasteString="Paste";
	String SelectAllString="Select All";
	String FindString="Find";
	String FindAgainString="Find Again";
	String GotoString="Goto line";
	String GotoErrorString="Goto Error";
	String NextMethodString="Next Method";
	String PrevMethodString="Previous Method";

	// copy buffer for now is kept in a simple string
	String copyBuffer;
	String searchkey="";

	/**
	* create the edit handler
	*/
	public EditHandler(JPE jpe) {
		super(jpe);
		name="Edit";
	}


	/**
	*  called by superclass to create its menu
	*/
	public Menu createMenu() {
		Menu m = new Menu(name);
		m.add(new MenuItem(CutString,new MenuShortcut('X')));
		m.add(new MenuItem(CopyString,new MenuShortcut('C')));
		m.add(new MenuItem(PasteString,new MenuShortcut('V')));

		Menu m3=new Menu("Find");
		m3.add(new MenuItem(FindString,new MenuShortcut('F')));
		m3.add(new MenuItem(FindAgainString,new MenuShortcut('F',true)));
		m3.addSeparator();
		m3.add(new MenuItem(NextMethodString, new MenuShortcut('.')));
		m3.add(new MenuItem(PrevMethodString, new MenuShortcut(',')));
		m3.addActionListener(this);
		m.add(m3);		

		Menu m2=new Menu("Goto");
		m2.add(new MenuItem(GotoString,new MenuShortcut('G',true)));
		m2.add(new MenuItem(GotoErrorString,new MenuShortcut('G')));
		m2.addActionListener(this);
		m.add(m2);		
			
		m.addSeparator();
		m.add(SelectAllString);    
		m.addActionListener(this);        
		return(m);
	}


	/**
	*  AWT callback (implemented in Handler) for menu events
	*/
    public void actionPerformed(ActionEvent evt)	{
		String cmd = evt.getActionCommand();
		if (cmd.equals(CopyString)) {
			doCopy();
		} else if (cmd.equals(CutString)) {
			doCut();
		} else if (cmd.equals(PasteString)) {
			doPaste();
		} else if (cmd.equals(SelectAllString)) {
			doSelectAll();
		} else if (cmd.equals(FindString)) {
			doFind();
		} else if (cmd.equals(FindAgainString)) {
			doFindAgain();
		} else if (cmd.equals(NextMethodString)) {
			doNextPrevMethod(true);
		} else if (cmd.equals(PrevMethodString)) {
			doNextPrevMethod(false);
		} else if (cmd.equals(GotoString)) {
			doGoto();
		} else if (cmd.equals(GotoErrorString)) {
			doGotoError();
		}
	}


	/**
	* do find uses the askInterface
	*/
	public void doFind() {
		jpe.setAskMode(this,"find","Find = ");
	}


	/**
	* handle find
	*/
	public void handleFind(String key) {
		this.searchkey=key;
		jpe.say("Searching for = '"+key+"'");
		TextHandler txth=jpe.getTextHandler();
		if (txth!=null) {
			if (txth.gotoLineWith(key)) {
					jpe.say("Found = '"+key+"'");
				} else {
					jpe.say("'"+key+"' not Found");
			}
		}	
	}

	/**
	* do find again
	*/
	public void doFindAgain() {
		jpe.say("Searching for = '"+searchkey+"'");
		TextHandler txth=jpe.getTextHandler();
		if (txth!=null) {
			if (txth.gotoLineWith(searchkey,txth.getCursorPos()+1)) {
					jpe.say("Found = '"+searchkey+"'");
				} else {
					jpe.say("no more '"+searchkey+"' not Found");
			}
		}	
	}

	/**
	* goto the first error
	*/
	public void doGotoError() {
		jpe.say("Jumping to first Error");
		TextHandler txth=jpe.getTextHandler();
		if (txth!=null) {
			OpenFile file=txth.getEditFile();
			int linenumber=file.getErrorPos();
			if (linenumber!=-1) {
				txth.gotoLine(linenumber);
				jpe.say("Jumped first error at line "+linenumber+" (ctrl-h to toggle log)");
				return;
			}
		}
		jpe.say("No compile errors detected");
	}


	/**
	* do goto line uses the askInterface
	*/
	public void doGoto() {
		jpe.setAskMode(this,"goto","Goto line number = ");
	}

	/**
	* handle find
	*/
	public void handleGoto(String key) {
		jpe.say("Jumping to line "+key);
		try {
			TextHandler txth=jpe.getTextHandler();
			int linenumber=Integer.parseInt(key);
			if (txth!=null) {
				int realline=txth.gotoLine(linenumber);
				//System.out.println("W="+linenumber+" R="+realline);
				if (realline==linenumber) {
					jpe.say("Jumped to line "+key);
				} else {
					jpe.say("Not that many lines, jumped to "+realline);
				}
			}
		} catch(Exception e) {
			jpe.say("Error in jumping to line "+key);
		}
	}

	/**
	* Handler the ask callback and finish the command started that
	* started with the setAskMode.
	*/
	public void askCallback(String command,String result) {
		if (command.equals("find")) {
			handleFind(result);
		} else if (command.equals("goto")) {
			handleGoto(result);
		} 
	}

	/**
	* copy the selected text to the buffer
	*/
	public void doCopy() {
		TextHandler txth=jpe.getTextHandler();
		copyBuffer=txth.getSelectedText();
		jpe.say("Text Copy");
	}

	/**
	* select the whole text
	*/
	public void doSelectAll() {
		TextHandler txth=jpe.getTextHandler();
		jpe.say("Select All");
		txth.selectAll();
	}

	/**
	* Cut the selected text and place it in the copy buffer
	*/
	public void doCut() {
		TextHandler txth=jpe.getTextHandler();
		copyBuffer=txth.cutSelectedText();
		jpe.say("Text Cut");
	}

	/**
	* insert the current buffer into the textareas at the cursor pos
	*/
	public void doPaste() {
		TextHandler txth=jpe.getTextHandler();
		if (copyBuffer!=null) txth.insertText(copyBuffer);
		jpe.say("Text Paste");
	}

	/**
	* Find next or previous method in current file.
	*/
	public void doNextPrevMethod(boolean next) {
		TextHandler txth=jpe.getTextHandler();
		if (txth!=null) {
			if (next && !txth.gotoNextMethod()) {
				jpe.say("No next method");
			} else if ( !next && !txth.gotoPrevMethod()) {
				jpe.say("No previous method");
			}
		}	
	}


}
