FileDocCategorySizeDatePackage
RankingMIDlet.javaAPI DocExample5694Sun Nov 04 15:21:00 GMT 2001ora.ch6

RankingMIDlet

public class RankingMIDlet extends javax.microedition.midlet.MIDlet implements javax.microedition.lcdui.CommandListener, Runnable

Fields Summary
private javax.microedition.lcdui.Command
exitCommand
private javax.microedition.lcdui.Command
okCommand
private javax.microedition.lcdui.Command
cancelCommand
private javax.microedition.lcdui.Command
newCommand
private javax.microedition.lcdui.Display
display
private javax.microedition.lcdui.TextField
isbnField
private javax.microedition.lcdui.StringItem
isbnDisplay
private javax.microedition.lcdui.StringItem
titleDisplay
private javax.microedition.lcdui.StringItem
rankingDisplay
private javax.microedition.lcdui.StringItem
reviewDisplay
private javax.microedition.lcdui.Form
isbnForm
private javax.microedition.lcdui.Form
searchForm
private javax.microedition.lcdui.Form
resultForm
private BookInfo
searchBookInfo
private Thread
searchThread
Constructors Summary
Methods Summary
public voidcommandAction(javax.microedition.lcdui.Command cmd, javax.microedition.lcdui.Displayable d)

        if (cmd == exitCommand) {
            try {
                destroyApp(true);
            } catch (MIDletStateChangeException ex) {
            }
            notifyDestroyed();
        } else if (cmd == okCommand) {
            String isbn = isbnField.getString().trim();
            if (!isbn.equals("")) {
                searchForBook(new BookInfo(isbn));
            }
        } else if (cmd == cancelCommand) {
            searchThread = null;
            isbnField.setString(null);
            display.setCurrent(isbnForm);
        } else if (cmd == newCommand) {
            isbnField.setString(null);
            display.setCurrent(isbnForm);
        }
    
protected voiddestroyApp(boolean unconditional)


    
private voidinitialize()

        display = Display.getDisplay(this);

        exitCommand = new Command("Exit", Command.EXIT, 0);
        okCommand = new Command("OK", Command.OK, 0);
        cancelCommand = new Command("Cancel", Command.CANCEL, 0);
        newCommand = new Command("New", Command.SCREEN, 1);

        isbnForm = new Form("Book Query");
        isbnForm.append("Enter an ISBN and press OK:");
        isbnField = new TextField("", null, 10, TextField.ANY);
        isbnForm.append(isbnField);
        isbnForm.addCommand(okCommand);
        isbnForm.addCommand(exitCommand);

        searchForm = new Form("Book Search");
        searchForm.append("Searching for ISBN\n");
        isbnDisplay = new StringItem(null, null);
        searchForm.append(isbnDisplay);
        searchForm.append("\nPlease wait....");
        searchForm.addCommand(cancelCommand);

        resultForm = new Form("Search Results");
        titleDisplay = new StringItem("Book title: ", null);
        rankingDisplay = new StringItem("Ranking:    ", null);
        reviewDisplay = new StringItem("Reviews:    ", null);
        resultForm.append(titleDisplay);
        resultForm.append(rankingDisplay);
        resultForm.append(reviewDisplay);
        resultForm.addCommand(newCommand);
        resultForm.addCommand(exitCommand);

        // Register for events from all of the forms
        isbnForm.setCommandListener(this);
        searchForm.setCommandListener(this);
        resultForm.setCommandListener(this);
    
protected voidpauseApp()

    
public voidrun()

        try {
            boolean found = Fetcher.fetch(searchBookInfo);
            if (searchThread == Thread.currentThread()) {
                if (found && searchBookInfo.getTitle() != null) {
                    titleDisplay.setText(searchBookInfo.getTitle());
                    rankingDisplay.setText(
                        searchBookInfo.getRanking() == 0 ? "" :
                                String.valueOf(searchBookInfo.getRanking()));
                    reviewDisplay.setText(
                        searchBookInfo.getReviews() == 0 ? "" :
                                String.valueOf(searchBookInfo.getReviews()));
                    display.setCurrent(resultForm);
                } else {
                    Alert alert = new Alert("Book not found", null,
                                        null, AlertType.ERROR);
                    alert.setTimeout(Alert.FOREVER);
                    alert.setString("No book with ISBN " +
                                        searchBookInfo.getIsbn() +
                                        " was found.");
                    isbnField.setString(null);
                    display.setCurrent(alert, isbnForm);
                }
            }
        } catch (Throwable ex) {
            if (searchThread == Thread.currentThread()) {
                Alert alert = new Alert("Search Failed", null,
                                        null, AlertType.ERROR);
                alert.setTimeout(Alert.FOREVER);
                alert.setString("Search failed:\n" + ex.getMessage());
                isbnField.setString(null);
                display.setCurrent(alert, isbnForm);
            }
        }
    
public voidsearchForBook(BookInfo info)

        searchBookInfo = info;
        isbnDisplay.setText(info.getIsbn().trim());
        display.setCurrent(searchForm);
        searchThread = new Thread(this);
        searchThread.start();
    
protected voidstartApp()

        if (display == null) {
            initialize();
            display.setCurrent(isbnForm);
        }