/* * ServerGui.java * * Created on November 17, 2006, 1:09 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.net.*;
/** * The GUI display for the NewShop server * @author dsi06dh Dave Hollowell * * @version 0.1 23rd November 2006 */public class ServerGui extends Thread implements ActionListener{ JPanel mainPanel, infoPanel, enquiryPanel; JTextField requiredCustomer; JButton orderHistory; JTextArea orderDetails, serverInfo; /** * End of line character */ private static final String NEWLINE = System.getProperty("line.separator"); Warehouse theWarehouse; /** Creates a new instance of ServerGui */ public ServerGui() { // set up the enquiry section enquiryPanel = createEnquirySection(); infoPanel = createInfoSection(); //Create the main panel to contain the two sub panels. mainPanel = new JPanel(new GridLayout(2,1)); mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); //Add the select and display panels to the main panel. mainPanel.add(enquiryPanel); mainPanel.add(infoPanel); //Create and set up the window. JFrame newShopFrame = new JFrame("NewShop plc"); newShopFrame.add(mainPanel); newShopFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); newShopFrame.setContentPane(mainPanel); //Display the window. newShopFrame.pack(); newShopFrame.setVisible(true);
} /** * Create the enquiry section * @return JPanel - the panel to use */ private JPanel createEnquirySection() { // made up of two panels // top part allows the user to enter a customer and request to see the // order history. the bottom part allows the user to see the order // history in a scrollable textarea. JPanel display = new JPanel(new BorderLayout()); JPanel pane = new JPanel(new FlowLayout()); JPanel pane2 = new JPanel(new BorderLayout()); // customer select field requiredCustomer = new JTextField(10); pane.add(requiredCustomer); // order history request button orderHistory = new JButton("Customer order history"); orderHistory.setActionCommand("History"); orderHistory.addActionListener(this); pane.add(orderHistory); // order history details orderDetails = new JTextArea(5, 20); orderDetails.setEditable(false); JScrollPane scrollPane = new JScrollPane(orderDetails, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); pane2.add(scrollPane, BorderLayout.CENTER); pane2.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); // add details to the branch panel display.add(pane, BorderLayout.PAGE_START); display.add(pane2, BorderLayout.CENTER); //Add a border around the enquiry panel. display.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Customer order history"), BorderFactory.createEmptyBorder(5,5,5,5))); return display; } /** * Create the info section * @return JPanel - the required panel */ private JPanel createInfoSection() { // set up the server information section JPanel display = new JPanel(new BorderLayout()); serverInfo = new JTextArea(7,40); serverInfo.setEditable(false); JScrollPane scrollPane = new JScrollPane(serverInfo, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); display.add(scrollPane, BorderLayout.CENTER); display.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Server information"), BorderFactory.createEmptyBorder(5,5,5,5))); return display; } /** * processes an event from the GUI display * @param event The window event */ public void actionPerformed(ActionEvent event) { Object source = event.getActionCommand(); Customer theCustomer = null; String details = null; if (source == "History") { String customer = requiredCustomer.getText().toUpperCase(); if (!(customer.equals(""))) { theCustomer = theWarehouse.getCustomer(customer); if (theCustomer != null) { orderDetails.setText("Order history for " + customer + NEWLINE); requiredCustomer.setText(""); details = theWarehouse.getOrderHistory(theCustomer); orderDetails.append(details + NEWLINE); orderDetails.append("** End of order history **" + NEWLINE); } else orderDetails.setText("No order history for " + customer); requiredCustomer.setText(""); } else { orderDetails.setText( "A customer is required to display order history"); } } } /** * Set reference to the warehouse to use * @param aWarehouse Warehouse - a reference to the warehouse to use */ public void setWarehouse(Warehouse aWarehouse) { theWarehouse = aWarehouse; } /** * Add the message to the info section * @param info The message to de displayed */ public void statusMessage(String info) { serverInfo.append(info); } /** * Main method allows for testing of the class. * * @param args the command line arguments */ public static void main(String[] args) { Warehouse newShop; ServerGui sg = new ServerGui(); // set up the warehouse deatils //String branchFile = "/home3/dsi06dh/Comp5226/BranchFile.txt"; //String customerFile = "/home3/dsi06dh/Comp5226/CustomerFile.txt"; String branchFile = "C://JavaWork//Coursework//BranchFile.txt"; String customerFile = "C://JavaWork//Coursework//CustomerFile.txt"; sg.statusMessage("Initialising warehouse." + NEWLINE); newShop = new Warehouse(branchFile, customerFile); sg.setWarehouse(newShop); sg.statusMessage("Warehouse initialised." + NEWLINE); }}