I added a progress bar to the search dialog, and updated comments
This commit is contained in:
@@ -78,12 +78,11 @@ public class OrderDB {
|
|||||||
try {
|
try {
|
||||||
addOrder(new Order(scan.nextLine()));
|
addOrder(new Order(scan.nextLine()));
|
||||||
} catch (OrderExistsException e) {
|
} catch (OrderExistsException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
} catch (NumberFormatException e1){
|
} catch (NumberFormatException e1){
|
||||||
e1.printStackTrace();
|
// e1.printStackTrace();
|
||||||
} catch (InvalidDateFormatException e2) {
|
} catch (InvalidDateFormatException e2) {
|
||||||
// TODO Auto-generated catch block
|
// e2.printStackTrace();
|
||||||
e2.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,9 +107,11 @@ public class OrderDB {
|
|||||||
*/
|
*/
|
||||||
public ArrayList<Order> search(String text) {
|
public ArrayList<Order> search(String text) {
|
||||||
ArrayList<Order> list = new ArrayList<Order>();
|
ArrayList<Order> list = new ArrayList<Order>();
|
||||||
for(Order item : array){
|
for(int i = 0; i < array.size(); i++){
|
||||||
if(item.getItemName().equalsIgnoreCase(text))
|
if(array.get(i).getItemName().equalsIgnoreCase(text)){
|
||||||
list.add(item);
|
list.add(array.get(i));
|
||||||
|
mListener.onProgressUpdate(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@@ -124,5 +125,14 @@ public class OrderDB {
|
|||||||
mListener = listener;
|
mListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the size of the data base
|
||||||
|
* @return
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public int getSize() {
|
||||||
|
return array.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -37,6 +37,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
|
|||||||
private static final long serialVersionUID = 1841715561053331517L;
|
private static final long serialVersionUID = 1841715561053331517L;
|
||||||
public static final boolean DEBUG = false;
|
public static final boolean DEBUG = false;
|
||||||
private JProgressBar mProgressBar;
|
private JProgressBar mProgressBar;
|
||||||
|
private String mCurrentFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new MainWindow
|
* Creates a new MainWindow
|
||||||
@@ -92,14 +93,29 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
|
|||||||
new SearchDialog();
|
new SearchDialog();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepares the progress bar, and loads the file using an executer to prevent UI hangups
|
||||||
|
*/
|
||||||
if(e.getSource() == mLoadFileButton){
|
if(e.getSource() == mLoadFileButton){
|
||||||
try {
|
try {
|
||||||
if(fc.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION)
|
if(fc.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION)
|
||||||
|
db.setOnProgressListerner(this);
|
||||||
mProgressBar.setIndeterminate(true);
|
mProgressBar.setIndeterminate(true);
|
||||||
mProgressBar.setString(fc.getSelectedFile().toString());
|
mCurrentFile = fc.getSelectedFile().toString();
|
||||||
|
mProgressBar.setString(mCurrentFile);
|
||||||
mProgressBar.setStringPainted(true);
|
mProgressBar.setStringPainted(true);
|
||||||
mProgressBar.setMaximum(countLines(fc.getSelectedFile()));
|
mProgressBar.setMaximum(countLines(fc.getSelectedFile())+1);
|
||||||
db.load(fc.getSelectedFile());
|
|
||||||
|
java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
db.load(fc.getSelectedFile());
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
pack();
|
pack();
|
||||||
|
|
||||||
} catch (FileNotFoundException e1) {
|
} catch (FileNotFoundException e1) {
|
||||||
@@ -122,10 +138,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
|
|||||||
public int countLines(File file) throws IOException {
|
public int countLines(File file) throws IOException {
|
||||||
LineNumberReader reader = new LineNumberReader(new FileReader(file));
|
LineNumberReader reader = new LineNumberReader(new FileReader(file));
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
String lineRead = "";
|
while ((reader.readLine()) != null) ;
|
||||||
while ((lineRead = reader.readLine()) != null) {
|
|
||||||
}
|
|
||||||
|
|
||||||
cnt = reader.getLineNumber();
|
cnt = reader.getLineNumber();
|
||||||
reader.close();
|
reader.close();
|
||||||
return cnt;
|
return cnt;
|
||||||
@@ -140,9 +153,13 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onProgressUpdate(int progress) {
|
public void onProgressUpdate(int progress) {
|
||||||
if(mProgressBar.isIndeterminate())
|
progress++;
|
||||||
mProgressBar.setIndeterminate(false);
|
|
||||||
mProgressBar.setValue(progress);
|
mProgressBar.setValue(progress);
|
||||||
|
mProgressBar.setString(" "+mCurrentFile + " ~ Parsing Order: "+progress+" of "+mProgressBar.getMaximum()+" ");
|
||||||
|
if(mProgressBar.isIndeterminate()) {
|
||||||
|
mProgressBar.setIndeterminate(false);
|
||||||
|
pack();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,11 +10,13 @@ import java.awt.BorderLayout;
|
|||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JProgressBar;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
import javax.swing.JTextField;
|
import javax.swing.JTextField;
|
||||||
import javax.swing.JTextPane;
|
import javax.swing.JTextPane;
|
||||||
@@ -25,18 +27,22 @@ import javax.swing.text.StyleContext;
|
|||||||
import javax.swing.text.StyledDocument;
|
import javax.swing.text.StyledDocument;
|
||||||
|
|
||||||
import com.TwentyCodes.java.OrderProcessor.Order;
|
import com.TwentyCodes.java.OrderProcessor.Order;
|
||||||
|
import com.TwentyCodes.java.OrderProcessor.ProgressListener;
|
||||||
import com.TwentyCodes.java.OrderProcessor.Status;
|
import com.TwentyCodes.java.OrderProcessor.Status;
|
||||||
import com.TwentyCodes.java.OrderProcessor.TextStyle;
|
import com.TwentyCodes.java.OrderProcessor.TextStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class will be the dialog that will ask the user for a specific make to show
|
* This class will be the dialog that will ask the user for a specific make to show
|
||||||
*/
|
*/
|
||||||
public class SearchDialog extends JFrame implements ActionListener {
|
public class SearchDialog extends JFrame implements ActionListener, ProgressListener {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1750326106927701404L;
|
private static final long serialVersionUID = 1750326106927701404L;
|
||||||
private JButton ok;
|
private JButton mOkButton;
|
||||||
private JTextPane mOutput;
|
private JTextPane mOutput;
|
||||||
private JTextField textField;
|
private JTextField mTextField;
|
||||||
|
private JProgressBar mProgressBar;
|
||||||
|
private boolean isProcessing = false;
|
||||||
|
private int mListSize;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new ShowAllMakeDialog
|
* Creates a new ShowAllMakeDialog
|
||||||
@@ -47,23 +53,27 @@ public class SearchDialog extends JFrame implements ActionListener {
|
|||||||
|
|
||||||
//create a JPanel to hold the text area and button
|
//create a JPanel to hold the text area and button
|
||||||
JPanel panel = new JPanel();
|
JPanel panel = new JPanel();
|
||||||
ok = new JButton("Ok");
|
mOkButton = new JButton("Ok");
|
||||||
JLabel label = new JLabel("Enter a Product name:");
|
JLabel label = new JLabel("Enter a Product name:");
|
||||||
ok.addActionListener(this);
|
mOkButton.addActionListener(this);
|
||||||
|
|
||||||
//add the JPanel to the frame, and display
|
//add the JPanel to the frame, and display
|
||||||
getContentPane().add(panel, BorderLayout.NORTH);
|
getContentPane().add(panel, BorderLayout.NORTH);
|
||||||
|
|
||||||
textField = new JTextField();
|
mTextField = new JTextField();
|
||||||
textField.setColumns(10);
|
mTextField.setColumns(10);
|
||||||
|
mTextField.addActionListener(this);
|
||||||
|
|
||||||
panel.add(label);
|
panel.add(label);
|
||||||
panel.add(textField);
|
panel.add(mTextField);
|
||||||
panel.add(ok);
|
panel.add(mOkButton);
|
||||||
|
|
||||||
JScrollPane scrollPane = new JScrollPane();
|
JScrollPane scrollPane = new JScrollPane();
|
||||||
getContentPane().add(scrollPane, BorderLayout.CENTER);
|
getContentPane().add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
mProgressBar = new JProgressBar();
|
||||||
|
getContentPane().add(mProgressBar, BorderLayout.SOUTH);
|
||||||
|
|
||||||
mOutput = new JTextPane();
|
mOutput = new JTextPane();
|
||||||
mOutput.setEditable(false);
|
mOutput.setEditable(false);
|
||||||
scrollPane.setViewportView(mOutput);
|
scrollPane.setViewportView(mOutput);
|
||||||
@@ -78,36 +88,54 @@ public class SearchDialog extends JFrame implements ActionListener {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (e.getSource() == ok) {
|
|
||||||
mOutput.setText(null);
|
|
||||||
float possible = 0;
|
|
||||||
float actual = 0;
|
|
||||||
int total = 0;
|
|
||||||
for(Order item : MainWindow.db.search(textField.getText())){
|
|
||||||
|
|
||||||
switch(item.getFinancialStatus()){
|
java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() {
|
||||||
case CANCELLED:
|
public void run() {
|
||||||
updateTextPane(item.toString(), TextStyle.RED);
|
mProgressBar.setString("Searching for: "+mTextField.getText());
|
||||||
break;
|
mProgressBar.setStringPainted(true);
|
||||||
case CANCELLED_BY_GOOGLE:
|
mProgressBar.setMaximum(MainWindow.db.getSize());
|
||||||
updateTextPane(item.toString(), TextStyle.RED);
|
MainWindow.db.setOnProgressListerner(SearchDialog.this);
|
||||||
break;
|
mOutput.setText(null);
|
||||||
default:
|
float possible = 0;
|
||||||
updateTextPane(item.toString(), TextStyle.REGULAR);
|
float actual = 0;
|
||||||
|
int total = 0;
|
||||||
|
ArrayList<Order> list = MainWindow.db.search(mTextField.getText());
|
||||||
|
mListSize = list.size();
|
||||||
|
|
||||||
|
mProgressBar.setMaximum(MainWindow.db.getSize() + mListSize);
|
||||||
|
isProcessing = true;
|
||||||
|
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++;
|
||||||
}
|
}
|
||||||
|
|
||||||
possible = possible + item.getOrderAmount();
|
updateTextPane("Possible sales: $"+possible, TextStyle.BOLD);
|
||||||
actual = actual + item.getAmountCharged();
|
updateTextPane("Actual sales: $"+actual, TextStyle.BOLD);
|
||||||
if(item.getFulfillmentStatus() == Status.DELIVERED)
|
updateTextPane("Possible sold:"+ list.size(), TextStyle.BOLD);
|
||||||
total++;
|
updateTextPane("Total sold: "+total, TextStyle.BOLD);
|
||||||
|
isProcessing = false;
|
||||||
|
|
||||||
|
pack();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
updateTextPane("Possible sales: $"+possible, TextStyle.BOLD);
|
|
||||||
updateTextPane("Actual sales: $"+actual, TextStyle.BOLD);
|
|
||||||
updateTextPane("Total sold: "+total, TextStyle.BOLD);
|
|
||||||
|
|
||||||
pack();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -161,4 +189,25 @@ public class SearchDialog extends JFrame implements ActionListener {
|
|||||||
StyleConstants.setForeground(s, Color.GREEN);
|
StyleConstants.setForeground(s, Color.GREEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* called by MainWindow.db on progress update, or internally from this window to update the progress bar
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see com.TwentyCodes.java.OrderProcessor.ProgressListener#onProgressUpdate(int)
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onProgressUpdate(int progress) {
|
||||||
|
progress++;
|
||||||
|
if(mProgressBar.isIndeterminate()) {
|
||||||
|
mProgressBar.setIndeterminate(false);
|
||||||
|
pack();
|
||||||
|
}
|
||||||
|
if(isProcessing){
|
||||||
|
mProgressBar.setString("Processing Order: "+ mListSize +" of "+ progress);
|
||||||
|
mProgressBar.setValue(progress + MainWindow.db.getSize()+1);
|
||||||
|
} else
|
||||||
|
mProgressBar.setValue(progress);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user