www.pudn.com > GMapViewer-src.zip > MainMenu.java
package org.sreid.j2me.gmapviewer;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import org.sreid.j2me.util.*;
public class MainMenu extends Form implements CommandListener {
private final GMapViewer app;
private static final String VIEW_MAP = "View map!";
private static final String MAP_PIN_MENU = "Map pin menu";
private static final String SEARCH_MENU = "Search menu";
private static final String CACHE_INFO = "Cache info";
private static final String VALIDATE_CACHE = "Validate cache";
private static final String CLEAR_CACHE = "Clear cache";
private static final String VIEW_LOG = "View error log";
private static final String PREFERENCES = "Preferences";
private static final String QUIT = "Quit";
MainMenu(GMapViewer app) {
super("GMapViewer");
this.app = app;
setCommandListener(this);
addCommand(new Command(VIEW_MAP, Command.SCREEN, 1));
addCommand(new Command(MAP_PIN_MENU, Command.SCREEN, 2));
addCommand(new Command(SEARCH_MENU, Command.SCREEN, 2));
addCommand(new Command(CACHE_INFO, Command.SCREEN, 3));
addCommand(new Command(VALIDATE_CACHE, Command.SCREEN, 3));
addCommand(new Command(CLEAR_CACHE, Command.SCREEN, 3));
addCommand(new Command(VIEW_LOG, Command.SCREEN, 3));
addCommand(new Command(PREFERENCES, Command.SCREEN, 3));
addCommand(new Command(QUIT, Command.EXIT, 4));
append("GMapViewer\nUnauthorized Google Maps viewer for J2ME.\n\nSelect an option from the menu.");
}
public void commandAction(Command c, Displayable d) {
String cmd = c.getLabel();
if (cmd.equals(VIEW_MAP)) {
app.display.setCurrent(app.canvas);
}
else if (cmd.equals(MAP_PIN_MENU)) {
app.mapPinMenu.cameFrom = this;
app.mapPinMenu.loadList();
app.display.setCurrent(app.mapPinMenu);
}
else if (cmd.equals(SEARCH_MENU)) {
app.searchMenu.cameFrom = this;
app.searchMenu.loadList();
app.display.setCurrent(app.searchMenu);
}
else if (cmd.equals(CACHE_INFO)) {
app.cache.cacheMaintenance(); // force update stats
Alert a = new Alert("Cache info", app.cache.getInfo(), null, null);
a.setTimeout(Alert.FOREVER);
app.alert(a);
}
else if (cmd.equals(VALIDATE_CACHE)) {
Dialog dlg = Dialog.createConfirmationDialog("Validating", "Validating map tile cache.\nPlease wait...", "", "");
dlg.showOn(app, dlg); // cannot be dismissed
app.display.callSerially(new Runnable() { public void run() {
String memResult, rmsResult;
try {
memResult = app.cache.validateMem();
}
catch (Throwable e) {
memResult = "Problem validating memory cache:\n" + e;
}
try {
rmsResult = app.cache.validateRMS();
}
catch (Throwable e) {
rmsResult = "Problem validating RMS cache:\n" + e;
}
Dialog dlg2 = Dialog.createConfirmationDialog("Validate complete", "Memory cache:\n" + memResult + "\nRMS cache:\n" + rmsResult, "OK", "");
dlg2.showOn(app, MainMenu.this);
}});
}
else if (cmd.equals(CLEAR_CACHE)) {
final Dialog dlg = Dialog.createConfirmationDialog("Clear cache?", "Are you sure you want to clear the cache? All map tiles will have to be re-downloaded.", "OK", "Cancel");
dlg.setCallback(new Runnable() { public void run() {
if (dlg.getUserResponse() == Dialog.USER_CONFIRMED) {
try {
app.cache.clearCache();
app.alert("Cache cleared", "Cleared tile cache and deleted record store.");
}
catch (Exception e) {
app.exception("Error clearing the cache.", e);
}
}
}});
dlg.showOn(app, MainMenu.this);
}
else if (cmd.equals(VIEW_LOG)) {
StringBuffer sb = new StringBuffer();
for (Enumeration enum = app.log.elements() ; enum.hasMoreElements() ; ) {
Object o = enum.nextElement();
sb.append(o.toString());
sb.append('\n');
}
if (sb.length() == 0) sb.append("No events logged.\nAll is well in GMapViewer land.");
Dialog dlg = Dialog.createConfirmationDialog("Error log", sb.toString(), "OK", "");
dlg.showOn(app);
}
else if (cmd.equals(PREFERENCES)) {
app.display.setCurrent(app.prefsEditor);
}
else if (cmd.equals(QUIT)) {
app.quit();
}
}
}