i added the required lib for SQL

i created OrderPane to be used in any frame that will display orders.
This commit is contained in:
2011-01-09 18:21:50 +00:00
parent 9070a0b21d
commit da038f97bb
7 changed files with 215 additions and 110 deletions

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="/home/ricky/Downloads/sqlitejdbc-v056.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/sqlitejdbc-v056.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

Binary file not shown.

View File

@@ -61,7 +61,7 @@ public class OrderDB {
try {
stat.executeUpdate(CREATE_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
//most likely the table already exist
e.printStackTrace();
}
conn.close();
@@ -104,14 +104,17 @@ public class OrderDB {
*/
public ArrayList<Order> getAllOrders() throws SQLException, IOException, ClassNotFoundException {
int row = 1;
Connection conn = getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from Orders;");
ArrayList<Order> list = new ArrayList<Order>();
ByteArrayInputStream bais;
ObjectInputStream ins;
while (rs.next()) {
if(mListener != null)
mListener.onProgressUpdate(row++);
bais = new ByteArrayInputStream(rs.getBytes("item"));
ins = new ObjectInputStream(bais);
list.add((Order) ins.readObject());

View File

@@ -93,7 +93,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == mShowAllButton){
new ShowAllDialog(db.toString());
new ShowAllDialog();
}
if (e.getSource() == mSearchButton) {

View File

@@ -0,0 +1,145 @@
/**
* OrderPanel.java
* @date Jan 9, 2011
* @author ricky barrette
* @author Twenty Codes, LLC
*/
package com.TwentyCodes.java.OrderProcessor.UI;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import com.TwentyCodes.java.OrderProcessor.Order;
import com.TwentyCodes.java.OrderProcessor.ProgressListener;
import com.TwentyCodes.java.OrderProcessor.Status;
import com.TwentyCodes.java.OrderProcessor.TextStyle;
/**
* a pane to display orders in color code.
* @author ricky barrette
*/
public class OrderPane extends JScrollPane {
private static final long serialVersionUID = -8762146025822173375L;
private JTextPane mOutput;
private ProgressListener mListener;
/**
* Creates a new OrderPanel
* @author ricky barrette
*/
public OrderPane() {
mOutput = new JTextPane();
mOutput.setEditable(false);
setViewportView(mOutput);
}
/**
* displays orders
*
* @param list
* of orders to display
* @author ricky barrette
*/
public void displayOrders(ArrayList<Order> list) {
mOutput.setText(null);
float possible = 0;
float actual = 0;
int total = 0;
for (int i = 0; i < list.size(); i++) {
if(mListener != null)
mListener.onProgressUpdate(i);
switch (list.get(i).getFinancialStatus()) {
case CANCELLED:
updateTextPane(list.get(i).toString(), TextStyle.RED);
break;
case CANCELLED_BY_GOOGLE:
updateTextPane(list.get(i).toString(), TextStyle.RED);
break;
default:
updateTextPane(list.get(i).toString(), TextStyle.REGULAR);
}
possible = possible + list.get(i).getOrderAmount();
actual = actual + list.get(i).getAmountCharged();
if (list.get(i).getFulfillmentStatus() == Status.DELIVERED)
total++;
}
updateTextPane("Possible sales: $" + possible, TextStyle.BOLD);
updateTextPane("Actual sales: $" + actual, TextStyle.BOLD);
updateTextPane("Possible sold:" + list.size(), TextStyle.BOLD);
updateTextPane("Total sold: " + total, TextStyle.BOLD);
}
/**
* adds the following string to the textpane
*
* @param string
* @author ricky barrette
*/
private void updateTextPane(String string, TextStyle style) {
String[] initString = { "\n" + string };
String[] initStyles = { style.toString() };
StyledDocument doc = mOutput.getStyledDocument();
addStylesToDocument(doc);
// Load the text pane with styled text.
try {
for (int i = 0; i < initString.length; i++) {
doc.insertString(doc.getLength(), initString[i],
doc.getStyle(initStyles[i]));
}
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane.");
}
mOutput.setDocument(doc);
}
/**
* adds supported styles to the document
*
* @param doc
* @author ricky barrette
*/
protected void addStylesToDocument(StyledDocument doc) {
// Initialize some styles.
Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("REGULAR", def);
StyleConstants.setFontFamily(def, "Arial");
StyleConstants.setFontSize(def, 14);
Style s = doc.addStyle("ITALIC", regular);
StyleConstants.setItalic(s, true);
s = doc.addStyle("BOLD", regular);
StyleConstants.setBold(s, true);
s = doc.addStyle("RED", regular);
StyleConstants.setForeground(s, Color.RED);
s = doc.addStyle("GREEN", regular);
StyleConstants.setForeground(s, Color.GREEN);
}
/**
* sets a listener for db progress updates
*
* @param listener
* @author ricky barrette
*/
public void setOnProgressListerner(ProgressListener listener) {
mListener = listener;
}
}

View File

@@ -7,7 +7,6 @@
package com.TwentyCodes.java.OrderProcessor.UI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
@@ -19,20 +18,11 @@ import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import com.TwentyCodes.java.OrderProcessor.Main;
import com.TwentyCodes.java.OrderProcessor.Order;
import com.TwentyCodes.java.OrderProcessor.ProgressListener;
import com.TwentyCodes.java.OrderProcessor.Status;
import com.TwentyCodes.java.OrderProcessor.TextStyle;
/**
* This class will be the dialog that will ask the user for a specific make to show
@@ -41,11 +31,11 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
private static final long serialVersionUID = 1750326106927701404L;
private JButton mOkButton;
private JTextPane mOutput;
private JTextField mTextField;
private JProgressBar mProgressBar;
private boolean isProcessing = false;
private int mListSize;
private OrderPane mOrderPanel;
/**
* Creates a new ShowAllMakeDialog
@@ -63,6 +53,9 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
//add the JPanel to the frame, and display
getContentPane().add(panel, BorderLayout.NORTH);
mOrderPanel = new OrderPane();
this.getContentPane().add(mOrderPanel, BorderLayout.CENTER);
mTextField = new JTextField();
mTextField.setColumns(20);
mTextField.addActionListener(this);
@@ -71,15 +64,10 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
panel.add(mTextField);
panel.add(mOkButton);
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
mProgressBar = new JProgressBar();
getContentPane().add(mProgressBar, BorderLayout.SOUTH);
mOutput = new JTextPane();
mOutput.setEditable(false);
scrollPane.setViewportView(mOutput);
pack();
setVisible(true);
}
@@ -102,10 +90,7 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
e1.printStackTrace();
}
MainWindow.db.setOnProgressListerner(SearchDialog.this);
mOutput.setText(null);
float possible = 0;
float actual = 0;
int total = 0;
ArrayList<Order> list = null;
try {
list = MainWindow.db.search(mTextField.getText());
@@ -123,29 +108,10 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
mProgressBar.setString("Processing Order: "+ 1 +" of "+ mListSize);
pack();
for(int i = 0; i < list.size(); i++){
onProgressUpdate(i);
switch(list.get(i).getFinancialStatus()){
case CANCELLED:
updateTextPane(list.get(i).toString(), TextStyle.RED);
break;
case CANCELLED_BY_GOOGLE:
updateTextPane(list.get(i).toString(), TextStyle.RED);
break;
default:
updateTextPane(list.get(i).toString(), TextStyle.REGULAR);
}
possible = possible + list.get(i).getOrderAmount();
actual = actual + list.get(i).getAmountCharged();
if(list.get(i).getFulfillmentStatus() == Status.DELIVERED)
total++;
}
mOrderPanel.setOnProgressListerner(SearchDialog.this);
mOrderPanel.displayOrders(list);
updateTextPane("Possible sales: $"+possible, TextStyle.BOLD);
updateTextPane("Actual sales: $"+actual, TextStyle.BOLD);
updateTextPane("Possible sold:"+ list.size(), TextStyle.BOLD);
updateTextPane("Total sold: "+total, TextStyle.BOLD);
//todo updates order panel
isProcessing = false;
pack();
@@ -154,56 +120,7 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
}
/**
* adds the folloing string to the textpane
* @param string
* @author ricky barrette
*/
private void updateTextPane(String string, TextStyle style) {
String[] initString = {"\n"+ string };
String[] initStyles = { style.toString() };
StyledDocument doc = mOutput.getStyledDocument();
addStylesToDocument(doc);
// Load the text pane with styled text.
try {
for (int i = 0; i < initString.length; i++) {
doc.insertString(doc.getLength(), initString[i],
doc.getStyle(initStyles[i]));
}
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane.");
}
mOutput.setDocument(doc);
}
/**
* adds supported styles to the document
* @param doc
* @author ricky barrette
*/
protected void addStylesToDocument(StyledDocument doc) {
//Initialize some styles.
Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("REGULAR", def);
StyleConstants.setFontFamily(def, "Arial");
StyleConstants.setFontSize(def, 14);
Style s = doc.addStyle("ITALIC", regular);
StyleConstants.setItalic(s, true);
s = doc.addStyle("BOLD", regular);
StyleConstants.setBold(s, true);
s = doc.addStyle("RED", regular);
StyleConstants.setForeground(s, Color.RED);
s = doc.addStyle("GREEN", regular);
StyleConstants.setForeground(s, Color.GREEN);
}
/**

View File

@@ -9,27 +9,67 @@ package com.TwentyCodes.java.OrderProcessor.UI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.TwentyCodes.java.OrderProcessor.ProgressListener;
/**
* This panel will be used to display all Vehicles in the VechicleDB
*/
public class ShowAllDialog extends JFrame implements ActionListener {
public class ShowAllDialog extends JFrame implements ActionListener, ProgressListener {
private static final long serialVersionUID = -8416144493079733535L;
private JProgressBar mProgressBar;
/**
* Creates a new ShowAllDialog
*/
public ShowAllDialog(final String list){
public ShowAllDialog(){
super();
setTitle("Show All Orders");
JButton ok = new JButton("Ok");
ok.addActionListener(this);
final OrderPane output = new OrderPane();
mProgressBar = new JProgressBar();
getContentPane().add(mProgressBar, BorderLayout.SOUTH);
this.getContentPane().add(output, BorderLayout.CENTER);
this.getContentPane().add(ok, BorderLayout.NORTH);
try {
mProgressBar.setMaximum(MainWindow.db.getCount());
} catch (SQLException e1) {
e1.printStackTrace();
}
mProgressBar.setString("Loading from Database");
mProgressBar.setStringPainted(true);
MainWindow.db.setOnProgressListerner(this);
initialize(list);
java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() {
public void run() {
try {
output.displayOrders(MainWindow.db.getAllOrders());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
pack();
setVisible(true);
}
/**
@@ -39,19 +79,9 @@ public class ShowAllDialog extends JFrame implements ActionListener {
super();
setTitle(title);
initialize(body);
}
/**
* initializes the dialog's components
* @param body
* @author ricky barrette
*/
private void initialize(String body) {
JButton ok = new JButton("Ok");
ok.addActionListener(this);
JScrollPane scrollPane = new JScrollPane();
final JTextArea results = new JTextArea();
@@ -65,9 +95,19 @@ public class ShowAllDialog extends JFrame implements ActionListener {
pack();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
}
@Override
public void onProgressUpdate(int progress) {
mProgressBar.setValue(progress);
if(progress >= mProgressBar.getMaximum()){
this.pack();
mProgressBar.setString("Done");
}
}
}