/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package club.ui;
import club.beans.ConfigBean;
import club.data.DataConfig;
import club.data.ConnectDB;
import club.ulti.Validator;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
*
* @author tuan2
*/
public class ConnectDBDialog extends javax.swing.JFrame {
/**
* Creates new form ConnectDBDialog
*/
public ConnectDBDialog() {
this.setLocation(450, 250);
initComponents();
jLabel1.setText("<html>This is your first time wizard , please enter your config setting for<br> storage server.");
}
private void submitInfo(){
String host = txtServerName.getText().trim().toLowerCase();
String name = txtDatabaseName.getText().trim().toLowerCase();
String port = txtPort.getText().trim().toLowerCase();
if (!port.isEmpty()){
port = ":" + port;
}
String user = txtUsername.getText().trim().toLowerCase();
String pass = txtPassword.getText().trim().toLowerCase();
if (ConnectDB.testConnect(host, port, user, pass, name)){
//can connect, now write the info to config file
DataConfig dataConfig = new DataConfig();
ConfigBean configBean = new ConfigBean();
configBean.setDBName(name);
configBean.setDBPass(pass);
configBean.setDBUser(user);
configBean.setDBServer(host);
configBean.setDBPort(port);
if(dataConfig.writeConfigFile(configBean)){
//write successfully
new MainFrame();
this.dispose();
} else{
//error
JOptionPane.showMessageDialog(null, "can not write config file!");
}
} else{
//can not connect
JOptionPane.showMessageDialog(null, "Can not connect");
}
}
private ArrayList validateAllForm(){
ArrayList errors = new ArrayList();
if (!Validator.checkIsNotEmpty(txtServerName.getText())){
errors.add("You have to enter the database server url");
}
if (!Validator.checkIsNotEmpty(txtUsername.getText())){
errors.add("You have to enter the username to log on ");
}
if (!Validator.checkIsNotEmpty(txtDatabaseName.getText())){
errors.add("Database name can not be empty");
}
return errors;
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
lbServerName = new javax.swing.JLabel();
lbPort = new javax.swing.JLabel();
lbDatabaseName = new javax.swing.JLabel();
txtServerName = new javax.swing.JTextField();
txtPort = new javax.swing.JTextField();
txtDatabaseName = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
txtPassword = new javax.swing.JPasswordField();
txtUsername = new javax.swing.JTextField();
lbPassword = new javax.swing.JLabel();
lbUsername = new javax.swing.JLabel();
btnConnect = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Database Login");
setResizable(false);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Server information"));
lbServerName.setText("Sever Name:");
lbPort.setText("Port");
lbDatabaseName.setText("Database Name");
txtServerName.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtServerNameKeyReleased(evt);
}
});
txtDatabaseName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtDatabaseNameActionPerformed(evt);
}
});
txtDatabaseName.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtDatabaseNameKeyReleased(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(lbServerName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtServerName, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbPort)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(lbDatabaseName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtDatabaseName, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)))
.addContainerGap()))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(17, 17, 17)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbServerName)
.addComponent(txtServerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lbPort)
.addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbDatabaseName)
.addComponent(txtDatabaseName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(17, Short.MAX_VALUE)))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Authentication"));
txtUsername.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtUsernameKeyReleased(evt);
}
});
lbPassword.setText("Password");
lbUsername.setText("UserName");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 336, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(lbPassword)
.addGap(18, 18, 18)
.addComponent(txtPassword))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(lbUsername)
.addGap(18, 18, 18)
.addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(103, Short.MAX_VALUE)))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 103, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbUsername)
.addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(23, 23, 23)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lbPassword)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
btnConnect.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
btnConnect.setText("Connect");
btnConnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConnectActionPerformed(evt);
}
});
jLabel1.setText("infor");
jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(16, 16, 16))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void txtDatabaseNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDatabaseNameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtDatabaseNameActionPerformed
private void txtServerNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtServerNameKeyReleased
// TODO add your handling code here:
Validator.Validate(txtServerName, Validator.checkIsNotEmpty(txtServerName.getText()));
}//GEN-LAST:event_txtServerNameKeyReleased
private void txtDatabaseNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDatabaseNameKeyReleased
// TODO add your handling code here:
Validator.Validate(txtDatabaseName, Validator.checkIsNotEmpty(txtDatabaseName.getText()));
}//GEN-LAST:event_txtDatabaseNameKeyReleased
private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed
// TODO add your handling code here:
if (validateAllForm().isEmpty()){
submitInfo();
} else{
Validator.showErrors(validateAllForm());
}
}//GEN-LAST:event_btnConnectActionPerformed
private void txtUsernameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtUsernameKeyReleased
// TODO add your handling code here:
Validator.Validate(txtUsername, Validator.checkIsNotEmpty(txtUsername.getText()));
}//GEN-LAST:event_txtUsernameKeyReleased
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnConnect;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JLabel lbDatabaseName;
private javax.swing.JLabel lbPassword;
private javax.swing.JLabel lbPort;
private javax.swing.JLabel lbServerName;
private javax.swing.JLabel lbUsername;
private javax.swing.JTextField txtDatabaseName;
private javax.swing.JPasswordField txtPassword;
private javax.swing.JTextField txtPort;
private javax.swing.JTextField txtServerName;
private javax.swing.JTextField txtUsername;
// End of variables declaration//GEN-END:variables
}