EntityGenWindow.java
6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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"));
}
});
}
}