// Oefening PropertiesTest, D&D 5, blz 1111,..
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class H21_PropertiesTest extends JFrame {
	
	private JLabel statusLabel;
	private Properties table;
	private JTextArea displayArea;
	private JTextField valueField, nameField;
	
	public H21_PropertiesTest() {
		
		super("properties test");
		
		table = new Properties();
		
		Container container = getContentPane();
		
		JPanel northSubPanel = new JPanel();
		
		northSubPanel.add(new JLabel("Property value"));
		valueField = new JTextField(10);
		northSubPanel.add(valueField);
		
		northSubPanel.add(new JLabel("Property name (key)"));
		nameField = new JTextField(10);
		northSubPanel.add(nameField);
		
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new BorderLayout());
		northPanel.add(northSubPanel, BorderLayout.NORTH);
		
		statusLabel = new JLabel();
		northPanel.add(statusLabel, BorderLayout.SOUTH);
		
		container.add(northPanel, BorderLayout.NORTH);
		
		displayArea = new JTextArea(4,35);
		container.add(new JScrollPane(displayArea), BorderLayout.CENTER);
		
		JPanel southPanel = new JPanel();
		southPanel.setLayout(new GridLayout(1,5));
		
		JButton putButton = new JButton("Put");
		southPanel.add(putButton);
		
		putButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					Object value = table.setProperty(nameField.getText(), valueField.getText());
					
					if (value == null)
						showStatus("Put: " + nameField.getText() + " " + valueField.getText());
					else
						showStatus("Put: " + nameField.getText() + " " + valueField.getText() + "; Replaced: " + value);
					
					listProperties();
				}
			}
		);
		
		JButton clearButton = new JButton("Clear");
		southPanel.add(clearButton);
		
		clearButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					table.clear();
					showStatus("Table in memory cleared");
					listProperties();
				}
			}
		);
		
		JButton getPropertyButton = new JButton("Get Property");
		southPanel.add(getPropertyButton);
		
		getPropertyButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					Object value = table.getProperty(nameField.getText());
					
					if(value != null)
						showStatus("Get property: " + nameField.getText() + " " + value.toString());
					else
						showStatus("Get: " + nameField.getText() + " not in table.");
					
					listProperties();
				}
			}
		);
		
		JButton saveButton = new JButton("Save");
		southPanel.add(saveButton);
		
		saveButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					try {
						FileOutputStream output = new FileOutputStream("H21_Properties.dat");
						
						table.store(output,"Sample Properties");
						output.close();
						
						listProperties();
					}
					catch (IOException i) {
						i.printStackTrace();
					}
				}
			}
		);
			
			
			JButton loadButton = new JButton("Load");
			southPanel.add(loadButton);
			
			loadButton.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						try {
							FileInputStream input = new FileInputStream("H21_Properties.dat");
							
							table.load(input);
							input.close();
							listProperties();
						}
						catch (IOException i) {
							i.printStackTrace();
						}
					}
				}
			);
			
			container.add(southPanel, BorderLayout.SOUTH);
			
			setSize (550,225);
			setVisible(true);
		}
		
		public void listProperties() {
			StringBuffer buffer = new StringBuffer();
			String name, value;
			
			Enumeration enumeration = table.propertyNames();
			
			while (enumeration.hasMoreElements()) {
				name = enumeration.nextElement().toString();
				value = table.getProperty(name);
				
				buffer.append(name).append('\t');
				buffer.append(value).append('\n');
			}
			
			displayArea.setText(buffer.toString());
		}
		
		public void showStatus(String s) {
			statusLabel.setText(s);
		}
		
		public static void main(String args[]) {
			H21_PropertiesTest app = new H21_PropertiesTest();
			app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}			
}
