Initial import.

This commit is contained in:
Erik C. Thauvin 2004-10-01 01:19:12 +00:00
commit d2f3da2c12
18 changed files with 2291 additions and 0 deletions

View file

@ -0,0 +1,215 @@
/*
* @(#)SpellME.java
*
* Copyright (c) 2003-2004, 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.spellme;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;
/**
* Spells the given text using a phonetic alphabet.
*
* @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
* @version $Revision$, $Date$
*
* @created January 15, 2003
* @since 1.0
*/
public class SpellME extends MIDlet
{
private static final String[] ALPHABET =
new String[]
{
"Adam", "Boy", "Charlie", "David", "Edward", "Frank", "George", "Henry", "Ida", "John", "King", "Lincoln",
"Mary", "Nora", "Ocean", "Paul", "Queen", "Robert", "Sam", "Tom", "Union", "Victor", "William", "X-Ray",
"Young", "Zebra"
};
private static final String[] NATO_ALPHABET =
new String[]
{
"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet", "Kilo", "Lima",
"Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey",
"X-Ray", "Yankee", "Zulu"
};
private String init;
private String input;
private boolean exit = true;
private boolean nato = false;
private int index = -1;
/**
* Constructor for the SpellME object.
*/
public SpellME()
{
init = getAppProperty("SpellME-Default");
if (init == null)
{
init = "";
}
else if ("false".equals(init))
{
init = "";
}
String prop = getAppProperty("SpellME-NATO");
if ((prop != null) && "true".equals(prop))
{
this.nato = true;
}
prop = getAppProperty("SpellME-Exit");
if ((prop != null) && "false".equals(prop))
{
this.exit = false;
}
}
/**
* Signals the MIDlet to terminate and enter the Destroyed state.
*
* @param ignore Ignored.
*/
protected void destroyApp(boolean ignore)
{
}
/**
* Signals the MIDlet to enter the Paused state.
*/
protected void pauseApp()
{
}
/**
* Signals the MIDlet that it has entered the Active state.
*/
protected void startApp()
{
Display.getDisplay(this).setCurrent(new TextInputScreen(this, init, exit));
}
/**
* Sets the Input attribute of the SpellME object.
*
* @param str The new Input value.
*/
void setInput(String str)
{
this.input = str;
}
/**
* Returns the nextCharacter attribute of the SpellME object.
*
* @return The NextCharacter value.
*/
String getNextCharacter()
{
index++;
if (index < input.length())
{
final char c = input.charAt(index);
if ((c < 91) && (c > 64))
{
if (nato)
{
return NATO_ALPHABET[(int) c - 65];
}
return ALPHABET[(int) c - 65];
}
else if ((c < 123) && (c > 96))
{
if (nato)
{
return NATO_ALPHABET[(int) c - 97];
}
return ALPHABET[(int) c - 97];
}
else
{
return String.valueOf(c);
}
}
return "";
}
/**
* Exit request.
*/
void exitRequested()
{
destroyApp(false);
notifyDestroyed();
}
/**
* Returns <code>true</code> if there is a next character to display.
*
* @return <code>true</code> if there is a next character, <code>false</code> otherwise.
*/
boolean hasNextCharacter()
{
if (input != null)
{
if ((index + 1) < input.length())
{
return true;
}
}
return false;
}
/**
* Displays the next character.
*/
void nextCharacter()
{
Display.getDisplay(this).setCurrent(new TextCanvas(this, exit));
}
}

View file

