import java.util.Vector;
import java.io.*;
import java.awt.*;
import java.net.InetAddress;


public class fbconf extends Frame implements FrameBufferInfo {

	private static final String view_buff = new String("View Frame Buffers");
	private static final String boot_config = new String(" Set Boot Options ");
	private static final String cde_config  = new String("  Configure CDE   ");
	private static String hostname = new String("");

	Configuration config;
	int fbcount;
	TextArea display;
	
	TextField device;
	TextField fbtype;
	TextField memory;
	TextField resolution;

	String Main = new String("Main");
	String Boot = new String("Boot");
	String CDE = new String("CDE");

	CardLayout deck;
	Panel workarea;
	EditConfig editor;
	CdeConfig cde_edit;

    public static void main(String args[]) {
		Runtime runtime = Runtime.getRuntime();
		String [] argv = new String[2];

		try {
			hostname = new String(InetAddress.getLocalHost().getHostName());
		} catch (Exception e) {
			hostname = ":0.";
		}

		fbconf f = new fbconf();
		f.start();
    }

	public fbconf () {
		super("Frame Buffer Configuration");
		
		setLayout(new BorderLayout());
		config = new Configuration();
		fbcount = config.fbcount();

		MenuBar mb = new MenuBar();
		Menu m1 = new Menu("View Option");
		m1.add(new MenuItem(view_buff));
		m1.add(new MenuItem(boot_config));
		m1.add(new MenuItem(cde_config));
		mb.add(m1);

		Menu m2 = new Menu("Frame Buffers");
		
		Panel buttons = new Panel();
		buttons.add(new Button(view_buff));
		buttons.add(new Button(boot_config));
		buttons.add(new Button(cde_config));
		add("North", buttons);

		display = new TextArea("\n  fbconf V1.01 Beta for JDK 1.1 by Dave Tong: 10th January 1997\n  Please provide feedback to david.tong@eng.sun.com\n\n  No frame buffer selected",8,40);
		add("Center",display);

		workarea = new Panel();
		deck = new CardLayout();
		workarea.setLayout(deck);

		Panel infosurround = new Panel();
		infosurround.setLayout(new BorderLayout());
		
        Panel fbselect = new Panel();
        fbselect.setLayout(new FlowLayout());
        for (int c = 0; c < fbcount; c++) {
            Node node = config.node(c);
            fbselect.add(new Button(node.identify()));
			m2.add(new MenuItem(node.identify()));
        }
        infosurround.add("South", fbselect);
		mb.add(m2);
		
		Panel infoarea = new Panel();
		infoarea.setLayout(new GridLayout(5, 2));
		
		infoarea.add(new Label("Selected Device"));
		device = new TextField(16);
		device.setEditable(false);
		infoarea.add(device);

		infoarea.add(new Label("Frame Buffer Type"));
		fbtype = new TextField(16);
		fbtype.setEditable(false);
		infoarea.add(fbtype);

		infoarea.add(new Label("Available Memory (Mb)"));
		memory = new TextField(16);
		memory.setEditable(false);
		infoarea.add(memory);

		infoarea.add(new Label("Current Resolution"));
		resolution = new TextField(16);
		resolution.setEditable(false);
		infoarea.add(resolution);

		infosurround.add("North", infoarea);
		deck.addLayoutComponent(infosurround, Main);
		workarea.add(infosurround, Main);

		editor = new EditConfig(this, config);
		deck.addLayoutComponent(editor, Boot);
		workarea.add(editor, Boot);

		cde_edit = new CdeConfig(this, config);
		deck.addLayoutComponent(cde_edit, CDE);
		workarea.add(cde_edit, CDE);

		add("South", workarea);
		setMenuBar(mb);
		deck.show(workarea, Main);
	}

	public void start() {
		setSize(500,400);
		show();
	}

	public boolean handleEvent(Event ev) {
		String which = "";

		if (ev.id == Event.WINDOW_DESTROY)
			System.exit(0);

		if (ev.id == Event.ACTION_EVENT) {
			if (ev.target instanceof MenuItem) {
				which = ((MenuItem)ev.target).getLabel();
			} else if (ev.target instanceof Button) {
				which = ((Button)ev.target).getLabel();
				Panel show = null;
				if (which.equals(view_buff)) {
					deck.show(workarea, Main);
					return true;
				}
				if (which.equals(boot_config)) {
					deck.show(workarea, Boot);
					return true;
				} else if (which.equals(cde_config)) {
					deck.show(workarea, CDE);
					return true;
				}

				if (which.length() > 0) {
					for (int c = 0; c < fbcount; c++) {
						if (which.equals(config.node(c).identify())) {
							Node n = config.node(c);
							device.setText(n.identify());
							showText(n.get_description());
							fbtype.setText(n.get_fbtype());
							memory.setText(n.get_memory());
							resolution.setText(n.get_resolution());
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	public void showText(String text) {
		display.setText(text);
	}
}

