Skip to content

Commit

Permalink
Merge pull request #46 from blackberry-webworks/next-back-customAsk
Browse files Browse the repository at this point in the history
Pull Req: Next back custom ask
  • Loading branch information
dmateescu committed Feb 8, 2012
2 parents bd49e27 + 53cedc8 commit e491ea1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
Expand Up @@ -37,28 +37,25 @@ public class CustomAskAsyncFunction extends ScriptableFunctionBase {
public Object execute( Object thiz, Object[] args ) throws Exception { public Object execute( Object thiz, Object[] args ) throws Exception {
String message; String message;
String[] buttons; String[] buttons;
int[] values; // the default value of the default choice. The developer cannot change it.
//the default value of the default choice. The developer cannot change it.
final int defaultChoice = 0; final int defaultChoice = 0;
//callback function // callback function
ScriptableFunction callback = (ScriptableFunction) args[ 2 ]; ScriptableFunction callback = (ScriptableFunction) args[ 2 ];
//the default value of the global status. The developer cannot change it. // the default value of the global status. The developer cannot change it.
final boolean global = false; final boolean global = false;
// message // message
message = (String) args[ 0 ]; message = (String) args[ 0 ];
// choices & values // choices & values
Scriptable stringArray = (Scriptable) args[ 1 ]; Scriptable stringArray = (Scriptable) args[ 1 ];
int count = stringArray.getElementCount(); int count = stringArray.getElementCount();
buttons = new String[ count ]; buttons = new String[ count ];
values = new int[ count ];
for( int i = 0; i < count; i++ ) { for( int i = 0; i < count; i++ ) {
buttons[ i ] = stringArray.getElement( i ).toString(); buttons[ i ] = stringArray.getElement( i ).toString();
values[ i ] = i;
} }


Runnable dr = DialogRunnableFactory.getCustomAskRunnable(message, buttons, values, defaultChoice, global, callback); Runnable dr = DialogRunnableFactory.getCustomAskRunnable( message, buttons, defaultChoice, global, callback );
// queue // queue
UiApplication.getUiApplication().invokeLater(dr); UiApplication.getUiApplication().invokeLater( dr );
// return value // return value
return Scriptable.UNDEFINED; return Scriptable.UNDEFINED;
} }
Expand All @@ -75,7 +72,7 @@ protected FunctionSignature[] getFunctionSignatures() {
fs.addParam( Scriptable.class, true ); fs.addParam( Scriptable.class, true );
// callback // callback
fs.addParam( ScriptableFunction.class, true ); fs.addParam( ScriptableFunction.class, true );
// filler // filler
fs.addParam( Object.class, false ); fs.addParam( Object.class, false );
return new FunctionSignature[] { fs }; return new FunctionSignature[] { fs };
} }
Expand Down
Expand Up @@ -82,9 +82,9 @@ public static Runnable getSelectRunnable(boolean allowMultiple, String[] labels,
* @param callback the callback function * @param callback the callback function
* @return the Runnable responsible for opening the dialog * @return the Runnable responsible for opening the dialog
*/ */
public static Runnable getCustomAskRunnable(String message, String[] buttons, int[] values, int defaultChoice, boolean global /* style, false */, ScriptableFunction callback) { public static Runnable getCustomAskRunnable(String message, String[] buttons, int defaultChoice, boolean global /* style, false */, ScriptableFunction callback) {
Dialog d = new Dialog( message, buttons, values, defaultChoice, null /* bitmap */, global ? Dialog.GLOBAL_STATUS : 0 /* style */); Dialog d = new Dialog( message, buttons, null, defaultChoice, null /* bitmap */, global ? Dialog.GLOBAL_STATUS : 0 /* style */);
return new DialogAsyncRunnable(d, callback); return new DialogAsyncRunnable( d, callback );
} }


/** /**
Expand Down Expand Up @@ -177,20 +177,19 @@ private static class DialogAsyncRunnable implements Runnable {
_callback = callback; _callback = callback;
} }



/** /**
* Run the dialog. * Run the dialog.
* *
* @see java.lang.Runnable#run() * @see java.lang.Runnable#run()
*/ */
public void run() { public void run() {
_dialogValue = new Integer(_dialog.doModal()); _dialogValue = new Integer( _dialog.doModal() );
//get object's string as all ecma primitives will return a valid string representation of themselves // get object's string as all ecma primitives will return a valid string representation of themselves
Object retVal = _dialogValue.toString(); Object retVal = _dialogValue.toString();
try { try {
_callback.invoke(null, new Object[] { retVal }); _callback.invoke( null, new Object[] { retVal } );
} catch (Exception e) { } catch( Exception e ) {
throw new RuntimeException("Invoke callback failed: " + e.getMessage()); throw new RuntimeException( "Invoke callback failed: " + e.getMessage() );
} }
} }
} }
Expand Down

0 comments on commit e491ea1

Please sign in to comment.