Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.kabili;
- import javax.swing.*;
- import javax.swing.tree.*;
- import javax.imageio.*;
- import org.kabili.sql.*;
- import org.kabili.sql.records.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Vector;
- import java.io.*;
- import org.jruby.embed.*;
- /**
- *
- * @author Andrew Nagle
- *
- */
- public class MainGUI extends JPanel {
- private static final long serialVersionUID = -7266491760673265140L;
- private final String dbFile = "Database.rb";
- Database db;
- static JFrame frame;
- Member currentMember;
- JTabbedPane tabbedPane;
- JPanel contentPane;
- JList userList;
- JTree productTree;
- JList itemList;
- JLabel toolbarUser, toolbarStatus;
- JTextField fName = new JTextField("First name");
- JTextField lName = new JTextField("Last name");
- JTextField phoneNo = new JTextField("Phone");
- JTextField username = new JTextField("username");
- JTextField productNo = new JTextField("Product Number");
- JTextField productName = new JTextField("Product Name");
- JTextArea productDesc = new JTextArea("test");
- JTextField userLoginField;
- JPasswordField passLoginField;
- /**
- *
- * @param args
- */
- public static void main(String[] args) {
- try {
- try {
- 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("Users", getUserPanel());
- tabbedPane.addTab("Products", getProductPanel());
- tabbedPane.addTab("Cart", 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);
- contentPane.add(getLoginPanel());
- if (db.isConnectionValid()) {
- toolbarStatus.setText("Connected");
- } else {
- toolbarStatus.setText("Not Connected");
- }
- add(contentPane,BorderLayout.CENTER);
- add(toolbar,BorderLayout.SOUTH);
- }
- /**
- *
- * @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.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);
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l1)
- .addComponent(fName))
- .addComponent(lName)
- );
- layout.setVerticalGroup(
- layout.createSequentialGroup()
- .addComponent(l1)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(fName)
- .addComponent(lName))
- );
- 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 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());
- 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 getProductPanel() {
- JPanel panel = new JPanel(new BorderLayout(4,4));
- /* Start Product Tree */
- productTree = new JTree();
- Vector<Department> dept = new Vector<Department>();
- dept.add(new Department(1,"Bedroom",0));
- dept.add(new Department(2,"Kitchen",0));
- dept.add(new Department(3,"Appliances",2));
- dept.add(new Department(4,"Living room",0));
- dept.add(new Department(5,"Sofas",4));
- dept.add(new Department(7,"Bathroom",0));
- DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root Node");
- DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
- productTree.setModel( treeModel );
- productTree.setRootVisible(false);
- productTree.setEditable(true);
- productTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
- productTree.setShowsRootHandles(true);
- DefaultMutableTreeNode parentNode;
- DefaultMutableTreeNode node;
- for(Department d : dept){
- TreeNode[] test = treeModel.getPathToRoot(
- new DefaultMutableTreeNode( new Department(d.getParent(),"",0)));
- //treeModel.insertNodeInto(arg0, arg1, arg2);
- parentNode = (DefaultMutableTreeNode) test[test.length-1];
- node = new DefaultMutableTreeNode( d );
- addNodeToDefaultTreeModel( treeModel, parentNode, node );
- }
- /*
- parentNode = (DefaultMutableTreeNode) treeModel.getRoot();
- node = new DefaultMutableTreeNode( new Department(1,"Bedroom",0) );
- addNodeToDefaultTreeModel( treeModel, parentNode, node );
- parentNode = (DefaultMutableTreeNode) treeModel.getRoot();
- node = new DefaultMutableTreeNode( new Department(2,"Kitchen",0) );
- addNodeToDefaultTreeModel( treeModel, parentNode, node );
- parentNode = node;
- node = new DefaultMutableTreeNode( new Department(3,"Appliances",2) );
- addNodeToDefaultTreeModel( treeModel, parentNode, node );
- TreeNode[] test = treeModel.getPathToRoot(new Department(2,"",0));
- parentNode = (DefaultMutableTreeNode) treeModel.getRoot();
- node = new DefaultMutableTreeNode( new Department(4,"Living room",0) );
- addNodeToDefaultTreeModel( treeModel, parentNode, node );
- parentNode = node;
- node = new DefaultMutableTreeNode( new Department(5,"Sofas",4) );
- addNodeToDefaultTreeModel( treeModel, parentNode, node );
- parentNode = (DefaultMutableTreeNode) treeModel.getRoot();
- node = new DefaultMutableTreeNode( new Department(6,"Stoves",3) );
- addNodeToDefaultTreeModel( treeModel, parentNode, node );
- parentNode = node;
- node = new DefaultMutableTreeNode( new Department(7,"Bathroom",0) );
- addNodeToDefaultTreeModel( treeModel, parentNode, node );
- */
- JScrollPane treeScroller = new JScrollPane(productTree,
- 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);
- JComboBox locationList = new JComboBox();
- //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 */
- JPanel products = new JPanel();
- GridLayout gl = new GridLayout(0,1);
- gl.setVgap(4);
- products.setLayout(gl);
- if(db.isConnectionValid()) {
- Vector<Product> prods = db.getProducts(1);
- for (int i = 0; i < prods.size(); i++) {
- products.add(createProductPanel(prods.get(i)));
- }
- }
- for (int i = 0; i < 10; i++) {
- Image image = null;
- Product p;
- try {
- InputStream is = new BufferedInputStream(
- new FileInputStream("D:\\gem.jpg"));
- image = ImageIO.read(is);
- p = new Product(0,"Test Product #" + i, "",0, 0, image);
- products.add(createProductPanel(p));
- } catch (Exception e) {}
- }
- JScrollPane productScroller = new JScrollPane(products,
- JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
- JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- panel.add(treeScroller,BorderLayout.WEST);
- panel.add(productScroller, BorderLayout.CENTER);
- panel.add(searchBar, BorderLayout.NORTH);
- panel.setPreferredSize(new Dimension(450, 300));
- return panel;
- }
- private static void addNodeToDefaultTreeModel( DefaultTreeModel treeModel, DefaultMutableTreeNode parentNode, DefaultMutableTreeNode node ) {
- treeModel.insertNodeInto( node, parentNode, parentNode.getChildCount() );
- if ( parentNode == treeModel.getRoot() ) {
- treeModel.nodeStructureChanged( (TreeNode) treeModel.getRoot() );
- }
- }
- /**
- * 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);
- JLabel productNum = new JLabel("",JLabel.LEFT);
- JLabel l2 = new JLabel("Product Name:", JLabel.LEFT);
- JLabel productName = new JLabel("", JLabel.LEFT);
- JLabel l3 = new JLabel("Product Description:", JLabel.LEFT);
- JLabel productDesc = new JLabel("", JLabel.LEFT);
- JLabel l4 = new JLabel("Product Department:", JLabel.LEFT);
- JLabel productDept = new JLabel("", JLabel.LEFT);
- JLabel l5 = new JLabel("Product Location:", JLabel.LEFT);
- JLabel productLocation = new JLabel("", JLabel.LEFT);
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l1)
- .addComponent(l2)
- .addComponent(l3)
- .addComponent(l4)
- .addComponent(l5))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(productNum)
- .addComponent(productName)
- .addComponent(productDesc)
- .addComponent(productDept)
- .addComponent(productLocation))
- //.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- //.addComponent(submit))
- );
- 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))
- //.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- //.addComponent(submit))
- );
- 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);
- JTextField productName = new JTextField();
- JLabel l2 = new JLabel("Product description:", JLabel.LEFT);
- JTextArea productDescription = new JTextArea(5,2);
- JLabel l3 = new JLabel("Quality", Label.LEFT);
- String[] quality = { "Great", "Good", "Fair"};
- 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"};
- JComboBox furnitureType = new JComboBox(type);
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(l1)
- .addComponent(l4)
- .addComponent(l2)
- .addComponent(l3))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(productName)
- .addComponent(furnitureType)
- .addComponent(productDescription)
- .addComponent(qualityList))
- );
- layout.setVerticalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l1)
- .addComponent(productName))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l4)
- .addComponent(furnitureType))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l2)
- .addComponent(productDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
- GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(l3)
- .addComponent(qualityList))
- );
- 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;
- JLabel description;
- panel = new JPanel();
- GroupLayout layout = new GroupLayout(panel);
- panel.setLayout(layout);
- panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
- button1 = new JButton(".");
- button2 = new JButton(".");
- button3 = new JButton(".");
- // 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());
- name.setFont(name.getFont().deriveFont(Font.BOLD));
- description = new JLabel(prod.getDescription());
- layout.setAutoCreateGaps(true);
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addComponent(image)
- .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED,
- 2, 2)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(name, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(description, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addComponent(button1)
- .addComponent(button2)
- .addComponent(button3))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(image)
- .addGroup(layout.createSequentialGroup()
- .addComponent(name)
- .addComponent(description))
- .addGroup(layout.createSequentialGroup()
- .addComponent(button1)
- .addComponent(button2)
- .addComponent(button3))
- );
- return panel;
- }
- /**
- * This inner class handles login related events.
- */
- class LoginListener implements ActionListener {
- /**
- * Allows the user to login to the program.
- *
- * @param e the event object.
- */
- @Override
- public void actionPerformed(ActionEvent e) {
- // FIXME: Remove this check before release
- if (db.isConnectionValid()) {
- currentMember = db.authMember(userLoginField.getText(), passLoginField.getPassword());
- try {
- toolbarUser.setText("Logged in as: " + currentMember.getUserName());
- } catch (NullPointerException npe) {
- return;
- }
- } else {
- toolbarUser.setText("Logged in as: " + "Username");
- }
- contentPane.removeAll();
- contentPane.add(tabbedPane);
- System.out.println("Fetching data");
- userList.setListData(db.getMembers());
- contentPane.repaint();
- frame.setVisible(true);
- frame.pack();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement