From a3e2c7872f5bc4393c147d7e3b58ceaf9879c533 Mon Sep 17 00:00:00 2001 From: ricky barrette Date: Wed, 19 Jan 2011 18:42:40 +0000 Subject: [PATCH] i created a settings panel that uses a .property file to save settings. i modified OrderDB to use the .property file --- Order Processor/.settings.propertys | 7 + .../java/OrderProcessor/DB/OrderDB.java | 60 +++-- .../java/OrderProcessor/UI/MainWindow.java | 2 + .../java/OrderProcessor/UI/SettingsPanel.java | 216 ++++++++++++++++++ Order Processor/version infomation/changelog | 9 +- Order Processor/version infomation/version | 2 +- 6 files changed, 276 insertions(+), 20 deletions(-) create mode 100644 Order Processor/.settings.propertys create mode 100644 Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SettingsPanel.java diff --git a/Order Processor/.settings.propertys b/Order Processor/.settings.propertys new file mode 100644 index 0000000..08497c5 --- /dev/null +++ b/Order Processor/.settings.propertys @@ -0,0 +1,7 @@ +#---No Comment--- +#Wed Jan 19 13:29:12 EST 2011 +server_parrword= +server_location=tcdevsvn1 +use_remote_server=true +server_port_number=3306 +server_user_name= diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java index 2f5b2b9..ed1f0ef 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/DB/OrderDB.java @@ -23,6 +23,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.Collections; import java.util.Locale; +import java.util.Properties; import com.Ostermiller.util.CSVParser; import com.TwentyCodes.java.OrderProcessor.Date; @@ -31,6 +32,7 @@ import com.TwentyCodes.java.OrderProcessor.Main; import com.TwentyCodes.java.OrderProcessor.Order; import com.TwentyCodes.java.OrderProcessor.OrderExistsException; import com.TwentyCodes.java.OrderProcessor.ProgressListener; +import com.TwentyCodes.java.OrderProcessor.UI.SettingsPanel; /** * An SQLite Order Data Base @@ -46,6 +48,7 @@ public class OrderDB { private String dbLocation; private boolean isLoadingFile = false; private ArrayList list; + private boolean isUsingRemoteDatabase; /** * Creates a new OrderDB @@ -56,7 +59,12 @@ public class OrderDB { * @throws Exception */ public OrderDB() throws ClassNotFoundException, SQLException, SocketException { - if(Main.USE_REMOTE_DB) { + + Properties props = getProperties(); + + isUsingRemoteDatabase = Boolean.parseBoolean(props.getProperty(SettingsPanel.IS_USING_REMOTE_SERVER, "false")); + + if(isUsingRemoteDatabase) { Class.forName("com.mysql.jdbc.Driver"); } else { @@ -69,7 +77,7 @@ public class OrderDB { // stat.executeUpdate("drop table if exists Orders;"); try { - if(Main.USE_REMOTE_DB) + if(isUsingRemoteDatabase) stat.executeUpdate(CREATE_TABLE); else stat.executeUpdate(CREATE_TABLE_LOCAL); @@ -80,6 +88,24 @@ public class OrderDB { conn.close(); } + /** + * gets the properties from the properties file + * @return properties + * @author ricky barrette + */ + private Properties getProperties() { + Properties props = new java.util.Properties(); + try { + FileInputStream in = new FileInputStream(".settings.propertys"); + props.load(in); + in.close(); + return props; + } catch (IOException e1) { + e1.printStackTrace(); + } + return props; + } + /** * counts the number of lines in a csv file * @param file to be read @@ -161,8 +187,20 @@ public class OrderDB { * @author ricky barrette */ private Connection getConnection() throws SQLException, SocketException{ - if(Main.USE_REMOTE_DB) - return DriverManager.getConnection("jdbc:mysql://tcdevsvn1/Orders"); + Properties props = getProperties(); + + if(isUsingRemoteDatabase){ + String url = "jdbc:mysql://"+props.getProperty(SettingsPanel.SERVER_LOCATION)+":"+props.getProperty(SettingsPanel.SERVER_PORT_NUMBER, "3306")+"/Orders"; + + if(props.getProperty(SettingsPanel.SERVER_USERNAME).length() > 0) + url += "?user="+ props.getProperty(SettingsPanel.SERVER_USERNAME); + + if(props.getProperty(SettingsPanel.SERVER_PASSWORD).length() > 0) + url += "&password="+ props.getProperty(SettingsPanel.SERVER_PASSWORD); + + return DriverManager.getConnection(url); + + } else return DriverManager.getConnection("jdbc:sqlite:"+ dbLocation+".db"); } @@ -283,20 +321,12 @@ public class OrderDB { int row = 1; int count = getCount(); -// Connection conn = getConnection(); -// Statement stat = conn.createStatement(); -// ResultSet rs = stat.executeQuery("select * from Orders;"); ArrayList list = new ArrayList(); String[] parts = text.split(", "); -// ByteArrayInputStream bais; -// ObjectInputStream ins; -// while (rs.next()) { + for(Order order : getAllOrders()){ if(mListener != null && ! isLoadingFile) mListener.onProgressUpdate("Searching for: "+text, false, row++, count); -// bais = new ByteArrayInputStream(rs.getBytes("item")); -// ins = new ObjectInputStream(bais); -// Order order = (Order) ins.readObject(); for (int i = 0; i < parts.length; i++) { if (isExclusive) { @@ -309,11 +339,7 @@ public class OrderDB { || order.getCustomerContry().toLowerCase(Locale.ENGLISH).contains(parts[i].toLowerCase(Locale.ENGLISH))) list.add(order); } -// ins.close(); -// bais.close(); } -// rs.close(); -// conn.close(); Collections.sort(list); return list; diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java index 5748cd6..5f0274d 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java @@ -66,6 +66,8 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen tabbedPane.addTab("Main", initializeMainPanel()); tabbedPane.addTab("Search Orders", new SearchPanel(this)); tabbedPane.addTab("Show All Orders", new ShowAllPanel(this)); + tabbedPane.addTab("Settings", new SettingsPanel()); + this.getContentPane().add(tabbedPane, BorderLayout.CENTER); diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SettingsPanel.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SettingsPanel.java new file mode 100644 index 0000000..c866fd2 --- /dev/null +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SettingsPanel.java @@ -0,0 +1,216 @@ +/** + * SettingsPanel.java + * @date Jan 19, 2011 + * @author ricky barrette + * @author Twenty Codes, LLC + */ +package com.TwentyCodes.java.OrderProcessor.UI; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Properties; + +import javax.swing.GroupLayout; +import javax.swing.GroupLayout.Alignment; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JPasswordField; +import javax.swing.JTextField; +import javax.swing.LayoutStyle.ComponentPlacement; + +/** + * this panel will be used to display and handle application settings + * @author ricky barrette + */ +public class SettingsPanel extends JPanel implements ActionListener { + + private static final long serialVersionUID = 2568209868143577096L; + public static final String IS_USING_REMOTE_SERVER = "use_remote_server"; + public static final String SERVER_LOCATION = "server_location"; + 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"; + private JTextField mServerLocation; + private JTextField mPortNumber; + private JTextField mUserName; + private JPasswordField mUserPassword; + private boolean isUsingRemoteServer; + private JButton mSaveButton; + private JCheckBox mUseRemoteDatabase; + private Properties mProps; + + /** + * Creates a new SettingsPanel + * @author ricky barrette + */ + public SettingsPanel() { + + JLabel lblMysqlServerLocation = new JLabel("MYsql server location:"); + + mServerLocation = new JTextField(); + mServerLocation.setColumns(10); + + mUseRemoteDatabase = new JCheckBox("Use Remote Database?"); + mUseRemoteDatabase.addActionListener(this); + + JLabel lblMysqlServerPort = new JLabel("MYsql server port:"); + + mPortNumber = new JTextField(); + mPortNumber.setColumns(10); + mPortNumber.setText("3306"); + + JLabel lblMysql = new JLabel("MYsql serevr username:"); + + JLabel lblMysqlServerPassword = new JLabel("MYsql server password:"); + + mUserName = new JTextField(); + mUserName.setColumns(10); + + mUserPassword = new JPasswordField(); + mUserPassword.setColumns(10); + + mServerLocation.setEnabled(false); + mPortNumber.setEnabled(false); + mUserName.setEnabled(false); + mUserPassword.setEnabled(false); + + mSaveButton = new JButton("Save"); + mSaveButton.addActionListener(this); + + GroupLayout groupLayout = new GroupLayout(this); + groupLayout.setHorizontalGroup( + groupLayout.createParallelGroup(Alignment.LEADING) + .addGroup(groupLayout.createSequentialGroup() + .addContainerGap() + .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) + .addGroup(groupLayout.createSequentialGroup() + .addComponent(mUseRemoteDatabase) + .addContainerGap()) + .addGroup(groupLayout.createSequentialGroup() + .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) + .addComponent(lblMysqlServerLocation) + .addComponent(lblMysqlServerPort) + .addComponent(lblMysql) + .addComponent(lblMysqlServerPassword)) + .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)))) + ); + groupLayout.setVerticalGroup( + groupLayout.createParallelGroup(Alignment.LEADING) + .addGroup(groupLayout.createSequentialGroup() + .addContainerGap() + .addComponent(mUseRemoteDatabase) + .addPreferredGap(ComponentPlacement.RELATED) + .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) + .addComponent(lblMysqlServerLocation) + .addComponent(mServerLocation, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(ComponentPlacement.RELATED) + .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) + .addComponent(lblMysqlServerPort) + .addComponent(mPortNumber, 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)) + .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)) + ); + setLayout(groupLayout); + + // create and load default properties + mProps = new Properties(); + + + + mProps = new java.util.Properties(); + try { + FileInputStream in = new FileInputStream(".settings.propertys"); + mProps.load(in); + in.close(); + isUsingRemoteServer = Boolean.parseBoolean(mProps.getProperty(IS_USING_REMOTE_SERVER, "false")); + + if(isUsingRemoteServer){ + mServerLocation.setEnabled(true); + mPortNumber.setEnabled(true); + mUserName.setEnabled(true); + mUserPassword.setEnabled(true); + mUseRemoteDatabase.setSelected(true); + } + + mServerLocation.setText(mProps.getProperty(SERVER_LOCATION)); + mPortNumber.setText(mProps.getProperty(SERVER_PORT_NUMBER));; + mUserName.setText(mProps.getProperty(SERVER_USERNAME));; + + + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void actionPerformed(ActionEvent e) { + if(e.getSource() == mSaveButton){ + mProps.put(SERVER_LOCATION, mServerLocation.getText()); + mProps.put(SERVER_PORT_NUMBER, mPortNumber.getText()); + mProps.put(SERVER_USERNAME, mUserName.getText()); + mProps.put(SERVER_PASSWORD, 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(); + } + } + + 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) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + + if(mUseRemoteDatabase.isSelected()){ + mServerLocation.setEnabled(true); + mPortNumber.setEnabled(true); + mUserName.setEnabled(true); + mUserPassword.setEnabled(true); + } else { + mServerLocation.setEnabled(false); + mPortNumber.setEnabled(false); + mUserName.setEnabled(false); + mUserPassword.setEnabled(false); + } + } + + } +} \ No newline at end of file diff --git a/Order Processor/version infomation/changelog b/Order Processor/version infomation/changelog index ee717a7..8848248 100644 --- a/Order Processor/version infomation/changelog +++ b/Order Processor/version infomation/changelog @@ -9,6 +9,11 @@ added an overwrite file dialog. added the customer country to the order object. updated .CSV export -1.0.2 +1.0.2 b 30 -i replaced the old sqlite driver (for local databases) with the mySQL driver to interface with TCDEVSVN1's order database \ No newline at end of file +i replaced the old sqlite driver (for local databases) with the mySQL driver to interface with TCDEVSVN1's order database + +1.0.3 b 31 + +i created a settings panel that uses a .property file to save settings. +i modified OrderDB to use the .property file \ No newline at end of file diff --git a/Order Processor/version infomation/version b/Order Processor/version infomation/version index 081d67b..81a50b9 100644 --- a/Order Processor/version infomation/version +++ b/Order Processor/version infomation/version @@ -1 +1 @@ -1.0.2 b 30 \ No newline at end of file +1.0.3 b 31 \ No newline at end of file