FileDocCategorySizeDatePackage
ExtendedBookServiceClient.javaAPI DocExample7102Tue Mar 11 23:43:04 GMT 2003ora.jwsnut.chapter6.client

ExtendedBookServiceClient

public class ExtendedBookServiceClient extends Object

Fields Summary
private static ora.jwsnut.chapter6.extendedbookservice.EBookQuery
bookQuery
private static Transformer
transformer
private static StreamResult
streamResult
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        
        if (args.length != 1) {
            usage();
        }

        try {   
            // Get the endpoint interface
            EBookService_Impl service = new EBookService_Impl();
            bookQuery = service.getEBookQueryPort();
            ((Stub)bookQuery)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]);
            
            // Get the book titles from the web service
            String[] titles = bookQuery.getBookTitles();
            Arrays.sort(titles);

            // Build the user interface
            showGui(titles);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    
private static voidshowGui(java.lang.String[] titles)

        JFrame frame = new JFrame("Extended Book Service Client");
        JPanel mainPanel = new JPanel(new BorderLayout());
        
        final JList titleList = new JList(titles);
        
        final JPanel imagePanel = new JPanel();
        mainPanel.add(new JScrollPane(imagePanel), BorderLayout.CENTER);
        JPanel controls = new JPanel();
        controls.setLayout(new BoxLayout(controls, BoxLayout.X_AXIS));
        mainPanel.add(controls, BorderLayout.SOUTH);
        final JCheckBox isGif = new JCheckBox("GIF images");
        final JCheckBox useDataHandler = new JCheckBox("DataHandler");
        final JCheckBox getDetails = new JCheckBox("Get XML");
        JButton fetchButton = new JButton("Fetch");
        controls.add(isGif);
        controls.add(Box.createHorizontalStrut(8));
        controls.add(useDataHandler);
        controls.add(Box.createHorizontalStrut(8));
        controls.add(getDetails);
        controls.add(Box.createHorizontalGlue());
        controls.add(fetchButton);
        
        frame.getRootPane().setDefaultButton(fetchButton);
        frame.getContentPane().add(new JScrollPane(titleList), BorderLayout.WEST);
        frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosed(WindowEvent evt) {
                System.exit(0);
            }
        });
        
        fetchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                Object[] selections = titleList.getSelectedValues();
                imagePanel.removeAll();
                imagePanel.validate();
                Dimension d = imagePanel.getSize();
                imagePanel.paintImmediately(new Rectangle(0, 0, d.width, d.height));
                try {
                    String[] selectedTitles;
                    int count = selections.length;
                    if (count == 0) {
                        // Get all images using MimeMultipart
                        MimeMultipart mp = bookQuery.getAllImages();
                        count = mp.getCount();
                        for (int i = 0; i < count; i++) {
                            BodyPart bp = mp.getBodyPart(i);
                            Image img = (Image)bp.getContent();
                            imagePanel.add(new JLabel(new ImageIcon(img)));
                        }
                        selectedTitles = titles;
                    } else {
                        selectedTitles = new String[count];
                        System.arraycopy(selections, 0, selectedTitles, 0, count);
                        if (useDataHandler.isSelected()) {
                            DataHandler[] handlers = bookQuery.getImageHandlers(selectedTitles, isGif.isSelected());
                            for (int i = 0; i < handlers.length; i++) {
                                imagePanel.add(new JLabel(new ImageIcon(
                                                (Image)handlers[i].getContent())));
                            }
                        } else {
                            Image[] images = bookQuery.getImages(selectedTitles, isGif.isSelected());
                            for (int i = 0; i < images.length; i++) {
                                imagePanel.add(new JLabel(new ImageIcon(images[i])));
                            }
                        }
                    }
                    imagePanel.revalidate();
                    imagePanel.repaint();   

                    // Get the XML if requested and
                    // print it all to System.out
                    if (getDetails.isSelected()) {
                        Source[] sources = bookQuery.getBookDetails(selectedTitles);
                        if (transformer == null) {
                            transformer = TransformerFactory.newInstance().newTransformer();
                            streamResult = new StreamResult(System.out);
                        }
                        for (int i = 0; i < sources.length; i++) {
                            transformer.transform(sources[i], streamResult);
                            System.out.println("\n------------------------");
                        }
                    }
                } catch (Exception ex) {
                    System.out.println(ex);
                }
            }
        });
        frame.setSize(640, 300);
        frame.setVisible(true);        
    
public static voidusage()

        System.err.println("Usage: java ExtendedBookServiceClient address");
        System.exit(1);