/*
 * Gui.java
 *
 * Created on November 4, 2002, 11:01 AM
 */

package net.sf.hibernate.tool.ddl2hbm;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import net.sf.hibernate.util.StringHelper;

/**
 *
 * @author  Administrator
 */
public class Gui extends javax.swing.JApplet {
	final static Object[] javaKeyTypes= new String[] {"java.util.Date", "java.lang.Long", "java.lang.String", "java.sql.Timestamp"};
	final static Object[] hibernateKeyTypes = new String[] {"date", "long",  "string", "timestamp"};
	final static int DATE_TYPE=0;
	final static int LONG_TYPE=1;
	final static int STRING_TYPE=2;
	final static int TIMESTAMP_TYPE=3;
	DefaultComboBoxModel javaTypesModel = new DefaultComboBoxModel(javaKeyTypes);
	DefaultComboBoxModel hibernateTypesModel = new DefaultComboBoxModel(hibernateKeyTypes);
	
	String[] generationParameters;
	Map schemas;
	/** Creates new form Gui */
	public Gui() {
		initComponents();
		
		keyFieldType.setModel(hibernateTypesModel);
		generatorNameItemStateChanged(null);
		
		readState();
		Runtime.getRuntime().addShutdownHook( new Thread() {
			public void run() {
				saveState();
			}
		} );
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	private void initComponents() {//GEN-BEGIN:initComponents
		java.awt.GridBagConstraints gridBagConstraints;
		
		mappingTypeGroup = new javax.swing.ButtonGroup();
		buttonGroup1 = new javax.swing.ButtonGroup();
		databasePanel = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		catalogName = new javax.swing.JComboBox();
		jLabel2 = new javax.swing.JLabel();
		schemaName = new javax.swing.JComboBox();
		jLabel3 = new javax.swing.JLabel();
		tableName = new javax.swing.JTextField();
		keyFieldTypeGroup = new javax.swing.ButtonGroup();
		tabbedPanel = new javax.swing.JTabbedPane();
		connectionPanel = new javax.swing.JPanel();
		jLabel6 = new javax.swing.JLabel();
		driverClass = new javax.swing.JTextField();
		jLabel7 = new javax.swing.JLabel();
		jLabel8 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		connectionUrl = new javax.swing.JTextField();
		dbUser = new javax.swing.JTextField();
		dbPassword = new javax.swing.JPasswordField();
		jPanel5 = new javax.swing.JPanel();
		tablesPanel = new javax.swing.JPanel();
		tableListHolder = new javax.swing.JPanel();
		cmdGetTables = new javax.swing.JButton();
		tablesScroller = new javax.swing.JScrollPane();
		selectedTables = new javax.swing.JList();
		mappingPanel = new javax.swing.JPanel();
		jLabel11 = new javax.swing.JLabel();
		mappingTypePanel = new javax.swing.JPanel();
		optionSingleMap = new javax.swing.JRadioButton();
		optionTableMap = new javax.swing.JRadioButton();
		mappingFile = new javax.swing.JTextField();
		jLabel10 = new javax.swing.JLabel();
		idFieldName = new javax.swing.JTextField();
		Generator = new javax.swing.JLabel();
		genPanel = new javax.swing.JPanel();
		generatorName = new javax.swing.JComboBox();
		cmdParams = new javax.swing.JButton();
		jLabel12 = new javax.swing.JLabel();
		jPanel1 = new javax.swing.JPanel();
		optionHibernateTypes = new javax.swing.JRadioButton();
		optionJavaTypes = new javax.swing.JRadioButton();
		keyFieldType = new javax.swing.JComboBox();
		jLabel14 = new javax.swing.JLabel();
		codePanel = new javax.swing.JPanel();
		jLabel4 = new javax.swing.JLabel();
		packageName = new javax.swing.JTextField();
		jLabel13 = new javax.swing.JLabel();
		baseClass = new javax.swing.JTextField();
		outputPanel = new javax.swing.JPanel();
		jLabel5 = new javax.swing.JLabel();
		outputDirectory = new javax.swing.JTextField();
		cmdPickDir = new javax.swing.JButton();
		cmdPanel = new javax.swing.JPanel();
		cmdGenerate = new javax.swing.JButton();
		
		databasePanel.setLayout(new java.awt.GridBagLayout());
		
		databasePanel.setMinimumSize(new java.awt.Dimension(250, 70));
		databasePanel.setPreferredSize(new java.awt.Dimension(250, 75));
		jLabel1.setText("Catalog");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		databasePanel.add(jLabel1, gridBagConstraints);
		
		catalogName.setMinimumSize(new java.awt.Dimension(31, 20));
		catalogName.setNextFocusableComponent(schemaName);
		catalogName.setPreferredSize(new java.awt.Dimension(31, 20));
		catalogName.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				catalogNameItemStateChanged(evt);
			}
		});
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
		databasePanel.add(catalogName, gridBagConstraints);
		
		jLabel2.setText("Schema");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		databasePanel.add(jLabel2, gridBagConstraints);
		
		schemaName.setMinimumSize(new java.awt.Dimension(31, 20));
		schemaName.setNextFocusableComponent(tableName);
		schemaName.setPreferredSize(new java.awt.Dimension(31, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
		databasePanel.add(schemaName, gridBagConstraints);
		
		jLabel3.setText("Table Filter");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		databasePanel.add(jLabel3, gridBagConstraints);
		
		tableName.setText("%");
		tableName.setNextFocusableComponent(catalogName);
		tableName.setPreferredSize(new java.awt.Dimension(200, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
		databasePanel.add(tableName, gridBagConstraints);
		
		tabbedPanel.setFont(new java.awt.Font("Dialog", 1, 11));
		tabbedPanel.setPreferredSize(new java.awt.Dimension(430, 240));
		connectionPanel.setLayout(new java.awt.GridBagLayout());
		
		jLabel6.setText("Driver Class");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		connectionPanel.add(jLabel6, gridBagConstraints);
		
		driverClass.setText("oracle.jdbc.OracleDriver");
		driverClass.setNextFocusableComponent(connectionUrl);
		driverClass.setPreferredSize(new java.awt.Dimension(200, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		connectionPanel.add(driverClass, gridBagConstraints);
		
		jLabel7.setText("Connection URL");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		connectionPanel.add(jLabel7, gridBagConstraints);
		
		jLabel8.setText("Username");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		connectionPanel.add(jLabel8, gridBagConstraints);
		
		jLabel9.setText("Password");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		connectionPanel.add(jLabel9, gridBagConstraints);
		
		connectionUrl.setText("jdbc:oracle:thin:@host:port:dbi");
		connectionUrl.setNextFocusableComponent(dbUser);
		connectionUrl.setPreferredSize(new java.awt.Dimension(200, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		connectionPanel.add(connectionUrl, gridBagConstraints);
		
		dbUser.setNextFocusableComponent(dbPassword);
		dbUser.setPreferredSize(new java.awt.Dimension(200, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		connectionPanel.add(dbUser, gridBagConstraints);
		
		dbPassword.setNextFocusableComponent(driverClass);
		dbPassword.setPreferredSize(new java.awt.Dimension(200, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		connectionPanel.add(dbPassword, gridBagConstraints);
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridwidth = 2;
		connectionPanel.add(jPanel5, gridBagConstraints);
		
		tabbedPanel.addTab("Connection", connectionPanel);
		
		tablesPanel.setLayout(new java.awt.BorderLayout());
		
		tableListHolder.setLayout(new java.awt.GridBagLayout());
		
		cmdGetTables.setFont(new java.awt.Font("Dialog", 0, 12));
		cmdGetTables.setText("tables...");
		cmdGetTables.setFocusPainted(false);
		cmdGetTables.setMaximumSize(new java.awt.Dimension(150, 26));
		cmdGetTables.setMinimumSize(new java.awt.Dimension(80, 20));
		cmdGetTables.setPreferredSize(new java.awt.Dimension(85, 20));
		cmdGetTables.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdGetTablesActionPerformed(evt);
			}
		});
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 10);
		tableListHolder.add(cmdGetTables, gridBagConstraints);
		
		selectedTables.setFont(new java.awt.Font("Dialog", 0, 12));
		selectedTables.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
			public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
				selectedTablesValueChanged(evt);
			}
		});
		
		tablesScroller.setViewportView(selectedTables);
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 10);
		tableListHolder.add(tablesScroller, gridBagConstraints);
		
		tablesPanel.add(tableListHolder, java.awt.BorderLayout.CENTER);
		
		tabbedPanel.addTab("Tables", tablesPanel);
		
		mappingPanel.setLayout(new java.awt.GridBagLayout());
		
		jLabel11.setText("Mapping File");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		mappingPanel.add(jLabel11, gridBagConstraints);
		
		mappingTypePanel.setLayout(new java.awt.GridBagLayout());
		
		mappingTypePanel.setBorder(new javax.swing.border.EtchedBorder());
		mappingTypePanel.setMinimumSize(new java.awt.Dimension(184, 55));
		optionSingleMap.setFont(new java.awt.Font("Dialog", 0, 12));
		optionSingleMap.setText("single map");
		mappingTypeGroup.add(optionSingleMap);
		optionSingleMap.setMinimumSize(new java.awt.Dimension(87, 20));
		optionSingleMap.setNextFocusableComponent(idFieldName);
		optionSingleMap.setPreferredSize(new java.awt.Dimension(87, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 2, 2, 0);
		mappingTypePanel.add(optionSingleMap, gridBagConstraints);
		
		optionTableMap.setFont(new java.awt.Font("Dialog", 0, 12));
		optionTableMap.setSelected(true);
		optionTableMap.setText("one map per database table");
		mappingTypeGroup.add(optionTableMap);
		optionTableMap.setNextFocusableComponent(optionSingleMap);
		optionTableMap.setPreferredSize(new java.awt.Dimension(183, 20));
		optionTableMap.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				optionTableMapItemStateChanged(evt);
			}
		});
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.insets = new java.awt.Insets(2, 2, 0, 2);
		mappingTypePanel.add(optionTableMap, gridBagConstraints);
		
		mappingFile.setText("mapping.hbm.xml");
		mappingFile.setEnabled(false);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.weightx = 0.5;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 2);
		mappingTypePanel.add(mappingFile, gridBagConstraints);
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		mappingPanel.add(mappingTypePanel, gridBagConstraints);
		
		jLabel10.setText("Key Field Name");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		mappingPanel.add(jLabel10, gridBagConstraints);
		
		idFieldName.setText("id");
		idFieldName.setMinimumSize(new java.awt.Dimension(220, 20));
		idFieldName.setNextFocusableComponent(generatorName);
		idFieldName.setPreferredSize(new java.awt.Dimension(220, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		mappingPanel.add(idFieldName, gridBagConstraints);
		
		Generator.setText("SchemaExport");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		mappingPanel.add(Generator, gridBagConstraints);
		
		genPanel.setLayout(new java.awt.GridBagLayout());
		
		genPanel.setPreferredSize(new java.awt.Dimension(250, 20));
		generatorName.setEditable(true);
		generatorName.setFont(new java.awt.Font("Dialog", 0, 12));
		generatorName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "uuid.hex", "uuid.string", "vm.long", "vm.hex", "assigned", "native", "sequence", "hilo.long", "hilo.hex", "seqhilo.long" }));
		generatorName.setSelectedIndex(6);
		generatorName.setNextFocusableComponent(cmdParams);
		generatorName.setPreferredSize(new java.awt.Dimension(140, 20));
		generatorName.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				generatorNameItemStateChanged(evt);
			}
		});
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.weightx = 1.0;
		genPanel.add(generatorName, gridBagConstraints);
		
		cmdParams.setFont(new java.awt.Font("Dialog", 0, 12));
		cmdParams.setText("params...");
		cmdParams.setNextFocusableComponent(keyFieldType);
		cmdParams.setPreferredSize(new java.awt.Dimension(88, 20));
		cmdParams.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdParamsActionPerformed(evt);
			}
		});
		
		genPanel.add(cmdParams, new java.awt.GridBagConstraints());
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		mappingPanel.add(genPanel, gridBagConstraints);
		
		jLabel12.setText("Key Field Type");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		mappingPanel.add(jLabel12, gridBagConstraints);
		
		jPanel1.setLayout(new java.awt.GridBagLayout());
		
		jPanel1.setBorder(new javax.swing.border.EtchedBorder());
		optionHibernateTypes.setFont(new java.awt.Font("Dialog", 0, 12));
		optionHibernateTypes.setSelected(true);
		optionHibernateTypes.setText("use Hibernate types");
		keyFieldTypeGroup.add(optionHibernateTypes);
		optionHibernateTypes.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				optionHibernateTypesItemStateChanged(evt);
			}
		});
		
		jPanel1.add(optionHibernateTypes, new java.awt.GridBagConstraints());
		
		optionJavaTypes.setFont(new java.awt.Font("Dialog", 0, 12));
		optionJavaTypes.setText("use Java types");
		keyFieldTypeGroup.add(optionJavaTypes);
		jPanel1.add(optionJavaTypes, new java.awt.GridBagConstraints());
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		mappingPanel.add(jPanel1, gridBagConstraints);
		
		keyFieldType.setFont(new java.awt.Font("Dialog", 0, 12));
		keyFieldType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "date", "integer", "long", "string", "timestamp" }));
		keyFieldType.setSelectedIndex(2);
		keyFieldType.setNextFocusableComponent(optionTableMap);
		keyFieldType.setPreferredSize(new java.awt.Dimension(31, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		mappingPanel.add(keyFieldType, gridBagConstraints);
		
		jLabel14.setText("Key Field Class");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		mappingPanel.add(jLabel14, gridBagConstraints);
		
		tabbedPanel.addTab("Mapping", mappingPanel);
		
		codePanel.setLayout(new java.awt.GridBagLayout());
		
		codePanel.setMinimumSize(new java.awt.Dimension(350, 112));
		codePanel.setPreferredSize(new java.awt.Dimension(350, 150));
		jLabel4.setText("Package Name");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		codePanel.add(jLabel4, gridBagConstraints);
		
		packageName.setNextFocusableComponent(baseClass);
		packageName.setPreferredSize(new java.awt.Dimension(220, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		codePanel.add(packageName, gridBagConstraints);
		
		jLabel13.setText("Base Class Name");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
		codePanel.add(jLabel13, gridBagConstraints);
		
		baseClass.setNextFocusableComponent(packageName);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		codePanel.add(baseClass, gridBagConstraints);
		
		tabbedPanel.addTab("Code", codePanel);
		
		outputPanel.setLayout(new java.awt.GridBagLayout());
		
		jLabel5.setText("Output Directory");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		outputPanel.add(jLabel5, gridBagConstraints);
		
		outputDirectory.setEditable(false);
		outputDirectory.setText("c:\\temp");
		outputDirectory.setPreferredSize(new java.awt.Dimension(100, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.weightx = 0.1;
		gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
		outputPanel.add(outputDirectory, gridBagConstraints);
		
		cmdPickDir.setFont(new java.awt.Font("Dialog", 0, 12));
		cmdPickDir.setText("pick...");
		cmdPickDir.setPreferredSize(new java.awt.Dimension(80, 20));
		cmdPickDir.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdPickDirActionPerformed(evt);
			}
		});
		
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
		outputPanel.add(cmdPickDir, gridBagConstraints);
		
		tabbedPanel.addTab("Output", outputPanel);
		
		getContentPane().add(tabbedPanel, java.awt.BorderLayout.CENTER);
		
		cmdGenerate.setText("Generate");
		cmdGenerate.setEnabled(false);
		cmdGenerate.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cmdGenerateActionPerformed(evt);
			}
		});
		
		cmdPanel.add(cmdGenerate);
		
		getContentPane().add(cmdPanel, java.awt.BorderLayout.SOUTH);
		
	}//GEN-END:initComponents
	
	private void optionHibernateTypesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_optionHibernateTypesItemStateChanged
		// Add your handling code here:
		int selectedIndex = keyFieldType.getSelectedIndex();
		if (optionHibernateTypes.isSelected()) {
			keyFieldType.setModel(hibernateTypesModel);
		}
		else {
			keyFieldType.setModel(javaTypesModel);
		}
		keyFieldType.setSelectedIndex(selectedIndex);      // Add your handling code here:
	}//GEN-LAST:event_optionHibernateTypesItemStateChanged
	
	private void catalogNameItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_catalogNameItemStateChanged
		// Add your handling code here:
		List tmp = (List)schemas.get(catalogName.getSelectedItem());
		Object[] schemaArr = (tmp != null) ? tmp.toArray() : new Object[0];
		DefaultComboBoxModel dcbm =  new DefaultComboBoxModel(schemaArr);
		schemaName.setModel(dcbm);
		schemaName.setSelectedItem(dbUser.getText());
		
	}//GEN-LAST:event_catalogNameItemStateChanged
	
	private void cmdGetTablesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdGetTablesActionPerformed
		// Add your handling code here:
		try {
			Connection c = null;
			try {
				c = getConnection();
				DefaultComboBoxModel dcbm = new DefaultComboBoxModel(JDBCUtil.getCatalogs(c).toArray());
				catalogName.setModel(dcbm);
				schemas = JDBCUtil.getSchemas(c);
				List tmp = (List)schemas.get(catalogName.getSelectedItem());
				Object[] schemaArr = (tmp != null) ? tmp.toArray() : new Object[0];
				dcbm =  new DefaultComboBoxModel(schemaArr);
				Object prevItem = schemaName.getSelectedItem();
				schemaName.setModel(dcbm);
				if (prevItem == null) schemaName.setSelectedItem(dbUser.getText());
				JOptionPane.showMessageDialog(this, databasePanel, "SimpleSelect Table Criteria", JOptionPane.QUESTION_MESSAGE);
				List tables = JDBCUtil.getTables(
					c,
					(String)catalogName.getSelectedItem(),
					(String)schemaName.getSelectedItem(),
					tableName.getText()
				);
				dcbm = new DefaultComboBoxModel(tables.toArray());
				selectedTables.setModel(dcbm);
				
			}
			finally {
				if (c != null) c.close();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e);
		}
	}//GEN-LAST:event_cmdGetTablesActionPerformed
	
	private void selectedTablesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_selectedTablesValueChanged
		// Add your handling code here:
		int[] selected = selectedTables.getSelectedIndices();
		cmdGenerate.setEnabled(!(selected == null || selected.length == 0));
	}//GEN-LAST:event_selectedTablesValueChanged
	
	private void optionTableMapItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_optionTableMapItemStateChanged
		// Add your handling code here:
		mappingFile.setEnabled(!optionTableMap.isSelected());
	}//GEN-LAST:event_optionTableMapItemStateChanged
	
	private void generatorNameItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_generatorNameItemStateChanged
		// Add your handling code here:
		if (generatorName.getSelectedIndex() != -1) {
			String gName = (String)generatorName.getSelectedItem();
			if (
				gName.equals("vm.long") ||
				gName.equals("native") ||
				gName.equals("sequence") ||
				gName.equals("hilo.long") ||
				gName.equals("seqhilo.long")
			) {
				keyFieldType.setSelectedIndex(LONG_TYPE);
			}
			else if (
				gName.equals("uuid.hex") ||
				gName.equals("uuid.string") ||
				gName.equals("vm.hex") ||
				gName.equals("hilo.hex")
			) {
				keyFieldType.setSelectedIndex(STRING_TYPE);
			}
		}
	}//GEN-LAST:event_generatorNameItemStateChanged
	
	private void cmdParamsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdParamsActionPerformed
		// Add your handling code here:
		ParamsPanel paramsPanel = new ParamsPanel();
		if (generationParameters != null) paramsPanel.setParameters(generationParameters);
		int choice =JOptionPane.showConfirmDialog(this, paramsPanel, "Generation Parameters", JOptionPane.OK_CANCEL_OPTION);
		if (choice == JOptionPane.OK_OPTION) {
			this.generationParameters = paramsPanel.getParameters();
		}
	}//GEN-LAST:event_cmdParamsActionPerformed
	
	private void cmdGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdGenerateActionPerformed
		// Add your handling code here:
		Connection connection=null;
		try {
			MapGenerator mg = new MapGenerator();
			String cat = (String)catalogName.getSelectedItem();
			if (cat != null && cat.length() > 0) mg.setCatalog(cat);
			String schem = (String)schemaName.getSelectedItem();
			if (schem != null && schem.length() > 0) mg.setSchemaPattern(schem);
			
			Object[] otables = selectedTables.getSelectedValues();
			if (otables != null && otables.length > 0) {
				String[] tables = (String[])Arrays.asList(otables).toArray(new String[0]);
				mg.setTableNames(tables);
			}
			String pack = packageName.getText();
			if (pack != null && pack.length() > 0) mg.setPackageName(pack);
			String dir = outputDirectory.getText();
			if (dir != null && dir.length() > 0) mg.setOutputDirectory(new File(dir));
			String id = idFieldName.getText();
			if (id != null && id.length() > 0) mg.setIdName(id);
			String idType = (String)keyFieldType.getSelectedItem();
			if (idType != null && idType.length() > 0) mg.setIdType(idType);
			String baseClassName = (String)baseClass.getText();
			if (baseClassName != null && baseClassName.length() > 0) mg.setBaseClass(baseClassName);
			mg.setHibernateTypes(optionHibernateTypes.isSelected());
			
			String generator = (String)generatorName.getSelectedItem();
			if (generator != null) {
				mg.setGenerator(generator);
				if (generationParameters != null) {
					mg.setGeneratorParameters(generationParameters);
				}
			}
			
			mg.setSingleMapFile(optionSingleMap.isSelected());
			if (this.optionSingleMap.isSelected()) {
				mg.setMappingFile(mappingFile.getText());
			}
			
			connection = getConnection();
			mg.generate(connection);
		}
		catch (Exception e) {
			JOptionPane.showMessageDialog(this, e);
			e.printStackTrace();
		}
		finally {
			if (connection != null) {
				try {
					connection.close();
				}
				catch (Exception e) {}
			}
		}
		
	}//GEN-LAST:event_cmdGenerateActionPerformed
	
	private void cmdPickDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPickDirActionPerformed
		// Add your handling code here:
		JFileChooser jfc = new JFileChooser();
		jfc.setDialogTitle("Choose an output directory");
		jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		String outputDir = outputDirectory.getText();
		if (outputDir != null && outputDir.length() > 0) {
			jfc.setCurrentDirectory(new File(outputDir));
		}
		int returnVal = jfc.showOpenDialog(this);
		if(returnVal == JFileChooser.APPROVE_OPTION) {
			this.outputDirectory.setText(jfc.getSelectedFile().getAbsolutePath());
		}
		
	}//GEN-LAST:event_cmdPickDirActionPerformed
	
	
	
	
	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JRadioButton optionSingleMap;
	private javax.swing.JComboBox generatorName;
	private javax.swing.JTextField tableName;
	private javax.swing.JTextField packageName;
	private javax.swing.JTextField outputDirectory;
	private javax.swing.JPanel cmdPanel;
	private javax.swing.JTextField mappingFile;
	private javax.swing.JTextField dbUser;
	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JList selectedTables;
	private javax.swing.JButton cmdParams;
	private javax.swing.JPanel tableListHolder;
	private javax.swing.JPanel mappingPanel;
	private javax.swing.JButton cmdGenerate;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel14;
	private javax.swing.JLabel jLabel13;
	private javax.swing.JComboBox schemaName;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel12;
	private javax.swing.ButtonGroup keyFieldTypeGroup;
	private javax.swing.JLabel jLabel11;
	private javax.swing.JTextField idFieldName;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JPanel databasePanel;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel10;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel connectionPanel;
	private javax.swing.JComboBox catalogName;
	private javax.swing.JRadioButton optionTableMap;
	private javax.swing.JButton cmdGetTables;
	private javax.swing.JScrollPane tablesScroller;
	private javax.swing.JPanel mappingTypePanel;
	private javax.swing.JPanel jPanel5;
	private javax.swing.JPasswordField dbPassword;
	private javax.swing.ButtonGroup mappingTypeGroup;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JComboBox keyFieldType;
	private javax.swing.JLabel Generator;
	private javax.swing.JTextField connectionUrl;
	private javax.swing.JTextField driverClass;
	private javax.swing.JTextField baseClass;
	private javax.swing.JPanel tablesPanel;
	private javax.swing.JRadioButton optionHibernateTypes;
	private javax.swing.JTabbedPane tabbedPanel;
	private javax.swing.JPanel codePanel;
	private javax.swing.JPanel genPanel;
	private javax.swing.JButton cmdPickDir;
	private javax.swing.JPanel outputPanel;
	private javax.swing.JRadioButton optionJavaTypes;
	// End of variables declaration//GEN-END:variables
	
	public static void main(String[] args) throws Exception {
		JFrame frame = new JFrame("Hibernate Mapping SchemaExport");
		try {
			Gui applet = (Gui)java.beans.Beans.instantiate(Gui.class.getClassLoader(), Gui.class.getName());
			frame.getContentPane().add(applet);
			frame.pack();
			frame.setResizable(true);
			
			java.awt.Dimension screenDim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
			java.awt.Dimension frameDim = frame.getSize();
			java.awt.Point frameCenter = new java.awt.Point(
				(int)(screenDim.getWidth()-frameDim.getWidth())/2,
				(int)(screenDim.getHeight()-frameDim.getHeight())/2
			);
			frame.setLocation(frameCenter);
			
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.show();
		}
		catch (Exception e) {
			JOptionPane.showConfirmDialog(frame, e);
		}
	}
	
	
	protected Connection getConnection() throws SQLException, ClassNotFoundException {
		Class.forName(driverClass.getText());
		return DriverManager.getConnection(
			connectionUrl.getText(),
			dbUser.getText(),
			new String(dbPassword.getPassword())
		);
	}
	
	protected void saveState() {
		Properties p = new Properties();
		//p.put("catalogName", catalogName.getText());
		//p.put("schemaName", schemaName.getText());
		//p.put("tableName", tableName.getText());
		p.put("driverClass", driverClass.getText());
		p.put("connectionUrl", connectionUrl.getText());
		p.put("dbUser", dbUser.getText());
		
		p.put("tableName", tableName.getText());
		
		p.put("singleMapType", StringHelper.EMPTY_STRING + optionSingleMap.isSelected());
		p.put("mapName", mappingFile.getText());
		p.put("generatorName", generatorName.getSelectedItem());
		if (this.generationParameters != null) {
			String gp=StringHelper.EMPTY_STRING;
			for (int i=0; i < generationParameters.length; i++) {
				gp += generationParameters[i] + "\t";
			}
			p.put("generatorParameters", gp);
		}
		p.put("keyFieldName", idFieldName.getText());
		p.put("keyFieldType", keyFieldType.getSelectedItem());
		p.put("useHibernateTypes", StringHelper.EMPTY_STRING + optionHibernateTypes.isSelected());
		p.put("packageName", packageName.getText());
		p.put("baseClass", baseClass.getText());
		
		p.put("outputDirectory", outputDirectory.getText());
		
		File propsFile = new File(System.getProperty("user.home"), "mapgen.properties");
		try {
			FileOutputStream fos = new FileOutputStream(propsFile);
			try{
				p.store(fos, "SchemaExport");
			}
			finally {
				fos.close();
			}
		}
		catch (IOException ioe) {
		}
		
	}
	
	protected void readState() {
		File propsFile = new File(System.getProperty("user.home"), "mapgen.properties");
		Properties p = new Properties();
		try {
			FileInputStream fis = new FileInputStream(propsFile);
			try{
				p.load(fis);
				
				driverClass.setText(p.getProperty("driverClass"));
				connectionUrl.setText(p.getProperty("connectionUrl"));
				dbUser.setText(p.getProperty("dbUser"));
				
				//catalogName.setText(p.getProperty("catalogName"));
				//schemaName.setSelectedItem(p.getProperty("schemaName"));
				tableName.setText(p.getProperty("tableName"));
				
				String mapType = p.getProperty("singleMapType");
				if (p.getProperty("mapName") != null) mappingFile.setText(p.getProperty("mapName"));
				optionSingleMap.setSelected(mapType != null && mapType.equals("true"));
				generatorName.setSelectedItem(p.getProperty("generatorName"));
				if (p.getProperty("keyFieldName") != null) idFieldName.setText(p.getProperty("keyFieldName"));
				keyFieldType.setSelectedItem(p.getProperty("keyFieldType"));
				String useHibernateTypes =  p.getProperty("useHibernateTypes");
				optionJavaTypes.setSelected(useHibernateTypes != null && useHibernateTypes.equals("false"));
				
				packageName.setText(p.getProperty("packageName"));
				baseClass.setText(p.getProperty("baseClass"));
				
				if (p.getProperty("outputDirectory") != null) outputDirectory.setText(p.getProperty("outputDirectory"));
				
				String gp = p.getProperty("generatorParameters");
				if (gp != null) {
					StringTokenizer st = new StringTokenizer(gp, "\t");
					generationParameters = new String[st.countTokens()];
					for (int i=0; i < generationParameters.length; i++) {
						generationParameters[i] = st.nextToken();
					}
				}
			}
			finally {
				fis.close();
			}
		}
		catch (IOException ioe) {}
	}
}






