/* * Created on Jun 13, 2005 * * HoldingsDisplay.java * */ /** * @author Paul Hogge * * This is the GUI that will display the list of books in * the library's holdings. A GUI will show the user the * book titles that are in the library's holdings. * */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HoldingsDisplay extends JFrame implements ActionListener, MouseListener{ // The state variable for this object. private JList list; private JMenuItem closeItem; private JMenuItem detailsItem; private JMenuItem editItem; private JButton editButton; private JTable table; private JButton closeButton; private JButton detailsButton; private JTextArea textArea; private Holdings htemp; private Book btemp; private BookDisplay bd; //private BookDisplay bd = new BookDisplay(); /** * Creates an empty constructor * for the object HoldingsDisplay. * */ public HoldingsDisplay() { } /** * This is the method that shall set up the * GUI, and display the book titles that are * in the library's holdings. Has a menu as * well as two buttons created in a panel. * @param h */ public void setHoldings(Holdings h) { // Gets the screen resolution. Optional here. Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenWidth = screenSize.width; int screenHeight = screenSize.height; // Creates the list. Obtains Book info from // Holdings.java method getList(). htemp = h; list = h.getList(); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setLayoutOrientation(JList.VERTICAL); list.addMouseListener(this); // Creates the window. JFrame.setDefaultLookAndFeelDecorated(true); JFrame displayMain = new JFrame("Holdings Display"); displayMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); displayMain.setSize(800, 600); // Option placement of GUI. Using default. // displayMain.setLocation(screenWidth/8, screenHeight/8); // Makes GUI visible displayMain.setVisible(true); // Builds the menu bar to the frame. JMenuBar menuBar = new JMenuBar(); displayMain.setJMenuBar(menuBar); // Creates menu object. JMenu editMenu = new JMenu("File"); // Top-level menu bar added. menuBar.add(editMenu); // Edit menu item added under file. // Listener set for this menu item. editItem = new JMenuItem("Edit"); editMenu.add(editItem); editItem.addActionListener(this); // Details menu item added under file. // Listener set for this menu item. detailsItem = new JMenuItem("Details"); editMenu.add(detailsItem); detailsItem.addActionListener(this); // Details menu item added under file. // Listener set for this menu item. closeItem = new JMenuItem("Quit"); editMenu.add(closeItem); closeItem.addActionListener(this); // Creates a panel, with three buttons on it. // Listeners are activated for these buttons. JPanel panel = new JPanel(); editButton = new JButton("Edit"); detailsButton = new JButton("Details"); closeButton = new JButton("Quit"); panel.add(editButton); editButton.addActionListener(this); panel.add(detailsButton); detailsButton.addActionListener(this); panel.add(closeButton); closeButton.addActionListener(this); // Displays the list and the panels. //displayMain.add(panel, BorderLayout.SOUTH); //mod - commented out - spec hw 3 - use JTable, not JList! //displayMain.add(list, BorderLayout.CENTER); // Updates the GUI with the mods. displayMain.validate(); bd = new BookDisplay(); bd.init(); ///* //mod tableDisplay(); displayMain.setLayout(new BorderLayout()); displayMain.add(table.getTableHeader(), BorderLayout.PAGE_START); displayMain.add(panel, BorderLayout.SOUTH); displayMain.add(table, BorderLayout.CENTER); displayMain.validate(); //*/ } public void tableDisplay() { String[] columnNames = { "Title", "First Name", "Last Name", "Publisher", "ISBN", "Year"}; Object[][] data = htemp.getMatrix(); table = new JTable(data, columnNames); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } /** * This is ActionListener and the results * that will occur for a given action within * the user interface. For buttons and menu. */ public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if (obj.equals(closeItem) || obj.equals(closeButton)) { System.exit(0); } else if ( (obj.equals(detailsItem) && (table.getSelectedRow() != -1)) || (obj.equals(detailsButton)) && (table.getSelectedRow() != -1) ) { System.out.println("DETAILS HIT"); System.out.println(list.getSelectedIndex()); btemp = htemp.getBook(table.getSelectedRow()); //btemp = htemp.getBook(list.getSelectedIndex()); //BookDisplay bd = new BookDisplay(); //bd.setBook(btemp); bd.setBook(btemp); } else if ( (obj.equals(editItem) && (table.getSelectedRow() != -1)) || (obj.equals(editButton)) && (table.getSelectedRow() != -1) ) { System.out.println("EDIT HIT"); } } /** * This is the MouseListener and the results * that will occur for a give action within * the user interface. For the mouse clicks. * Only double click method is defined here. */ public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { System.out.println("DETAILS HIT"); System.out.println(list.getSelectedIndex()); btemp = htemp.getBook(list.getSelectedIndex()); //BookDisplay bd = new BookDisplay(); //bd.setBook(btemp); bd.setBook(btemp); } } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }