package com.sc.chordbook;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Vector;
import java.util.Timer;
import java.util.TimerTask;
public class FindShapeMidlet extends MIDlet implements CommandListener
{
private Display mDisplay;
private Form mRootForm, mTypeForm, mProgressForm;
private ChordViewCanvas mChordViewCanvas;
private ChoiceGroup mRootChoice, mTypeChoice;
private StringItem mProgressString;
private Gauge mProgressGauge;
private Command mNextCommand, mBackCommand;
private Command mFindCommand, mMoreCommand;
private Command mExitCommand;
private String mRoot, mType;
private Vector mChords;
private int mProgressValue = 0;
public FindShapeMidlet()
{
mChords = new Vector();
mRootForm = new Form("Chord root");
String[] roots = {"A", "Bb", "B", "C", "C#", "D", "Eb", "E", "F", "F#", "G", "Ab"};
mRootChoice = new ChoiceGroup(null, Choice.EXCLUSIVE, roots, null);
mRootForm.append(mRootChoice);
mTypeForm = new Form("Chord type");
String[] types = {"maj", "m", "7", "maj7", "m7", "6", "maj6", "m6", "9", "maj9",
"m9", "6/9", "sus4", "7sus4", "9sus4", "dim", "aug", "aug7",
"aug9", "m11", "13", "maj13", "min13", "7b5", "m7b5", "9b5"};
mTypeChoice = new ChoiceGroup(null, Choice.EXCLUSIVE, types, null);
mTypeForm.append(mTypeChoice);
mProgressForm = new Form("Fetching chord...");
mProgressGauge = new Gauge(null, false, 10, 0);
mProgressString = new StringItem(null, null);
mProgressForm.append(mProgressGauge);
mProgressForm.append(mProgressString);
mChordViewCanvas = new ChordViewCanvas();
mNextCommand = new Command("Next", Command.SCREEN, 0);
mBackCommand = new Command("Back", Command.BACK, 0);
mFindCommand = new Command("Find", Command.SCREEN, 0);
mMoreCommand = new Command("More", Command.SCREEN, 0);
mExitCommand = new Command("Exit", Command.EXIT, 0);
mRootForm.addCommand(mNextCommand);
mRootForm.addCommand(mExitCommand);
mRootForm.setCommandListener(this);
mTypeForm.addCommand(mBackCommand);
mTypeForm.addCommand(mFindCommand);
mTypeForm.setCommandListener(this);
mProgressForm.addCommand(mBackCommand);
mProgressForm.setCommandListener(this);
mChordViewCanvas.addCommand(mBackCommand);
mChordViewCanvas.addCommand(mMoreCommand);
mChordViewCanvas.setCommandListener(this);
}
public void startApp()
{
mDisplay = Display.getDisplay(this);
mChordViewCanvas.setUseColour(mDisplay.isColor());
mChordViewCanvas.setNumColours(mDisplay.numColors());
Display.getDisplay(this).setCurrent(mRootForm);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
if (c == mNextCommand)
{
mRoot = mRootChoice.getString(mRootChoice.getSelectedIndex());
mDisplay.setCurrent(mTypeForm);
}
else
if (c == mBackCommand)
{
mDisplay.setCurrent(mRootForm);
}
else
if (c == mFindCommand)
{
mType = mTypeChoice.getString(mTypeChoice.getSelectedIndex());
mDisplay.setCurrent(mProgressForm);
Fetcher f = new Fetcher(getAppProperty("xxx"), mRoot, mType);
f.start();
}
else
if (c == mMoreCommand)
{
mChordViewCanvas.nextChord();
mChordViewCanvas.repaint();
}
else
if (c == mExitCommand)
{
notifyDestroyed();
}
}
private class Fetcher extends Thread
{
private StringBuffer url;
public Fetcher(String baseURL, String rootValue, String typeValue)
{
mChords.removeAllElements();
rootValue = rootValue.replace('#', 's');
url = new StringBuffer();
url.append(baseURL);
url.append("?root=");
url.append(rootValue);
url.append("&type=");
url.append(typeValue);
System.out.println("url = " + url.toString());
}
public void run()
{
HttpConnection c = null;
InputStream in = null;
Timer timer = new Timer();
timer.schedule(new ProgressSpinner(), 0, 100);
try
{
mProgressString.setText("Sending...");
c = (HttpConnection)Connector.open(url.toString());
mProgressString.setText("Waiting...");
in = c.openInputStream();
if (c.getResponseCode() != HttpConnection.HTTP_OK)
{
System.out.println("HTTP error: " + c.getResponseCode());
}
else
{
int numChords = Integer.parseInt(c.getHeaderField("numChords"));
for (int i = 0; i < numChords; i++)
{
byte pos = (byte)in.read();
byte[] frets = new byte[Settings.NUM_STRINGS];
in.read(frets);
byte[] fingers = new byte[Settings.NUM_STRINGS];
in.read(fingers);
ChordBox chord = new ChordBox(mRoot + mType, pos, frets, fingers);
mChords.addElement(chord);
}
if (mChords.size() == 0)
{
String msg = "sorry, no match for " + mRoot + mType;
Alert results = new Alert("Search Result", msg, null, null);
mDisplay.setCurrent(results, mRootForm);
}
else
{
mChordViewCanvas.reset();
mChordViewCanvas.setChords(mChords);
mDisplay.setCurrent(mChordViewCanvas);
}
}
}
catch (IOException ioe)
{
String msg = "Servlet error: " + ioe;
Alert error = new Alert("Error", msg, null, null);
mDisplay.setCurrent(error, mRootForm);
}
finally
{
try
{
in.close();
c.close();
}
catch (IOException ioe) {}
}
}
}
private class ProgressSpinner extends TimerTask
{
public void run()
{
mProgressValue++;
mProgressValue %= 11;
mProgressGauge.setValue(mProgressValue);
}
}
}