added export feature to search, so we can save *.cvs files to be used as reports
This commit is contained in:
BIN
Order Processor/images/floppy_disc.png
Normal file
BIN
Order Processor/images/floppy_disc.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.3 KiB |
@@ -80,6 +80,10 @@ public class OrderPane extends JScrollPane {
|
||||
updateTextPane("Total sold: " + total, TextStyle.BOLD);
|
||||
}
|
||||
|
||||
public String getText(){
|
||||
return mOutput.getText();
|
||||
}
|
||||
|
||||
/**
|
||||
* adds the following string to the textpane
|
||||
*
|
||||
|
||||
@@ -9,10 +9,10 @@ package com.TwentyCodes.java.OrderProcessor.UI;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.awt.event.ItemListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
@@ -20,6 +20,7 @@ import java.util.ArrayList;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
@@ -28,6 +29,7 @@ import javax.swing.JTextField;
|
||||
|
||||
import com.TwentyCodes.java.OrderProcessor.Date;
|
||||
import com.TwentyCodes.java.OrderProcessor.DatePickerListener;
|
||||
import com.TwentyCodes.java.OrderProcessor.FileFilter;
|
||||
import com.TwentyCodes.java.OrderProcessor.InvalidDateFormatException;
|
||||
import com.TwentyCodes.java.OrderProcessor.Main;
|
||||
import com.TwentyCodes.java.OrderProcessor.Order;
|
||||
@@ -37,7 +39,7 @@ import com.TwentyCodes.java.OrderProcessor.ProgressListener;
|
||||
* This class will be the dialog that will ask the user for a specific make to show
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class SearchDialog extends JFrame implements ActionListener, ProgressListener, MouseListener, DatePickerListener, ItemListener {
|
||||
public class SearchDialog extends JFrame implements ActionListener, ProgressListener, MouseListener, DatePickerListener {
|
||||
|
||||
private static final long serialVersionUID = 1750326106927701404L;
|
||||
private static final int END_DATE_RESULT = 1;
|
||||
@@ -50,8 +52,10 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
private OrderPane mOrderPanel;
|
||||
private JTextField mStartDateField;
|
||||
private JTextField mEndDateField;
|
||||
private boolean isExclusive = false;
|
||||
private JCheckBox mExclusiveCheckBox;
|
||||
private JButton mExportButton;
|
||||
private JFileChooser fc;
|
||||
private JCheckBox fcCheckBox;
|
||||
|
||||
/**
|
||||
* Creates a new ShowAllMakeDialog
|
||||
@@ -77,7 +81,6 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
|
||||
//exclusive check box
|
||||
mExclusiveCheckBox = new JCheckBox("Exclusive Search");
|
||||
mExclusiveCheckBox.addItemListener(this);
|
||||
panel.add(mExclusiveCheckBox);
|
||||
|
||||
//start date
|
||||
@@ -99,6 +102,11 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
mOkButton.addActionListener(this);
|
||||
panel.add(mOkButton);
|
||||
|
||||
//export button
|
||||
mExportButton = new JButton("Export", new ImageIcon(getClass().getResource("/floppy_disc.png")));
|
||||
panel.add(mExportButton);
|
||||
mExportButton.addActionListener(this);
|
||||
|
||||
//progress bar
|
||||
mProgressBar = new JProgressBar();
|
||||
getContentPane().add(mProgressBar, BorderLayout.SOUTH);
|
||||
@@ -107,6 +115,13 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
mOrderPanel = new OrderPane();
|
||||
this.getContentPane().add(mOrderPanel, BorderLayout.CENTER);
|
||||
|
||||
//file chooser
|
||||
fc = new JFileChooser();
|
||||
fc.setFileFilter(new FileFilter());
|
||||
fcCheckBox = new JCheckBox("Automatically append extention");
|
||||
fcCheckBox.setSelected(true);
|
||||
fc.setAccessory(fcCheckBox);
|
||||
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
@@ -119,16 +134,54 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
*/
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(! mStartDateField.getText().isEmpty() && !mEndDateField.getText().isEmpty())
|
||||
try {
|
||||
preformSearch(mTextField.getText(), new Date(mStartDateField.getText()), new Date(mEndDateField.getText()));
|
||||
} catch (InvalidDateFormatException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
if(e.getSource() == mExportButton)
|
||||
exportToCSV();
|
||||
else
|
||||
preformSearch(mTextField.getText(), null, null);
|
||||
if(! mStartDateField.getText().isEmpty() && !mEndDateField.getText().isEmpty())
|
||||
try {
|
||||
preformSearch(mTextField.getText(), new Date(mStartDateField.getText()), new Date(mEndDateField.getText()));
|
||||
} catch (InvalidDateFormatException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
else
|
||||
preformSearch(mTextField.getText(), null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* displays a JFileChooser dialog, and saves the current search results to an *.CSV file
|
||||
* @return true is save was successfull
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private boolean exportToCSV() {
|
||||
if(fc.showDialog(SearchDialog.this, "Export") == JFileChooser.APPROVE_OPTION){
|
||||
StringBuffer sb = new StringBuffer("Order Number, Time & date, Product Name, Order Amount, Amount Charged, Financial Status, Fulfillment Status, Customer Name, Customer Email");
|
||||
sb.append(mOrderPanel.getText());
|
||||
|
||||
try {
|
||||
FileOutputStream theFile;
|
||||
if (fcCheckBox.isSelected())
|
||||
theFile = new FileOutputStream(new File(fc.getSelectedFile().toString()+".csv"));
|
||||
else
|
||||
theFile = new FileOutputStream(fc.getSelectedFile());
|
||||
theFile.write(sb.toString().getBytes());
|
||||
theFile.flush();
|
||||
theFile.close();
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* executes a concurrent database search
|
||||
* @param search string
|
||||
* @param start date
|
||||
* @param end date
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private void preformSearch(final String search, final Date start, final Date end) {
|
||||
java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() {
|
||||
public void run() {
|
||||
@@ -139,9 +192,9 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
ArrayList<Order> list = null;
|
||||
try {
|
||||
if((start != null) && (end != null)){
|
||||
list = MainWindow.db.search(search, start, end, isExclusive);
|
||||
list = MainWindow.db.search(search, start, end, mExclusiveCheckBox.isSelected());
|
||||
} else{
|
||||
list = MainWindow.db.search(search, isExclusive);
|
||||
list = MainWindow.db.search(search, mExclusiveCheckBox.isSelected());
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
@@ -180,8 +233,6 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
if(Main.DEBUG)
|
||||
System.out.println("search progress = "+ progress);
|
||||
|
||||
|
||||
|
||||
progress++;
|
||||
if(mProgressBar.isIndeterminate()) {
|
||||
mProgressBar.setIndeterminate(false);
|
||||
@@ -200,7 +251,12 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* called when either of the date fields are clicked
|
||||
* (non-Javadoc)
|
||||
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if(e.getSource() == mEndDateField){
|
||||
@@ -240,6 +296,12 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* called when the date picker returns a vaid date
|
||||
* (non-Javadoc)
|
||||
* @see com.TwentyCodes.java.OrderProcessor.DatePickerListener#onDatePicked(int, com.TwentyCodes.java.OrderProcessor.Date)
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public void onDatePicked(int resultCode, Date date) {
|
||||
switch(resultCode){
|
||||
@@ -252,16 +314,4 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void itemStateChanged(ItemEvent e) {
|
||||
if(e.getSource() == mExclusiveCheckBox)
|
||||
if(e.getStateChange() == java.awt.event.ItemEvent.SELECTED)
|
||||
isExclusive = true;
|
||||
if(e.getStateChange() == java.awt.event.ItemEvent.DESELECTED)
|
||||
isExclusive = false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user