import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.text.*; /** * Class representing an order tracking UI. Responsible * for displaying the state of the order queue at all times. */ public class TrackingDisplayUI extends JFrame implements Observer { private JTextArea _textArea; // Multiline area for order display /** Constructor */ public TrackingDisplayUI() { super("APDTS - Order Tracking"); _textArea = new JTextArea(10,40); _textArea.setEditable(false); JScrollPane areaScrollPane = new JScrollPane(_textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); this.getContentPane().add(areaScrollPane); this.pack(); } /** * Makes the UI display the data from the given * queue. * @param queue Queue to load data from (non-null) */ public void display(OrderQueue queue) { _textArea.setText(""); /* Suggested interface: Iterator i = queue.iterator(); while (i.hasNext()) { Order ord = (Order)i.next(); _textArea.append(displayableString(ord)); } */ } // Formatter object to use to write just the hour and minute // information from a Date object into a String: private final SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a"); // Retrieve a displayable (possibly multiline) string from // an Order object private String displayableString(Order ord) { // Example: 5:12 12" OPR 1245 Oak St. StringBuffer buf = new StringBuffer(); /* Suggested interface: Iterator i = ord.pizzaIterator(); while (i.hasNext()) { buf.append(formatter.format(ord.getEnqueueTime())); buf.append("\t"); Pizza p = (Pizza)i.next(); buf.append(p.getSize()); buf.append("\t"); buf.append(p.getToppings()); buf.append("\t"); buf.append(ord.getCustomerAddress()); buf.append("\n"); } */ return buf.toString(); } /** * Updates display when interesting events happen. * @param subject Must be an OrderQueue * @param data unused */ public void update(Observable subject, Object data) { this.show(); // Just in case it was hidden. this.display((OrderQueue)subject); } // Unit test. public static void main(String[] args) { new TrackingDisplayUI().show(); } };