/*
 * Created on Aug 2, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.biomoby.shared.schema;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * @author lixin
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Frame extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1939367498077695253L;

	public Frame()
	{
		
		super("XML Schema generator");

		//setBackground(Color.magenta);
		setSize(500,250);
		
		final JTextField textField;
		textField=new JTextField(20);
	
		final JLabel result=new JLabel();
		JPanel resultPanel=new JPanel();
		resultPanel.add(result);
		
		JPanel jp=new JPanel();
		jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
		
		JLabel hint=new JLabel("Input the name of object");
		JPanel h1=new JPanel();
		h1.add(hint);
		jp.add(h1);
		
		JButton b1=new JButton("Generate");
		JButton clear=new JButton("Clear");
		JButton quit=new JButton("Quit");
	
		
		
		JPanel textPanel=new JPanel();
		textPanel.add(textField);
		
		jp.add(textPanel);

		jp.add(resultPanel);
		
		JPanel h2=new JPanel();
		
		h2.add(b1);
		h2.add(clear);
		h2.add(quit);
		jp.add(h2);
		
		
		getContentPane().add(jp);
		
		clear.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						textField.setText("");
						result.setText("");
					}
				});
				
		quit.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						System.exit(0);
					}
				});
		
		textField.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						String str=textField.getText();
						
						try
						{
							Builder b=new Builder();
				
							Element root=b.buildSchema(str);

							Document doc=new Document(root);

							XMLOutputter fmt=new XMLOutputter();
	
							Format format=Format.getPrettyFormat();

							format.setIndent(" ");

							fmt.setFormat(format);

							fmt.output(doc, System.out);

							//send the result to a file
							String name=str+"xsd.xml";
							FileWriter fw=new FileWriter(name);
							fmt.output(doc,fw);

							result.setText(name+" has been created in the directory");
						}
						catch(Exception ee)
						{
							System.err.println("Catch Exception:"+ee.getMessage());
							ee.printStackTrace();
						}
					

						
					
						
					}
				}
		);
		
		
		
		b1.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						String str=textField.getText();
						
						try
						{
							Builder b=new Builder();
				
							Element root=b.buildSchema(str);

							Document doc=new Document(root);

							XMLOutputter fmt=new XMLOutputter();
	
							Format format=Format.getPrettyFormat();

							format.setIndent(" ");

							fmt.setFormat(format);

							fmt.output(doc, System.out);

							//send the result to a file
							String name=str+"xsd.xml";
							FileWriter fw=new FileWriter(name);
							fmt.output(doc,fw);

							result.setText(name+" has been created in the directory");
						}
						catch(Exception ee)
						{
							System.err.println("Catch Exception:"+ee.getMessage());
							ee.printStackTrace();
						}
					

						
					
						
					}
				}
		);
		
	}

}
