EntityGenWindow.java 6.36 KB
package com.taover.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.taover.db.Tools;

public class EntityGenWindow {

	private JFrame frame;
	private JTextField tf_outputDir;
	private JTextField tf_dbPackage;
	private JTextArea ta_tableList;
	private Properties prop;
	private boolean createAllTable;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EntityGenWindow window = new EntityGenWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public EntityGenWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 746, 464);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		frame.setTitle("生成实体文件");
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("表名列表");
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
		lblNewLabel.setVerticalAlignment(SwingConstants.TOP);
		lblNewLabel.setBounds(40, 66, 89, 15);
		panel.add(lblNewLabel);
		
		JLabel label = new JLabel("数据库配置文件");
		label.setBounds(40, 29, 89, 15);
		panel.add(label);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setBounds(139, 26, 249, 21);
		this.initPropCombobox(comboBox);
		panel.add(comboBox);
		
		ta_tableList = new JTextArea();
		ta_tableList.setTabSize(4);
		ta_tableList.setBounds(139, 62, 516, 157);
		panel.add(ta_tableList);
		
		JLabel label_1 = new JLabel("输出文件夹");
		label_1.setBounds(40, 243, 89, 15);
		panel.add(label_1);
		
		tf_outputDir = new JTextField();
		tf_outputDir.setBounds(139, 240, 413, 21);
		panel.add(tf_outputDir);
		tf_outputDir.setColumns(10);		
		
		JLabel lblClass = new JLabel("CLASS包名");
		lblClass.setBounds(40, 283, 89, 15);
		panel.add(lblClass);
		
		tf_dbPackage = new JTextField();
		tf_dbPackage.setColumns(10);
		tf_dbPackage.setBounds(139, 280, 413, 21);
		panel.add(tf_dbPackage);
		
		JButton btnNewButton = new JButton("生成文件");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				List<String> tableNameList = new ArrayList<String>();
				String tableNameStr = ta_tableList.getText();
				String[] tableNameSplitBack = tableNameStr.split("\n");
				for(int i=0; i<tableNameSplitBack.length; ++i){
					String tableNameBack = tableNameSplitBack[i];
					String[] tableNameCommoArr = tableNameBack.split(",");
					for(int j=0; j<tableNameCommoArr.length; ++j){
						tableNameList.add(tableNameCommoArr[j]);
					}
				}
				try {
					JButton eBtn = (JButton)e.getSource();
					eBtn.setEnabled(false);
					UiUtils.generateEntity(EntityGenWindow.this.prop, tf_dbPackage.getText(), tf_outputDir.getText(), tableNameList, createAllTable);					
					JOptionPane.showMessageDialog(EntityGenWindow.this.frame, "生成完成", "提示信息", JOptionPane.INFORMATION_MESSAGE);
					eBtn.setEnabled(true);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		btnNewButton.setBounds(319, 348, 113, 38);
		panel.add(btnNewButton);
		
		JLabel label_2 = new JLabel("(逗号分隔或换行)");
		label_2.setVerticalAlignment(SwingConstants.TOP);
		label_2.setHorizontalAlignment(SwingConstants.LEFT);
		label_2.setBounds(40, 85, 101, 15);
		panel.add(label_2);
		
		JButton button = new JButton("选择文件");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				fileChooser.setCurrentDirectory(new File(tf_outputDir.getText()));
				int status = fileChooser.showOpenDialog(EntityGenWindow.this.frame);
				if (status == JFileChooser.APPROVE_OPTION) {
			      File selectedFile = fileChooser.getSelectedFile();
			      tf_outputDir.setText(selectedFile.getPath());
			    }
			}
		});
		button.setBounds(562, 239, 93, 23);
		panel.add(button);
		
		JCheckBox chckbxNewCheckBox = new JCheckBox("所有表");
		chckbxNewCheckBox.setBounds(408, 26, 82, 21);
		chckbxNewCheckBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JCheckBox tempCheck = (JCheckBox)e.getSource();
				if(tempCheck.isSelected()){
					createAllTable = true;
					ta_tableList.setEnabled(false);
				}else{
					createAllTable = false;
					ta_tableList.setEnabled(true);
				}
			}
		});
		panel.add(chckbxNewCheckBox);
	}
	
	private void initPropCombobox(JComboBox comboBox){
		String binDirPath = UiUtils.getProjectRoot();
		File binDir = new File(binDirPath);
		File[] sonFileArr = binDir.listFiles();
		for(int i=0; i<sonFileArr.length; ++i){
			File sonFile = sonFileArr[i];
			String sonFileName = sonFile.getName();
			if(sonFileName.endsWith("properties")){
				comboBox.addItem(sonFileName);
			}
		}
		
		comboBox.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				String propertyName = ((JComboBox)e.getSource()).getSelectedItem().toString();
				String binDir = UiUtils.getProjectRoot();
				Properties prop = new Properties();
				try {
					FileInputStream fis = new FileInputStream(new File(binDir+File.separator+propertyName));
					prop.load(fis);
					fis.close();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				EntityGenWindow.this.prop = prop;
				EntityGenWindow.this.tf_outputDir.setText(prop.getProperty("OutputPath"));
				EntityGenWindow.this.tf_dbPackage.setText(prop.getProperty("packageName"));
			}
		});
	}
}