import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; import java.util.*; import javax.swing.border.*; /** * Class representing the Order UI dialog. * Responsible for collecting user input, * creating Order and Pizza objects, and * updating the order queue. */ public class OrderUI extends JFrame { private final OrderQueue _queue; // The central queue of active orders private final OrderPrinter _printer; // Printer to print pizza labels on //--> These fields and buttons need to be referenced throughout //--> the lifetime of the OrderUI, so they're created at //--> construction time and kept track of with reference fields. private final JTextField _sizeField = new JTextField(2); private final JTextField _toppingsField = new JTextField(12); private final JTextField _nameField = new JTextField(30); private final JTextField _addressField = new JTextField(45); private final JTextField _phoneField = new JTextField(14); private final JLabel _statusField = new JLabel(" "); private final JButton _addButton = new JButton("Add"); private final JButton _cancelButton = new JButton("Cancel"); private final JButton _doneButton = new JButton("Done"); //--> The _order represents the current (partially complete) //--> order; it starts out empty and returns to that state //--> after each order is put onto the queue. private Order _order = new Order(); /** Constructor; creates UI given an OrderQueue to place orders and an OrderPrinter to print orders. */ public OrderUI(OrderQueue queue, OrderPrinter printer) { //--> Superclass JFrame constructor sets text of title bar from argument: super("APDTS - Order"); // Keep track of queue and printer: _queue = queue; _printer = printer; setupLayout(); _addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addPizzaToOrder(); } }); _cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { resetUI(); } }); _doneButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { recordOrder(); } }); // Shut down system when user closes window. this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("Shutting down APDTS."); System.exit(0); } } ); } // Reset UI back to initial state private void resetUI() { _sizeField.setText(""); _toppingsField.setText(""); _nameField.setText(""); _addressField.setText(""); _phoneField.setText(""); _order = new Order(); updateUI(); } // Record order w/current customer info, putting information on the queue. private void recordOrder() { // If there's information in the pizza fields, assume // it's supposed to go into the order: if (!_sizeField.getText().equals("")) addPizzaToOrder(); /* Suggested interface: // Copy customer information to Order: _order.setCustomer(_nameField.getText(), _addressField.getText(), _phoneField.getText()); // Move the order onto the central queue: _queue.add(_order); */ printOrder(); resetUI(); } // Print current order's pizzas onto label printer private void printOrder() { /* Suggested interface: Iterator i = _order.pizzaIterator(); while (i.hasNext()) { Pizza p = (Pizza)i.next(); _printer.printLabel(_order,p); } */ } // Add the current pizza (specified in UI fields) to the order. private void addPizzaToOrder() { /* Suggested interface: Pizza p = new Pizza(_sizeField.getText(),_toppingsField.getText()); _order.add(p); */ _sizeField.setText(""); _toppingsField.setText(""); updateUI(); } // Updates all parts of the UI that depend on the current incomplete // order private void updateUI() { /* Suggested interface: int numPizzas = _order.getNumPizzas(); double totalPrice = _order.getTotalPrice(); _statusField.setText(""+numPizzas+" pizzas, total: $"+totalPrice); */ } // Create a combination label/edit field component made out of // a panel + subcomponents: private JComponent makeLabelledField(String label, JTextComponent field) { JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JLabel l = new JLabel(label); panel.add(l); panel.add(field); panel.add(Box.createHorizontalGlue()); panel.setAlignmentX(Component.LEFT_ALIGNMENT); return panel; } // Set up UI layout for all the AWT/Swing components. private void setupLayout() { Border paneEdge = BorderFactory.createEmptyBorder(5,5,5,5); getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS)); JPanel orderPane = new JPanel(); orderPane.setLayout(new BoxLayout(orderPane,BoxLayout.Y_AXIS)); orderPane.setBorder(paneEdge); getContentPane().add(orderPane); JPanel customerPane = new JPanel(); customerPane.setLayout(new BoxLayout(customerPane,BoxLayout.Y_AXIS)); customerPane.setBorder(paneEdge); getContentPane().add(customerPane); _statusField.setBorder(paneEdge); getContentPane().add(_statusField); //---Set up orderPane--- orderPane.add(new JLabel("Enter Order")); orderPane.add(makeLabelledField("Size",_sizeField)); orderPane.add(makeLabelledField("Toppings",_toppingsField)); JPanel buttonPanel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); buttonPanel1.setAlignmentX(Component.LEFT_ALIGNMENT); orderPane.add(buttonPanel1); buttonPanel1.add(_addButton); //---Set up customerPane--- customerPane.add(makeLabelledField("Name",_nameField)); customerPane.add(makeLabelledField("Address",_addressField)); customerPane.add(makeLabelledField("Phone",_phoneField)); JPanel buttonPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); buttonPanel2.setAlignmentX(Component.LEFT_ALIGNMENT); customerPane.add(buttonPanel2); buttonPanel2.add(_cancelButton); buttonPanel2.add(_doneButton); pack(); } // Unit test public static void main(String[] args) { new OrderUI(new OrderQueue(), new OrderPrinter()).show(); } };