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

import java.awt.BorderLayout;
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;

public class EntityGenWindow extends JFrame{

	private JTextField tf_outputDir;
	private JTextField tf_dbPackage;
	private JTextField tf_dbName;
	private JTextArea ta_tableList;
	private Properties prop;
	private boolean createAllTable;

	public EntityGenWindow(){
		initComponents();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initComponents() {
		this.setBounds(100, 100, 746, 464);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		this.getContentPane().add(panel, BorderLayout.CENTER);
		this.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);

		JLabel lblClass1 = new JLabel("数据库名称");
		lblClass1.setBounds(40, 320, 89, 15);
		panel.add(lblClass1);

		tf_dbName = new JTextField();
		tf_dbName.setColumns(10);
		tf_dbName.setBounds(139, 320, 413, 21);
		panel.add(tf_dbName);
		
		JButton btnNewButton = new JButton("生成文件");
		btnNewButton.addActionListener(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(), tf_dbName.getText(), tableNameList, createAllTable);
				JOptionPane.showMessageDialog(EntityGenWindow.this, "生成完成", "提示信息", JOptionPane.INFORMATION_MESSAGE);
				eBtn.setEnabled(true);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		});
		btnNewButton.setBounds(319, 360, 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(e -> {
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fileChooser.setCurrentDirectory(new File(tf_outputDir.getText()));
			int status = fileChooser.showOpenDialog(EntityGenWindow.this);
			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"));
				EntityGenWindow.this.tf_dbName.setText(prop.getProperty("SchameName"));
			}
		});
	}
}