/**
 * Copyright (c) 2001/2003 Distributed Systems Research Group.
 * All rights reserved.
 *
 * For more information on Distributed Systems Research Group (DSRG),
 * please see <http://www.ics.agh.edu.pl/>.
 *
 * @author radzisz
 * file name: NoteClient.java
 *
 */
package edu.agh.sr.madej;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * Klasa reprezentujaca klienta.
 */
public class NoteBoardClient extends JFrame {

	JTextArea textArea = new JTextArea(30, 60);
	JButton bClear = new JButton("clear");
	JButton bAppend = new JButton("append");
	JButton bGet = new JButton("get");
	JTextField userName = new JTextField(10);
	JTextField text = new JTextField(50);
	NoteBoard board;

	/**
 	 * Konstruktor bezargumentowy.
 	 */
	NoteBoardClient() {
		super("NoteClient");

		bClear.addActionListener(new ActionHandler());
		bGet.addActionListener(new ActionHandler());
		bAppend.addActionListener(new ActionHandler());

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		JScrollPane scrollPane =
			new JScrollPane(
				textArea,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		textArea.setEditable(false);
		getContentPane().add(scrollPane, BorderLayout.CENTER);

		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));

		JPanel textPanel = new JPanel();
		textPanel.add(new JLabel("name"));
		textPanel.add(userName);
		textPanel.add(new JLabel("text"));
		textPanel.add(text);
		textPanel.add(bAppend);

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(bClear);
		buttonPanel.add(bGet);

		bottomPanel.add(textPanel);
		bottomPanel.add(buttonPanel);

		getContentPane().add(bottomPanel, BorderLayout.SOUTH);

		initRemote();

		pack();
		setVisible(true);
	}
	
	/**
 	 * Klasa zagniezdzona obslugujaca zdarzenia.
 	 */
	class ActionHandler implements ActionListener {
	
		/**
 	 	 * Metoda wywolywana przy zajsciu zdarzenia.
 	 	 *
 	 	 * @param e obiekt identyfikujacy zdarzenie
 	 	 */
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == bClear)
				clean();
			if (e.getSource() == bAppend)
				appendText(userName.getText() + ": " + text.getText());
			if (e.getSource() == bGet)
				textArea.setText(getText());
		}
	}

	/**
 	 * Pobranie referencji do obiektu zdalnego.
 	 */
	private void initRemote() {
		try {
			Object obj = Naming.lookup("madej");
			board = (NoteBoard)obj;
		} catch (Exception e) {
			displayError(e.toString());
		}
	}

	/**
 	 * Dopisanie linijki tekstu na zdlanej tablicy.
 	 *
 	 * @param text tekst
 	 */
	private void appendText(String text) {
		try {
			board.appendText(text);
		} catch (Exception e) {
			displayError(e.toString());
		}
	}

	/**
 	 * Wyczyszczenie zdalnej tablicy.
 	 */
	private void clean() {
		try {
			board.clean();
		} catch (Exception e) {
			displayError(e.toString());
		}
		
		textArea.setText("");
	}

	/**
 	 * Pobranie zawartosci zdalnej tablicy.
 	 */
	private String getText() {
		String ret;
	
		try {
			ret = board.getText();
		} catch (Exception e) {
			displayError(e.toString());
			ret = "sorry";
		}
		
		return ret;
	}

	/**
 	 * Wyswietlenie okienka dialogowego z zadanym tekstem.
 	 *
 	 * @param text tekst
 	 */
	private void displayError(String text) {
		JOptionPane.showInternalMessageDialog(
			this.getContentPane(),
			text,
			"error",
			JOptionPane.INFORMATION_MESSAGE);
	}

	/**
 	 * Metoda pozwalajaca na uruchomienie programu
 	 *
 	 * @param args tablica argumentow
 	 */
	public static void main(String[] args) {
		new NoteBoardClient();
	}
}