// Philip Kao
// IB Comsci Dossier
// TreeClassics Program
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TreeClassics extends JFrame
{
//include aspects of GUI
private JTextField quantityField, thicknessField, widthField,
plysField, modelField, heightField, tipField, groupField;
// modelField, heightField, tipField, groupField have not been
// implemented yet; for panel 3
//
private JEditorPane scrollListContent;
private JTextArea enteredContent;
private JList enteredItemsList, loadedList;
// Labels for field input identifier
private JLabel quantityLabel, thicknessLabel,
widthLabel, colorLabel, plysLabel,
productionLabel, informLabel, specifyLabel,
plys1Label, color1Label, quantity1Label,
model1Label, thickness1Label, width1Label,
helper1Label, helper2Label, helper3Label,
helper4Label, helper5Label;
// The three tab panels
private JPanel panel1, panel2, panel3;
// Combobox selection system
private JComboBox modelComboBox, colorComboBox;
private JScrollPane orderScrollPane, enteredScrollPane;
private JButton
loadModelsButton, enterButton, removeTempItemsButton,
saveListButton, deleteModelsButton, modelInfoButton,
modelButton, thicknessButton, widthButton, colorButton,
loadListButton;
private javax.swing.JButton clearListButton, // NOTE: saveModelB, loadModelB, delModelB pertain to the
saveModelB, loadModelB, delModelB;
private Vector<Model> modelList;
private String[] colors = {"Green", "Teal", "Dark Blue", "White", "Gray"};
private OrderList prodList;
private TreeClassics()
{
// GUI Setup for program
super("Tree Classics Production Line");
this.prodList = new OrderList();
this.modelList = new Vector<Model>();
// Creating JTabbed Pane
JTabbedPane tabbedPane = new JTabbedPane();
// -------------------------PANEL 1 INTERFACE---------------------------
// Set up Panel 1
JLabel label1 = new JLabel();
panel1 = new JPanel( null );
panel1.add( label1 );
tabbedPane.addTab( " Production Order ",
null, panel1, "Production orders are created here");
// Model number selection label
productionLabel = new JLabel( "Model Number");
productionLabel.setBounds( 20, 20, 100, 20 );
panel1.add( productionLabel );
// Add Combo Box into Panel 1: PRODUCTION ORDER
modelComboBox = new JComboBox();
modelComboBox.setBounds(20, 40, 150, 20);
panel1.add(modelComboBox);
// Add Help Button into Panel1
modelInfoButton = new JButton( "Model Information" );
modelInfoButton.setBounds( 20, 70, 150, 20);
panel1.add(modelInfoButton);
// Titles for Selection and treeData types
// Quantity
quantityLabel = new JLabel( "Quantity");
quantityLabel.setBounds( 250, 20, 100, 20 );
panel1.add(quantityLabel);
// Thickness
thicknessLabel = new JLabel( "Thickness");
thicknessLabel.setBounds( 250, 75, 100, 20 );
panel1.add(thicknessLabel);
// Width
widthLabel = new JLabel( "Width");
widthLabel.setBounds( 370, 20, 100, 20 );
panel1.add(widthLabel);
// Color
colorLabel = new JLabel( "Color");
colorLabel.setBounds( 490, 20, 100, 20 );
panel1.add(colorLabel);
// Plys
plysLabel = new JLabel( "Plys");
plysLabel.setBounds( 370, 75, 100, 20 );
panel1.add( plysLabel );
// Color 1 Label
color1Label = new JLabel( "Color");
color1Label.setBounds( 320, 130, 100, 20 );
panel1.add(color1Label);
// Models 1 Label
model1Label = new JLabel( "Model Number");
model1Label.setBounds( 20, 130, 100, 20 );
panel1.add(model1Label);
// quantity 1 Label
quantity1Label = new JLabel( "Quantity");
quantity1Label.setBounds( 120, 130, 100, 20 );
panel1.add(quantity1Label);
// thickness 1 Label
thickness1Label = new JLabel( "Thickness");
thickness1Label.setBounds( 185, 130, 100, 20 );
panel1.add(thickness1Label);
// width 1 Label
width1Label = new JLabel( "Width");
width1Label.setBounds( 260, 130, 100, 20 );
panel1.add(width1Label);
// plys 1 Label
plys1Label = new JLabel( "Plys");
plys1Label.setBounds( 370, 130, 100, 20 );
panel1.add(plys1Label);
// Quantity field input
quantityField = new JTextField();
quantityField = new JTextField("0", 15 );
quantityField.setBounds( 250, 40 , 100, 20);
panel1.add( quantityField );
// Width field input
widthField = new JTextField();
widthField = new JTextField("0", 15 );
widthField.setBounds( 370, 40 , 100, 20);
panel1.add( widthField );
// Thickness field input
thicknessField = new JTextField();
thicknessField = new JTextField("0", 15 );
thicknessField.setBounds( 250, 95 , 100, 20);
panel1.add(thicknessField);
// Plys field input
plysField = new JTextField();
plysField = new JTextField("0", 15 );
plysField.setBounds( 370, 95 , 100, 20);
panel1.add(plysField);
// Color scroll pane input
colorComboBox = new JComboBox( colors );
colorComboBox.setMaximumRowCount( 5 );
colorComboBox.setBounds( 490, 40, 100, 20 );
colorComboBox.setSelectedIndex( 0 );
panel1.add(colorComboBox);
// Panel 1 scroll pane for entered information
//enteredContent = new JTextArea();
//enteredContent.setEditable(false);
enteredItemsList = new JList(prodList);
enteredScrollPane = new JScrollPane(enteredItemsList);
enteredScrollPane.setBounds(20,150,400,150);
enteredScrollPane.createVerticalScrollBar();
panel1.add(enteredScrollPane);
// Add Enter Button into Panel1
enterButton = new JButton("Enter");
enterButton.setBounds( 450, 150 , 100, 20);
panel1.add(enterButton);
removeTempItemsButton = new JButton("Remove Selected");
removeTempItemsButton.setBounds( 450, 180 , 150, 20);
panel1.add(removeTempItemsButton);
// Add Save Button into Panel1
saveListButton = new JButton( "Save List" );
saveListButton.setBounds( 450, 210 , 150, 20);
panel1.add(saveListButton);
// Add load model button into Panel1
loadModelsButton = new JButton("Load Model Set");
loadModelsButton.setBounds( 450, 240, 150, 20);
panel1.add( loadModelsButton);
// Add delete model button into Panel1
deleteModelsButton = new JButton("Delete Model Set");
deleteModelsButton.setBounds( 450, 270, 150, 20);
panel1.add(deleteModelsButton);
// ------------------PANEL 2 INTERFACE----------------------------------
// Set up Panel 2
JLabel label2 = new JLabel( "Panel Two", SwingConstants.CENTER );
panel2 = new JPanel( null );
panel2.add( label2 );
tabbedPane.addTab( " List Orders ",
null, panel2, "Orders are listed and organized here");
// Add Infomer Label into Panel 2
informLabel = new JLabel( "Information of this list");
informLabel.setBounds( 40, 20, 300, 20 );
panel2.add( informLabel );
// Add scroll pane label into Panel 2
informLabel = new JLabel( "Model Quantity Thickness Width Color Plys");
informLabel.setBounds( 20, 60, 400, 20 );
panel2.add( informLabel );
// Panel 2 scroll pane
scrollListContent = new JEditorPane();
scrollListContent.setEditable(false);
orderScrollPane = new JScrollPane(scrollListContent);
orderScrollPane.setBounds(20,80,400,220);
orderScrollPane.createVerticalScrollBar();
panel2.add(orderScrollPane);
// Add Sort by Model Button into Panel2
modelButton = new JButton( "Sort by Model #" );
modelButton.setBounds( 450, 80 , 150, 20);
panel2.add( modelButton );
// Add Sort by Thickness Button into Panel2
thicknessButton = new JButton( "Sort by Thickness" );
thicknessButton.setBounds( 450, 170 , 150, 20);
panel2.add( thicknessButton );
// Add Sort by Width Button into Panel2
widthButton = new JButton( "Sort by Width" );
widthButton.setBounds( 450, 110 , 150, 20);
panel2.add( widthButton );
// Add Sort by Color Button into Panel2
colorButton = new JButton( "Sort by Color" );
colorButton.setBounds( 450, 140 , 150, 20);
panel2.add( colorButton );
// Add Load Button into Panel2
loadListButton = new JButton( "Load List" );
loadListButton.setBounds( 450, 200 , 150, 20);
panel2.add(loadListButton);
// Add Clear Button into Panel2
clearListButton = new JButton( "Clear List" );
clearListButton.setBounds( 450, 230 , 150, 20);
panel2.add(clearListButton);
// ------------------PANEL 3 INTERFACE----------------------------------
// Set up Panel 3: For Printing
JLabel label3 = new JLabel( "Panel Three", SwingConstants.CENTER );
panel3 = new JPanel( null );
panel3.add( label3 );
tabbedPane.addTab( " HELP PANEL ",
null, panel3, "Select the set of data of models to use");
// Add Specify Label into Panel 3
specifyLabel = new JLabel( "WELCOME TO THE HELP MENU!");
specifyLabel.setBounds( 40, 20, 200, 20 );
panel3.add( specifyLabel );
// Add Helper Label into Panel 3
helper1Label = new JLabel( "Follow these steps and you \n won't have any problem using the program!");
helper1Label.setBounds( 40, 50, 400, 20 );
panel3.add( helper1Label );
// Add Helper2 Label into Panel 3
helper2Label = new JLabel( "Step 1. Load Model set");
helper2Label.setBounds( 40, 80, 400, 20 );
panel3.add( helper2Label );
// Add Helper2 Label into Panel 3
helper3Label = new JLabel( "Step 2. Fill in necessary fields of input");
helper3Label.setBounds( 40, 110, 400, 20 );
panel3.add( helper3Label );
// Add Helper2 Label into Panel 3
helper4Label = new JLabel( "Step 3. Save/Remove List of inputs");
helper4Label.setBounds( 40, 80, 400, 20 );
panel3.add( helper4Label );
getContentPane().add( tabbedPane );
setSize( 640, 400 );
setVisible(true);
//Panel 1 handlers------------------------------------------------------
// Help button to check fields entry
modelInfoButton.addActionListener (
new ActionListener()
{ public void actionPerformed( ActionEvent e) {showModelInfo();} }
);
// Enter button to check fields entry
enterButton.addActionListener (
new ActionListener()
{ public void actionPerformed(ActionEvent e) {readInput();} }
);
// button to remove selected items from temp list
removeTempItemsButton.addActionListener (
new ActionListener()
{ public void actionPerformed(ActionEvent e) {removeTempItems();} }
);
// Save button call
saveListButton.addActionListener (
new ActionListener()
{ public void actionPerformed(ActionEvent e) {saveList();} }
);
// Load button call
loadModelsButton.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent e) {loadModels();} }
);
// Clear button
deleteModelsButton.addActionListener (
new ActionListener()
{ public void actionPerformed( ActionEvent e) {deleteModels();} }
);
//Panel 2 handlers------------------------------------------------------
// Load button call
loadListButton.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent e) {openList();} }
);
// Clear button
clearListButton.addActionListener (
new ActionListener()
{ public void actionPerformed(ActionEvent e) {clearList();} }
);
// Sort by model button
modelButton.addActionListener (
new ActionListener()
{
public void actionPerformed(ActionEvent e) {prodList.sort(0);
scrollListContent.setText(prodList.toString());
} }
);
// Sort by thickness button
modelButton.addActionListener (
new ActionListener()
{
public void actionPerformed(ActionEvent e) {prodList.sort(1);
scrollListContent.setText(prodList.toString());
} }
);
// Width sort button
widthButton.addActionListener (
new ActionListener()
{
public void actionPerformed(ActionEvent e) {prodList.sort(2);
scrollListContent.setText(prodList.toString());
} }
);
// Color sort button
colorButton.addActionListener (
new ActionListener()
{
public void actionPerformed(ActionEvent e) {prodList.sort(3);
scrollListContent.setText(prodList.toString());
} }
);
}
//read from input fields, then add item to list
private void readInput()
{
String modelNo = (String)(modelComboBox.getSelectedItem());
if (modelNo == null) return;
try {
int quantity = Integer.parseInt(quantityField.getText());
double thickness = Double.parseDouble(thicknessField.getText());
double width = Double.parseDouble(widthField.getText());
String color = (String)(colorComboBox.getSelectedItem());
double plys = Double.parseDouble(plysField.getText());
if (quantity < 0 || thickness < 0 || width < 0 || plys < 0)
throw new NumberFormatException("Negative attributes");
prodList.add(
new TreeData(modelNo, quantity, thickness, width, color, plys));
// Repaints the scroll pane to display newly entered data
enteredItemsList.setListData(prodList);
}
catch (NumberFormatException e)
{ JOptionPane.showMessageDialog(this, "At least 1 field is illegal",
"Error", JOptionPane.ERROR_MESSAGE ); }
}
private void removeTempItems()
{
int[] select = enteredItemsList.getSelectedIndices();
for (int i = select.length-1; i >= 0; i--)
{
int selectIndex = select[i];
if (selectIndex < 0) return;
prodList.removeElementAt(selectIndex);
enteredItemsList.removeSelectionInterval(selectIndex, selectIndex);
enteredItemsList.repaint();
}
}
//pop up a window showing model info
private void showModelInfo()
{
JOptionPane.showMessageDialog( null, "Always remember to load model info \n Create a text file using this template \n 48-209-351 1.3 300 \n 560-344-351 1.45 250 12 \n 72-554-351 1.32 200 9 \n 84-754-351 1.35 350 3 \n 96-1042-351 1.4 280 7",
"REMINDER", JOptionPane.PLAIN_MESSAGE );
}
/**
* CLEAR FILE
* Clears the scroll list pane in the 2nd panel
*/
private void clearList()
{
this.prodList = null; //Cleanup by garbage collector
scrollListContent.setText("No list loaded.");
panel2.repaint();
}
/**
* OPEN FILE
* Opens
*/
private void openList()
{
File inFile = chooseFile(false);
if (inFile == null) return;
prodList = new OrderList(inFile, this);
scrollListContent.setText(prodList.toString());
//panel2.repaint();
}
//save list to file
private void saveList()
{
if (prodList.size() == 0) {
JOptionPane.showMessageDialog(this, "There is nothing to save",
"ERROR", JOptionPane.ERROR_MESSAGE );
return;
}
File outFile = chooseFile(true);
if (outFile == null) return;
if (outFile.exists())
if (JOptionPane.showConfirmDialog(this,
"File " + outFile.getName() + " already exists. Overwrite?",
"WARNING", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE) == JOptionPane.NO_OPTION)
return;
try {
FileWriter output = new FileWriter(outFile);
String toWrite = prodList.toString();
output.write(toWrite, 0, toWrite.length());
output.flush();
output.close();
}
catch (IOException e)
{ JOptionPane.showMessageDialog(this, "File cannot be saved",
"ERROR", JOptionPane.ERROR_MESSAGE ); }
}
//load list of available models to combo box
private void loadModels()
{
File inFile = chooseFile(false);
if (inFile == null) return;
try {
if (!(inFile.exists()))
throw new IOException("File does not exist");
modelComboBox.removeAllItems(); //clean up before adding
Scanner in = new Scanner(inFile);
while (in.hasNextLine()) {
Scanner lineScanner = new Scanner(in.nextLine());
try {
String modelID = lineScanner.next();
double height = lineScanner.nextDouble();
int tipNum = lineScanner.nextInt();
int group = lineScanner.nextInt();
modelList.add(new Model(modelID, height, tipNum, group));
modelComboBox.addItem(modelID);
}
catch (NoSuchElementException e) { continue; }
}
}
catch (IOException e)
{ JOptionPane.showMessageDialog(this, "Error loading file",
"Error", JOptionPane.ERROR_MESSAGE); }
}
private File chooseFile(boolean saveDialog)
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
// open file chooser; if user clicked Cancel then return null
int chooserDialogReturn;
if (saveDialog)
chooserDialogReturn = fileChooser.showSaveDialog(this);
else
chooserDialogReturn = fileChooser.showOpenDialog(this);
if (chooserDialogReturn == JFileChooser.CANCEL_OPTION)
return null;
return fileChooser.getSelectedFile(); // get selected file
}
//clear model dropdown list
private void deleteModels()
{
modelList.removeAllElements();
modelComboBox.removeAllItems();
}
//================================MAIN======================================
public static void main(String args[])
{
TreeClassics tabbedPane = new TreeClassics();
tabbedPane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}