package wetfish;

import com.sun.java.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

class GroupView implements Serializable {
	transient static private Vector Saves;
	transient private JMenu groupMenu;
	transient private JFrame Root;
	private String Filter;
	private String Name;
	private String Path;
	
	GroupView() {
		this(false);
	}

	GroupView(final boolean visible) {
		super();
		Root = new JFrame("Ephemeral");
		fish(true);
	}

	public void fish(final boolean visible) {
		final JDirectoryPane pathchooser = new JDirectoryPane(Path);
		final JDirectoryPane filechooser = new JDirectoryPane(Path);
		final JMenuBar menuBar = new JMenuBar();
		final JCheckBox ephemeral = new JCheckBox("Ephemeral", visible);
		final JTextField filter = new JTextField(40);
		final JTextField path;
		GridBagConstraints gc = new GridBagConstraints();
		Container cp;
		JMenuItem mi;
		JMenu m;

		groupMenu = new JMenu("Group");
		Root = new JFrame("Ephemeral");
		m = new JMenu("System");

		mi = new JMenuItem("Save preferences");
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ObjectOutputStream o =
						new ObjectOutputStream(new FileOutputStream("fred"));

					System.err.println("a " + Saves);
					o.writeObject(Saves);
					o.flush();
				} catch(IOException ex) {
					ex.printStackTrace();
				}
			}
		});
		m.add(mi);

		mi = new JMenuItem("Exit");
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		m.add(mi);

		menuBar.add(m);
		//		populateGroupMenu();
		menuBar.add(groupMenu);
		
		Root.addWindowListener(new WindowAdapter() {
 			public void windowClosing(WindowEvent e) {
				Root.setVisible(false);
				if(ephemeral.isSelected())
					Root.dispose();
			}
		});

		Root.setJMenuBar(menuBar);

		cp = Root.getContentPane();
		cp.setLayout(new GridBagLayout());

		path = new JTextField(Path, 40);
		path.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pathchooser.setCurrentDirectory(new File(path.getText()));
				filechooser.setCurrentDirectory(new File(path.getText()));
				Path = path.getText();
			}
		});

		ephemeral.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean ef = ephemeral.isSelected();
				pathchooser.setVisible(ef);
				filter.setEditable(ef);
				path.setEditable(ef);
				if(ef) {
					Saves.removeElement(GroupView.this);
				} else {
					Saves.addElement(GroupView.this);
				}
				Root.pack();
			}
		});
		cp.add(ephemeral, gc);
		
		gc.gridx = 1;
		cp.add(new JLabel("Path:"), gc);
		
		gc.gridy = 1;
		cp.add(new JLabel("Filter:"), gc);

		gc.gridx = 2;
		gc.gridy = 0;
		gc.gridwidth = GridBagConstraints.REMAINDER;
		gc.weightx = 1;
		gc.fill = GridBagConstraints.HORIZONTAL;
		cp.add(path, gc);
		
		gc.gridy = 1;
		filter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Filter = filter.getText();
			}
		});
		cp.add(filter, gc);

 		filechooser.getModel().setShownType(new FileType() {
 			public String getPresentationName() { return "Java Class File"; }
 			// Should check for non-directoriness
 			public boolean testFile(File file) {
 				return file.getName().endsWith(".class");
 			}
 			public Icon getIcon() { return null; }
 			public boolean isContainer() { return false; }
 		});
		filechooser.getModel().setHiddenRule(FileType.SharedFolder);
		filechooser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.err.println("action " + e.getActionCommand());
			}
		});
		gc.gridx = 0;
		gc.gridy = 2;
		gc.gridheight = 5;
		gc.weighty = 5;
		gc.fill = GridBagConstraints.BOTH;
		cp.add(filechooser, gc);

		pathchooser.getModel().setShownType(FileType.SharedFolder);
 		pathchooser.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
				String ac = e.getActionCommand();

				if((ac != null) && (ac.equals("doubleClickContainer"))) {
					File f = pathchooser.getSelectedFile();
					path.setText(f.getAbsolutePath());
					Path = f.getAbsolutePath();
					filechooser.setCurrentDirectory(f);
				}
 			}
 		});
		gc.gridy = GridBagConstraints.RELATIVE;
		gc.gridheight = 3;
		gc.weighty = 4;
		cp.add(pathchooser, gc);

		populateGroupMenu();
		pathchooser.setVisible(visible);
		Root.pack();
		Root.setVisible(true);
	}
	
	public static void main(String[] args) {
		Saves	= new Vector();

		try {
			ObjectInputStream o =
				new ObjectInputStream(new FileInputStream("fred"));
			Saves = (Vector)o.readObject();
			System.err.println("saves " + Saves);
			Enumeration e = Saves.elements();

			while(e.hasMoreElements()) {
				((GroupView)e.nextElement()).fish(false);
			}
		} catch(Exception e) {
			e.printStackTrace();
			new GroupView(true);
		}
	}

 	private void writeObject(ObjectOutputStream o)
 		throws IOException
 		{
 			System.err.println("fred " + Path);
			o.writeObject(Path);
 		}

 	private void readObject(ObjectInputStream o)
 		throws ClassNotFoundException, IOException
 		{
			Root = new JFrame("non Ephemeral");
			
			String foo;
 			System.err.println("Restoring...");
			foo = (String)o.readObject();
			System.err.println("got " + foo + this + " " + Saves);
			Path = foo;
 		}
	
	void populateAll() {
		Enumeration e = Saves.elements();

		while(e.hasMoreElements()) {
			((GroupView)e.nextElement()).populateGroupMenu();
		}
	}
	
	protected void populateGroupMenu() {
		Enumeration el = Saves.elements();
		JMenuItem mi;

		groupMenu.removeAll();
		mi = new JMenuItem("New Group...");
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new GroupView(true);
				populateAll();
 			}
		});
		groupMenu.add(mi);
		while(el.hasMoreElements()) {
			final GroupView tmp = (GroupView)el.nextElement();

			mi = new JMenuItem(tmp.getName());
			mi.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					tmp.kick();
				}
			});
			groupMenu.add(mi);
		}
	}

	public void kick() {
		Root.setVisible(true);
		Root.toFront();
	}

	public String getName() { return Name; }
}

// Local Variables:
// compile-command: "javac GroupView.java"
// End:

