/*
 * %W% %E% Sun Microsystems, Inc.
 *
 * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import PrintRemote;

public class JiniPrintClient extends Applet implements ActionListener {

    final static Color BLUE = new Color(104, 111, 130);
    static String printerArray[] = {"brita", "dripgrnd"};

    Label	entryLabel = 
	new Label("Enter full pathname of file to print:");
    Panel	panel = new Panel();
    Label	label = new Label("Label");
    TextArea	status = new TextArea();
    TextField	textField;
    Panel	buttonPanel = new Panel();
    Button	printButton = new Button("Print");
    Button	checkStatusButton = new Button("Check Status");
    Choice	printerChoice = new Choice();

    PrintRemote		service = null;
    
    public JiniPrintClient() {		
    }

    public JiniPrintClient(String printerName, String fileName) {	
	System.out.println(printToJiniService(printerName, fileName));
    }

    public PrintRemote getPrintService() {
	Socket			socket;
	InputStream		inputStream;
	ObjectInputStream	objInStream;
	try {

	    // Open the socket for the ObjectInputStream
	    socket = new Socket("talon", PrintRemote.SOCKET);
	    inputStream = socket.getInputStream();
	    objInStream = new ObjectInputStream(inputStream);

	    // Read the PrintRemote object from the server
	    service = (PrintRemote) objInStream.readObject();

	    // Close the socket
	    socket.close();

	} catch (Exception e) {
	    e.printStackTrace();
	}

	return(service);
    }

    public String printToJiniService(String printerName, String fileName) {
	String retStr = null;

	if (service == null) {
	    // Get the PrintRemote object from the server
	    service = getPrintService();
	}

	try {
	    retStr = service.print(printerName, fileName);
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return(retStr);
    }

    public String checkQueueJiniService(String printerName) {
	String retStr = null;

	if (service == null) {
	    // Get the PrintRemote object from the server
	    service = getPrintService();
	}

	try {
	    retStr = service.checkQueue(printerName);
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return(retStr);
    }

    public void start() {
	setLayout(new BorderLayout());

	// Add a Container
	panel.setLayout(new BorderLayout());
	entryLabel.setForeground(Color.white);
	entryLabel.setBackground(BLUE);
	panel.add("North", entryLabel);	

	// Add a Component
	textField = new TextField();
	textField.setText("/usr/local/java/pjava1.1/pjee11-solaris/README");
	entryLabel.setForeground(Color.white);
	textField.setBackground(Color.white);
	panel.add("Center", textField);

	printButton.addActionListener(this);
	checkStatusButton.addActionListener(this);

	for (int index=0; index<printerArray.length; index++) {
	    printerChoice.add(printerArray[index]);
	}	

	buttonPanel.setLayout(new FlowLayout());
	buttonPanel.add(printerChoice);
	buttonPanel.add(printButton);
	buttonPanel.add(checkStatusButton);

	panel.add("South", buttonPanel);

	add("North", panel);

	// Add a status area
	status.setEditable(false);
	status.setForeground(Color.white);
	status.setBackground(BLUE);
	add("Center", status);
    }

    public void actionPerformed(ActionEvent evt) {
	String retStr;

	if (evt.getSource() == printButton) {
	    retStr = printToJiniService(printerChoice.getSelectedItem(),
					       textField.getText());
	    if (retStr != null) {
		status.append(retStr);
	    } else {
		status.append("Error: no connection to Jini PrintService.");
	    }
	    status.append("\n");

	} else if (evt.getSource() == checkStatusButton) {
	    status.append(printerChoice.getSelectedItem()+":\n");
	    retStr = checkQueueJiniService(printerChoice.getSelectedItem());

	    if (retStr != null) {
		status.append(retStr);
	    } else {
		status.append("Error: no connection to Jini PrintService.");
	    }
	    status.append("\n");
	}
    }

    public static void main (String[] args) {

	if (args.length < 2) {
	    System.out.println("usage: java -cp .:<PrintService> "+
			       "JiniPrintClient <printer_name> <file_name>");
	    System.exit(0);
	}

	JiniPrintClient jiniPrintClient = 
	    new JiniPrintClient(args[0], args[1]);

    }

}

