PersistentRankingMIDletpublic class PersistentRankingMIDlet extends javax.microedition.midlet.MIDlet implements javax.microedition.lcdui.CommandListener, Runnable, javax.microedition.rms.RecordListener
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.Command | checkCommand | private javax.microedition.lcdui.Command | detailsCommand | private javax.microedition.lcdui.Command | backCommand | private javax.microedition.lcdui.Command | deleteCommand | 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.StringItem | checkTitle | private javax.microedition.lcdui.Form | isbnForm | private javax.microedition.lcdui.Form | searchForm | private javax.microedition.lcdui.Form | resultForm | private javax.microedition.lcdui.Form | checkForm | private javax.microedition.lcdui.List | bookList | private Vector | bookInfoList | private Thread | searchThread | private BookStore | bookStore | private BookInfo | searchBookInfo |
Methods Summary |
---|
public void | commandAction(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("")) {
isbnDisplay.setText(isbn);
display.setCurrent(searchForm);
searchForBook(new BookInfo(isbn));
}
} else if (cmd == cancelCommand) {
searchThread = null;
isbnField.setString(null);
display.setCurrent(getSelectionScreen());
} else if (cmd == newCommand) {
isbnField.setString(null);
display.setCurrent(isbnForm);
} else if (cmd == detailsCommand || cmd == List.SELECT_COMMAND) {
int index = bookList.getSelectedIndex();
searchBookInfo = (BookInfo)bookInfoList.elementAt(index);
isbnDisplay.setText(searchBookInfo.getIsbn());
showBookInfo(searchBookInfo);
} else if (cmd == deleteCommand) {
int index = bookList.getSelectedIndex();
BookInfo bookInfo = (BookInfo)bookInfoList.elementAt(index);
try {
bookStore.deleteBook(bookInfo);
} catch (RecordStoreException ex) {
System.out.println("Delete failed: " + ex);
}
} else if (cmd == checkCommand) {
String isbn = searchBookInfo.getIsbn();
checkTitle.setText(searchBookInfo.getTitle());
display.setCurrent(checkForm);
searchForBook(searchBookInfo);
} else if (cmd == backCommand) {
display.setCurrent(getSelectionScreen());
}
| protected void | destroyApp(boolean unconditional)
// Close the book store
if (bookStore != null) {
try {
bookStore.removeRecordListener(this);
bookStore.close();
} catch (RecordStoreException ex) {
}
}
| private javax.microedition.lcdui.Screen | getSelectionScreen()
return
bookInfoList.isEmpty() ? (Screen)isbnForm : (Screen)bookList;
| private void | initialize()
display = Display.getDisplay(this);
// Open the book store
bookStore = new BookStore();
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);
detailsCommand = new Command("Details", Command.OK, 0);
checkCommand = new Command("Check", Command.OK, 0);
backCommand = new Command("Back", Command.CANCEL, 1);
deleteCommand = new Command("Delete", Command.SCREEN, 1);
bookList = new List("Books", List.IMPLICIT);
bookList.addCommand(detailsCommand);
bookList.addCommand(newCommand);
bookList.addCommand(deleteCommand);
bookList.addCommand(exitCommand);
bookInfoList = new Vector();
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);
isbnForm.addCommand(backCommand);
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);
checkForm = new Form("Details Update");
checkForm.append("Getting details for\n");
checkTitle = new StringItem(null, null);
checkForm.append(checkTitle);
checkForm.append(" from amazon.com\nPlease wait....");
checkForm.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(backCommand);
resultForm.addCommand(checkCommand);
// Register for events from all of the forms
isbnForm.setCommandListener(this);
searchForm.setCommandListener(this);
resultForm.setCommandListener(this);
bookList.setCommandListener(this);
// Listen for changes in the content of the book store
bookStore.addRecordListener(this);
// Install the books held in the record store
populateBookList();
| protected void | pauseApp()
| private void | populateBookList()
// Clear out any existing content
int count = bookList.size();
for (int i = 0; i < count; i++) {
bookList.delete(0);
}
bookInfoList.removeAllElements();
// Add an entry for each book in the store
try {
RecordEnumeration enum = bookStore.getBooks();
while (enum.hasNextElement()) {
int id = enum.nextRecordId();
BookInfo info = bookStore.getBookInfo(id);
bookInfoList.addElement(info);
String title = info.getTitle();
if (title == null || title.equals("")) {
title = info.getIsbn();
}
bookList.append(title, null);
}
enum.destroy();
} catch (Exception ex) {
// Just leave an empty list.
}
// The ISBN list should have an exit command
// only if the List screen is empty.
isbnForm.removeCommand(exitCommand);
if (bookInfoList.isEmpty()) {
isbnForm.addCommand(exitCommand);
}
| public void | recordAdded(javax.microedition.rms.RecordStore recordStore, int recordId)
// Update the book list
populateBookList();
| public void | recordChanged(javax.microedition.rms.RecordStore recordStore, int recordId)
// Update the book list
populateBookList();
| public void | recordDeleted(javax.microedition.rms.RecordStore recordStore, int recordId)
// Update the book list
populateBookList();
| public void | run()
try {
boolean found = Fetcher.fetch(searchBookInfo);
if (searchThread == Thread.currentThread()) {
if (found && searchBookInfo.getTitle() != null) {
// Display the book details
showBookInfo(searchBookInfo);
// Add the new book to the book store
bookStore.saveBookInfo(searchBookInfo);
} 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, getSelectionScreen());
}
}
} 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, getSelectionScreen());
}
}
| public void | searchForBook(BookInfo info)
searchBookInfo = info;
searchThread = new Thread(this);
searchThread.start();
| private void | showBookInfo(BookInfo info)
titleDisplay.setText(info.getTitle());
int ranking = info.getRanking();
int lastRanking = info.getLastRanking();
int change = ranking - lastRanking;
String rankingText =
ranking == 0 ? "" :
String.valueOf(ranking);
if (change > 0) {
rankingText += ", down by " + change;
} else if (change < 0) {
rankingText += ", UP by " + (-change);
}
rankingDisplay.setText(rankingText);
int reviews = info.getReviews();
int lastReviews = info.getLastReviews();
change = reviews - lastReviews;
String reviewText =
reviews == 0 ? "" :
String.valueOf(reviews);
if (change > 0) {
reviewText += ", up by " + change;
}
reviewDisplay.setText(reviewText);
display.setCurrent(resultForm);
| protected void | startApp()
if (display == null) {
initialize();
// If there are any books in the
// book store, display the list.
// Otherwise, start with the ISBN form.
display.setCurrent(getSelectionScreen());
}
|
|