Added a history list. (from 0.2 beta 1)
Added a permanent location setting. (from 0.2 beta 1) Added SMS number setting. (from 0.2 beta 2) Added Movie Showtimes search otpion. Added Local Showtimes search option.
This commit is contained in:
parent
a2aa9fe4e1
commit
722ac10dcf
18 changed files with 1396 additions and 268 deletions
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* @(#)GooglME.java
|
||||
*
|
||||
* Copyright (c) 2004, Erik C. Thauvin (http://www.thauvin.net/erik/)
|
||||
* Copyright (c) 2004-2005, Erik C. Thauvin (http://www.thauvin.net/erik/)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -36,83 +36,210 @@
|
|||
*/
|
||||
package net.thauvin.j2me.googlme;
|
||||
|
||||
import javax.microedition.io.*;
|
||||
import javax.microedition.lcdui.Alert;
|
||||
import javax.microedition.lcdui.AlertType;
|
||||
import javax.microedition.lcdui.Command;
|
||||
import javax.microedition.lcdui.CommandListener;
|
||||
import javax.microedition.lcdui.Display;
|
||||
import javax.microedition.lcdui.Displayable;
|
||||
import javax.microedition.lcdui.Item;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.microedition.io.Connector;
|
||||
import javax.microedition.lcdui.*;
|
||||
import javax.microedition.midlet.MIDlet;
|
||||
import javax.microedition.midlet.MIDletStateChangeException;
|
||||
import javax.microedition.rms.RecordStoreException;
|
||||
|
||||
import javax.wireless.messaging.*;
|
||||
import javax.wireless.messaging.MessageConnection;
|
||||
import javax.wireless.messaging.TextMessage;
|
||||
|
||||
|
||||
/**
|
||||
* The <code>GooglME</code> class implements a simple front-end for Google SMS.
|
||||
*
|
||||
* @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
|
||||
* @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
|
||||
* @version $Revision$, $Date$
|
||||
*
|
||||
* @created October 8, 2004
|
||||
* @since 1.0
|
||||
* @since 1.0
|
||||
*/
|
||||
public class GooglME extends MIDlet implements CommandListener, Runnable
|
||||
{
|
||||
/**
|
||||
* Definition choice.
|
||||
*/
|
||||
protected static final String CHOICE_DEFINITION = "Definition";
|
||||
|
||||
/**
|
||||
* Froogle Prices choice.
|
||||
*/
|
||||
protected static final String CHOICE_FROOGLE_PRICES = "Froogle Prices";
|
||||
|
||||
/**
|
||||
* Google Local choice.
|
||||
*/
|
||||
protected static final String CHOICE_GOOGLE_LOCAL = "Google Local";
|
||||
|
||||
/**
|
||||
* Google Search choice.
|
||||
*/
|
||||
protected static final String CHOICE_GOOGLE_SEARCH = "Google Search";
|
||||
|
||||
/**
|
||||
* Google SMS choice.
|
||||
*/
|
||||
protected static final String CHOICE_GOOGLE_SMS = "Google SMS";
|
||||
|
||||
/**
|
||||
* Local Showtimes choice.
|
||||
*/
|
||||
protected static final String CHOICE_LOCAL_SHOWTIMES = "Local Showtimes";
|
||||
|
||||
/**
|
||||
* Movie Showtimes choice.
|
||||
*/
|
||||
protected static final String CHOICE_MOVIE_SHOWTIMES = "Movie Showtimes";
|
||||
|
||||
|
||||
/**
|
||||
* The <code>About</code> command.
|
||||
*/
|
||||
protected /* final */ Command aboutCommand = new Command("About", Command.SCREEN, 5);
|
||||
|
||||
/**
|
||||
* The <code>Back</code> command.
|
||||
*/
|
||||
protected /* final */ Command backCommand = new Command("Back", Command.BACK, 2);
|
||||
|
||||
/**
|
||||
* The <code>Clear</code> command.
|
||||
*/
|
||||
protected /* final */ Command clearCommand = new Command("Clear", Command.SCREEN, 3);
|
||||
|
||||
/**
|
||||
* The <code>Exit</code> command.
|
||||
*/
|
||||
protected /* final */ Command exitCommand = new Command("Exit", Command.EXIT, 2);
|
||||
|
||||
/**
|
||||
* The <code>Help</code> command.
|
||||
*/
|
||||
protected /* final */ Command helpCommand = new Command("Help", Command.HELP, 4);
|
||||
|
||||
/**
|
||||
* The <code>Send</code> command.
|
||||
*/
|
||||
protected /* final */ Command sendCommand = new Command("Send", Command.SCREEN, 2);
|
||||
protected static final Command COMMAND_ABOUT = new Command("About", Command.SCREEN, 2);
|
||||
|
||||
/**
|
||||
* The application name.
|
||||
*/
|
||||
protected /* final */ String appName = "GooglME";
|
||||
protected static final String APP_NAME = "GooglME";
|
||||
|
||||
/**
|
||||
* The application version.
|
||||
*/
|
||||
protected /* final */ String appVersion = "0.1";
|
||||
private Display display;
|
||||
protected static final String APP_VERSION = "0.3";
|
||||
|
||||
/**
|
||||
* The help screen.
|
||||
* The <code>Back</code> command.
|
||||
*/
|
||||
private /* final */ HelpScreen helpScreen;
|
||||
protected static final Command COMMAND_BACK = new Command("Back", Command.BACK, 2);
|
||||
|
||||
/**
|
||||
* The main screen.
|
||||
* The action choices.
|
||||
*/
|
||||
private /* final */ MainScreen mainScreen;
|
||||
private String query;
|
||||
private int action;
|
||||
protected static final String[] CHOICES = new String[]
|
||||
{
|
||||
CHOICE_GOOGLE_SMS, CHOICE_GOOGLE_LOCAL, CHOICE_FROOGLE_PRICES,
|
||||
CHOICE_GOOGLE_SEARCH, CHOICE_MOVIE_SHOWTIMES, CHOICE_LOCAL_SHOWTIMES,
|
||||
CHOICE_DEFINITION
|
||||
};
|
||||
|
||||
/**
|
||||
* The <code>Clear</code> command.
|
||||
*/
|
||||
protected static final Command COMMAND_CLEAR = new Command("Clear", Command.SCREEN, 2);
|
||||
|
||||
/**
|
||||
* The <code>Exit</code> command.
|
||||
*/
|
||||
protected static final Command COMMAND_EXIT = new Command("Exit", Command.EXIT, 2);
|
||||
|
||||
/**
|
||||
* The <code>Help</code> command.
|
||||
*/
|
||||
protected static final Command COMMAND_HELP = new Command("Help", Command.HELP, 2);
|
||||
|
||||
/**
|
||||
* The <code>History</code> command.
|
||||
*/
|
||||
protected static final Command COMMAND_HISTORY = new Command("History", Command.SCREEN, 2);
|
||||
|
||||
/**
|
||||
* The <code>Settings</code> command.
|
||||
*/
|
||||
protected static final Command COMMAND_SETTINGS = new Command("Settings", Command.SCREEN, 2);
|
||||
|
||||
/**
|
||||
* The <code>Save</code> command.
|
||||
*/
|
||||
protected static final Command COMMAND_SAVE = new Command("Save", Command.OK, 1);
|
||||
|
||||
/**
|
||||
* The <code>Send</code> command.
|
||||
*/
|
||||
protected static final Command COMMAND_SEND = new Command("Send", Command.OK, 1);
|
||||
|
||||
/**
|
||||
* The history maximum size.
|
||||
*/
|
||||
private static final int MAX_HISTORY = 8;
|
||||
|
||||
/**
|
||||
* The description query prefix.
|
||||
*/
|
||||
private static final String PREFIX_DEFINITION = "d ";
|
||||
|
||||
/**
|
||||
* The Google query prefix.
|
||||
*/
|
||||
private static final String PREFIX_GOOGLE = "g ";
|
||||
|
||||
/**
|
||||
* The Froogle query prefix.
|
||||
*/
|
||||
private static final String PREFIX_FROOGLE = "f ";
|
||||
|
||||
/**
|
||||
* The Movie query prefix.
|
||||
*/
|
||||
private static final String PREFIX_MOVIE = "movie: ";
|
||||
|
||||
/**
|
||||
* The history preferences key.
|
||||
*/
|
||||
private static final String PREFS_HISTORY_KEY = "h";
|
||||
|
||||
/**
|
||||
* The location preferences key.
|
||||
*/
|
||||
private static final String PREFS_LOCATION_KEY = "l";
|
||||
|
||||
/**
|
||||
* The address preferences key.
|
||||
*/
|
||||
private static final String PREFS_ADDRESS_KEY = "a";
|
||||
|
||||
// The current action.
|
||||
private String _action = CHOICE_GOOGLE_SMS;
|
||||
|
||||
// The Google SMS address.
|
||||
private String _address = "46645";
|
||||
|
||||
// The default error/message alert dialog.
|
||||
private Alert _defaultAlert;
|
||||
|
||||
// The display instance.
|
||||
private Display _display;
|
||||
|
||||
// The first time flag.
|
||||
private boolean _firstTime;
|
||||
|
||||
// The help screen.
|
||||
private HelpScreen _helpScreen;
|
||||
|
||||
// The history.
|
||||
private /* final */ Vector _history = new Vector(8);
|
||||
|
||||
// The history screen.
|
||||
private HistoryScreen _historyScreen;
|
||||
|
||||
// The location.
|
||||
private String _location = "";
|
||||
|
||||
// The main screen.
|
||||
private /* final */ MainScreen _mainScreen;
|
||||
|
||||
// The preferences store.
|
||||
private RmsIndex _prefs;
|
||||
|
||||
// The preferences screen.
|
||||
private PrefsScreen _prefsScreen;
|
||||
|
||||
// The current query.
|
||||
private String _query = "";
|
||||
|
||||
// The sending message alert dialog.
|
||||
private Alert _sendingAlert;
|
||||
|
||||
/**
|
||||
* Creates a new GooglME instance.
|
||||
|
@ -121,7 +248,8 @@ public class GooglME extends MIDlet implements CommandListener, Runnable
|
|||
{
|
||||
super();
|
||||
|
||||
mainScreen = new MainScreen(this);
|
||||
_mainScreen = new MainScreen(this);
|
||||
_firstTime = true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -130,56 +258,187 @@ public class GooglME extends MIDlet implements CommandListener, Runnable
|
|||
* @param c The command action.
|
||||
* @param d The diplayable screen.
|
||||
*/
|
||||
public void commandAction(Command c, Displayable d)
|
||||
public /* final */ void commandAction(Command c, Displayable d)
|
||||
{
|
||||
if (c == exitCommand)
|
||||
if (c == COMMAND_EXIT)
|
||||
{
|
||||
exit();
|
||||
}
|
||||
else if (c == aboutCommand)
|
||||
else if (c == COMMAND_ABOUT)
|
||||
{
|
||||
msg("About " + appName, appName + ' ' + appVersion + "\nCopyright 2004\nErik C. Thauvin\nerik@thauvin.net",
|
||||
d, false);
|
||||
alert("About " + APP_NAME,
|
||||
APP_NAME + ' ' + APP_VERSION + "\nCopyright 2005\nErik C. Thauvin\nerik@thauvin.net", d, false);
|
||||
}
|
||||
else if (c == clearCommand)
|
||||
else if (c == COMMAND_CLEAR)
|
||||
{
|
||||
mainScreen.queryFld.setString("");
|
||||
Util.setCurrentItem(display, (Item) mainScreen.queryFld);
|
||||
_mainScreen.queryFld.setString("");
|
||||
Util.setCurrentItem(_display, (Item) _mainScreen.queryFld);
|
||||
}
|
||||
else if (c == sendCommand)
|
||||
else if (c == COMMAND_SEND)
|
||||
{
|
||||
query = mainScreen.queryFld.getString();
|
||||
action = mainScreen.actionPopup.getSelectedIndex();
|
||||
|
||||
if ((query != null) && (query.length() > 0))
|
||||
if (isValidString(_address))
|
||||
{
|
||||
new Thread(this).start();
|
||||
_query = _mainScreen.queryFld.getString();
|
||||
_action = CHOICES[_mainScreen.actionPopup.getSelectedIndex()];
|
||||
|
||||
if (isValidString(_query))
|
||||
{
|
||||
if (((_action.equals(CHOICE_GOOGLE_LOCAL)) || (_action.equals(CHOICE_LOCAL_SHOWTIMES))) &&
|
||||
!isValidString(_location))
|
||||
{
|
||||
alert("No Location", "Please set a location first.", d, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_sendingAlert == null)
|
||||
{
|
||||
_sendingAlert = new Alert("");
|
||||
}
|
||||
|
||||
alert(_sendingAlert, "Sending SMS", "Sending message to " + _address + "...", d, 2500, false);
|
||||
|
||||
new Thread(this).start();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Util.setCurrentItem(_display, (Item) _mainScreen.queryFld);
|
||||
alert("Invalid Input", "Please specify a query.", d, true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
msg("Invalid Input", "Please specify a query.", d, true);
|
||||
Util.setCurrentItem(display, (Item) mainScreen.queryFld);
|
||||
alert("No SMS Address", "Please set the Google SMS number.", d, true);
|
||||
}
|
||||
}
|
||||
else if (c == helpCommand)
|
||||
else if (c == COMMAND_SETTINGS)
|
||||
{
|
||||
if (helpScreen == null)
|
||||
if (_prefsScreen == null)
|
||||
{
|
||||
helpScreen = new HelpScreen(this);
|
||||
_prefsScreen = new PrefsScreen(this);
|
||||
}
|
||||
|
||||
display.setCurrent(helpScreen);
|
||||
_prefsScreen.smsFld.setString(_address);
|
||||
_prefsScreen.locationFld.setString(_location);
|
||||
|
||||
_display.setCurrent(_prefsScreen);
|
||||
}
|
||||
else if (c == backCommand)
|
||||
else if (c == COMMAND_SAVE)
|
||||
{
|
||||
display.setCurrent(mainScreen);
|
||||
_location = _prefsScreen.locationFld.getString();
|
||||
_address = _prefsScreen.smsFld.getString();
|
||||
|
||||
_display.setCurrent(_mainScreen);
|
||||
|
||||
saveSettings();
|
||||
}
|
||||
else if (c == COMMAND_HISTORY)
|
||||
{
|
||||
if (_historyScreen == null)
|
||||
{
|
||||
_historyScreen = new HistoryScreen(this);
|
||||
}
|
||||
|
||||
int size = _historyScreen.size();
|
||||
|
||||
if (size > 0)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
_historyScreen.delete(0);
|
||||
}
|
||||
}
|
||||
|
||||
size = _history.size();
|
||||
|
||||
if (size > 0)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
_historyScreen.append((String) _history.elementAt(i), null);
|
||||
}
|
||||
|
||||
_display.setCurrent(_historyScreen);
|
||||
}
|
||||
else
|
||||
{
|
||||
alert("No History", "There is no history to display.", d, true);
|
||||
}
|
||||
}
|
||||
else if (c == List.SELECT_COMMAND)
|
||||
{
|
||||
/* final */ String query = _historyScreen.getString(_historyScreen.getSelectedIndex());
|
||||
|
||||
// Definition
|
||||
if (query.startsWith(PREFIX_DEFINITION))
|
||||
{
|
||||
_mainScreen.queryFld.setString(query.substring(query.indexOf(' ') + 1));
|
||||
_mainScreen.actionPopup.setSelectedIndex(choiceIndex(CHOICE_DEFINITION), true);
|
||||
}
|
||||
|
||||
// Google
|
||||
else if (query.startsWith(PREFIX_GOOGLE))
|
||||
{
|
||||
_mainScreen.queryFld.setString(query.substring(query.indexOf(' ') + 1));
|
||||
_mainScreen.actionPopup.setSelectedIndex(choiceIndex(CHOICE_GOOGLE_SEARCH), true);
|
||||
}
|
||||
|
||||
// Froogle
|
||||
else if (query.startsWith(PREFIX_FROOGLE))
|
||||
{
|
||||
_mainScreen.queryFld.setString(query.substring(query.indexOf(' ') + 1));
|
||||
_mainScreen.actionPopup.setSelectedIndex(choiceIndex(CHOICE_FROOGLE_PRICES), true);
|
||||
}
|
||||
|
||||
// Showtimes
|
||||
else if (query.startsWith(PREFIX_MOVIE))
|
||||
{
|
||||
// Local
|
||||
if (isValidString(_location) && query.endsWith(' ' + _location))
|
||||
{
|
||||
_mainScreen.queryFld.setString(query.substring(PREFIX_MOVIE.length(), query.lastIndexOf(' ')));
|
||||
_mainScreen.actionPopup.setSelectedIndex(choiceIndex(CHOICE_LOCAL_SHOWTIMES), true);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mainScreen.queryFld.setString(query.substring(PREFIX_MOVIE.length()));
|
||||
_mainScreen.actionPopup.setSelectedIndex(choiceIndex(CHOICE_MOVIE_SHOWTIMES), true);
|
||||
}
|
||||
}
|
||||
|
||||
// Local
|
||||
else if (isValidString(_location) && query.endsWith('.' + _location))
|
||||
{
|
||||
_mainScreen.queryFld.setString(query.substring(0, query.lastIndexOf('.')));
|
||||
_mainScreen.actionPopup.setSelectedIndex(choiceIndex(CHOICE_GOOGLE_LOCAL), true);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mainScreen.queryFld.setString(query);
|
||||
_mainScreen.actionPopup.setSelectedIndex(choiceIndex(CHOICE_GOOGLE_SMS), true);
|
||||
}
|
||||
|
||||
_display.setCurrent(_mainScreen);
|
||||
}
|
||||
else if (c == COMMAND_HELP)
|
||||
{
|
||||
if (_helpScreen == null)
|
||||
{
|
||||
_helpScreen = new HelpScreen(this);
|
||||
}
|
||||
|
||||
_display.setCurrent(_helpScreen);
|
||||
}
|
||||
else if (c == COMMAND_BACK)
|
||||
{
|
||||
_display.setCurrent(_mainScreen);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the thread.
|
||||
*/
|
||||
public void run()
|
||||
public /* final */ void run()
|
||||
{
|
||||
sendSMS();
|
||||
}
|
||||
|
@ -190,13 +449,25 @@ public class GooglME extends MIDlet implements CommandListener, Runnable
|
|||
protected void destroyApp(boolean b)
|
||||
throws MIDletStateChangeException
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_prefs != null)
|
||||
{
|
||||
_prefs.close();
|
||||
}
|
||||
}
|
||||
catch (RecordStoreException e)
|
||||
{
|
||||
alert("Close Error", "Could not close the preferences store: " + e.getMessage(), _mainScreen, true);
|
||||
}
|
||||
|
||||
notifyDestroyed();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see javax.microedition.midlet.MIDlet#pauseApp()
|
||||
*/
|
||||
protected void pauseApp()
|
||||
protected /* final */ void pauseApp()
|
||||
{
|
||||
;
|
||||
}
|
||||
|
@ -204,11 +475,118 @@ public class GooglME extends MIDlet implements CommandListener, Runnable
|
|||
/**
|
||||
* @see javax.microedition.midlet.MIDlet#startApp()
|
||||
*/
|
||||
protected void startApp()
|
||||
throws MIDletStateChangeException
|
||||
protected /* final */ void startApp()
|
||||
throws MIDletStateChangeException
|
||||
{
|
||||
display = Display.getDisplay(this);
|
||||
display.setCurrent(mainScreen);
|
||||
if (_firstTime)
|
||||
{
|
||||
try
|
||||
{
|
||||
_prefs = new RmsIndex("GooglME");
|
||||
|
||||
String item;
|
||||
|
||||
// Load the history.
|
||||
for (int i = 0; i < MAX_HISTORY; i++)
|
||||
{
|
||||
item = _prefs.get(PREFS_HISTORY_KEY + i);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
_history.addElement(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Load the location.
|
||||
item = _prefs.get(PREFS_LOCATION_KEY);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
_location = item;
|
||||
}
|
||||
|
||||
// Load the address.
|
||||
item = _prefs.get(PREFS_ADDRESS_KEY);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
_address = item;
|
||||
}
|
||||
}
|
||||
catch (RecordStoreException e)
|
||||
{
|
||||
alert("Load Error", "Could not load the preferences: " + e.getMessage(), _mainScreen, true);
|
||||
}
|
||||
|
||||
_firstTime = false;
|
||||
}
|
||||
|
||||
_display = Display.getDisplay(this);
|
||||
_display.setCurrent(_mainScreen);
|
||||
}
|
||||
|
||||
// Return the index of the given choice.
|
||||
private static int choiceIndex(String choice)
|
||||
{
|
||||
for (int i = 0; i < CHOICES.length; i++)
|
||||
{
|
||||
if (CHOICES[i].equals(choice))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Validates a string.
|
||||
private static boolean isValidString(String s)
|
||||
{
|
||||
return ((s != null) && (s.length() > 0));
|
||||
}
|
||||
|
||||
// Adds to the history.
|
||||
private void addHistory(String query)
|
||||
{
|
||||
if (_history.size() == MAX_HISTORY)
|
||||
{
|
||||
_history.removeElementAt(MAX_HISTORY - 1);
|
||||
}
|
||||
|
||||
_history.insertElementAt(query, 0);
|
||||
|
||||
saveHistory();
|
||||
}
|
||||
|
||||
// Displays a modal message/error alert dialog.
|
||||
private void alert(String title, String msg, Displayable d, boolean isError)
|
||||
{
|
||||
if (_defaultAlert == null)
|
||||
{
|
||||
_defaultAlert = new Alert("");
|
||||
}
|
||||
|
||||
alert(_defaultAlert, title, msg, d, Alert.FOREVER, isError);
|
||||
}
|
||||
|
||||
// Displays a message/error alert dialog.
|
||||
private void alert(Alert alert, String title, String msg, Displayable d, int timeout, boolean isError)
|
||||
{
|
||||
if (title != null)
|
||||
{
|
||||
alert.setTitle(title);
|
||||
}
|
||||
|
||||
alert.setString(msg);
|
||||
alert.setType(isError ? AlertType.ERROR : AlertType.INFO);
|
||||
|
||||
alert.setTimeout(timeout);
|
||||
|
||||
_display.setCurrent(alert, d);
|
||||
}
|
||||
|
||||
// Exits the application.
|
||||
|
@ -218,30 +596,58 @@ public class GooglME extends MIDlet implements CommandListener, Runnable
|
|||
{
|
||||
destroyApp(false);
|
||||
}
|
||||
catch (MIDletStateChangeException e)
|
||||
catch (MIDletStateChangeException ignore)
|
||||
{
|
||||
; // Do nothing
|
||||
; // Do nothing
|
||||
}
|
||||
}
|
||||
|
||||
// Displays a message/error dialog.
|
||||
private void msg(String title, String msg, Displayable d, boolean isError)
|
||||
// Saves the history.
|
||||
private void saveHistory()
|
||||
{
|
||||
/* final */ Alert alert = new Alert(title, msg, null, AlertType.INFO);
|
||||
|
||||
if (isError)
|
||||
try
|
||||
{
|
||||
alert.setType(AlertType.ERROR);
|
||||
}
|
||||
if (_prefs != null)
|
||||
{
|
||||
/* final */ int size = _history.size();
|
||||
|
||||
alert.setTimeout(Alert.FOREVER);
|
||||
display.setCurrent(alert, d);
|
||||
// Save the history.
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
_prefs.put(PREFS_HISTORY_KEY + i, (String) _history.elementAt(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (RecordStoreException e)
|
||||
{
|
||||
alert("Save Error", "Could not save the history: " + e.getMessage(), _mainScreen, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Saves the settings.
|
||||
private void saveSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_prefs != null)
|
||||
{
|
||||
// Save the location.
|
||||
_prefs.put(PREFS_LOCATION_KEY, _location);
|
||||
|
||||
// Save the address.
|
||||
_prefs.put(PREFS_ADDRESS_KEY, _address);
|
||||
}
|
||||
}
|
||||
catch (RecordStoreException e)
|
||||
{
|
||||
alert("Save Error", "Could not save the settings: " + e.getMessage(), _mainScreen, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Sends the SMS.
|
||||
private void sendSMS()
|
||||
{
|
||||
/* final */ String address = "sms://46645";
|
||||
/* final */ String address = "sms://" + _address;
|
||||
MessageConnection conn = null;
|
||||
|
||||
try
|
||||
|
@ -251,35 +657,76 @@ public class GooglME extends MIDlet implements CommandListener, Runnable
|
|||
/* final */ TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
|
||||
msg.setAddress(address);
|
||||
|
||||
// Froogle
|
||||
if (action == 1)
|
||||
/* final */ String text;
|
||||
|
||||
if (_action.equals(CHOICE_GOOGLE_LOCAL))
|
||||
{
|
||||
msg.setPayloadText("f " + query);
|
||||
if (isValidString(_location))
|
||||
{
|
||||
text = _query + '.' + _location;
|
||||
}
|
||||
else
|
||||
{
|
||||
text = _query;
|
||||
}
|
||||
}
|
||||
|
||||
// Froogle
|
||||
else if (_action.equals(CHOICE_FROOGLE_PRICES))
|
||||
{
|
||||
text = PREFIX_FROOGLE + _query;
|
||||
}
|
||||
|
||||
// Google
|
||||
else if (action == 2)
|
||||
else if (_action.equals(CHOICE_GOOGLE_SEARCH))
|
||||
{
|
||||
msg.setPayloadText("g " + query);
|
||||
text = PREFIX_GOOGLE + _query;
|
||||
}
|
||||
|
||||
// Definition
|
||||
else if (action == 3)
|
||||
else if (_action.equals(CHOICE_DEFINITION))
|
||||
{
|
||||
msg.setPayloadText("d " + query);
|
||||
text = PREFIX_DEFINITION + _query;
|
||||
}
|
||||
|
||||
// Showtimes
|
||||
else if (_action.equals(CHOICE_MOVIE_SHOWTIMES))
|
||||
{
|
||||
text = PREFIX_MOVIE + _query;
|
||||
}
|
||||
|
||||
// Local Showtimes
|
||||
else if (_action.equals(CHOICE_LOCAL_SHOWTIMES))
|
||||
{
|
||||
if (isValidString(_location))
|
||||
{
|
||||
text = PREFIX_MOVIE + _query + ' ' + _location;
|
||||
}
|
||||
else
|
||||
{
|
||||
text = PREFIX_MOVIE + _query;
|
||||
}
|
||||
}
|
||||
|
||||
// Default
|
||||
else
|
||||
{
|
||||
msg.setPayloadText(query);
|
||||
text = _query;
|
||||
}
|
||||
|
||||
msg.setPayloadText(text);
|
||||
conn.send(msg);
|
||||
addHistory(text);
|
||||
|
||||
msg("SMS Sent", "The text message was sent.", mainScreen, false);
|
||||
//alert("SMS Sent", "The text message was sent.", _mainScreen, false);
|
||||
}
|
||||
catch (SecurityException e)
|
||||
{
|
||||
alert("SMS Error", "Access to SMS was refused: " + e.getMessage(), _mainScreen, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
msg("SMS Error", "The text message could not be sent: " + e.getMessage(), mainScreen, true);
|
||||
alert("SMS Error", "The text message could not be sent: " + e.getMessage(), _mainScreen, true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* @(#)HelpScreen.java
|
||||
*
|
||||
* Copyright (c) 2004, Erik C. Thauvin (http://www.thauvin.net/erik/)
|
||||
* Copyright (c) 2004-2005, Erik C. Thauvin (http://www.thauvin.net/erik/)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -37,16 +37,16 @@
|
|||
package net.thauvin.j2me.googlme;
|
||||
|
||||
import javax.microedition.lcdui.Form;
|
||||
import javax.microedition.lcdui.StringItem;
|
||||
|
||||
|
||||
/**
|
||||
* The <code>HelpScreen</code> class implements a form used to display some help.
|
||||
*
|
||||
* @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
|
||||
* @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
|
||||
* @version $Revision$, $Date$
|
||||
*
|
||||
* @created October 8, 2004
|
||||
* @since 1.0
|
||||
* @since 1.0
|
||||
*/
|
||||
public class HelpScreen extends Form
|
||||
{
|
||||
|
@ -57,12 +57,22 @@ public class HelpScreen extends Form
|
|||
*/
|
||||
public HelpScreen(GooglME midlet)
|
||||
{
|
||||
super(midlet.appName + " Help");
|
||||
super(GooglME.APP_NAME + " Help");
|
||||
|
||||
append("Google SMS: To get business listings, enter business name and city, state or zip. If you want to make sure you get Google Local results, put a period between the business name and the location. To get phone numbers, enter name of person and city, state or zip.\n\nFroogle Prices: For product prices, enter the product name.\n\nGoogle Search: For Google search snippets, enter the search terms.\n\nDefinition: For definitions, enter the word or term.");
|
||||
append(new StringItem(GooglME.CHOICE_GOOGLE_SMS + ": ",
|
||||
"To get business listings, enter business name and city, state or zip. To get phone numbers, enter name of person and city, state or zip.\n\n"));
|
||||
append(new StringItem(GooglME.CHOICE_GOOGLE_LOCAL + ": ",
|
||||
"To get local business listings, enter business name. To get local phone numbers, enter name of person. The permanent location setting is automatically added to your query.\n\n"));
|
||||
append(new StringItem(GooglME.CHOICE_FROOGLE_PRICES + ": ", "For product prices, enter the product name.\n\n"));
|
||||
append(new StringItem(GooglME.CHOICE_GOOGLE_SEARCH + ": ",
|
||||
"For Google search snippets, enter the search terms.\n\n"));
|
||||
append(new StringItem(GooglME.CHOICE_MOVIE_SHOWTIMES + ": ",
|
||||
"To get movie showtimes, enter the movie's title followed by your location (a zip code or city and state). For a listing of theaters, enter 'theaters' followed by your location. For a listing of movies playing, enter 'movies' followed by your location.\n\n"));
|
||||
append(new StringItem(GooglME.CHOICE_LOCAL_SHOWTIMES + ": ",
|
||||
"To get local movie showtimes, enter the movie's title. For a listing of theaters near you, enter 'theaters'. For a listing of movies playing near you, enter 'movies'. The permanent location setting is automatically added to your query.\n\n"));
|
||||
append(new StringItem(GooglME.CHOICE_DEFINITION + ": ", "For definitions, enter the word or term.\n\n"));
|
||||
|
||||
addCommand(midlet.exitCommand);
|
||||
addCommand(midlet.backCommand);
|
||||
addCommand(GooglME.COMMAND_BACK);
|
||||
|
||||
setCommandListener(midlet);
|
||||
}
|
||||
|
|
65
src/net/thauvin/j2me/googlme/HistoryScreen.java
Normal file
65
src/net/thauvin/j2me/googlme/HistoryScreen.java
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* @(#)HistoryScreen.java
|
||||
*
|
||||
* Copyright (c) 2004-2005, Erik C. Thauvin (http://www.thauvin.net/erik/)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of the authors nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* $Id$
|
||||
*
|
||||
*/
|
||||
package net.thauvin.j2me.googlme;
|
||||
|
||||
import javax.microedition.lcdui.List;
|
||||
|
||||
|
||||
/**
|
||||
* The <code>HistoryScreen</code> class implements a form used to display the history.
|
||||
*
|
||||
* @author Erik C. Thauvin
|
||||
* @version $Revision$, $Date$
|
||||
* @created Oct 12, 2004
|
||||
* @since 1.0
|
||||
*/
|
||||
public class HistoryScreen extends List
|
||||
{
|
||||
/**
|
||||
* Creates a new HistoryScreen object.
|
||||
*
|
||||
* @param midlet The MIDlet instance.
|
||||
*/
|
||||
public HistoryScreen(GooglME midlet)
|
||||
{
|
||||
super(GooglME.APP_NAME + " History", List.IMPLICIT);
|
||||
|
||||
addCommand(GooglME.COMMAND_BACK);
|
||||
|
||||
setCommandListener(midlet);
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* @(#)MainScreen.java
|
||||
*
|
||||
* Copyright (c) 2004, Erik C. Thauvin (http://www.thauvin.net/erik/)
|
||||
* Copyright (c) 2004-2005, Erik C. Thauvin (http://www.thauvin.net/erik/)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -44,11 +44,10 @@ import javax.microedition.lcdui.TextField;
|
|||
/**
|
||||
* The <code>MainScreen</code> class implements a form used to gather the data needed to perform a Google SMS query.
|
||||
*
|
||||
* @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
|
||||
* @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
|
||||
* @version $Revision$, $Date$
|
||||
*
|
||||
* @created October 8, 2004
|
||||
* @since 1.0
|
||||
* @since 1.0
|
||||
*/
|
||||
public class MainScreen extends Form
|
||||
{
|
||||
|
@ -69,21 +68,22 @@ public class MainScreen extends Form
|
|||
*/
|
||||
public MainScreen(GooglME midlet)
|
||||
{
|
||||
super(midlet.appName);
|
||||
super(GooglME.APP_NAME);
|
||||
|
||||
queryFld = new TextField("Query: ", "", 25, TextField.ANY);
|
||||
Util.setInitialInputMode(queryFld, "MIDP_LOWERCASE_LATIN");
|
||||
actionPopup =
|
||||
Util.getChoiceGroup("Type: ", new String[] { "Google SMS", "Froogle Prices", "Google Search", "Definition" });
|
||||
actionPopup = Util.getChoiceGroup("Type: ", GooglME.CHOICES);
|
||||
actionPopup.setSelectedIndex(0, true);
|
||||
append(queryFld);
|
||||
append(actionPopup);
|
||||
|
||||
addCommand(midlet.helpCommand);
|
||||
addCommand(midlet.aboutCommand);
|
||||
addCommand(midlet.clearCommand);
|
||||
addCommand(midlet.exitCommand);
|
||||
addCommand(midlet.sendCommand);
|
||||
addCommand(GooglME.COMMAND_SEND);
|
||||
addCommand(GooglME.COMMAND_CLEAR);
|
||||
addCommand(GooglME.COMMAND_HISTORY);
|
||||
addCommand(GooglME.COMMAND_SETTINGS);
|
||||
addCommand(GooglME.COMMAND_HELP);
|
||||
addCommand(GooglME.COMMAND_ABOUT);
|
||||
addCommand(GooglME.COMMAND_EXIT);
|
||||
|
||||
setCommandListener(midlet);
|
||||
}
|
||||
|
|
88
src/net/thauvin/j2me/googlme/PrefsScreen.java
Normal file
88
src/net/thauvin/j2me/googlme/PrefsScreen.java
Normal file
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* @(#)PrefsScreen.java
|
||||
*
|
||||
* Copyright (c) 2004-2005, Erik C. Thauvin (http://www.thauvin.net/erik/)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of the authors nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* $Id$
|
||||
*
|
||||
*/
|
||||
package net.thauvin.j2me.googlme;
|
||||
|
||||
import javax.microedition.lcdui.Form;
|
||||
import javax.microedition.lcdui.StringItem;
|
||||
import javax.microedition.lcdui.TextField;
|
||||
|
||||
|
||||
/**
|
||||
* The <code>PrefsScreen</code> class implements a form used to set the preferences.
|
||||
*
|
||||
* @author Erik C. Thauvin
|
||||
* @version $Revision$, $Date$
|
||||
* @created Oct 12, 2004
|
||||
* @since 1.0
|
||||
*/
|
||||
public class PrefsScreen extends Form
|
||||
{
|
||||
/**
|
||||
* The query field.
|
||||
*/
|
||||
protected /* final */ TextField locationFld;
|
||||
|
||||
/**
|
||||
* The SMS number field.
|
||||
*/
|
||||
protected /* final */ TextField smsFld;
|
||||
|
||||
/**
|
||||
* Creates a new PrefsScreen object.
|
||||
*
|
||||
* @param midlet The MIDlet instance.
|
||||
*/
|
||||
public PrefsScreen(GooglME midlet)
|
||||
{
|
||||
super(GooglME.APP_NAME + " Settings");
|
||||
|
||||
locationFld = new TextField("Location: ", "", 25, TextField.ANY);
|
||||
|
||||
append(locationFld);
|
||||
append(new StringItem("", "Your current city and state or zip. (e.g.: san francisco ca or 94123)"));
|
||||
|
||||
smsFld = new TextField("Google SMS: ", "", 25, TextField.PHONENUMBER);
|
||||
|
||||
append(smsFld);
|
||||
append(new StringItem("", "The short code or number of the Google SMS service. (e.g.: 46645)"));
|
||||
|
||||
addCommand(GooglME.COMMAND_BACK);
|
||||
addCommand(GooglME.COMMAND_SAVE);
|
||||
|
||||
setCommandListener(midlet);
|
||||
}
|
||||
}
|
269
src/net/thauvin/j2me/googlme/RmsIndex.java
Normal file
269
src/net/thauvin/j2me/googlme/RmsIndex.java
Normal file
|
@ -0,0 +1,269 @@
|
|||
/* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.thauvin.j2me.googlme;
|
||||
|
||||
import javax.microedition.rms.*;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* An Rms based BTree index. Currently, the functionality is similar to a
|
||||
* persistent hashtable.
|
||||
*/
|
||||
public class RmsIndex {
|
||||
|
||||
static byte[] DUMMY = new byte[0];
|
||||
|
||||
RecordStore store;
|
||||
|
||||
Node root;
|
||||
|
||||
int n = 32;
|
||||
|
||||
String cachedKey;
|
||||
|
||||
Node cachedNode;
|
||||
|
||||
int cachedIndex;
|
||||
|
||||
/** Node helper class */
|
||||
class Node {
|
||||
int index;
|
||||
|
||||
int size;
|
||||
|
||||
String[] keys = new String[n + n + 1];
|
||||
|
||||
String[] values = new String[n + n + 1];
|
||||
|
||||
int[] children = new int[n + n + 2];
|
||||
|
||||
/** Create a new Node */
|
||||
|
||||
Node() throws RecordStoreException {
|
||||
index = store.addRecord(DUMMY, 0, 0);
|
||||
}
|
||||
|
||||
Node(Node split) throws RecordStoreException {
|
||||
this();
|
||||
System.arraycopy(split.keys, n + 1, keys, 0, n);
|
||||
System.arraycopy(split.values, n + 1, values, 0, n);
|
||||
System.arraycopy(split.children, n + 1, children, 0, n + 1);
|
||||
size = n;
|
||||
split.size = n;
|
||||
split.store();
|
||||
store();
|
||||
}
|
||||
|
||||
/** Load the node at the given index position */
|
||||
|
||||
Node(int index) throws RecordStoreException {
|
||||
this.index = index;
|
||||
byte[] data = store.getRecord(index);
|
||||
|
||||
if (data == null || data.length == 0)
|
||||
return;
|
||||
|
||||
try {
|
||||
DataInputStream dis = new DataInputStream(
|
||||
new ByteArrayInputStream(data));
|
||||
|
||||
size = dis.readInt();
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
children[i] = dis.readInt();
|
||||
keys[i] = dis.readUTF();
|
||||
values[i] = dis.readUTF();
|
||||
}
|
||||
|
||||
children[size] = dis.readInt();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void put(String key, String value) throws RecordStoreException {
|
||||
|
||||
int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
int cmp = key.compareTo(keys[i]);
|
||||
if (cmp == 0) {
|
||||
values[i] = value;
|
||||
store();
|
||||
return;
|
||||
} else if (cmp < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
int newIndex = 0;
|
||||
|
||||
if (children[i] != 0) {
|
||||
Node child = new Node(children[i]);
|
||||
child.put(key, value);
|
||||
|
||||
if (child.size < n + n + 1)
|
||||
return;
|
||||
|
||||
Node split = new Node(child);
|
||||
newIndex = split.index;
|
||||
|
||||
key = child.keys[n];
|
||||
value = child.values[n];
|
||||
}
|
||||
|
||||
System.arraycopy(keys, i, keys, i + 1, size - i);
|
||||
System.arraycopy(values, i, values, i + 1, size - i);
|
||||
System.arraycopy(children, i + 1, children, i + 2, size - i);
|
||||
|
||||
keys[i] = key;
|
||||
values[i] = value;
|
||||
children[i + 1] = newIndex;
|
||||
size++;
|
||||
|
||||
if (size < n + n + 1)
|
||||
// otherwise, store will be performed by uplink
|
||||
store();
|
||||
}
|
||||
|
||||
public void store() throws RecordStoreException {
|
||||
|
||||
try {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
dos.writeInt(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
dos.writeInt(children[i]);
|
||||
dos.writeUTF(keys[i]);
|
||||
dos.writeUTF(values[i]);
|
||||
}
|
||||
dos.writeInt(children[size]);
|
||||
byte[] data = bos.toByteArray();
|
||||
store.setRecord(index, data, 0, data.length);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new index with the given Name
|
||||
*/
|
||||
public RmsIndex(String name) throws RecordStoreException {
|
||||
|
||||
store = RecordStore.openRecordStore(name, true);
|
||||
root = store.getNumRecords() == 0 ? new Node() : new Node(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value for the given key
|
||||
*
|
||||
* @param key: The key
|
||||
* @return String: The value associated with the key, or null if none.
|
||||
* @throws RecordStoreException
|
||||
*/
|
||||
public String get(String key) throws RecordStoreException {
|
||||
|
||||
Node current = root;
|
||||
|
||||
while (true) {
|
||||
int i;
|
||||
for (i = 0; i < current.size; i++) {
|
||||
int cmp = key.compareTo(current.keys[i]);
|
||||
if (cmp == 0)
|
||||
return current.values[i];
|
||||
else if (cmp < 0)
|
||||
break;
|
||||
}
|
||||
if (current.children[i] == 0)
|
||||
return null;
|
||||
current = new Node(current.children[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns the given value to the given key persistently
|
||||
*
|
||||
* @param key: The key
|
||||
* @param value: The value
|
||||
* @throws RecordStoreException
|
||||
*/
|
||||
public void put(String key, String value) throws RecordStoreException {
|
||||
|
||||
root.put(key, value);
|
||||
if (root.size < n + n + 1)
|
||||
return;
|
||||
|
||||
// split root....
|
||||
|
||||
Node left = root;
|
||||
root = new Node();
|
||||
|
||||
left.index = root.index;
|
||||
root.index = 1;
|
||||
|
||||
Node right = new Node(left);
|
||||
|
||||
root.keys[0] = left.keys[n];
|
||||
root.values[0] = left.values[n];
|
||||
root.children[0] = left.index;
|
||||
root.children[1] = right.index;
|
||||
root.size = 1;
|
||||
root.store();
|
||||
}
|
||||
|
||||
public void close() throws RecordStoreException {
|
||||
store.closeRecordStore();
|
||||
}
|
||||
|
||||
public int size() throws RecordStoreException {
|
||||
|
||||
Node current = root;
|
||||
|
||||
return current.size;
|
||||
|
||||
}
|
||||
|
||||
public String get(int recordNum) throws RecordStoreException {
|
||||
|
||||
Node current = root;
|
||||
|
||||
return current.values[recordNum];
|
||||
|
||||
}
|
||||
|
||||
public String getKey(int recordNum) throws RecordStoreException {
|
||||
|
||||
Node current = root;
|
||||
|
||||
return current.keys[recordNum];
|
||||
|
||||
}
|
||||
|
||||
public void deleteAll(String name) throws RecordStoreException {
|
||||
|
||||
store.closeRecordStore();
|
||||
RecordStore.deleteRecordStore(name);
|
||||
store = RecordStore.openRecordStore(name, true);
|
||||
root = store.getNumRecords() == 0 ? new Node() : new Node(1);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue