001: /**
002: * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Gregory Lapouchnian
022: * Patrick Smith
023: * --------------------------------------------------------------------------
024: * $Id: OlstoreSwingClient.java 7027 2005-07-08 14:00:45Z glapouch $
025: * --------------------------------------------------------------------------
026: */package olstore.client;
027:
028: import java.awt.BorderLayout;
029: import java.awt.Color;
030: import java.awt.Dimension;
031: import java.awt.FlowLayout;
032: import java.awt.Font;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.awt.event.KeyEvent;
036: import java.awt.image.BufferedImage;
037: import java.math.BigDecimal;
038: import java.text.DecimalFormat;
039: import java.util.Iterator;
040: import java.util.List;
041:
042: import javax.imageio.ImageIO;
043: import javax.swing.Box;
044: import javax.swing.BoxLayout;
045: import javax.swing.ButtonGroup;
046: import javax.swing.ImageIcon;
047: import javax.swing.JButton;
048: import javax.swing.JFrame;
049: import javax.swing.JLabel;
050: import javax.swing.JMenu;
051: import javax.swing.JMenuBar;
052: import javax.swing.JMenuItem;
053: import javax.swing.JOptionPane;
054: import javax.swing.JPanel;
055: import javax.swing.JProgressBar;
056: import javax.swing.JRadioButton;
057: import javax.swing.JScrollPane;
058: import javax.swing.JSplitPane;
059: import javax.swing.JTextField;
060: import javax.xml.namespace.QName;
061: import javax.xml.rpc.ParameterMode;
062: import javax.xml.soap.AttachmentPart;
063: import javax.xml.soap.SOAPMessage;
064:
065: import org.apache.axis.client.Call;
066:
067: /**
068: * A GUI client that allows to use some of the functionality of the OLStore
069: * example by connecting to its web service endpoint.
070: */
071: public class OlstoreSwingClient implements ActionListener {
072:
073: /** The default URL for the web service. */
074: private static String url = "http://localhost:9000/axis-olstorews";
075:
076: /** The path for the olstoreEndpoint. */
077: private static String storeEndpointPath = "/olstoreEndpoint/ItemSpecialHelperLocal";
078:
079: /** The path for the wsShoppingCartHelper endpoint. */
080: private static String cartEndpointPath = "/wsShoppingCartHelper/ShoppingCartHelperLocal";
081:
082: /** The button to retrieve all specials. */
083: private JButton bSpecials;
084:
085: /** The button to select the type of specials. */
086: private JButton bSpecialsByType;
087:
088: /** The button to empty the shopping cart. */
089: private JButton bEmpty;
090:
091: /**
092: * Progress bar that indicates that the client is in the process of
093: * retrieving the information from the web service.
094: */
095: private JProgressBar progressBar;
096:
097: /** The main window of the client. */
098: private JFrame frame;
099:
100: /** The main panel of the client. */
101: private JPanel panel;
102:
103: /** The scroll pane which contains the displayed items. */
104: private JScrollPane scrollPane;
105:
106: /** The "Exit" menu item. */
107: private JMenuItem exitMenuItem;
108:
109: /**
110: * The Call object that is used to send/receive information via the web
111: * service.
112: */
113: private Call call;
114:
115: /** Contains the different types of items available from the olstore. */
116: private String[] types;
117:
118: /**
119: * The panel which holds the "Get all specials" and "Specials by type"
120: * buttons.
121: */
122: private JPanel buttonPane;
123:
124: /** Provide a way to modify the URL of the web serice. */
125: private JPanel urlPane;
126:
127: /** The field which contains the URL to access the web services from. */
128: private JTextField urlField = new JTextField(url);
129:
130: /** A label for the url pane. */
131: private JLabel urlLabel = new JLabel("Web Service URL:");
132:
133: /** The handler to drag and drop items into the shopping cart. */
134: private ItemPanelTransferHandler handler;
135:
136: /** The panel which contains shopping cart related components. */
137: private JPanel cartPanel;
138:
139: /** The panel which contains the individual order items in the shopping cart. */
140: private ShoppingCartPanel cartMainPanel;
141:
142: /** The shopping cart's total price. */
143: private JLabel totalPrice;
144:
145: /** The checkout button. */
146: private JButton bCheckout;
147:
148: /** The value of the shopping cart's total price. */
149: private BigDecimal priceValue = new BigDecimal(0.00f);
150:
151: /** The primary item colour */
152: public static final Color itemColor1 = new Color(139, 147, 171);
153:
154: /** The secondary item colour */
155: public static final Color itemColor2 = new Color(180, 185, 200);
156:
157: /** The colour of buttons. */
158: public static final Color buttonColor = new Color(115, 160, 195);
159:
160: /** The formatter used to display the prices with 2 decimal places. */
161: private static DecimalFormat formatter = new DecimalFormat("#0.00");
162:
163: /** The scroll pane which individual order items are placed in. */
164: private JScrollPane cartScrollPane;
165:
166: /** The maximum of the JProgressBar */
167: private static final int PROGRESSMAX = 100;
168:
169: /** Used for small pane sizing. */
170: private static final int PANESIZE_SMALL = 300;
171:
172: /** Used for pane sizing. */
173: private static final int PANESIZE_MEDIUM = 500;
174:
175: /** Used for slightly larger pane sizing. */
176: private static final int PANESIZE = 600;
177:
178: /** Used for larger pane sizing. */
179: private static final int PANESIZE_LARGE = 800;
180:
181: /** Used for small spacers. */
182: private static final int SPACER = 15;
183:
184: /** Used for large spacers. */
185: private static final int LARGE_SPACER = 200;
186:
187: /** Used for defining a maximum pane height. */
188: private static final int PANEL_HEIGHT = 30;
189:
190: /** Used for defining a maximum banner height. */
191: private static final int BANNER_HEIGHT = 80;
192:
193: /** Used for selecting between getting the WSDL from a textfield or the UDDI registry. */
194: private ButtonGroup bGroup = new ButtonGroup();
195:
196: /** Used to get the WSDL url from the textfield. */
197: private JRadioButton bURL = new JRadioButton("Use Web Service URL:");
198:
199: /** Used to get the WSDL url from the UDDI registry */
200: private JRadioButton bDiscover = new JRadioButton(
201: "Discover via UDDI");
202:
203: /** Connect to the UDDI registry to retrieve WSDL URLs. */
204: private RegistryConnector registryConnector = new RegistryConnector();
205:
206: /**
207: * The constructor which creates the components and places them into the
208: * appropriate layouts.
209: */
210: public OlstoreSwingClient() {
211: handler = new ItemPanelTransferHandler();
212: frame = new JFrame("Olstore Swing Client");
213:
214: // the buttons to display all specials
215: bSpecials = new JButton("Get All Specials");
216: bSpecials.addActionListener(this );
217:
218: // the button to select specials from a single category
219: bSpecialsByType = new JButton("Specials By Type");
220: bSpecialsByType.addActionListener(this );
221:
222: // the button to checkout the shopping cart
223: bCheckout = new JButton("Checkout");
224: bCheckout.addActionListener(this );
225:
226: // the button to empty the shopping cart.
227: bEmpty = new JButton("Empty Cart");
228: bEmpty.addActionListener(this );
229: bEmpty.setBackground(buttonColor);
230:
231: // initialize the progress bar
232: progressBar = new JProgressBar(0, PROGRESSMAX);
233: progressBar.setValue(0);
234: progressBar.setStringPainted(true);
235: progressBar.setString("");
236:
237: // the main area where all the special are displayed
238: JPanel mainPanel = new JPanel();
239: mainPanel.setLayout(new BorderLayout());
240:
241: panel = new JPanel();
242: panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
243: panel.setBackground(itemColor1);
244: scrollPane = new JScrollPane(panel);
245: scrollPane.setMinimumSize(new Dimension(PANESIZE_MEDIUM,
246: PANESIZE_MEDIUM));
247: scrollPane.setPreferredSize(new Dimension(PANESIZE_LARGE,
248: PANESIZE));
249:
250: // add a menu to this application
251: createMenu();
252:
253: // creates and fills the url panel.
254: urlPane = new JPanel();
255: urlPane.setLayout(new BoxLayout(urlPane, BoxLayout.X_AXIS));
256: urlPane.add(urlLabel);
257: urlPane.add(urlField);
258: urlPane.setMaximumSize(new Dimension(Integer.MAX_VALUE,
259: PANEL_HEIGHT));
260:
261: buttonPane = new JPanel();
262: buttonPane
263: .setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
264: bSpecials.setBackground(buttonColor);
265: bSpecialsByType.setBackground(buttonColor);
266:
267: buttonPane.add(bSpecials);
268: buttonPane.add(bSpecialsByType);
269:
270: JPanel bannerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
271: bannerPanel.setBackground(Color.BLACK);
272: bannerPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,
273: BANNER_HEIGHT));
274: JLabel shadowMan = new JLabel(new ImageIcon(
275: "images/olstoreLogo.png"));
276: bannerPanel.add(shadowMan);
277:
278: addExplanationPanel();
279:
280: // create a shopping cart panel
281: cartPanel = new JPanel();
282: cartPanel.setLayout(new BorderLayout());
283: cartPanel.add(new JLabel("Shopping Cart"), BorderLayout.NORTH);
284:
285: cartMainPanel = new ShoppingCartPanel(this );
286: cartMainPanel.setTransferHandler(handler);
287: cartMainPanel.setLayout(new BoxLayout(cartMainPanel,
288: BoxLayout.Y_AXIS));
289: cartMainPanel.setBackground(itemColor1);
290:
291: totalPrice = new JLabel("Your total: $0.00");
292: JPanel checkoutPanel = new JPanel();
293: checkoutPanel.setLayout(new BoxLayout(checkoutPanel,
294: BoxLayout.X_AXIS));
295: checkoutPanel.add(totalPrice);
296: checkoutPanel.add(Box.createHorizontalStrut(SPACER));
297: checkoutPanel.add(bCheckout);
298: checkoutPanel.add(bEmpty);
299: checkoutPanel.setBackground(itemColor1);
300: bCheckout.setBackground(buttonColor);
301: cartPanel.add(checkoutPanel, BorderLayout.SOUTH);
302:
303: cartScrollPane = new JScrollPane(cartMainPanel);
304: cartScrollPane.setMinimumSize(new Dimension(PANESIZE_SMALL,
305: PANESIZE_MEDIUM));
306: cartScrollPane.setBackground(itemColor1);
307: cartPanel.add(cartScrollPane, BorderLayout.CENTER);
308: cartPanel.setBackground(itemColor1);
309: // the split pane to display products and the shopping cart side by side
310: JSplitPane splitPane = new JSplitPane(
311: JSplitPane.HORIZONTAL_SPLIT, scrollPane, cartPanel);
312: splitPane.setBackground(itemColor1);
313: splitPane.setForeground(itemColor1);
314: JPanel topPanel = new JPanel();
315: topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
316: topPanel.add(bannerPanel);
317: topPanel.add(buttonPane);
318: topPanel.setBackground(Color.BLACK);
319:
320: bURL.setSelected(true);
321: bGroup.add(bURL);
322: bGroup.add(bDiscover);
323: bURL.addActionListener(this );
324: bDiscover.addActionListener(this );
325:
326: JPanel discoverPanel = new JPanel();
327: discoverPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
328: discoverPanel.add(bDiscover);
329: // creates and fills the url panel.
330: urlPane = new JPanel();
331: urlPane.setLayout(new BoxLayout(urlPane, BoxLayout.X_AXIS));
332: urlPane.add(bURL);
333: urlPane.add(urlField);
334: urlPane.add(progressBar);
335: urlPane.setMaximumSize(new Dimension(Integer.MAX_VALUE,
336: PANEL_HEIGHT));
337:
338: JPanel bottomPanel = new JPanel();
339: bottomPanel.setLayout(new BoxLayout(bottomPanel,
340: BoxLayout.Y_AXIS));
341: bottomPanel.add(discoverPanel);
342: bottomPanel.add(urlPane);
343:
344: mainPanel.add(topPanel, BorderLayout.NORTH);
345: mainPanel.add(splitPane, BorderLayout.CENTER);
346: mainPanel.add(bottomPanel, BorderLayout.SOUTH);
347: mainPanel.setBackground(itemColor1);
348: frame.getContentPane().add(mainPanel);
349:
350: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
351: frame.pack();
352: frame.setVisible(true);
353:
354: }
355:
356: /**
357: * Create a panel that gives the user instructions on how to use the client
358: * on startup.
359: */
360: private void addExplanationPanel() {
361:
362: panel.setLayout(new BorderLayout());
363:
364: JPanel explainPanel = new JPanel();
365: explainPanel.setPreferredSize(new Dimension(200, 100));
366: explainPanel.setLayout(new BoxLayout(explainPanel,
367: BoxLayout.Y_AXIS));
368: explainPanel.setBackground(itemColor2);
369:
370: JLabel label0 = new JLabel("Welcome to OLStore");
371: label0.setFont(new Font("sans-serif", Font.BOLD, 17));
372: explainPanel.add(label0);
373: explainPanel.add(Box.createVerticalStrut(SPACER));
374: explainPanel
375: .add(new JLabel(
376: "To view all the specials click the 'All Specials' button above."));
377:
378: explainPanel.add(Box.createVerticalStrut(SPACER));
379: explainPanel
380: .add(new JLabel(
381: "To view the specials by type click the 'Specials By Type' button."));
382: explainPanel.add(Box.createVerticalStrut(SPACER));
383: explainPanel
384: .add(new JLabel(
385: "To add an item to the shopping cart, simply drag it to the "));
386: explainPanel
387: .add(new JLabel("shopping cart panel on the right."));
388:
389: panel.add(explainPanel, BorderLayout.CENTER);
390: panel.add(Box.createVerticalStrut(LARGE_SPACER),
391: BorderLayout.PAGE_START);
392: panel.add(Box.createHorizontalStrut(LARGE_SPACER),
393: BorderLayout.LINE_START);
394: panel.add(Box.createVerticalStrut(LARGE_SPACER),
395: BorderLayout.PAGE_END);
396: panel.add(Box.createHorizontalStrut(LARGE_SPACER),
397: BorderLayout.LINE_END);
398: }
399:
400: /**
401: * Creates and displays a new olstore client.
402: * @param args The program's arguments.
403: */
404: public static void main(String[] args) {
405: OlstoreSwingClient client = new OlstoreSwingClient();
406: }
407:
408: /**
409: * Creates a menu bar and adds an 'Exit' menu item.
410: */
411: public void createMenu() {
412: JMenuBar menuBar = new JMenuBar();
413:
414: JMenu menu = new JMenu("File");
415: menu.setMnemonic(KeyEvent.VK_F);
416: menuBar.add(menu);
417:
418: exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_E);
419: exitMenuItem.addActionListener(this );
420:
421: menu.add(exitMenuItem);
422:
423: frame.setJMenuBar(menuBar);
424: }
425:
426: /**
427: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
428: */
429: public void actionPerformed(ActionEvent event) {
430: try {
431: if (event.getSource().equals(bSpecials)) {
432: // Create a new thread to execute the call to the web service
433: // while allowing the GUI thread to update the progress bar
434: new Thread() {
435: public void run() {
436: try {
437: displaySpecials("allSpecials", null);
438: } catch (Exception ex) {
439: reportError(ex);
440: }
441: }
442: }.start();
443: }
444: if (event.getSource().equals(bSpecialsByType)) {
445: if (types == null) {
446: new Thread() {
447: public void run() {
448: try {
449: getSpecialTypes();
450: displayTypes();
451: } catch (Exception ex) {
452: reportError(ex);
453: }
454: }
455: }.start();
456: } else {
457: displayTypes();
458: }
459:
460: }
461: if (event.getSource().equals(bCheckout)) {
462: doCheckout();
463:
464: }
465: if (event.getSource().equals(bDiscover)) {
466: urlField.setEnabled(false);
467: }
468: if (event.getSource().equals(bURL)) {
469: urlField.setEnabled(true);
470: }
471:
472: if (event.getSource().equals(bEmpty)) {
473: cartMainPanel.removeAllItems();
474: }
475: if (event.getSource().equals(exitMenuItem)) {
476: int n = JOptionPane.showConfirmDialog(frame,
477: "Do you want to exit the client?", "Exit",
478: JOptionPane.YES_NO_OPTION);
479:
480: if (n == 0) {
481: System.exit(0);
482: }
483: }
484:
485: } catch (Exception e) {
486: reportError(e);
487: }
488:
489: }
490:
491: /**
492: * Contacts the web service via the base URL entered in the urlField, and
493: * the defined store endpoint, and stores the types in the String[]
494: * <code>types</code>.
495: */
496: private void getSpecialTypes() {
497: // start the progress bar
498: progressBar.setIndeterminate(true);
499: progressBar.setString("Loading types...");
500: bSpecialsByType.setEnabled(false);
501:
502: try {
503: call = new Call(getStoreEndpoint());
504: types = (String[]) (call.invoke("getTypes", null));
505: } catch (Exception e) {
506: reportError(e);
507: }
508:
509: bSpecialsByType.setEnabled(true);
510: // stop the progress bar
511: progressBar.setIndeterminate(false);
512: progressBar.setString("Done");
513:
514: }
515:
516: /**
517: * Prompts the user which type (via <code>types</code>) of special they
518: * would like to view, and displays the specials of that type.
519: */
520: private void displayTypes() {
521: if (types != null) {
522: final String s = (String) JOptionPane.showInputDialog(
523: frame, "Select a type...", "Choose A Type",
524: JOptionPane.PLAIN_MESSAGE, null, types, null);
525:
526: if ((s != null) && (s.length() > 0)) {
527: new Thread() {
528: public void run() {
529: try {
530: displaySpecials("getSpecials",
531: new Object[] { s });
532: } catch (Exception ex) {
533: reportError(ex);
534: }
535: }
536: }.start();
537: }
538: }
539: }
540:
541: /**
542: * Given a method name and array of parameters, invokes the web service
543: * through Call.invoke passing it the method name and parameters. The web
544: * service's response consists of a String[][] which contains the
545: * information for each item, as well as an image attachment for each item.
546: * The information and image are used to create new items which are
547: * displayed in the scroll pane.
548: * @param method The name of the method to invoke.
549: * @param params The parameters for the method.
550: * @throws Exception If the call fails.
551: */
552: private void displaySpecials(String method, Object[] params)
553: throws Exception {
554:
555: // set the layout to BoxLayout, it might have been different to display
556: // the welcome screen
557: panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
558:
559: // start the progress bar
560: progressBar.setIndeterminate(true);
561: progressBar.setString("Loading specials...");
562:
563: // disable the appropriate button while the webservice is called
564: if (params == null) {
565: bSpecials.setEnabled(false);
566: } else {
567: bSpecialsByType.setEnabled(false);
568: }
569:
570: call = new Call(getStoreEndpoint());
571:
572: String[][] result = (String[][]) (call.invoke(method, params));
573:
574: SOAPMessage msg = call.getResponseMessage();
575: Iterator j = msg.getAttachments();
576: int counter = 0;
577: Color color;
578:
579: // empty the current contents of this panel
580: panel.removeAll();
581:
582: while (j.hasNext()) {
583: AttachmentPart ap = (AttachmentPart) j.next();
584:
585: BufferedImage bi = ImageIO.read(ap.getDataHandler()
586: .getInputStream());
587:
588: if (counter % 2 == 0) {
589: color = itemColor1;
590: } else {
591: color = itemColor2;
592: }
593:
594: ItemPanel itemPanel = new ItemPanel(result[counter][1],
595: result[counter][2], result[counter][3],
596: result[counter][0], result[counter][4], bi, color);
597:
598: itemPanel.setBackground(color);
599: panel.add(itemPanel);
600:
601: itemPanel.setTransferHandler(handler);
602:
603: counter++;
604: }
605: // make sure the new components are drawn
606: scrollPane.validate();
607: scrollPane.repaint();
608:
609: // turn the button back on
610: if (params == null) {
611: bSpecials.setEnabled(true);
612: } else {
613: bSpecialsByType.setEnabled(true);
614: }
615: // set the flag for the timer
616: progressBar.setIndeterminate(false);
617: progressBar.setString("Done");
618:
619: }
620:
621: /**
622: * Get the web services endpoint URL for OLStore. Returns the URL from UDDI
623: * registry if the bDiscover radio button is selected, uses the URL from the
624: * textfield otherwise.
625: *
626: * @return the URL of OLStore web services endpoint
627: * @throws Exception if it is unable to retrieve it from the UDDI registry
628: */
629: private String getStoreEndpoint() throws Exception {
630: // only connect to the UDDI registry if the 'Discover' radio button is on
631: if (bDiscover.isSelected()) {
632: try {
633: registryConnector.retrieveWSDLLocations();
634: } catch (Exception e) {
635: throw new Exception(
636: "Could not retrieve the WSDL URLs from the registry. "
637: + e.getMessage());
638: }
639:
640: List locations = registryConnector.getCartWSDLLocations();
641:
642: if (locations.isEmpty()) {
643: throw new Exception("No WSDL URL was returned.");
644: } else {
645: // TODO provide the user with a list of available URLs?
646: return (String) registryConnector
647: .getSpecialsWSDLLocations().get(0);
648: }
649: }
650:
651: // return the endpoint by combining the value in the urlField with the
652: // constant for the endpoint
653: return urlField.getText() + storeEndpointPath;
654: }
655:
656: /**
657: * Get the web services endpoint URL for OLStore shopping cart. Returns the
658: * URL from UDDI registry if the bDiscover radio button is selected, uses
659: * the URL from the textfield otherwise.
660: *
661: * @return the URL of OLStor shopping cart web services endpoint
662: * @throws Exception if it is unable to retrieve it from the UDDI registry
663: */
664: private String getShoppingCartEndpoint() throws Exception {
665: // only connect to the UDDI registry if the 'Discover' radio button is on
666: if (bDiscover.isSelected()) {
667: try {
668: registryConnector.retrieveWSDLLocations();
669: } catch (Exception e) {
670: throw new Exception(
671: "Could not retrieve the WSDL URLs from the registry. "
672: + e.getMessage());
673: }
674:
675: List locations = registryConnector.getCartWSDLLocations();
676:
677: if (locations.isEmpty()) {
678: throw new Exception("No WSDL URL was returned.");
679: } else {
680: System.out.println("using registry URL: "
681: + (String) registryConnector
682: .getCartWSDLLocations().get(0));
683: return (String) registryConnector
684: .getCartWSDLLocations().get(0);
685: }
686: }
687:
688: // return the endpoint by combining the value in the urlField with the
689: // constant for the endpoint
690: return urlField.getText() + cartEndpointPath;
691: }
692:
693: /**
694: * Reports the given exception via a JOptionPane.
695: * @param e The exception to report.
696: */
697: private void reportError(Exception e) {
698: JOptionPane.showMessageDialog(frame,
699: "Could not retrieve the required information from OLStore.\nError: "
700: + e.getMessage(), "Client error",
701: JOptionPane.ERROR_MESSAGE);
702:
703: // turn off the progress bar
704: progressBar.setIndeterminate(false);
705: progressBar.setString("Error");
706:
707: // turn on the buttons
708: bSpecialsByType.setEnabled(true);
709: bSpecials.setEnabled(true);
710: }
711:
712: /**
713: * Updates the price value and price field by adding the given price to the
714: * current total.
715: * @param price The amount to add to the current total.
716: */
717: public void updatePrice(BigDecimal price) {
718: priceValue = priceValue.add(price);
719: setPrice(priceValue);
720: }
721:
722: /**
723: * Sets the price label to the given price.
724: * @param priceValue The value to set the price label to.
725: */
726: public void setPrice(BigDecimal priceValue) {
727: this .priceValue = priceValue;
728: totalPrice.setText("Your total: $"
729: + formatter.format(priceValue));
730: }
731:
732: /**
733: * Prompts the user for a username and password, and sends the login
734: * information and cart contents (item IDs and the corresponding quantities)
735: * to the server via web services. If the login information is accepted the
736: * cart contents are added to a server side, user specific, shopping cart
737: * and the order is created and completed. If the login information is
738: * incorrect, then the user is prompted to try again.
739: * @throws Exception If the call fails.
740: */
741: public void doCheckout() throws Exception {
742:
743: if (cartMainPanel.isEmpty()) {
744: JOptionPane.showMessageDialog(frame,
745: "The shopping cart does not contain any items.",
746: "Empty Cart", JOptionPane.INFORMATION_MESSAGE);
747: } else {
748: LoginDialog ld = new LoginDialog(frame);
749: ld.show();
750: if (ld.login()) {
751: String[] info = ld.getLogin();
752:
753: Boolean result = new Boolean(false);
754:
755: Object[] orderItems = cartMainPanel.getCartContents();
756: String[][] contents = new String[orderItems.length][2];
757: for (int i = 0; i < orderItems.length; i++) {
758: OrderItem item = (OrderItem) orderItems[i];
759: contents[i][0] = item.getId();
760: contents[i][1] = String.valueOf(item.getQuantity());
761: }
762:
763: Object[] params = new Object[] { info[0], info[1],
764: (String[][]) contents };
765:
766: call = new Call(getShoppingCartEndpoint());
767:
768: // Not the best way to handle sending the parameters, but in
769: // order to
770: // send the String[][] that contains the shopping cart item IDs
771: // and
772: // quantities, we have to use addParameter.
773: // For some strange reason this results in the 2-dimensional
774: // array being sent as a 4-dimensional array through the
775: // web service, though on the server side, it is still received
776: // as a 2-dimensional array.
777: call.addParameter("name", new QName("String"),
778: String.class, ParameterMode.IN);
779: call.addParameter("pwd", new QName("String"),
780: String.class, ParameterMode.IN);
781: call.addParameter("cartContents", new QName(
782: "String[][]"), String[][].class,
783: ParameterMode.IN);
784: call.setReturnType(new QName("Boolean"));
785: result = (Boolean) (call.invoke(
786: "createAndCheckoutOrder", params));
787:
788: if (!result.booleanValue()) {
789: JOptionPane
790: .showMessageDialog(
791: frame,
792: "Cart checkout could not complete. \n"
793: + "Please check your username and password and try again.",
794: "Shopping cart error",
795: JOptionPane.ERROR_MESSAGE);
796: return;
797: }
798: cartMainPanel.removeAllItems();
799: JOptionPane.showMessageDialog(frame,
800: "Thank you for your order!",
801: "Order has been sent",
802: JOptionPane.INFORMATION_MESSAGE);
803:
804: }
805: ld.dispose();
806: }
807: }
808: }
|