
//--------------------------------------------------------------------
// Class Display extends Frame implements Runnable
//
// Purpose  : text editor for file
//--------------------------------------------------------------------

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class Display extends Frame implements WindowListener, ActionListener 
{

	protected TextArea theDisplay;
	protected MenuBar theMenuBar;
	protected Menu theFileMenu;
	protected MenuItem theSaveMenuItem;
	protected MenuItem theCloseMenuItem;

	protected static ClientCacheMgr theCacheMgr;
	protected PrintStream theOutput;
	protected DataInputStream theInput;
	protected int theMFTIndex;

	protected String theDataPackage;


//--------------------------------------------------------------------
// Contructor for Display (1)
//
// Input	: title, dataPackage
// Output   : None
// Purpose  : display the editor windows with dataPackage content
//--------------------------------------------------------------------

	Display(String title, String dataPackage)
	{
		super (title);
		addWindowListener(this);
		theDataPackage = dataPackage;
		theDisplay = new TextArea ();
		theMenuBar = new MenuBar();
		theFileMenu = new Menu("File");
		setMenuBar(theMenuBar);
		theMenuBar.add(theFileMenu);
		theSaveMenuItem = new MenuItem("Save");
		theSaveMenuItem.addActionListener(this);
		theCloseMenuItem = new MenuItem("Close");
		theCloseMenuItem.addActionListener(this);
		theFileMenu.add(theSaveMenuItem);
		theFileMenu.add(theCloseMenuItem);
		add ("Center", theDisplay ); 

		if (title.indexOf("read") != -1)
		{
			theDisplay.setEditable(false);
			theSaveMenuItem.setEnabled(false);
		}

		theDisplay.append(dataPackage);
		theDisplay.setFont(new Font("Monospaced", Font.PLAIN, 17));

		setSize(640, 480);		
		show();
		toFront();
		
	}

	
//--------------------------------------------------------------------
// Contructor for Display (2)
//
// Input	: title, dataPackage
//            DataInputStream input, PrintStream output
// Output   : None
// Purpose  : display the editor windows with dataPackage content
//            - link Stream with server (for closing file)
//--------------------------------------------------------------------

	Display(String title, String dataPackage,
		DataInputStream input, PrintStream output)
	{
		this (title, dataPackage);
		this.theInput = input;
		this.theOutput = output;
	}

	
//--------------------------------------------------------------------
// Contructor for Display (3)
//
// Input	: title, dataPackage
//            DataInputStream input, PrintStream output
//            theCacheMgr
// Output   : None
// Purpose  : display the editor windows with dataPackage content
//            - link Stream with server (for closing file)
//            - update the cache through cache manager (for saving file)
//--------------------------------------------------------------------

	Display(String title, int MFTIndex, String dataPackage,
		DataInputStream input, PrintStream output, ClientCacheMgr cacheMgr)
	{
		this (title, dataPackage, input, output);
		this.theMFTIndex = MFTIndex;
		theCacheMgr = cacheMgr;
	}



//--------------------------------------------------------------------
// Method for AWTEvent (override method for standard handleEvent
//
// Input	: Event
// Output   : None
// Purpose  : handle event on the windows editor such as close windows,
//            save file.
//--------------------------------------------------------------------
 
	public void actionPerformed(ActionEvent event)
	{
		Object evtSource = event.getSource();

		// event when -Close- from fileMenu is selected
		if (evtSource == theCloseMenuItem) 
		{
			close();
			dispose();
		}

		// event when -Save- from fileMenu is selected
		if (evtSource == theSaveMenuItem) 
		{
			save();
		}
	}

	// standard window event
	public void windowClosed(WindowEvent event) { }
	public void windowDeiconified(WindowEvent event) { }
	public void windowIconified(WindowEvent event) { }
	public void windowActivated(WindowEvent event) { }
	public void windowDeactivated(WindowEvent event) { }
	public void windowOpened(WindowEvent event) { }

	public void windowClosing(WindowEvent event)
	{
		// event when -Close- from windowDialog is selected
		close();
		dispose();
	}


//--------------------------------------------------------------------
// Method for close
//
// Input	: None
// Output   : None
// Purpose  : handle event when window is closed (close file).  Also
//            inform the server that file is no longer in use by this
//            client
//--------------------------------------------------------------------

	public void close()
	{
		StringTokenizer files = new StringTokenizer(getTitle());

		String fileName = files.nextToken();
		try 
		{
			fileName = files.nextToken();
			theOutput.println("close " + fileName);
			String confirm = theInput.readLine();

			if (confirm.indexOf("end of transmission") != -1)
				System.out.println("\nserver: close " + fileName + " successfully.");

			System.out.print(theInput.readLine() + ">");
		}
		catch (IOException e) { }
	}

	
//--------------------------------------------------------------------
// Method for save
//
// Input	: None
// Output   : None
// Purpose  : handle event when file is saved (save file).  Also
//            inform the server that file is modified by this
//            client.  Cache on both sides get updated.
//--------------------------------------------------------------------

	public void save()
	{
		StringTokenizer files = new StringTokenizer(getTitle());

		String fileName = files.nextToken();
		String dataPackage = theDisplay.getText();
		int fileSize = dataPackage.length();
		try 
		{
			fileName = files.nextToken();
			theOutput.println("save " + fileName);
			theOutput.println(Integer.toString(theMFTIndex));

			// enter catWrite
			theOutput.println(Integer.toString(fileSize));

			String confirm = theInput.readLine();
			if (confirm.indexOf("Exception:") != -1)
			{
				System.out.println("\n" + confirm);
				confirm = theInput.readLine();
			}
			else
			{
				theCacheMgr.writeFile (theMFTIndex, dataPackage,
									theInput, theOutput);

				confirm = theInput.readLine();
				if (confirm.indexOf("Exception:") != -1)
				{
					System.out.println("\n" + confirm);
					confirm = theInput.readLine();
				} 
				else
				{
					System.out.println("\nserver: save " + fileName + " successfully.");
				}
			}
			System.out.print(theInput.readLine() + ">");
		}
		catch (IOException e) { }
	}
}

//--------------------------------------------------------------------
// End Class Display
//--------------------------------------------------------------------
