001package myhw3.ui;
002
003import javax.swing.JOptionPane;
004
005public final class PopupUI implements UI {
006        public void displayMessage(String message) {
007                JOptionPane.showMessageDialog(null,message);
008        }
009
010        public void displayError(String message) {
011                JOptionPane.showMessageDialog(null,message,"Error",JOptionPane.ERROR_MESSAGE);
012        }
013
014        public void processMenu(UIMenu menu) {
015                StringBuilder b = new StringBuilder();
016                b.append(menu.getHeading());
017                b.append("\n");
018                b.append("Enter choice by number:");
019                b.append("\n");
020
021                for (int i = 1; i < menu.size(); i++) {
022                        b.append("  " + i + ". " + menu.getPrompt(i));
023                        b.append("\n");
024                }
025
026                String response = JOptionPane.showInputDialog(b.toString());
027                if (response == null) {
028                        response = "";
029                }
030                int selection;
031                try {
032                        selection = Integer.parseInt(response, 10);
033                        if ((selection < 0) || (selection >= menu.size()))
034                                selection = 0;
035                } catch (NumberFormatException e) {
036                        selection = 0;
037                }
038
039                menu.runAction(selection);
040        }
041
042        public String[] processForm(UIForm form) {
043                // TODO
044                return null;
045        }
046}