@ -0,0 +1,188 @@
/*
* @(#)TextCanvas.java
*
* Copyright (c) 2003-2004, 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.spellme;
import javax.microedition.lcdui.*;
/**
* The {@link Canvas} used to display the spelled characters.
*
* @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
* @version $Revision$, $Date$
*
* @created January 15, 2003
* @since 1.0
*/
class TextCanvas extends Canvas implements CommandListener
{
/**
* The backgroud color.
*/
private static final int BG_COLOR = 0xffffff;
/**
* The forground color.
*/
private static final int FG_COLOR = 0x000000;
/**
* The <code>Exit</code> command.
*/
private final Command exitCommand;
/**
* The <code>Next</code> command.
*/
private final Command nextCommand;
/**
* The MIDlet instance.
*/
private final SpellME midlet;
private String output;
/**
* Constructor for the TextCanvas object.
*
* @param spellMidlet The MIDlet instance.
* @param exit <code>true</code> if the <code>Exit</code> command should be added.
*/
TextCanvas(SpellME spellMidlet, boolean exit)
{
this.midlet = spellMidlet;
nextCommand = new Command("Next", Command.SCREEN, 1);
exitCommand = new Command("Exit", Command.EXIT, 2);
if (exit)
{
addCommand(exitCommand);
}
if (spellMidlet.hasNextCharacter())
{
output = spellMidlet.getNextCharacter();
if (spellMidlet.hasNextCharacter())
{
addCommand(nextCommand);
}
setCommandListener(this);
}
else
{
spellMidlet.exitRequested();
}
}
/**
* Indicates that a command event has occurred.
*
* @param c Command object identifying the command.
* @param d The {@link Displayable} on which this event has occurred.
*/
public void commandAction(Command c, Displayable d)
{
if (c == exitCommand)
{
midlet.exitRequested();
}
else if (c == nextCommand)
{
dismiss();
}
}
/**
* Called when a key is pressed.
*
* @param keyCode The key code of the key that was pressed.
*/
public void keyPressed(int keyCode)
{
dismiss();
}
/**
* Renders the Canvas.
*
* @param g The {@link Graphics} object to be used for rendering the Canvas
*/
public void paint(Graphics g)
{
final Font font = g.getFont();
g.setFont(Font.getFont(font.getFace(), Font.STYLE_BOLD, Font.SIZE_LARGE));
g.setColor(BG_COLOR);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(FG_COLOR);
int height = getHeight() >> 1;
final int width = getWidth() >> 1;
final int align = Graphics.HCENTER | Graphics.BASELINE;
if (output.length() > 1)
{
height -= font.getHeight();
g.drawString(output.substring(0, 1), width, height, align);
g.setFont(Font.getFont(font.getFace(), Font.STYLE_PLAIN, Font.SIZE_SMALL));
height += font.getHeight();
g.drawString("as in", width, height, align);
height += font.getHeight();
g.setFont(Font.getFont(font.getFace(), Font.STYLE_BOLD, Font.SIZE_LARGE));
}
g.drawString(output, width, height, align);
}
/**
* Dissmisses the current screen.
*/
private void dismiss()
{
if (midlet.hasNextCharacter())
{
midlet.nextCharacter();
}
else
{
midlet.exitRequested();
}
}
}

View file

@ -0,0 +1,110 @@
/*
* @(#)TextInputScreen.java
*
* Copyright (c) 2003-2004, 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.spellme;
import javax.microedition.lcdui.*;
/**
* The {@link TextBox} screen used to enter the text to spell.
*
* @author <a href="http://www.thauvin.net/erik/">Erik C. Thauvin</a>
* @version $Revision$, $Date$
*
* @created January 15, 2003
* @since 1.0
*/
class TextInputScreen extends TextBox implements CommandListener
{
/**
* The <code>Exit</code> command.
*/
private final Command exitCommand;
/**
* The <code>Start</code> command.
*/
private final Command startCommand;
/**
* The MIDlet instance.
*/
private final SpellME midlet;
/**
* Constructor for the TextInputScreen object.
*
* @param spellMidlet The MIDlet instance.
* @param str The default string, if any.
* @param exit <code>true</code> if the <code>Exit</code> command should be added.
*/
TextInputScreen(SpellME spellMidlet, String str, boolean exit)
{
super("Enter text: ", str, 256, TextField.ANY);
this.midlet = spellMidlet;
exitCommand = new Command("Exit", Command.EXIT, 2);
startCommand = new Command("Start", Command.SCREEN, 1);
if (exit)
{
addCommand(exitCommand);
}
addCommand(startCommand);
setCommandListener(this);
}
/**
* Indicates that a command event has occurred.
*
* @param c Command object identifying the command.
* @param d The {@link Displayable} on which this event has occurred.
*/
public void commandAction(Command c, Displayable d)
{
if (c == exitCommand)
{
midlet.exitRequested();
}
else if (c == startCommand)
{
midlet.setInput(getString());
midlet.nextCharacter();
}
}
}