Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.54 KB | None | 0 0
  1. package org.kabili;
  2.  
  3.  
  4. import javax.imageio.ImageIO;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7.  
  8. import org.kabili.sql.*;
  9. import org.kabili.sql.records.*;
  10.  
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.sql.SQLException;
  14. import java.util.ArrayList;
  15. import java.util.Vector;
  16.  
  17. import java.io.*;
  18.  
  19. import org.jruby.embed.*;
  20.  
  21. /**
  22. * Creates a Swing frame that allows administrative control over the Off The Floor
  23. * database. In order to access the database, the <code>MainGUI</code> makes use
  24. * of the <code>JRuby</code> framework.
  25. *
  26. * @author Andrew Nagle
  27. * @author Shayne Simmons
  28. */
  29. public class MainGUI extends JPanel implements Runnable {
  30.  
  31. private static final long serialVersionUID = -7266491760673265140L;
  32. private final String dbFile = "Database.rb";
  33.  
  34. private Database db;
  35. private static JFrame frame;
  36.  
  37. private Member currentMember;
  38. private Vector<Department> departments;
  39. private ArrayList<Member> members;
  40. private ArrayList<Product> products;
  41. private Vector<Order> orders;
  42. private Vector<Location> locations;
  43.  
  44.  
  45. private JTabbedPane tabbedPane;
  46. private JPanel contentPane;
  47. private JPanel productPane;
  48. private JList userList;
  49. private JList departmentList;
  50. private JList itemList;
  51. private JLabel toolbarUser, toolbarStatus;
  52. private JComboBox locationList;
  53.  
  54. // User pane
  55. private JTextField fName = new JTextField("");
  56. private JTextField lName = new JTextField("");
  57. private JTextField phoneNo = new JTextField("");
  58. private JTextField username = new JTextField("");
  59. private JTextField add1 = new JTextField("");
  60. private JTextField add2 = new JTextField("");
  61. private JTextField email = new JTextField("");
  62. private JTextField city = new JTextField("");
  63. private JComboBox state = new JComboBox();
  64. private JTextField zip = new JTextField("");
  65.  
  66. // Product info pane
  67. private JTextField prodInfoName;
  68. private JTextArea prodInfoDesc;
  69. private JComboBox prodInfoLoc;
  70. private JComboBox prodInfoDept;
  71. private JImage prodInfoImage;
  72.  
  73. private JTextField userLoginField;
  74. private JPasswordField passLoginField;
  75.  
  76. // Order Panel info
  77.  
  78. private JLabel productNum;
  79. private JLabel productName;
  80. private JLabel productDesc;
  81. private JLabel productDept;
  82. private JLabel productLocation;
  83.  
  84. // Donation Panel info
  85. private JTextField productDName;
  86. private JTextArea productDescription;
  87.  
  88.  
  89. //order and donation button
  90. private JButton confirm;
  91. private JButton cancel;
  92.  
  93. /**
  94. * The main entry point for the program. This basically starts everything
  95. *
  96. * @param args A string array of command line arguments
  97. */
  98. public static void main(String[] args) {
  99. try {
  100. try {
  101. // We do this because Xfce is defaulted to the hideous Motif theme...
  102. UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
  103. } catch (UnsupportedLookAndFeelException e) {
  104. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  105. } catch (ClassNotFoundException e) {
  106. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  107. }
  108. }catch (Exception e) {
  109. e.printStackTrace();
  110. }
  111.  
  112.  
  113. frame = new JFrame("Off The Floor Administrative Panel");
  114. frame.setContentPane(new MainGUI());
  115. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  116. //frame.setPreferredSize(new Dimension(320, 240));
  117. //frame.setSize(320, 240);
  118. frame.setResizable(false);
  119. frame.setVisible(true);
  120. frame.pack();
  121. }
  122.  
  123. /**
  124. *
  125. */
  126. private MainGUI() {
  127. try {
  128. ScriptingContainer container = new ScriptingContainer();
  129. // implemented by a Ruby class
  130. Object receiver = container.runScriptlet(PathType.CLASSPATH, dbFile);
  131. db = container.getInstance(receiver, Database.class);
  132. } catch (Exception e) {}
  133.  
  134. setLayout(new BorderLayout());
  135. contentPane = new JPanel();
  136. tabbedPane = new JTabbedPane();
  137.  
  138. tabbedPane.addTab("Members", getUserPanel());
  139.  
  140. tabbedPane.addTab("Products", getProductPanel());
  141.  
  142. tabbedPane.addTab("Orders", getCartPanel());
  143.  
  144. tabbedPane.addTab("Donations", getDonationPanel());
  145.  
  146. //tabbedPane.setSelectedIndex(2);
  147.  
  148. JToolBar toolbar = new JToolBar();
  149. toolbar.setLayout(new BorderLayout());
  150. toolbar.setFloatable(false);
  151. toolbar.add(toolbarUser = new JLabel(""), BorderLayout.WEST);
  152. toolbar.add(toolbarStatus = new JLabel("",JLabel.RIGHT), BorderLayout.EAST);
  153.  
  154. new Thread(this).start();
  155. contentPane.add(getLoginPanel());
  156.  
  157. add(contentPane,BorderLayout.CENTER);
  158. add(toolbar,BorderLayout.SOUTH);
  159.  
  160.  
  161. }
  162.  
  163. /**
  164. *
  165. * @return
  166. */
  167. private JPanel getLoginPanel() {
  168. JPanel panel = new JPanel();
  169. panel.setLayout(new FlowLayout());
  170.  
  171. JPanel login = new JPanel();
  172. GroupLayout layout = new GroupLayout(login);
  173. login.setLayout(layout);
  174.  
  175. layout.setAutoCreateGaps(true);
  176. layout.setAutoCreateContainerGaps(true);
  177. JLabel welcome = new JLabel("Welcome!");
  178. welcome.setFont(welcome.getFont().deriveFont(welcome.getFont().getSize2D()+5).deriveFont(Font.BOLD));
  179.  
  180. JLabel l1 = new JLabel("Username:",JLabel.LEFT);
  181. JLabel l2 = new JLabel("Password:",JLabel.LEFT);
  182.  
  183. userLoginField = new JTextField();
  184. passLoginField =new JPasswordField();
  185.  
  186. Dimension uNameSize = userLoginField.getPreferredSize();
  187. uNameSize.width = 120;
  188. JButton proceed = new JButton("Login");
  189. JButton exit = new JButton("Exit");
  190. proceed.addActionListener(new LoginListener());
  191. exit.addActionListener(new ExitListener());
  192. userLoginField.addActionListener(new LoginListener());
  193. passLoginField.addActionListener(new LoginListener());
  194.  
  195. userLoginField.setPreferredSize(uNameSize);
  196. passLoginField.setPreferredSize(uNameSize);
  197.  
  198. layout.setHorizontalGroup(
  199. layout.createParallelGroup(GroupLayout.Alignment.CENTER)
  200. .addComponent(welcome)
  201. .addGroup(layout.createSequentialGroup()
  202. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  203. .addComponent(l1)
  204. .addComponent(l2))
  205. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
  206. .addComponent(userLoginField)
  207. .addComponent(passLoginField))
  208. ).addGroup(layout.createSequentialGroup()
  209. .addComponent(proceed)
  210. .addComponent(exit))
  211. );
  212. layout.setVerticalGroup(
  213. layout.createSequentialGroup()
  214. .addComponent(welcome)
  215. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  216. .addComponent(l1)
  217. .addComponent(userLoginField))
  218. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  219. .addComponent(l2)
  220. .addComponent(passLoginField))
  221. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  222. .addComponent(proceed)
  223. .addComponent(exit))
  224. );
  225.  
  226. panel.add(login);
  227.  
  228. Dimension size = new Dimension((int)tabbedPane.getPreferredSize().getWidth(), (int)tabbedPane.getPreferredSize().getHeight());
  229.  
  230. panel.setPreferredSize(size);
  231. return panel;
  232. }
  233.  
  234. /**
  235. *
  236. * @return
  237. */
  238. private JPanel getUserPanel() {
  239. JPanel panel = new JPanel();
  240. panel.setLayout(new BorderLayout());
  241. JPanel info = new JPanel();
  242.  
  243. userList = new JList();
  244. userList.setVisibleRowCount(5);
  245. userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  246. userList.addListSelectionListener(new UserListSelectionListener());
  247. //userList.setFixedCellHeight(15);
  248. //userList.setFixedCellWidth(110);
  249.  
  250. GroupLayout layout = new GroupLayout(info);
  251. info.setLayout(layout);
  252.  
  253. layout.setAutoCreateGaps(true);
  254. layout.setAutoCreateContainerGaps(true);
  255. JLabel l1 = new JLabel("Name:",JLabel.LEFT);
  256. JLabel l2 = new JLabel("Email:",JLabel.LEFT);
  257. JLabel l3 = new JLabel("Username:",JLabel.LEFT);
  258. JLabel l4 = new JLabel("Address:",JLabel.LEFT);
  259. JLabel l5 = new JLabel("Phone:", JLabel.LEFT);
  260.  
  261. String[] stateAbbr = { "AK", "AL", "AR", "AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC",
  262. "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA",
  263. "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE",
  264. "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC",
  265. "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY" };
  266.  
  267. state.setModel(new DefaultComboBoxModel( stateAbbr));
  268. zip.setColumns(6);
  269.  
  270. layout.setHorizontalGroup(
  271. layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  272. .addGroup(layout.createSequentialGroup()
  273. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  274. .addComponent(l1)
  275. .addComponent(fName)
  276. .addComponent(l3)
  277. .addComponent(username)
  278. )
  279. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  280. .addComponent(lName)
  281. .addComponent(l5)
  282. .addComponent(phoneNo)
  283. )
  284. )
  285. .addComponent(l2)
  286. .addComponent(email)
  287. .addComponent(l4)
  288.  
  289. .addGroup(layout.createSequentialGroup()
  290. .addComponent(add1)
  291. .addComponent(add2))
  292. .addGroup(layout.createSequentialGroup()
  293. .addComponent(city)
  294. .addGroup(layout.createSequentialGroup()
  295. .addComponent(state,GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
  296. GroupLayout.PREFERRED_SIZE)
  297. .addComponent(zip, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
  298. GroupLayout.PREFERRED_SIZE)
  299. )
  300. )
  301. );
  302. layout.setVerticalGroup(
  303. layout.createSequentialGroup()
  304. .addComponent(l1)
  305. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  306. .addComponent(fName)
  307. .addComponent(lName))
  308. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  309. .addComponent(l3)
  310. .addComponent(l5)
  311. )
  312. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  313. .addComponent(username)
  314. .addComponent(phoneNo)
  315. )
  316. .addComponent(l2)
  317. .addComponent(email, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
  318. GroupLayout.PREFERRED_SIZE)
  319. .addComponent(l4)
  320. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  321. .addComponent(add1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
  322. GroupLayout.PREFERRED_SIZE)
  323. .addComponent(add2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
  324. GroupLayout.PREFERRED_SIZE))
  325. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  326. .addComponent(city)
  327. .addComponent(state)
  328. .addComponent(zip))
  329. );
  330.  
  331.  
  332. JScrollPane userScroller = new JScrollPane(userList,
  333. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  334. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  335. userScroller.setPreferredSize(new Dimension(120,0));
  336.  
  337. panel.add(userScroller,BorderLayout.WEST);
  338.  
  339. panel.add(info);
  340.  
  341. panel.setPreferredSize(new Dimension(450, 300));
  342. return panel;
  343. }
  344.  
  345. /**
  346. *
  347. * @return
  348. */
  349. private JPanel getProductPanel() {
  350. JPanel panel = new JPanel(new BorderLayout(4,4));
  351.  
  352. productPane = new JPanel();
  353.  
  354. /* Start Product Tree */
  355. departmentList = new JList();
  356. locationList = new JComboBox();
  357.  
  358.  
  359. departmentList.setVisibleRowCount(5);
  360. departmentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  361.  
  362.  
  363. JScrollPane treeScroller = new JScrollPane(departmentList,
  364. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  365. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  366. treeScroller.setPreferredSize(new Dimension(120,0));
  367.  
  368. /* End Product Tree */
  369. /* Start Search bar */
  370.  
  371. JPanel searchBar = new JPanel();
  372. GroupLayout layout = new GroupLayout(searchBar);
  373. searchBar.setLayout(layout);
  374.  
  375.  
  376. //locationList.setSelectedIndex(0);
  377. locationList.setPreferredSize(new Dimension(120, locationList.getPreferredSize().height));
  378. JTextField searchTerms = new JTextField();
  379. JButton searchButton = new JButton("Search");
  380.  
  381. layout.setAutoCreateGaps(true);
  382.  
  383.  
  384. layout.setHorizontalGroup(
  385. layout.createSequentialGroup()
  386. .addComponent(locationList, GroupLayout.PREFERRED_SIZE,
  387. GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  388. .addComponent(searchTerms)
  389. .addComponent(searchButton)
  390. );
  391. layout.setVerticalGroup(
  392. layout.createSequentialGroup()
  393. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  394. .addComponent(locationList, GroupLayout.PREFERRED_SIZE,
  395. GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  396. .addComponent(searchTerms)
  397. .addComponent(searchButton))
  398. );
  399.  
  400.  
  401.  
  402. /* End Search Bar */
  403.  
  404. GridLayout gl = new GridLayout(0,1);
  405. gl.setVgap(4);
  406. productPane = new JPanel (gl);
  407.  
  408.  
  409. JScrollPane productScroller = new JScrollPane(productPane,
  410. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  411. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  412.  
  413.  
  414. panel.add(treeScroller,BorderLayout.WEST);
  415. panel.add(productScroller, BorderLayout.CENTER);
  416. panel.add(searchBar, BorderLayout.NORTH);
  417. panel.setPreferredSize(new Dimension(450, 300));
  418. return panel;
  419. }
  420.  
  421.  
  422. /**
  423. * The cart panel will track the items that the users have requested
  424. *
  425. * @return
  426. */
  427. private JPanel getCartPanel() {
  428. JPanel cart = new JPanel(new BorderLayout(4,4));
  429. JPanel info = new JPanel();
  430.  
  431. itemList = new JList();
  432. itemList.setVisibleRowCount(5);
  433. itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  434.  
  435. GroupLayout layout = new GroupLayout(info);
  436. info.setLayout(layout);
  437.  
  438. layout.setAutoCreateGaps(true);
  439. layout.setAutoCreateContainerGaps(true);
  440. //JButton submit = new JButton("Submit");
  441. JLabel l1 = new JLabel("Product Number:",JLabel.LEFT);
  442. productNum = new JLabel("",JLabel.LEFT);
  443. JLabel l2 = new JLabel("Product Name:", JLabel.LEFT);
  444. productName = new JLabel("", JLabel.LEFT);
  445. JLabel l3 = new JLabel("Product Description:", JLabel.LEFT);
  446. productDesc = new JLabel("", JLabel.LEFT);
  447. JLabel l4 = new JLabel("Product Department:", JLabel.LEFT);
  448. productDept = new JLabel("", JLabel.LEFT);
  449. JLabel l5 = new JLabel("Product Location:", JLabel.LEFT);
  450. productLocation = new JLabel("", JLabel.LEFT);
  451.  
  452. // TODO: You should assign the name of these buttons as well
  453. // See productInfoPane() for an example
  454. confirm = new JButton("Confirm");
  455. cancel = new JButton("Cancel");
  456. confirm.addActionListener(new OrderConfirmButtonListener());
  457. cancel.addActionListener(new OrderCancelButtonListener());
  458.  
  459. layout.setHorizontalGroup(
  460. layout.createSequentialGroup()
  461. .addGap(10)
  462. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  463. .addComponent(l1)
  464. .addComponent(l2)
  465. .addComponent(l3)
  466. .addComponent(l4)
  467. .addComponent(l5)
  468.  
  469. .addComponent(confirm, GroupLayout.Alignment.TRAILING))
  470. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  471. .addComponent(productNum)
  472. .addComponent(productName)
  473. .addComponent(productDesc)
  474. .addComponent(productDept)
  475. .addComponent(productLocation)
  476.  
  477. .addComponent(cancel))
  478. );
  479. layout.setVerticalGroup(
  480. layout.createSequentialGroup()
  481. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  482. .addComponent(l1)
  483. .addComponent(productNum))
  484. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  485. .addComponent(l2)
  486. .addComponent(productName))
  487. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  488. .addComponent(l3)
  489. .addComponent(productDesc))
  490. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  491. .addComponent(l4)
  492. .addComponent(productDept))
  493. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  494. .addComponent(l5)
  495. .addComponent(productLocation)
  496. .addGap(175, 175, 175))
  497. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
  498. .addComponent(confirm)
  499. .addComponent(cancel))
  500. );
  501.  
  502.  
  503.  
  504. JScrollPane userScroller = new JScrollPane(itemList,
  505. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  506. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  507. userScroller.setPreferredSize(new Dimension(120,0));
  508.  
  509. cart.add(userScroller,BorderLayout.WEST);
  510.  
  511. cart.add(info);
  512.  
  513.  
  514.  
  515.  
  516. cart.setPreferredSize(new Dimension(450, 300));
  517. return cart;
  518. }
  519.  
  520. private JPanel getDonationPanel() {
  521. JPanel donate = new JPanel(new BorderLayout(4,4));
  522. JPanel info = new JPanel();
  523.  
  524. itemList = new JList();
  525. itemList.setVisibleRowCount(5);
  526. itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  527.  
  528. GroupLayout layout = new GroupLayout(info);
  529. info.setLayout(layout);
  530.  
  531. layout.setAutoCreateGaps(true);
  532. layout.setAutoCreateContainerGaps(true);
  533. JLabel l1 = new JLabel("Product name:", JLabel.LEFT);
  534. productDName = new JTextField();
  535. JLabel l2 = new JLabel("Product description:", JLabel.LEFT);
  536. productDescription = new JTextArea(5,2);
  537.  
  538. // FIXME: Locations need added.
  539. JLabel l3 = new JLabel("Location", Label.LEFT);
  540. String[] quality = { "", "", ""};
  541. JComboBox qualityList = new JComboBox(quality);
  542.  
  543. JLabel l4 = new JLabel("Furniture type:", JLabel.LEFT);
  544. String[] type = {"Bed", "Box Spring", "Bed Frame", "Table/Chairs", "Dresser", "Couch/Loveseat", "Living room Chair", "Lamp", "End Table", "Crib", "Crib Mattress"};
  545.  
  546. // TODO: This data should be obtained from the global variable 'departments'
  547. JComboBox furnitureType = new JComboBox(type);
  548.  
  549.  
  550. confirm = new JButton("Confirm");
  551. cancel = new JButton("Cancel");
  552. confirm.addActionListener(new DonationButtonListener());
  553. cancel.addActionListener(new DonationCancelButtonListener());
  554.  
  555. layout.setHorizontalGroup(
  556. layout.createSequentialGroup()
  557. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  558. .addComponent(l1, GroupLayout.Alignment.LEADING)
  559. .addComponent(l4, GroupLayout.Alignment.LEADING)
  560. .addComponent(l2, GroupLayout.Alignment.LEADING)
  561. .addComponent(l3, GroupLayout.Alignment.LEADING)
  562. .addGap(200, 200, 200)
  563. .addComponent(confirm, GroupLayout.Alignment.TRAILING))
  564. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  565. .addComponent(productName, GroupLayout.Alignment.LEADING)
  566. .addComponent(furnitureType, GroupLayout.Alignment.LEADING)
  567. .addComponent(productDescription, GroupLayout.Alignment.LEADING)
  568. .addComponent(qualityList, GroupLayout.Alignment.LEADING)
  569. .addGap(200, 200, 200)
  570. .addComponent(cancel, GroupLayout.Alignment.LEADING))
  571. );
  572. layout.setVerticalGroup(
  573. layout.createSequentialGroup()
  574. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  575. .addComponent(l1)
  576. .addComponent(productName))
  577. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  578. .addComponent(l4)
  579. .addComponent(furnitureType))
  580. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  581. .addComponent(l2)
  582. .addComponent(productDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
  583. GroupLayout.PREFERRED_SIZE))
  584. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  585. .addComponent(l3)
  586. .addComponent(qualityList))
  587. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  588. .addGap(75, 75, 75))
  589. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  590.  
  591. .addComponent(confirm)
  592. .addComponent(cancel))
  593. );
  594.  
  595. donate.add(info);
  596.  
  597. donate.setPreferredSize(new Dimension(450, 300));
  598. return donate;
  599. }
  600.  
  601.  
  602. private JPanel createProductPanel(Product prod) {
  603.  
  604. JPanel panel;
  605. JButton button1;
  606. JButton button2;
  607. JButton button3;
  608. JImage image;
  609. JLabel name;
  610. JTextArea description;
  611.  
  612.  
  613. panel = new JPanel();
  614. GroupLayout layout = new GroupLayout(panel);
  615. panel.setLayout(layout);
  616. panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  617.  
  618. button1 = new JButton(new ImageIcon("resources/remove.png"));
  619. button2 = new JButton(new ImageIcon("resources/pencil.png"));
  620. button3 = new JButton(new ImageIcon("resources/edit.png"));
  621.  
  622. button1.setBorderPainted(false);
  623. button2.setBorderPainted(false);
  624. button3.setBorderPainted(false);
  625.  
  626. button1.addActionListener(new RemoveProductListener());
  627. button2.addActionListener(new EditProductListener());
  628.  
  629. // Setting the name allows us to determine which product
  630. // this button belongs to when an ActionListener is fired
  631. button1.setName("" + prod.getId());
  632. button2.setName("" + prod.getId());
  633. button3.setName("" + prod.getId());
  634.  
  635. image = new JImage(prod.getImage(), 80, 80);
  636. name = new JLabel(prod.getName(), JLabel.LEFT);
  637. name.setFont(getFont().deriveFont(Font.BOLD));
  638. description = new JTextArea(prod.getDescription());
  639. description.setLineWrap(true);
  640. description.setEditable(false);
  641. description.setOpaque(false);
  642. description.setWrapStyleWord(true);
  643. description.setForeground(name.getForeground());
  644.  
  645. layout.setAutoCreateGaps(true);
  646. layout.setHorizontalGroup(
  647. layout.createSequentialGroup()
  648. .addComponent(image)
  649. .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED,
  650. 2, 2)
  651. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  652. .addComponent(name)
  653. .addComponent(description))
  654. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  655. .addComponent(button1)
  656. .addComponent(button2))
  657. );
  658. layout.setVerticalGroup(
  659. layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  660. .addComponent(image)
  661. .addGroup(layout.createSequentialGroup()
  662. .addComponent(name)
  663. .addComponent(description))
  664. .addGroup(layout.createSequentialGroup()
  665. .addComponent(button1)
  666. .addComponent(button2))
  667. );
  668.  
  669. return panel;
  670.  
  671. }
  672.  
  673. private JPanel getProductInfoPane(Product product) {
  674. JPanel panel = new JPanel();
  675.  
  676. GroupLayout layout = new GroupLayout(panel);
  677. panel.setLayout(layout);
  678. panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  679.  
  680. JButton change = new JButton("Change");
  681. JButton clear = new JButton("Clear");
  682. JButton close = new JButton("Close", new ImageIcon("resources/edit.png"));
  683. JButton save = new JButton("Save", new ImageIcon("resources/edit.png"));
  684. close.addActionListener(new CloseProductListener());
  685. save.addActionListener(new SaveProductListener());
  686. change.addActionListener(new ChangeProductImageListener());
  687.  
  688.  
  689. // Setting the name allows us to determine which product
  690. // this button belongs to when an ActionListener is fired
  691. change.setName("" + product.getId());
  692. clear.setName("" + product.getId());
  693. close.setName("" + product.getId());
  694. save.setName("" + product.getId());
  695.  
  696.  
  697.  
  698. JLabel l1 = new JLabel("Product " + product.getId());
  699. JLabel l2 = new JLabel("Name:");
  700. JLabel l3 = new JLabel("Description:");
  701. JLabel l4 = new JLabel("Location:");
  702. JLabel l5 = new JLabel("Department:");
  703.  
  704. prodInfoName = new JTextField(product.getName());
  705. prodInfoDesc = new JTextArea(product.getDescription());
  706. prodInfoLoc = new JComboBox(locations);
  707. prodInfoDept = new JComboBox(departments);
  708. prodInfoImage = new JImage(product.getImage(), 130, 130);
  709.  
  710. prodInfoDesc.setLineWrap(true);
  711. prodInfoDesc.setWrapStyleWord(true);
  712.  
  713. prodInfoLoc.setSelectedItem(new Location(product.getLocation(), "","","","","",""));
  714. prodInfoDept.setSelectedItem(new Department(product.getDepartment(), ""));
  715.  
  716.  
  717. layout.setAutoCreateGaps(true);
  718. layout.setHorizontalGroup(
  719. layout.createSequentialGroup()
  720. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  721. .addComponent(prodInfoImage)
  722. .addGroup(layout.createSequentialGroup()
  723. .addComponent(change)
  724. .addComponent(clear)
  725. )
  726. .addComponent(l4)
  727. .addComponent(prodInfoLoc)
  728. )
  729. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  730. .addComponent(l1)
  731. .addComponent(l2)
  732. .addComponent(prodInfoName)
  733. .addComponent(l3)
  734. .addComponent(prodInfoDesc)
  735. .addComponent(l5)
  736. .addComponent(prodInfoDept)
  737. .addGroup(layout.createSequentialGroup()
  738. .addComponent(close)
  739. .addComponent(save)
  740. ))
  741. );
  742. layout.setVerticalGroup(
  743. layout.createSequentialGroup()
  744. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  745. .addGroup(layout.createSequentialGroup()
  746. .addComponent(prodInfoImage)
  747. .addGroup(layout.createParallelGroup()
  748. .addComponent(change)
  749. .addComponent(clear)
  750. )
  751. )
  752. .addGroup(layout.createSequentialGroup()
  753. .addComponent(l1)
  754. .addComponent(l2)
  755. .addComponent(prodInfoName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
  756. GroupLayout.PREFERRED_SIZE)
  757. .addComponent(l3)
  758. .addComponent(prodInfoDesc)
  759. )
  760. )
  761. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  762. .addComponent(l4)
  763. .addComponent(l5)
  764. )
  765. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  766. .addComponent(prodInfoLoc)
  767. .addComponent(prodInfoDept)
  768. )
  769. .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
  770. .addComponent(close)
  771. .addComponent(save)
  772. )
  773. );
  774.  
  775. return panel;
  776. }
  777.  
  778. /**
  779. * Loads all data from the database
  780. */
  781. private void loadData() {
  782.  
  783.  
  784. if(!db.isConnectionValid())
  785. return;
  786.  
  787. // FIXME: We should check where the user is an admin or not
  788. // and adjust the the interface accordingly
  789.  
  790. try {
  791. members = db.getMembers();
  792. products = db.getProducts(1);
  793. locations = db.getLocations();
  794. departments = db.getDepartments();
  795. //orders = db.getOrders();
  796. } catch (SQLException e) {
  797. e.printStackTrace();
  798. }
  799.  
  800.  
  801. userList.setListData(members.toArray());
  802. departmentList.setListData(departments.toArray());
  803. locationList.setModel(new DefaultComboBoxModel(locations));
  804.  
  805. /* Start productPane creation */
  806. productPane.removeAll();
  807.  
  808. for (int i = 0; i < products.size(); i++) {
  809. productPane.add(createProductPanel(products.get(i)));
  810. }
  811.  
  812.  
  813.  
  814.  
  815. }
  816.  
  817. /**
  818. * Handles login related actions
  819. */
  820. class LoginListener implements ActionListener {
  821.  
  822. /**
  823. * Reacts when an event is raised
  824. *
  825. * @param e the event object.
  826. */
  827. @Override
  828. public void actionPerformed(ActionEvent e) {
  829.  
  830. // FIXME: Change this check before release
  831. if (db.isConnectionValid()) {
  832. try {
  833. currentMember = db.authMember(userLoginField.getText(), passLoginField.getPassword());
  834. toolbarUser.setText("Logged in as: " + currentMember.getUserName());
  835. } catch (NullPointerException npe) {
  836. return;
  837. } catch (SQLException sqle) {
  838. // TODO Auto-generated catch block
  839. sqle.printStackTrace();
  840. }
  841. } else {
  842. toolbarUser.setText("Logged in as: " + "Username");
  843. currentMember = new Member(0, "","","","","","", "","","","","", false);
  844. }
  845.  
  846. //System.out.println("Fetching data");
  847. loadData();
  848. contentPane.removeAll();
  849. contentPane.add(tabbedPane);
  850. contentPane.repaint();
  851. frame.setVisible(true);
  852. frame.pack();
  853.  
  854. }
  855. }
  856.  
  857. /**
  858. * Allows the program to run actions on program exit
  859. */
  860. class ExitListener implements ActionListener {
  861.  
  862. /**
  863. * Reacts when an event is raised
  864. *
  865. * @param e the event object.
  866. */
  867. @Override
  868. public void actionPerformed(ActionEvent e) {
  869. System.exit(0);
  870.  
  871. }
  872. }
  873.  
  874. class UserListSelectionListener implements ListSelectionListener {
  875.  
  876. public void valueChanged(ListSelectionEvent evt) {
  877. // When the user release the mouse button and completes the selection,
  878. // getValueIsAdjusting() becomes false
  879. if (!evt.getValueIsAdjusting()) {
  880. Member selected = (Member) userList.getSelectedValue();
  881.  
  882. fName.setText(selected.getFName());
  883. lName.setText(selected.getLName());
  884. phoneNo.setText(selected.getPhoneNo());
  885. username.setText(selected.getUserName());
  886. email.setText(selected.getEmail());
  887. add1.setText(selected.getAdd1());
  888. add2.setText(selected.getAdd2());
  889. city.setText(selected.getCity());
  890. state.setSelectedItem(selected.getState());
  891. zip.setText(selected.getZip());
  892.  
  893. }
  894. }
  895. }
  896.  
  897.  
  898. class RemoveProductListener implements ActionListener {
  899.  
  900.  
  901. public void actionPerformed(ActionEvent e) {
  902. int choice = JOptionPane.showConfirmDialog(frame,
  903. "You attempted to remove item " + ((JButton)e.getSource()).getName() + ".\n" +
  904. "Would you like to continue?",
  905. "Remove item",
  906. JOptionPane.YES_NO_OPTION);
  907. if(choice == JOptionPane.YES_OPTION){
  908.  
  909.  
  910. }
  911.  
  912. System.out.println(choice);
  913. }
  914. }
  915.  
  916.  
  917. class EditProductListener implements ActionListener {
  918.  
  919.  
  920. public void actionPerformed(ActionEvent e) {
  921. int productID = Integer.parseInt(((JButton)e.getSource()).getName());
  922.  
  923. productPane.removeAll();
  924. productPane.add(getProductInfoPane(products.get(products.indexOf(new Product(productID, "", "", 0, 0, null)))));
  925.  
  926. //products.indexOf(new Product(productID, "", "", 0, 0, null));
  927.  
  928. }
  929. }
  930.  
  931. class CloseProductListener implements ActionListener {
  932.  
  933.  
  934. public void actionPerformed(ActionEvent e) {
  935.  
  936. productPane.removeAll();
  937. for (int i = 0; i < products.size(); i++) {
  938. productPane.add(createProductPanel(products.get(i)));
  939. }
  940.  
  941. //products.indexOf(new Product(productID, "", "", 0, 0, null));
  942.  
  943. }
  944. }
  945.  
  946. class ChangeProductImageListener implements ActionListener {
  947.  
  948.  
  949. public void actionPerformed(ActionEvent e) {
  950. JFileChooser fc = new JFileChooser();
  951. fc.addChoosableFileFilter(new ImageFilter());
  952. fc.setAcceptAllFileFilterUsed(false);
  953. int returnVal = fc.showDialog(MainGUI.this, "Select");
  954.  
  955. if (returnVal == JFileChooser.APPROVE_OPTION) {
  956. File file = fc.getSelectedFile();
  957.  
  958. try {
  959. prodInfoImage.setImage(ImageIO.read(file));
  960. prodInfoImage.repaint();
  961. } catch (IOException ex) {
  962. JOptionPane.showMessageDialog(frame, "Could not change image.\nPlease try again.",
  963. "Error",JOptionPane.ERROR_MESSAGE);
  964. }
  965. }
  966.  
  967. }
  968. }
  969.  
  970. class SaveProductListener implements ActionListener {
  971.  
  972.  
  973. public void actionPerformed(ActionEvent e) {
  974. int productID = Integer.parseInt(((JButton)e.getSource()).getName());
  975. int prodIndex = products.indexOf(new Product(productID, "", "", 0, 0, null));
  976.  
  977. Product prod = products.get(prodIndex);
  978. prod.setImage(prodInfoImage.getImage());
  979. prod.setName(prodInfoName.getText());
  980. prod.setDescription(prodInfoDesc.getText());
  981. prod.setDepartment(((Department)prodInfoDept.getSelectedItem()).getId());
  982. prod.setLocation(((Location)prodInfoLoc.getSelectedItem()).getId());
  983.  
  984. products.set(prodIndex, prod);
  985.  
  986. try {
  987. if(!db.isConnectionValid()) throw new SQLException();
  988. db.updateProduct(prod);
  989. } catch (SQLException e1) {
  990. JOptionPane.showMessageDialog(frame, "An error occured when trying to save the product!",
  991. "Error",JOptionPane.ERROR_MESSAGE);
  992. }
  993.  
  994. }
  995. }
  996.  
  997.  
  998. class OrderConfirmButtonListener implements ActionListener{
  999.  
  1000. public void actionPerformed(ActionEvent e)
  1001. {
  1002. JOptionPane.showMessageDialog(frame, "Order has been confrimed", "Confirmataion", JOptionPane.PLAIN_MESSAGE);
  1003.  
  1004. }
  1005.  
  1006. }
  1007.  
  1008.  
  1009. //Should this remove an order too as if it was a canceled order?
  1010.  
  1011. class OrderCancelButtonListener implements ActionListener{
  1012.  
  1013. public void actionPerformed(ActionEvent e)
  1014. {
  1015. productNum.setText("");
  1016. productName.setText("");
  1017. productDesc.setText("");
  1018. productDept.setText("");
  1019. productLocation.setText("");
  1020.  
  1021.  
  1022.  
  1023. }
  1024. }
  1025.  
  1026. // needs to add product to the product database
  1027. class DonationButtonListener implements ActionListener{
  1028.  
  1029. public void actionPerformed(ActionEvent e)
  1030. {
  1031. JOptionPane.showMessageDialog(frame, "Donation has been accepted. Thank you for your contributions", "Donation", JOptionPane.PLAIN_MESSAGE);
  1032.  
  1033. }
  1034. }
  1035.  
  1036. class DonationCancelButtonListener implements ActionListener{
  1037.  
  1038. public void actionPerformed(ActionEvent e)
  1039. {
  1040. productDName.setText("");
  1041. productDescription.setText("");
  1042. }
  1043. }
  1044.  
  1045. /**
  1046. * Runs in a separate thread to perform actions concurrently with
  1047. * the program's <code>main</code> function.
  1048. */
  1049. public synchronized void run() {
  1050. while (true) {
  1051. if (db != null && db.isConnectionValid()) {
  1052. toolbarStatus.setText("Connected");
  1053. } else {
  1054. toolbarStatus.setText("Not Connected");
  1055. db.reconnect();
  1056. }
  1057. try {
  1058. wait(15000);
  1059. } catch (InterruptedException e) {
  1060. // TODO Auto-generated catch block
  1061. e.printStackTrace();
  1062. }
  1063. }
  1064.  
  1065. }
  1066.  
  1067. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement