added more options in settings allowing user to pick the database name.

added an option to allow the user to save their password
added a password field to the main panel to allow for easy password entry
added a connect button to the main panel
This commit is contained in:
2011-01-27 18:49:30 +00:00
parent c85e599530
commit 4be7476de7
8 changed files with 195 additions and 60 deletions

View File

@@ -1,7 +0,0 @@
#---No Comment---
#Wed Jan 26 13:29:05 EST 2011
server_parrword=kwz5vDLcNnKG6agAnGSm8nwzSncF2SK5
server_location=tcdevsvn1
use_remote_server=true
server_port_number=3306
server_user_name=ricky.barrette

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@@ -57,12 +57,13 @@ public class OrderDB {
/**
* Creates a new OrderDB
* @author ricky barrette
* @param password
* @throws ClassNotFoundException
* @throws SQLException
* @throws SocketException
* @throws Exception
*/
public OrderDB() throws ClassNotFoundException, SQLException, SocketException {
public OrderDB(String password) throws ClassNotFoundException, SQLException, SocketException {
Properties props = getProperties();
@@ -76,7 +77,12 @@ public class OrderDB {
dbLocation = System.getProperty("user.home") +"/.TwentyCodesOrders";
}
Connection conn = getConnection();
Connection conn;
if(password == null)
conn = getConnection();
else
conn = getConnection(password);
Statement stat = conn.createStatement();
// stat.executeUpdate("drop table if exists Orders;");
@@ -216,7 +222,7 @@ public class OrderDB {
passwordEncryptor.setPassword(PASSWORD);
if(isUsingRemoteDatabase){
String url = "jdbc:mysql://"+props.getProperty(SettingsPanel.SERVER_LOCATION)+":"+props.getProperty(SettingsPanel.SERVER_PORT_NUMBER, "3306")+"/Orders";
String url = "jdbc:mysql://"+props.getProperty(SettingsPanel.SERVER_LOCATION)+":"+props.getProperty(SettingsPanel.SERVER_PORT_NUMBER, "3306")+"/"+props.getProperty(SettingsPanel.DATABASE_NAME,"Orders");
if(props.getProperty(SettingsPanel.SERVER_USERNAME).length() > 0)
url += "?user="+ props.getProperty(SettingsPanel.SERVER_USERNAME);
@@ -231,6 +237,26 @@ public class OrderDB {
return DriverManager.getConnection("jdbc:sqlite:"+ dbLocation+".db");
}
private Connection getConnection(String password) throws SQLException {
Properties props = getProperties();
BasicTextEncryptor passwordEncryptor = new BasicTextEncryptor();
passwordEncryptor.setPassword(PASSWORD);
if(isUsingRemoteDatabase){
String url = "jdbc:mysql://"+props.getProperty(SettingsPanel.SERVER_LOCATION)+":"+props.getProperty(SettingsPanel.SERVER_PORT_NUMBER, "3306")+"/"+props.getProperty(SettingsPanel.DATABASE_NAME,"Orders");
if(props.getProperty(SettingsPanel.SERVER_USERNAME).length() > 0)
url += "?user="+ props.getProperty(SettingsPanel.SERVER_USERNAME);
url += "&password="+ password;
return DriverManager.getConnection(url);
}
else
return DriverManager.getConnection("jdbc:sqlite:"+ dbLocation+".db");
}
/**
* returns the row count
* @return number orders in the database

View File

@@ -9,10 +9,12 @@ package com.TwentyCodes.java.OrderProcessor.UI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;
import java.sql.SQLException;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.swing.JButton;
@@ -27,6 +29,7 @@ import com.TwentyCodes.java.OrderProcessor.FileFilter;
import com.TwentyCodes.java.OrderProcessor.ProgressListener;
import com.TwentyCodes.java.OrderProcessor.UncaughtExceptionHandler;
import com.TwentyCodes.java.OrderProcessor.DB.OrderDB;
import javax.swing.JPasswordField;
/**
* this is the main window and class of this application
@@ -42,6 +45,9 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
private UncaughtExceptionHandler mExceptionReport = new UncaughtExceptionHandler(this.getClass());
private static JLabel orderCountLabel;
private static JLabel mWarningLabel;
private static JButton mConnectButton;
private static JPasswordField mPassWord;
private JLabel lblDatabasePassword;
/**
* Creates a new MainWindow
@@ -97,6 +103,17 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
panel.add(mLoadFileButton);
lblDatabasePassword = new JLabel("Database Password");
panel.add(lblDatabasePassword);
mPassWord = new JPasswordField();
mPassWord.setColumns(10);
panel.add(mPassWord);
mConnectButton = new JButton("Connect", new ImageIcon(getClass().getResource("/repeat.png")));
mConnectButton.addActionListener(this);
panel.add(mConnectButton);
panel.add(mWarningLabel);
//progress bar
@@ -104,10 +121,24 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
mProgressBar.setStringPainted(true);
mainPanel.add(mProgressBar, BorderLayout.SOUTH);
return mainPanel;
}
public static void loadDatabase() {
try {
Properties p = getProperties();
mPassWord.setEnabled(! Boolean.parseBoolean(p.getProperty(SettingsPanel.SAVE_PASSWORD, "false")));
if(! mPassWord.isEnabled())
mPassWord.setText(p.getProperty(SettingsPanel.SERVER_PASSWORD));
} catch (IOException e) {
e.printStackTrace();
}
mConnectButton.setEnabled(false);
mProgressBar.setString("Connecting to database");
mProgressBar.setIndeterminate(true);
java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() {
@@ -115,23 +146,30 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
//order count labels
try {
db = new OrderDB();
db = new OrderDB(mPassWord.isEnabled() ? new String(mPassWord.getPassword()) : null);
orderCountLabel.setText(db.getCount()+"");
mWarningLabel.setText(null);
mWarningLabel.setText("Connected");
mPassWord.setEnabled(false);
} catch (SQLException e) {
e.printStackTrace();
mWarningLabel.setText(e.getMessage());
mWarningLabel.setVisible(true);
mConnectButton.setEnabled(true);
orderCountLabel.setText("");
} catch (SocketException e) {
mWarningLabel.setText(e.getMessage());
mWarningLabel.setVisible(true);
mConnectButton.setEnabled(true);
orderCountLabel.setText("");
} catch (NullPointerException e){
mWarningLabel.setText("Database not avilable");
mWarningLabel.setVisible(true);
mConnectButton.setEnabled(true);
orderCountLabel.setText("");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
mProgressBar.setString(null);
mProgressBar.setString(mWarningLabel.getText());
mProgressBar.setIndeterminate(false);
}
});
@@ -178,6 +216,10 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
mProgressBar.setIndeterminate(false);
}
}
if(e.getSource() == mConnectButton){
loadDatabase();
}
}
/**
@@ -207,4 +249,19 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
}
}
/**
* gets the properties from the save file
* @return
* @throws IOException
* @author ricky barrette
*/
private static Properties getProperties() throws IOException{
// create and load default properties
Properties props = new Properties();
FileInputStream in = new FileInputStream(".settings.propertys");
props.load(in);
in.close();
return props;
}
}

View File

@@ -16,6 +16,7 @@ import java.util.Properties;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
@@ -40,6 +41,8 @@ public class SettingsPanel extends JPanel implements ActionListener {
public static final String SERVER_PORT_NUMBER = "server_port_number";
public static final String SERVER_USERNAME = "server_user_name";
public static final String SERVER_PASSWORD = "server_parrword";
public static final String SAVE_PASSWORD = "save_password";
public static final String DATABASE_NAME = "database_name";
private JTextField mServerLocation;
private JTextField mPortNumber;
private JTextField mUserName;
@@ -48,6 +51,8 @@ public class SettingsPanel extends JPanel implements ActionListener {
private JButton mSaveButton;
private JCheckBox mUseRemoteDatabase;
private Properties mProps;
private JTextField mDatabaseName;
private JCheckBox mSavePassword;
/**
* Creates a new SettingsPanel
@@ -69,10 +74,10 @@ public class SettingsPanel extends JPanel implements ActionListener {
//server port number
mPortNumber = new JTextField();
mPortNumber.setEnabled(false);
mPortNumber.setColumns(10);
mPortNumber.setText("3306");
JLabel lblMysql = new JLabel("MYsql serevr username:");
JLabel lblMysql = new JLabel("MYsql server user name:");
JLabel lblMysqlServerPassword = new JLabel("MYsql server password:");
//username
mUserName = new JTextField();
@@ -84,38 +89,58 @@ public class SettingsPanel extends JPanel implements ActionListener {
//disable everything
mServerLocation.setEnabled(false);
mPortNumber.setEnabled(false);
mUserName.setEnabled(false);
mUserPassword.setEnabled(false);
//save button
mSaveButton = new JButton("Save");
mSaveButton = new JButton("Save", new ImageIcon(getClass().getResource("/floppy_disc.png")));
mSaveButton.addActionListener(this);
mSavePassword = new JCheckBox("Save Password?");
mSavePassword.addActionListener(this);
JLabel lblDatabaseName = new JLabel("Database Name:");
mDatabaseName = new JTextField();
mDatabaseName.setEnabled(false);
mDatabaseName.setColumns(10);
//group layout bullshit
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(mUseRemoteDatabase)
.addContainerGap(1087, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(mUseRemoteDatabase)
.addComponent(mSavePassword)
.addContainerGap())
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblMysqlServerLocation)
.addComponent(lblMysqlServerPort)
.addComponent(lblMysql)
.addComponent(lblMysqlServerPassword))
.addComponent(lblDatabaseName)
.addComponent(lblMysql))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
.addComponent(mServerLocation)
.addComponent(mSaveButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(mUserPassword)
.addComponent(mUserName)
.addComponent(mPortNumber))
.addContainerGap(139, Short.MAX_VALUE))))
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(mPortNumber, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 1079, Short.MAX_VALUE)
.addComponent(mServerLocation, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 1079, Short.MAX_VALUE)
.addComponent(mDatabaseName, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 1079, Short.MAX_VALUE)
.addComponent(mUserName, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 1079, Short.MAX_VALUE))
.addContainerGap())))
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblMysqlServerPassword)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(mUserPassword, GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
.addComponent(mSaveButton, GroupLayout.DEFAULT_SIZE, 949, Short.MAX_VALUE))
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
@@ -131,16 +156,22 @@ public class SettingsPanel extends JPanel implements ActionListener {
.addComponent(lblMysqlServerPort)
.addComponent(mPortNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(lblDatabaseName)
.addComponent(mDatabaseName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblMysql)
.addComponent(mUserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addComponent(mUserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblMysql))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mSavePassword)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblMysqlServerPassword)
.addComponent(mUserPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mSaveButton)
.addContainerGap(142, Short.MAX_VALUE))
.addContainerGap(264, Short.MAX_VALUE))
);
setLayout(groupLayout);
@@ -157,17 +188,22 @@ public class SettingsPanel extends JPanel implements ActionListener {
mServerLocation.setEnabled(true);
mPortNumber.setEnabled(true);
mUserName.setEnabled(true);
mUserPassword.setEnabled(true);
mUserPassword.setEnabled( Boolean.parseBoolean(mProps.getProperty(SAVE_PASSWORD, "false")));
mUseRemoteDatabase.setSelected(true);
mDatabaseName.setEnabled(true);
mSavePassword.setEnabled(true);
}
mDatabaseName.setText(mProps.getProperty(DATABASE_NAME, "Orders"));
mServerLocation.setText(mProps.getProperty(SERVER_LOCATION));
mPortNumber.setText(mProps.getProperty(SERVER_PORT_NUMBER));;
mUserName.setText(mProps.getProperty(SERVER_USERNAME));;
mPortNumber.setText(mProps.getProperty(SERVER_PORT_NUMBER, "3306"));
mUserName.setText(mProps.getProperty(SERVER_USERNAME));
mSavePassword.setSelected(Boolean.parseBoolean(mProps.getProperty(SAVE_PASSWORD, "false")));
} catch (IOException e) {
e.printStackTrace();
mDatabaseName.setText("Orders");
mPortNumber.setText("3306");
}
}
@@ -187,49 +223,67 @@ public class SettingsPanel extends JPanel implements ActionListener {
mProps.put(SERVER_LOCATION, mServerLocation.getText());
mProps.put(SERVER_PORT_NUMBER, mPortNumber.getText());
mProps.put(SERVER_USERNAME, mUserName.getText());
mProps.put(DATABASE_NAME, mDatabaseName.getText());
if(mSavePassword.isSelected())
mProps.put(SERVER_PASSWORD, new String(passwordEncryptor.encrypt(new String(mUserPassword.getPassword()))));
try {
FileOutputStream out = new FileOutputStream(".settings.propertys");
mProps.store(out, "---No Comment---");
out.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-gene1rated catch block
e1.printStackTrace();
}
writeProperties(mProps);
}
//use remote database checkbox
if(e.getSource() == mUseRemoteDatabase){
mProps.put(IS_USING_REMOTE_SERVER, Boolean.toString(mUseRemoteDatabase.isSelected()));
try {
FileOutputStream out = new FileOutputStream(".settings.propertys");
mProps.store(out, "---No Comment---");
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
writeProperties(mProps);
if(mUseRemoteDatabase.isSelected()){
mServerLocation.setEnabled(true);
mPortNumber.setEnabled(true);
mUserName.setEnabled(true);
mUserPassword.setEnabled(true);
mDatabaseName.setEnabled(true);
mSavePassword.setEnabled(true);
} else {
mServerLocation.setEnabled(false);
mPortNumber.setEnabled(false);
mUserName.setEnabled(false);
mUserPassword.setEnabled(false);
mDatabaseName.setEnabled(false);
mSavePassword.setEnabled(false);
}
}
if(e.getSource() == mSavePassword){
mProps.setProperty(SAVE_PASSWORD, Boolean.toString(mSavePassword.isSelected()));
writeProperties(mProps);
if(mSavePassword.isSelected()){
mUserPassword.setEnabled(true);
return;
} else {
mUserPassword.setEnabled(false);
return;
}
}
MainWindow.loadDatabase();
}
/**
* writes the Properties to a persitent file
* @param p
* @author ricky barrette
*/
private void writeProperties(Properties p){
try {
FileOutputStream out = new FileOutputStream(".settings.propertys");
p.store(out, "---Order Processor Settings---");
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

View File

@@ -43,3 +43,8 @@ added new order stats to the order pane
fixed processed order stats to only display new order stats when theres new orders
added password encryption using jasypt
1.1.4 b 49
added more options in settings allowing user to pick the database name.
added an option to allow the user to save their password
added a password field to the main panel to allow for easy password entry
added a connect button to the main panel

View File

@@ -1 +1 @@
1.1.3 b 48
1.1.4 b 49