Decorator client


import java.awt.*;
import java.awt.event.*;

public class Client extends Frame implements ActionListener{

  private MenuBar mbar;
  private Menu action;
  private TextField filename, record;
  private MenuItem serve,open,close,read,isEof;
  private Component server;
  private Resources manager = new Resources();
  private Label l1 = new Label("Filename:");
  private Label l2 = new Label("Action result:");

  public Client() {
    super("Decorator client");
    mbar = new MenuBar();
    setMenuBar(mbar);
    setLayout(new FlowLayout());
    enableEvents(WindowEvent.WINDOW_CLOSING);
    action = new Menu("Action");
    mbar.add(action);
    add(l1);
    filename = new TextField("No file name set",20);
    add(filename);
    add(l2);
    record = new TextField("No server set",20);
    add(record);
    serve = new MenuItem("Get Server");
    serve.addActionListener(this);
    action.add(serve);
    open = new MenuItem("Open File");
    open.addActionListener(this);
    open.setEnabled(false);
    action.add(open);
    close = new MenuItem("Close File");
    close.addActionListener(this);
    close.setEnabled(false);
    action.add(close);
    read = new MenuItem("Read record");
    read.addActionListener(this);
    read.setEnabled(false);
    action.add(read);
    isEof = new MenuItem("Is End of File ?");
    isEof.addActionListener(this);
    isEof.setEnabled(false);
    action.add(isEof);
    setSize(200,200);
    setVisible(true);
  } //end Client()

  public void actionPerformed(ActionEvent ae){
    MenuItem source = (MenuItem)ae.getSource();
    if (source == serve){
      server = manager.getServer();
      record.setText("Server set");
      open.setEnabled(true);
    }  
    else if (source == open)
      if (server.open(filename.getText())){
        close.setEnabled(true);
	read.setEnabled(true);
	isEof.setEnabled(true);
	open.setEnabled(false);
	filename.setEditable(false);
        record.setText("File Opened");
      }
      else
        record.setText("Open Failed");
    else if (source == close){
      close.setEnabled(false);
      read.setEnabled(false);
      isEof.setEnabled(false);
      open.setEnabled(true);
      server.close(filename.getText());
      filename.setEditable(true);
      record.setText("File Closed");
    }
    else if (source == read)
      record.setText(server.read(filename.getText()));
    else 
      if (server.endOfFile(filename.getText()))
        record.setText("End of File");
      else
        record.setText("Not end of file");
  } //end actionPerformed()

  public void processWindowEvent(WindowEvent we){
    if (we.getID() == WindowEvent.WINDOW_CLOSING){
      dispose();
      System.exit(0);
    } //end if
    super.processWindowEvent(we);
  } //end processWindowEvent()

  public static void main(String args[]){
    Frame f = new Client();
  } //end main
  
} //end class Client