Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.kabili;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- import javax.swing.event.*;
- import org.kabili.sql.*;
- import org.kabili.sql.records.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Vector;
- import java.io.*;
- import org.jruby.embed.*;
- /**
- * Creates a Swing frame that allows administrative control over the Off The Floor
- * database. In order to access the database, the <code>MainGUI</code> makes use
- * of the <code>JRuby</code> framework.
- *
- * @author Andrew Nagle
- * @author Shayne Simmons
- */
- public class MainGUI extends JPanel implements Runnable {
- private static final long serialVersionUID = -7266491760673265140L;
- private final String dbFile = "Database.rb";
- private Database db;
- private static JFrame frame;
- private Member currentMember;
- private Vector<Department> departments;
- private ArrayList<Member> members;
- private ArrayList<Product> products;
- private Vector<Order> orders;
- private Vector<Location> locations;
- private JTabbedPane tabbedPane;
- private JPanel contentPane;
- private JPanel productPane;
- private JList userList;
- private JList departmentList;
- private JList itemList;
- private JLabel toolbarUser, toolbarStatus;
- private JComboBox locationList;
- // User pane
- private JTextField fName = new JTextField("");
- private JTextField lName = new JTextField("");
- private JTextField phoneNo = new JTextField("");
- private JTextField username = new JTextField("");
- private JTextField add1 = new JTextField("");
- private JTextField add2 = new JTextField("");
- private JTextField email = new JTextField("");
- private JTextField city = new JTextField("");
- private JComboBox state = new JComboBox();
- private JTextField zip = new JTextField("");
- // Product info pane
- private JTextField prodInfoName;
- private JTextArea prodInfoDesc;
- private JComboBox prodInfoLoc;
- private JComboBox prodInfoDept;
- private JImage prodInfoImage;
- private JTextField userLoginField;
- private JPasswordField passLoginField;
- // Order Panel info
- private JLabel productNum;
- private JLabel productName;
- private JLabel productDesc;
- private JLabel productDept;
- private JLabel productLocation;
- // Donation Panel info
- private JTextField productDName;
- private JTextArea productDescription;
- //order and donation button
- private JButton confirm;
- private JButton cancel;
- /**
- * The main entry point for the program. This basically starts everything
- *
- * @param args A string array of command line arguments
- */
- public static void main(String[] args) {
- try {
- try {
- // We do this because Xfce is defaulted to the hideous Motif theme...
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
- } catch (UnsupportedLookAndFeelException e) {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (ClassNotFoundException e) {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- frame = new JFrame("Off The Floor Administrative Panel");
- frame.setContentPane(new MainGUI());
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //frame.setPreferredSize(new Dimension(320, 240));
- //frame.setSize(320, 240);
- frame.setResizable(false);
- frame.setVisible(true);
- frame.pack();
- }
- /**
- *
- */
- private MainGUI() {
- try {
- ScriptingContainer container = new ScriptingContainer();
- // implemented by a Ruby class
- Object receiver = container.runScriptlet(PathType.CLASSPATH, dbFile);
- db = container.getInstance(receiver, Database.class);
- } catch (Exception e) {}
- setLayout(new BorderLayout());
- contentPane = new JPanel();
- tabbedPane = new JTabbedPane();
- tabbedPane.addTab("Members", getUserPanel());
- tabbedPane.addTab("Products", getProductPanel());
- tabbedPane.addTab("Orders", getCartPanel());
- tabbedPane.addTab("Donations", getDonationPanel());
- //tabbedPane.setSelectedIndex(2);
- JToolBar toolbar = new JToolBar();
- toolbar.setLayout(new BorderLayout());
- toolbar.setFloatable(false);
- toolbar.add(toolbarUser = new JLabel(""), BorderLayout.WEST);
- toolbar.add(toolbarStatus = new JLabel("",JLabel.RIGHT), BorderLayout.EAST);
- new Thread(this).start();
- contentPane.add(getLoginPanel());
- add(contentPane,BorderLayout.CENTER);
- add(toolbar,BorderLayout.SOUTH);
- }
- /**
- *
- * @return
- */
- private JPanel getLoginPanel() {
- JPanel panel = new JPanel();
- panel.setLayout(new FlowLayout());
- JPanel login = new JPanel();
- GroupLayout layout = new GroupLayout(login);
- login.setLayout(layout);
- layout.setAutoCreateGaps(true);
- layout.setAutoCreateContainerGaps(true);
- JLabel welcome = new JLabel("Welcome!");
- welcome.setFont(welcome.getFont().deriveFont(welcome.getFont().getSize2D()+5).deriveFont(Font.BOLD));
- JLabel l1 = new JLabel("Username:",JLabel.LEFT);
- JLabel l2 = new JLabel("Password:",JLabel.LEFT);
- userLoginField = new JTextField();
- passLoginField =new JPasswordField();
- Dimension uNameSize = userLoginField.getPreferredSize();
- uNameSize.width = 120;
- JButton proceed = new JButton("Login");
- JButton exit = new JButton("Exit");
- proceed.addActionListener(new LoginListener());
- exit.addActionListener(new ExitListener());
- userLoginField.addActionListener(new LoginListener());
- passLoginField.addActionListener(new LoginListener());
- userLoginField.setPreferredSize(uNameSize);
- passLoginField.setPreferredSize(uNameSize);
- layout.setHorizontalGroup(
- layout.createParallelGroup(GroupLayout.Alignment.CENTER)
- .addComponent(welcome)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l1)
- .addComponent(l2))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
- .addComponent(userLoginField)
- .addComponent(passLoginField))
- ).addGroup(layout.createSequentialGroup()
- .addComponent(proceed)
- .addComponent(exit))
- );
- layout.setVerticalGroup(
- layout.createSequentialGroup()
- .addComponent(welcome)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l1)
- .addComponent(userLoginField))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l2)
- .addComponent(passLoginField))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(proceed)
- .addComponent(exit))
- );
- panel.add(login);
- Dimension size = new Dimension((int)tabbedPane.getPreferredSize().getWidth(), (int)tabbedPane.getPreferredSize().getHeight());
- panel.setPreferredSize(size);
- return panel;
- }
- /**
- *
- * @return
- */
- private JPanel getUserPanel() {
- JPanel panel = new JPanel();
- panel.setLayout(new BorderLayout());
- JPanel info = new JPanel();
- userList = new JList();
- userList.setVisibleRowCount(5);
- userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- userList.addListSelectionListener(new UserListSelectionListener());
- //userList.setFixedCellHeight(15);
- //userList.setFixedCellWidth(110);
- GroupLayout layout = new GroupLayout(info);
- info.setLayout(layout);
- layout.setAutoCreateGaps(true);
- layout.setAutoCreateContainerGaps(true);
- JLabel l1 = new JLabel("Name:",JLabel.LEFT);
- JLabel l2 = new JLabel("Email:",JLabel.LEFT);
- JLabel l3 = new JLabel("Username:",JLabel.LEFT);
- JLabel l4 = new JLabel("Address:",JLabel.LEFT);
- JLabel l5 = new JLabel("Phone:", JLabel.LEFT);
- String[] stateAbbr = { "AK", "AL", "AR", "AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC",
- "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA",
- "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE",
- "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC",
- "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY" };
- state.setModel(new DefaultComboBoxModel( stateAbbr));
- zip.setColumns(6);
- layout.setHorizontalGroup(
- layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l1)
- .addComponent(fName)
- .addComponent(l3)
- .addComponent(username)
- )
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(lName)
- .addComponent(l5)
- .addComponent(phoneNo)
- )
- )
- .addComponent(l2)
- .addComponent(email)
- .addComponent(l4)
- .addGroup(layout.createSequentialGroup()
- .addComponent(add1)
- .addComponent(add2))
- .addGroup(layout.createSequentialGroup()
- .addComponent(city)
- .addGroup(layout.createSequentialGroup()
- .addComponent(state,GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
- GroupLayout.PREFERRED_SIZE)
- .addComponent(zip, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
- GroupLayout.PREFERRED_SIZE)
- )
- )
- );
- layout.setVerticalGroup(
- layout.createSequentialGroup()
- .addComponent(l1)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(fName)
- .addComponent(lName))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l3)
- .addComponent(l5)
- )
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(username)
- .addComponent(phoneNo)
- )
- .addComponent(l2)
- .addComponent(email, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
- GroupLayout.PREFERRED_SIZE)
- .addComponent(l4)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(add1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
- GroupLayout.PREFERRED_SIZE)
- .addComponent(add2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
- GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(city)
- .addComponent(state)
- .addComponent(zip))
- );
- JScrollPane userScroller = new JScrollPane(userList,
- JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
- JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- userScroller.setPreferredSize(new Dimension(120,0));
- panel.add(userScroller,BorderLayout.WEST);
- panel.add(info);
- panel.setPreferredSize(new Dimension(450, 300));
- return panel;
- }
- /**
- *
- * @return
- */
- private JPanel getProductPanel() {
- JPanel panel = new JPanel(new BorderLayout(4,4));
- productPane = new JPanel();
- /* Start Product Tree */
- departmentList = new JList();
- locationList = new JComboBox();
- departmentList.setVisibleRowCount(5);
- departmentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- JScrollPane treeScroller = new JScrollPane(departmentList,
- JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
- JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- treeScroller.setPreferredSize(new Dimension(120,0));
- /* End Product Tree */
- /* Start Search bar */
- JPanel searchBar = new JPanel();
- GroupLayout layout = new GroupLayout(searchBar);
- searchBar.setLayout(layout);
- //locationList.setSelectedIndex(0);
- locationList.setPreferredSize(new Dimension(120, locationList.getPreferredSize().height));
- JTextField searchTerms = new JTextField();
- JButton searchButton = new JButton("Search");
- layout.setAutoCreateGaps(true);
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addComponent(locationList, GroupLayout.PREFERRED_SIZE,
- GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(searchTerms)
- .addComponent(searchButton)
- );
- layout.setVerticalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(locationList, GroupLayout.PREFERRED_SIZE,
- GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(searchTerms)
- .addComponent(searchButton))
- );
- /* End Search Bar */
- GridLayout gl = new GridLayout(0,1);
- gl.setVgap(4);
- productPane = new JPanel (gl);
- JScrollPane productScroller = new JScrollPane(productPane,
- JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
- JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- panel.add(treeScroller,BorderLayout.WEST);
- panel.add(productScroller, BorderLayout.CENTER);
- panel.add(searchBar, BorderLayout.NORTH);
- panel.setPreferredSize(new Dimension(450, 300));
- return panel;
- }
- /**
- * The cart panel will track the items that the users have requested
- *
- * @return
- */
- private JPanel getCartPanel() {
- JPanel cart = new JPanel(new BorderLayout(4,4));
- JPanel info = new JPanel();
- itemList = new JList();
- itemList.setVisibleRowCount(5);
- itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- GroupLayout layout = new GroupLayout(info);
- info.setLayout(layout);
- layout.setAutoCreateGaps(true);
- layout.setAutoCreateContainerGaps(true);
- //JButton submit = new JButton("Submit");
- JLabel l1 = new JLabel("Product Number:",JLabel.LEFT);
- productNum = new JLabel("",JLabel.LEFT);
- JLabel l2 = new JLabel("Product Name:", JLabel.LEFT);
- productName = new JLabel("", JLabel.LEFT);
- JLabel l3 = new JLabel("Product Description:", JLabel.LEFT);
- productDesc = new JLabel("", JLabel.LEFT);
- JLabel l4 = new JLabel("Product Department:", JLabel.LEFT);
- productDept = new JLabel("", JLabel.LEFT);
- JLabel l5 = new JLabel("Product Location:", JLabel.LEFT);
- productLocation = new JLabel("", JLabel.LEFT);
- // TODO: You should assign the name of these buttons as well
- // See productInfoPane() for an example
- confirm = new JButton("Confirm");
- cancel = new JButton("Cancel");
- confirm.addActionListener(new OrderConfirmButtonListener());
- cancel.addActionListener(new OrderCancelButtonListener());
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addGap(10)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l1)
- .addComponent(l2)
- .addComponent(l3)
- .addComponent(l4)
- .addComponent(l5)
- .addComponent(confirm, GroupLayout.Alignment.TRAILING))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(productNum)
- .addComponent(productName)
- .addComponent(productDesc)
- .addComponent(productDept)
- .addComponent(productLocation)
- .addComponent(cancel))
- );
- layout.setVerticalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l1)
- .addComponent(productNum))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l2)
- .addComponent(productName))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l3)
- .addComponent(productDesc))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l4)
- .addComponent(productDept))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l5)
- .addComponent(productLocation)
- .addGap(175, 175, 175))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
- .addComponent(confirm)
- .addComponent(cancel))
- );
- JScrollPane userScroller = new JScrollPane(itemList,
- JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
- JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- userScroller.setPreferredSize(new Dimension(120,0));
- cart.add(userScroller,BorderLayout.WEST);
- cart.add(info);
- cart.setPreferredSize(new Dimension(450, 300));
- return cart;
- }
- private JPanel getDonationPanel() {
- JPanel donate = new JPanel(new BorderLayout(4,4));
- JPanel info = new JPanel();
- itemList = new JList();
- itemList.setVisibleRowCount(5);
- itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- GroupLayout layout = new GroupLayout(info);
- info.setLayout(layout);
- layout.setAutoCreateGaps(true);
- layout.setAutoCreateContainerGaps(true);
- JLabel l1 = new JLabel("Product name:", JLabel.LEFT);
- productDName = new JTextField();
- JLabel l2 = new JLabel("Product description:", JLabel.LEFT);
- productDescription = new JTextArea(5,2);
- // FIXME: Locations need added.
- JLabel l3 = new JLabel("Location", Label.LEFT);
- String[] quality = { "", "", ""};
- JComboBox qualityList = new JComboBox(quality);
- JLabel l4 = new JLabel("Furniture type:", JLabel.LEFT);
- String[] type = {"Bed", "Box Spring", "Bed Frame", "Table/Chairs", "Dresser", "Couch/Loveseat", "Living room Chair", "Lamp", "End Table", "Crib", "Crib Mattress"};
- // TODO: This data should be obtained from the global variable 'departments'
- JComboBox furnitureType = new JComboBox(type);
- confirm = new JButton("Confirm");
- cancel = new JButton("Cancel");
- confirm.addActionListener(new DonationButtonListener());
- cancel.addActionListener(new DonationCancelButtonListener());
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l1, GroupLayout.Alignment.LEADING)
- .addComponent(l4, GroupLayout.Alignment.LEADING)
- .addComponent(l2, GroupLayout.Alignment.LEADING)
- .addComponent(l3, GroupLayout.Alignment.LEADING)
- .addGap(200, 200, 200)
- .addComponent(confirm, GroupLayout.Alignment.TRAILING))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(productName, GroupLayout.Alignment.LEADING)
- .addComponent(furnitureType, GroupLayout.Alignment.LEADING)
- .addComponent(productDescription, GroupLayout.Alignment.LEADING)
- .addComponent(qualityList, GroupLayout.Alignment.LEADING)
- .addGap(200, 200, 200)
- .addComponent(cancel, GroupLayout.Alignment.LEADING))
- );
- layout.setVerticalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l1)
- .addComponent(productName))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l4)
- .addComponent(furnitureType))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l2)
- .addComponent(productDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
- GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l3)
- .addComponent(qualityList))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGap(75, 75, 75))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(confirm)
- .addComponent(cancel))
- );
- donate.add(info);
- donate.setPreferredSize(new Dimension(450, 300));
- return donate;
- }
- private JPanel createProductPanel(Product prod) {
- JPanel panel;
- JButton button1;
- JButton button2;
- JButton button3;
- JImage image;
- JLabel name;
- JTextArea description;
- panel = new JPanel();
- GroupLayout layout = new GroupLayout(panel);
- panel.setLayout(layout);
- panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
- button1 = new JButton(new ImageIcon("resources/remove.png"));
- button2 = new JButton(new ImageIcon("resources/pencil.png"));
- button3 = new JButton(new ImageIcon("resources/edit.png"));
- button1.setBorderPainted(false);
- button2.setBorderPainted(false);
- button3.setBorderPainted(false);
- button1.addActionListener(new RemoveProductListener());
- button2.addActionListener(new EditProductListener());
- // Setting the name allows us to determine which product
- // this button belongs to when an ActionListener is fired
- button1.setName("" + prod.getId());
- button2.setName("" + prod.getId());
- button3.setName("" + prod.getId());
- image = new JImage(prod.getImage(), 80, 80);
- name = new JLabel(prod.getName(), JLabel.LEFT);
- name.setFont(getFont().deriveFont(Font.BOLD));
- description = new JTextArea(prod.getDescription());
- description.setLineWrap(true);
- description.setEditable(false);
- description.setOpaque(false);
- description.setWrapStyleWord(true);
- description.setForeground(name.getForeground());
- layout.setAutoCreateGaps(true);
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addComponent(image)
- .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED,
- 2, 2)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(name)
- .addComponent(description))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(button1)
- .addComponent(button2))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(image)
- .addGroup(layout.createSequentialGroup()
- .addComponent(name)
- .addComponent(description))
- .addGroup(layout.createSequentialGroup()
- .addComponent(button1)
- .addComponent(button2))
- );
- return panel;
- }
- private JPanel getProductInfoPane(Product product) {
- JPanel panel = new JPanel();
- GroupLayout layout = new GroupLayout(panel);
- panel.setLayout(layout);
- panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
- JButton change = new JButton("Change");
- JButton clear = new JButton("Clear");
- JButton close = new JButton("Close", new ImageIcon("resources/edit.png"));
- JButton save = new JButton("Save", new ImageIcon("resources/edit.png"));
- close.addActionListener(new CloseProductListener());
- save.addActionListener(new SaveProductListener());
- change.addActionListener(new ChangeProductImageListener());
- // Setting the name allows us to determine which product
- // this button belongs to when an ActionListener is fired
- change.setName("" + product.getId());
- clear.setName("" + product.getId());
- close.setName("" + product.getId());
- save.setName("" + product.getId());
- JLabel l1 = new JLabel("Product " + product.getId());
- JLabel l2 = new JLabel("Name:");
- JLabel l3 = new JLabel("Description:");
- JLabel l4 = new JLabel("Location:");
- JLabel l5 = new JLabel("Department:");
- prodInfoName = new JTextField(product.getName());
- prodInfoDesc = new JTextArea(product.getDescription());
- prodInfoLoc = new JComboBox(locations);
- prodInfoDept = new JComboBox(departments);
- prodInfoImage = new JImage(product.getImage(), 130, 130);
- prodInfoDesc.setLineWrap(true);
- prodInfoDesc.setWrapStyleWord(true);
- prodInfoLoc.setSelectedItem(new Location(product.getLocation(), "","","","","",""));
- prodInfoDept.setSelectedItem(new Department(product.getDepartment(), ""));
- layout.setAutoCreateGaps(true);
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(prodInfoImage)
- .addGroup(layout.createSequentialGroup()
- .addComponent(change)
- .addComponent(clear)
- )
- .addComponent(l4)
- .addComponent(prodInfoLoc)
- )
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l1)
- .addComponent(l2)
- .addComponent(prodInfoName)
- .addComponent(l3)
- .addComponent(prodInfoDesc)
- .addComponent(l5)
- .addComponent(prodInfoDept)
- .addGroup(layout.createSequentialGroup()
- .addComponent(close)
- .addComponent(save)
- ))
- );
- layout.setVerticalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addGroup(layout.createSequentialGroup()
- .addComponent(prodInfoImage)
- .addGroup(layout.createParallelGroup()
- .addComponent(change)
- .addComponent(clear)
- )
- )
- .addGroup(layout.createSequentialGroup()
- .addComponent(l1)
- .addComponent(l2)
- .addComponent(prodInfoName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
- GroupLayout.PREFERRED_SIZE)
- .addComponent(l3)
- .addComponent(prodInfoDesc)
- )
- )
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l4)
- .addComponent(l5)
- )
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(prodInfoLoc)
- .addComponent(prodInfoDept)
- )
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
- .addComponent(close)
- .addComponent(save)
- )
- );
- return panel;
- }
- /**
- * Loads all data from the database
- */
- private void loadData() {
- if(!db.isConnectionValid())
- return;
- // FIXME: We should check where the user is an admin or not
- // and adjust the the interface accordingly
- try {
- members = db.getMembers();
- products = db.getProducts(1);
- locations = db.getLocations();
- departments = db.getDepartments();
- //orders = db.getOrders();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- userList.setListData(members.toArray());
- departmentList.setListData(departments.toArray());
- locationList.setModel(new DefaultComboBoxModel(locations));
- /* Start productPane creation */
- productPane.removeAll();
- for (int i = 0; i < products.size(); i++) {
- productPane.add(createProductPanel(products.get(i)));
- }
- }
- /**
- * Handles login related actions
- */
- class LoginListener implements ActionListener {
- /**
- * Reacts when an event is raised
- *
- * @param e the event object.
- */
- @Override
- public void actionPerformed(ActionEvent e) {
- // FIXME: Change this check before release
- if (db.isConnectionValid()) {
- try {
- currentMember = db.authMember(userLoginField.getText(), passLoginField.getPassword());
- toolbarUser.setText("Logged in as: " + currentMember.getUserName());
- } catch (NullPointerException npe) {
- return;
- } catch (SQLException sqle) {
- // TODO Auto-generated catch block
- sqle.printStackTrace();
- }
- } else {
- toolbarUser.setText("Logged in as: " + "Username");
- currentMember = new Member(0, "","","","","","", "","","","","", false);
- }
- //System.out.println("Fetching data");
- loadData();
- contentPane.removeAll();
- contentPane.add(tabbedPane);
- contentPane.repaint();
- frame.setVisible(true);
- frame.pack();
- }
- }
- /**
- * Allows the program to run actions on program exit
- */
- class ExitListener implements ActionListener {
- /**
- * Reacts when an event is raised
- *
- * @param e the event object.
- */
- @Override
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- }
- class UserListSelectionListener implements ListSelectionListener {
- public void valueChanged(ListSelectionEvent evt) {
- // When the user release the mouse button and completes the selection,
- // getValueIsAdjusting() becomes false
- if (!evt.getValueIsAdjusting()) {
- Member selected = (Member) userList.getSelectedValue();
- fName.setText(selected.getFName());
- lName.setText(selected.getLName());
- phoneNo.setText(selected.getPhoneNo());
- username.setText(selected.getUserName());
- email.setText(selected.getEmail());
- add1.setText(selected.getAdd1());
- add2.setText(selected.getAdd2());
- city.setText(selected.getCity());
- state.setSelectedItem(selected.getState());
- zip.setText(selected.getZip());
- }
- }
- }
- class RemoveProductListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- int choice = JOptionPane.showConfirmDialog(frame,
- "You attempted to remove item " + ((JButton)e.getSource()).getName() + ".\n" +
- "Would you like to continue?",
- "Remove item",
- JOptionPane.YES_NO_OPTION);
- if(choice == JOptionPane.YES_OPTION){
- }
- System.out.println(choice);
- }
- }
- class EditProductListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- int productID = Integer.parseInt(((JButton)e.getSource()).getName());
- productPane.removeAll();
- productPane.add(getProductInfoPane(products.get(products.indexOf(new Product(productID, "", "", 0, 0, null)))));
- //products.indexOf(new Product(productID, "", "", 0, 0, null));
- }
- }
- class CloseProductListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- productPane.removeAll();
- for (int i = 0; i < products.size(); i++) {
- productPane.add(createProductPanel(products.get(i)));
- }
- //products.indexOf(new Product(productID, "", "", 0, 0, null));
- }
- }
- class ChangeProductImageListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- JFileChooser fc = new JFileChooser();
- fc.addChoosableFileFilter(new ImageFilter());
- fc.setAcceptAllFileFilterUsed(false);
- int returnVal = fc.showDialog(MainGUI.this, "Select");
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File file = fc.getSelectedFile();
- try {
- prodInfoImage.setImage(ImageIO.read(file));
- prodInfoImage.repaint();
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(frame, "Could not change image.\nPlease try again.",
- "Error",JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- }
- class SaveProductListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- int productID = Integer.parseInt(((JButton)e.getSource()).getName());
- int prodIndex = products.indexOf(new Product(productID, "", "", 0, 0, null));
- Product prod = products.get(prodIndex);
- prod.setImage(prodInfoImage.getImage());
- prod.setName(prodInfoName.getText());
- prod.setDescription(prodInfoDesc.getText());
- prod.setDepartment(((Department)prodInfoDept.getSelectedItem()).getId());
- prod.setLocation(((Location)prodInfoLoc.getSelectedItem()).getId());
- products.set(prodIndex, prod);
- try {
- if(!db.isConnectionValid()) throw new SQLException();
- db.updateProduct(prod);
- } catch (SQLException e1) {
- JOptionPane.showMessageDialog(frame, "An error occured when trying to save the product!",
- "Error",JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- class OrderConfirmButtonListener implements ActionListener{
- public void actionPerformed(ActionEvent e)
- {
- JOptionPane.showMessageDialog(frame, "Order has been confrimed", "Confirmataion", JOptionPane.PLAIN_MESSAGE);
- }
- }
- //Should this remove an order too as if it was a canceled order?
- class OrderCancelButtonListener implements ActionListener{
- public void actionPerformed(ActionEvent e)
- {
- productNum.setText("");
- productName.setText("");
- productDesc.setText("");
- productDept.setText("");
- productLocation.setText("");
- }
- }
- // needs to add product to the product database
- class DonationButtonListener implements ActionListener{
- public void actionPerformed(ActionEvent e)
- {
- JOptionPane.showMessageDialog(frame, "Donation has been accepted. Thank you for your contributions", "Donation", JOptionPane.PLAIN_MESSAGE);
- }
- }
- class DonationCancelButtonListener implements ActionListener{
- public void actionPerformed(ActionEvent e)
- {
- productDName.setText("");
- productDescription.setText("");
- }
- }
- /**
- * Runs in a separate thread to perform actions concurrently with
- * the program's <code>main</code> function.
- */
- public synchronized void run() {
- while (true) {
- if (db != null && db.isConnectionValid()) {
- toolbarStatus.setText("Connected");
- } else {
- toolbarStatus.setText("Not Connected");
- db.reconnect();
- }
- try {
- wait(15000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement