Skip to content

Commit

Permalink
Add J2ME version
Browse files Browse the repository at this point in the history
Tested on Nokia N97 mini and WTK emulator
This version is using Textfields. Unfortunately the event handler
doesn't really fit this application's needs (i.e. there's no way to just
execute something when a Textfield looses focus). Additionally the WTK
emulator is loosing focus of the active field when adding a new
Textfield. This results in some strange focus handling...
  • Loading branch information
laenion committed Mar 6, 2014
1 parent 97f7fdb commit b3f3280
Show file tree
Hide file tree
Showing 6 changed files with 691 additions and 20 deletions.
78 changes: 60 additions & 18 deletions build.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
<project name="Entscheidungsfinder" default="dist" basedir=".">
<project name="Entscheidungsfinder" default="desktop" basedir=".">
<!-- Overwrite settings with this file -->
<property file="build.properties" />

<property name="version" value="1.0" />
<property name="src" location="." />
<property name="build" location="build" />
<property name="dist" location="dist" />

<target name="compile">
<mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}" includeAntRuntime="false" target="1.6" source="1.6" bootclasspath="${buildclasspath}" />
<copy todir="${build}">
<fileset dir=".">
<include name="*.properties" />
</fileset>
</copy>
<property name="src_desktop" location="desktop" />
<property name="src_j2me" location="j2me" />
<property name="build_dir" location="build" />
<property name="dist_dir" location="dist" />

<macrodef name="build">
<attribute name="bootclasspath" />
<attribute name="src" />
<attribute name="java-compat" />
<attribute name="build-dir" />
<sequential>
<mkdir dir="@{build-dir}"/>
<javac srcdir="@{src}" destdir="@{build-dir}" includeAntRuntime="false" target="@{java-compat}" source="@{java-compat}" bootclasspath="@{bootclasspath}" />
<copy todir="@{build-dir}">
<fileset dir=".">
<include name="localization*.properties" />
</fileset>
</copy>
</sequential>
</macrodef>

<target name="build_desktop">
<build bootclasspath="${buildclasspath}" src="${src_desktop}" java-compat="1.6" build-dir="${build_dir}/${src_desktop}" />
</target>

<target name="dist" depends="compile">
<mkdir dir="${dist}" />
<jar jarfile="${dist}/Entscheidungsfinder.jar" basedir="${build}">
<target name="dist_desktop" depends="build_desktop">
<mkdir dir="${dist_dir}" />
<jar jarfile="${dist_dir}/Entscheidungsfinder.jar" basedir="${build_dir}/${src_desktop}">
<manifest>
<attribute name="Main-Class" value="Entscheidungsfinder" />
<attribute name="Codebase" value="*" />
Expand All @@ -28,8 +39,39 @@
</jar>
</target>

<!-- Compile for J2ME capable mobile devices; j2me_sdk_path has to be set in build.properties. -->
<target name="build_j2me">
<build bootclasspath="${j2me_sdk_path}/lib/cldcapi11.jar:${j2me_sdk_path}/lib/midpapi20.jar" src="${src_j2me}" java-compat="1.3" build-dir="${build_dir}/${src_j2me}" />
<echo>Preverifying classes</echo>
<exec executable="${j2me_sdk_path}/bin/preverify">
<arg value="-classpath" />
<arg value="${j2me_sdk_path}/lib/cldcapi11.jar:${j2me_sdk_path}/lib/midpapi20.jar" />
<arg value="-d" />
<arg value="${build_dir}/${src_j2me}" />
<arg value="${build_dir}/${src_j2me}" />
</exec>
</target>

<target name="dist_j2me" depends="build_j2me">
<mkdir dir="${dist_dir}" />
<jar jarfile="${dist_dir}/EntscheidungsfinderME.jar" basedir="${build_dir}/${src_j2me}">
<manifest>
<attribute name="MIDlet-1" value="Entscheidungsfinder, ,EntscheidungsfinderME" />
<attribute name="MIDlet-Name" value="Entscheidungsfinder" />
<attribute name="MIDlet-Version" value="1.0" />
<attribute name="MIDlet-Vendor" value="digitalimagecorp.de" />
<attribute name="MicroEdition-Profile" value="MIDP-2.0" />
<attribute name="MicroEdition-Configuration" value="CLDC-1.1" />
</manifest>
</jar>
</target>

<target name="clean">
<delete dir="${build}" />
<delete dir="${dist}" />
<delete dir="${build_dir}" />
<delete dir="${dist_dir}" />
</target>

<target name="desktop" depends="dist_desktop" />
<target name="j2me" depends="dist_j2me" />
<target name="all" depends="desktop, j2me" />
</project>
File renamed without changes.
156 changes: 156 additions & 0 deletions j2me/EntscheidungsfinderME.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
Copyright (c) 2014, Ignaz Forster
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.
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 HOLDER 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.
*/
import java.util.Random;
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.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;

public class EntscheidungsfinderME extends MIDlet implements CommandListener {
Display display;
Form form;
Command exit;
Command decision;
Item lastFocusedItem;
int inputItems = 0;

public void startApp() {
display = Display.getDisplay(this);
form = new Form(LocalizationSupport.getMessage("PROGRAM_TITLE"));
form.append(LocalizationSupport.getMessage("ENTER_OPTIONS"));
form.setItemStateListener(new TextFieldListener());
form.setCommandListener(this);
exit = new Command(LocalizationSupport.getMessage("EXIT"), Command.EXIT, 1);
form.addCommand(exit);
decision = new Command(LocalizationSupport.getMessage("BUTTON_CALCULATE_SHORT"), LocalizationSupport.getMessage("BUTTON_CALCULATE"), Command.SCREEN, 2);
form.addCommand(decision);

lastFocusedItem = addField();
addField();
addField();
display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

protected Item addField() {
inputItems++;
TextField input = new TextField(inputItems + ".", null, 100, TextField.ANY);
input.setLayout(TextField.LAYOUT_EXPAND | TextField.LAYOUT_SHRINK);
form.append(input);
return input;
}

protected void changeLabel(TextField textfield, int counter) {
if (!textfield.getLabel().equals(counter + ".")) {
textfield.setLabel(counter + ".");
}
}

/**
* Parse the list, remove empty fields and fix numeration.
*
* As it's not sure when itemStateChanged will be called - it may be
* either after typing a single key or after leaving a TextField -
* be sure always keep 2 spare empty fields so that the user
* has something to change to in the later case.
*
* @param item
*/
protected void fixList(Item item) {
TextField textfield;
for (int i = 1; i <= inputItems; i++) {
textfield = (TextField) form.get(i);
// Check for empty fields, but leave the last two fields alone
if (textfield.getString().equals("")) {
if (i <= inputItems - 2) {
deleteField(i);
i--;
}
} else { // Make sure the last two field are empty
if (i >= inputItems - 1) {
addField();
fixFocus(i);

// Someone entered something in the last field - go back and clean up...
if (i == inputItems - 1) {
i = i - 2;
}
}
}
changeLabel(textfield, i);
}
}

/**
* Focus handling seems to be broken in the Sun Java Wireless Toolkit Emulator when adding new elements.
* Make sure that the user will continue typing in the field where he started
* @param i Field to set the focus on
*/
protected void fixFocus(int i) {
display.setCurrentItem(form.get(i - 1));
display.setCurrentItem(form.get(i));
}

protected void deleteField(int i) {
form.delete(i);
display.setCurrentItem(form.get(i));
inputItems--;
}

public void commandAction(Command c, Displayable s) {
if (c == exit) {
destroyApp(false);
notifyDestroyed();
}

if (c == decision) {
String result;

// Clean up fields in case phone implementation didn't call ItemStateListener yet
fixList(form.get(1));

if (((TextField)form.get(1)).getString().equals("")) {
result = LocalizationSupport.getMessage("NOTHING");
} else {
Random randomizer = new Random();
result = ((TextField) form.get(randomizer.nextInt(inputItems - 2) + 1)).getString();
}
Alert resultScreen = new Alert(LocalizationSupport.getMessage("RESULT"), result, null, AlertType.INFO);
resultScreen.setTimeout(Alert.FOREVER);
display.setCurrent(resultScreen, form);
}
}

protected class TextFieldListener implements ItemStateListener {
public void itemStateChanged(Item item) {
// Simulte focusLost
if (lastFocusedItem == item) {
return;
}
fixList(item);

lastFocusedItem = item;
}
}
}
Loading

0 comments on commit b3f3280

Please sign in to comment.