Posted By

donfips on 06/27/10


Tagged


Versions (?)

MOO


 / Published in: Java
 

  1. package gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.GridBagConstraints;
  7. import java.awt.GridBagLayout;
  8. import java.awt.Insets;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11.  
  12. import javax.swing.BoxLayout;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JTextArea;
  20.  
  21. import controls.Warenkorb;
  22. import controls.WarenkorbEintrag;
  23.  
  24. import bar.XMLBar;
  25. import drinks.Drink;
  26. import drinks.Zutat;
  27.  
  28. /**
  29.  * GUI für die Bar
  30.  *
  31.  * @author Markus Tacker <[email protected]>
  32.  * @author Philipp Siegmund <[email protected]>
  33.  * @version $Id: UserInterface.java 208 2010-06-26 23:10:06Z m $
  34.  */
  35. public class UserInterface {
  36.  
  37. private XMLBar bar;
  38. private JTextArea warenkorbPanel;
  39. private boolean orderView = true;
  40. private JPanel moo;
  41. private JFrame frame;
  42. private JScrollPane scrollPane;
  43. private Warenkorb<Drink> warenkorb;
  44. private Drink currentDrink;
  45. private JButton undoOrder;
  46.  
  47. /**
  48. * Erzeugt das GUI
  49. */
  50. public UserInterface(XMLBar bar) {
  51. this.bar = bar;
  52. warenkorb = new Warenkorb<Drink>();
  53.  
  54. frame = new JFrame("MOO - Milliway's Online Order");
  55. frame.setSize(600, 750);
  56. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57.  
  58. moo = new JPanel();
  59. moo.setLayout(new BoxLayout(moo, BoxLayout.Y_AXIS));
  60.  
  61. JPanel head = new JPanel();
  62. head.setSize(new Dimension(moo.getWidth(), 50));
  63. JLabel headline = new JLabel(
  64. "Willkommen bei MOO - Milliway's Online Order!");
  65. head.add(headline);
  66. moo.add(head);
  67.  
  68. scrollPane = new JScrollPane();
  69. scrollPane.setSize(new Dimension(moo.getWidth(), 200));
  70. moo.add(scrollPane);
  71. showDrinkView();
  72.  
  73. JPanel warenPanel = new JPanel(new BorderLayout());
  74. warenkorbPanel = new JTextArea(10, 1);
  75. warenPanel.add(warenkorbPanel);
  76. moo.add(warenPanel);
  77. updateOrderPanel();
  78.  
  79. // Buttons dazu
  80. JPanel buttonPanel = new JPanel();
  81. JButton finishOrder = new JButton("Finish!");
  82. finishOrder.addActionListener(new SubmitAction(this));
  83. undoOrder = new JButton("Undo!");
  84. undoOrder.addActionListener(new UndoAction(this));
  85. JButton cancelOrder = new JButton("Cancel!");
  86. cancelOrder.addActionListener(new CancelAction(this));
  87. buttonPanel.add(finishOrder);
  88. buttonPanel.add(undoOrder);
  89. buttonPanel.add(cancelOrder);
  90. moo.add(buttonPanel);
  91.  
  92. frame.add(moo);
  93. frame.setVisible(true);
  94. }
  95.  
  96. /**
  97. * Zeigt das die Getränkekarte an
  98. */
  99. private void showDrinkView() {
  100. orderView = true;
  101. JPanel karte = new JPanel(new GridBagLayout());
  102. constraints.gridwidth = 5;
  103. constraints.anchor = GridBagConstraints.NORTH;
  104. if (undoOrder != null)
  105. undoOrder.setVisible(true);
  106.  
  107. int startStLine = 0;
  108. int startNdLine = 1;
  109.  
  110. for (Drink drink : bar.getDrinks()) {
  111.  
  112. JLabel drinkLabel = new JLabel(drink.getNummer() + " - "
  113. + drink.getName());
  114. constraints.gridx = 0;
  115. constraints.weightx = 4;
  116. constraints.gridy = startStLine;
  117. constraints.anchor = GridBagConstraints.WEST;
  118. karte.add(drinkLabel, constraints);
  119.  
  120. JLabel preis = new JLabel(drink.getPreisAsString());
  121. constraints.gridx = 5;
  122. constraints.weightx = 1;
  123. constraints.gridy = startStLine;
  124. constraints.anchor = GridBagConstraints.NORTH;
  125. karte.add(preis, constraints);
  126.  
  127. JTextArea text = new JTextArea(drink.getBeschreibung());
  128. text.setLineWrap(true);
  129. text.setWrapStyleWord(true);
  130. text.setBackground(null);
  131. text.setEditable(false);
  132. text.setSize(480, 50);
  133. constraints.gridx = 0;
  134. constraints.weightx = 4;
  135. constraints.gridy = startNdLine;
  136. constraints.anchor = GridBagConstraints.WEST;
  137. karte.add(text, constraints);
  138.  
  139. addDrinkOrderButton("Order", karte, constraints, startNdLine, drink);
  140.  
  141. startStLine += 2;
  142. startNdLine += 2;
  143. }
  144.  
  145. scrollPane.getViewport().setView(karte);
  146. }
  147.  
  148. /**
  149. * Zeigt die Zutatenauswahl für Custom-Drinks an
  150. */
  151. private void showZutatenView() {
  152. orderView = false;
  153. JPanel karte = new JPanel(new GridBagLayout());
  154. constraints.gridwidth = 5;
  155. constraints.anchor = GridBagConstraints.NORTH;
  156. if (undoOrder != null)
  157. undoOrder.setVisible(false);
  158.  
  159. int startStLine = 0;
  160. for (Zutat zutat : bar.getZutaten()) {
  161.  
  162. addZutatenOrderButton("Rein da!", karte, constraints, startStLine,
  163. zutat);
  164.  
  165. JLabel text = new JLabel(zutat.toString());
  166. constraints.gridx = 1;
  167. constraints.weightx = 4;
  168. constraints.gridy = startStLine;
  169. constraints.anchor = GridBagConstraints.WEST;
  170. constraints.insets = new Insets(0, 40, 0, 0);
  171. karte.add(text, constraints);
  172.  
  173. startStLine += 1;
  174. }
  175.  
  176. scrollPane.getViewport().setView(karte);
  177. }
  178.  
  179. /**
  180. * Erzeugt einen Button zum Bestellen von Zutaten
  181. *
  182. * @param label
  183. * @param karte
  184. * @param constraints
  185. * @param startNdLine
  186. * @param zutat
  187. */
  188. private void addZutatenOrderButton(String label, JPanel karte,
  189. GridBagConstraints constraints, int startNdLine, Zutat zutat) {
  190. ZutatenAction buttonAction = new ZutatenAction(this, zutat);
  191. JButton button = new JButton(label);
  192. button.setSize(100, 20);
  193. constraints.gridx = 0;
  194. constraints.weightx = 1;
  195. constraints.gridy = startNdLine;
  196. constraints.anchor = GridBagConstraints.EAST;
  197. constraints.insets = new Insets(0, 0, 0, 0);
  198. button.addActionListener(buttonAction);
  199. karte.add(button, constraints);
  200.  
  201. }
  202.  
  203. /**
  204. * Erzeugt einen Button zum Bestellen von Drinks
  205. *
  206. * @param label
  207. * @param karte
  208. * @param constraints
  209. * @param startNdLine
  210. * @param drink
  211. */
  212. private void addDrinkOrderButton(String label, Container karte,
  213. GridBagConstraints constraints, int startNdLine, Drink drink) {
  214. OrderAction buttonAction = new OrderAction(this, drink);
  215. JButton button = new JButton(label);
  216. button.setSize(100, 20);
  217. constraints.gridx = 5;
  218. constraints.weightx = 1;
  219. constraints.gridy = startNdLine;
  220. constraints.anchor = GridBagConstraints.NORTH;
  221. button.addActionListener(buttonAction);
  222. karte.add(button, constraints);
  223. }
  224.  
  225. /**
  226. * Fügt dem Warenkorb eine Getränk hinzu
  227. *
  228. * @param drink
  229. */
  230. private void orderDrink(Drink drink) {
  231. warenkorb.add(drink);
  232. updateOrderPanel();
  233. }
  234.  
  235. /**
  236. * Fügt dem aktuellen Custom-Drink einen Zutat hinzu
  237. *
  238. * @param zutat
  239. */
  240. public void addZutat(Zutat zutat) {
  241. currentDrink.addZutat(zutat.clone());
  242. updateZutatenPanel();
  243. }
  244.  
  245. /**
  246. * Aktualisiert die Bestell-Anzeige
  247. */
  248. private void updateOrderPanel() {
  249. String orderSummary = "Ihre aktuelle Bestellung:\n\n";
  250. float orderPrice = 0;
  251.  
  252. if (warenkorb.isEmpty()) {
  253. orderSummary += "Keine Getränke bestellt.\n";
  254. } else {
  255. for (WarenkorbEintrag<Drink> entry : warenkorb) {
  256. Drink drink = entry.getItem();
  257. orderSummary += entry.getAmount() + "x " + drink.getName()
  258. + " " + drink.getPreisAsString() + "\n";
  259. orderPrice += drink.getPreis() * entry.getAmount();
  260. }
  261.  
  262. }
  263.  
  264. orderSummary += "\nSumme: " + Drink.formatPrice(orderPrice);
  265.  
  266. warenkorbPanel.setText(orderSummary);
  267.  
  268. }
  269.  
  270. /**
  271. * Aktualisiert die Custom-Drink-Anzeige
  272. */
  273. private void updateZutatenPanel() {
  274. String orderSummary = "Ihre aktueller Drink:\n";
  275.  
  276. if (currentDrink.getZutaten().isEmpty()) {
  277. orderSummary += "Keine Zutaten hinzugefügt.\n";
  278. } else {
  279. for (Zutat zutat : currentDrink.getZutaten().values()) {
  280. orderSummary += "\n" + zutat.toString();
  281.  
  282. }
  283.  
  284. }
  285. warenkorbPanel.setText(orderSummary);
  286. }
  287.  
  288. /**
  289. * Zeigt die Zutatenkonfiguration für einen neuen Drink an
  290. *
  291. * @param drink
  292. */
  293. private void showDrinkConfigurator(Drink drink) {
  294. currentDrink = drink.clone();
  295. showZutatenView();
  296. }
  297.  
  298. /**
  299. * Macht die letzte Bestellung rückgängig
  300. */
  301. private void undoOrder() {
  302. warenkorb.removeLastItem();
  303. updateOrderPanel();
  304. }
  305.  
  306. /**
  307. * Bricht die Bestellung ab
  308. */
  309. private void cancelOrder() {
  310. warenkorb.clear();
  311. updateOrderPanel();
  312. }
  313.  
  314. /**
  315. * Bricht die Bestellung ab
  316. */
  317. private void placeOrder() {
  318. .showMessageDialog(frame, "Vielen Dank für Ihre Bestellung.");
  319. warenkorb.clear();
  320. updateOrderPanel();
  321. }
  322.  
  323. /**
  324. * Bestellt einen Drink
  325. *
  326. * @author Markus Tacker <[email protected]>
  327. * @author Philipp Siegmund <[email protected]>
  328. * @version $Id: UserInterface.java 208 2010-06-26 23:10:06Z m $
  329. */
  330. private class OrderAction implements ActionListener {
  331. private Drink drink;
  332. private UserInterface ui;
  333.  
  334. /**
  335. * Konstruktor
  336. *
  337. * @param ui
  338. * @param drink
  339. */
  340. private OrderAction(UserInterface ui, Drink drink) {
  341. this.ui = ui;
  342. this.drink = drink;
  343. }
  344.  
  345. /**
  346. * Handler für AWT Events
  347. */
  348. public void actionPerformed(ActionEvent event) {
  349. if (drink.isCustom()) {
  350. ui.showDrinkConfigurator(drink);
  351. } else {
  352. ui.orderDrink(drink);
  353. }
  354. }
  355. }
  356.  
  357. /**
  358. * Führt die Bestellung aus
  359. *
  360. * @author Markus Tacker <[email protected]>
  361. * @author Philipp Siegmund <[email protected]>
  362. * @version $Id: Warenkorb.java 206 2010-06-26 21:54:21Z m
  363. */
  364. private class SubmitAction implements ActionListener {
  365.  
  366. private UserInterface ui;
  367.  
  368. /**
  369. * Konstruktor
  370. *
  371. * @param ui
  372. */
  373. private SubmitAction(UserInterface ui) {
  374. this.ui = ui;
  375. }
  376.  
  377. /**
  378. * Handler für AWT Events
  379. */
  380. public void actionPerformed(ActionEvent event) {
  381. if (orderView) {
  382. ui.placeOrder();
  383. } else {
  384. ui.warenkorb.add(currentDrink);
  385. ui.currentDrink = null;
  386. ui.showDrinkView();
  387. ui.updateOrderPanel();
  388. }
  389. }
  390.  
  391. }
  392.  
  393. /**
  394. * Fügt Zutaten zum Drink hinzu
  395. *
  396. * @author Markus Tacker <[email protected]>
  397. * @author Philipp Siegmund <[email protected]>
  398. * @version $Id: Warenkorb.java 206 2010-06-26 21:54:21Z m
  399. */
  400. private class ZutatenAction implements ActionListener {
  401.  
  402. private UserInterface ui;
  403. private Zutat zutat;
  404.  
  405. /**
  406. * Konstruktor
  407. *
  408. * @param ui
  409. * @param zutat
  410. */
  411. private ZutatenAction(UserInterface ui, Zutat zutat) {
  412. this.ui = ui;
  413. this.zutat = zutat;
  414. }
  415.  
  416. /**
  417. * Handler für AWT Events
  418. */
  419. public void actionPerformed(ActionEvent event) {
  420. ui.addZutat(zutat);
  421. }
  422.  
  423. }
  424.  
  425. /**
  426. * Macht die letzte Aktion rückgängi
  427. *
  428. * @author Markus Tacker <[email protected]>
  429. * @author Philipp Siegmund <[email protected]>
  430. * @version $Id: Warenkorb.java 206 2010-06-26 21:54:21Z m
  431. */
  432. private class UndoAction implements ActionListener {
  433.  
  434. private UserInterface ui;
  435.  
  436. /**
  437. * Konstruktor
  438. *
  439. * @param ui
  440. */
  441. private UndoAction(UserInterface ui) {
  442. this.ui = ui;
  443. }
  444.  
  445. /**
  446. * Handler für AWT Events
  447. */
  448. public void actionPerformed(ActionEvent event) {
  449. ui.undoOrder();
  450. }
  451.  
  452. }
  453.  
  454. /**
  455. * Bricht die Bestellung ab
  456. *
  457. * @author Markus Tacker <[email protected]>
  458. * @author Philipp Siegmund <[email protected]>
  459. * @version $Id: Warenkorb.java 206 2010-06-26 21:54:21Z m
  460. */
  461. private class CancelAction implements ActionListener {
  462.  
  463. private UserInterface ui;
  464.  
  465. /**
  466. * Konstruktor
  467. *
  468. * @param ui
  469. */
  470. private CancelAction(UserInterface ui) {
  471. this.ui = ui;
  472. }
  473.  
  474. /**
  475. * Handler für AWT Events
  476. */
  477. public void actionPerformed(ActionEvent event) {
  478. if (orderView) {
  479. ui.cancelOrder();
  480. } else {
  481. ui.showDrinkView();
  482. ui.updateOrderPanel();
  483. }
  484. }
  485.  
  486. }
  487.  
  488. }

Report this snippet  

You need to login to post a comment.