Permalink
Browse files

Merge pull request #96 from blackberry-webworks/master

The 2.3.1 version of the BB SDK for Smartphone
  • Loading branch information...
2 parents 306ce7f + 1352a61 commit c5c40489f25b4098dd10e9cb856d96273ea3df64 Ken Wallis committed Feb 21, 2012
@@ -41,7 +41,7 @@ public Object execute( Object thiz, Object[] args ) throws Exception {
ScriptableFunction callback = (ScriptableFunction) args[ 1 ];
// create dialog
- Runnable dr = DialogRunnableFactory.getColorPickerRunnable( initialColor, callback, thiz );
+ Runnable dr = DialogRunnableFactory.getColorPickerRunnable( initialColor, callback );
// queue
UiApplication.getUiApplication().invokeLater( dr );
@@ -0,0 +1,79 @@
+/*
+* Copyright 2010-2011 Research In Motion Limited.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package blackberry.ui.dialog;
+
+import net.rim.device.api.script.Scriptable;
+import net.rim.device.api.script.ScriptableFunction;
+import net.rim.device.api.ui.UiApplication;
+import blackberry.core.FunctionSignature;
+import blackberry.core.ScriptableFunctionBase;
+
+/**
+ * Implementation of asynchronous custom ask function (blackberry.ui.dialog.customAskAsync)
+ *
+ * @author dmateescu
+ *
+ */
+public class CustomAskAsyncFunction extends ScriptableFunctionBase {
+
+ public static final String NAME = "customAskAsync";
+
+ /**
+ * @see blackberry.core.ScriptableFunctionBase#execute(Object, Object[])
+ */
+ public Object execute( Object thiz, Object[] args ) throws Exception {
+ String message;
+ String[] buttons;
+ // the default value of the default choice. The developer cannot change it.
+ final int defaultChoice = 0;
+ // callback function
+ ScriptableFunction callback = (ScriptableFunction) args[ 2 ];
+ // the default value of the global status. The developer cannot change it.
+ final boolean global = false;
+ // message
+ message = (String) args[ 0 ];
+ // choices & values
+ Scriptable stringArray = (Scriptable) args[ 1 ];
+ int count = stringArray.getElementCount();
+ buttons = new String[ count ];
+ for( int i = 0; i < count; i++ ) {
+ buttons[ i ] = stringArray.getElement( i ).toString();
+ }
+
+ Runnable dr = DialogRunnableFactory.getCustomAskRunnable( message, buttons, defaultChoice, global, callback );
+ // queue
+ UiApplication.getUiApplication().invokeLater( dr );
+ // return value
+ return Scriptable.UNDEFINED;
+ }
+
+
+ /**
+ * @see blackberry.core.ScriptableFunctionBase#getFunctionSignatures()
+ */
+ protected FunctionSignature[] getFunctionSignatures() {
+ FunctionSignature fs = new FunctionSignature( 3 );
+ // message
+ fs.addParam( String.class, true );
+ // choices
+ fs.addParam( Scriptable.class, true );
+ // callback
+ fs.addParam( ScriptableFunction.class, true );
+ // filler
+ fs.addParam( Object.class, false );
+ return new FunctionSignature[] { fs };
+ }
+}
@@ -47,7 +47,7 @@ public Object execute(Object thiz, Object[] args) throws Exception {
String min = (String)options.getField("min");
String max = (String)options.getField("max");
- Runnable dr = DialogRunnableFactory.getDateTimeRunnable(type, value, min, max, callback, thiz);
+ Runnable dr = DialogRunnableFactory.getDateTimeRunnable(type, value, min, max, callback);
// queue
UiApplication.getUiApplication().invokeLater(dr);
@@ -17,7 +17,6 @@
import java.util.Hashtable;
-import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.script.Scriptable;
import net.rim.device.api.ui.component.Dialog;
@@ -52,7 +51,9 @@ public DialogNamespace() {
_fields = new Hashtable();
_fields.put( StandardAskFunction.NAME, new StandardAskFunction() );
+ _fields.put( StandardAskAsyncFunction.NAME, new StandardAskAsyncFunction());
_fields.put( CustomAskFunction.NAME, new CustomAskFunction() );
+ _fields.put(CustomAskAsyncFunction.NAME, new CustomAskAsyncFunction());
_fields.put( SelectAsyncFunction.NAME, new SelectAsyncFunction() );
_fields.put( DateTimeAsyncFunction.NAME, new DateTimeAsyncFunction() );
_fields.put( ColorPickerAsyncFunction.NAME, new ColorPickerAsyncFunction() );
@@ -18,37 +18,92 @@
import java.util.Vector;
-import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.script.ScriptableFunction;
import net.rim.device.api.script.ScriptableImpl;
-
+import net.rim.device.api.system.DeviceInfo;
+import net.rim.device.api.ui.component.Dialog;
+import blackberry.ui.dialog.color.ColorPickerDialog;
import blackberry.ui.dialog.datetime.DateTimeDialog;
-import blackberry.ui.dialog.IWebWorksDialog;
import blackberry.ui.dialog.select.SelectDialog;
-import blackberry.ui.dialog.color.ColorPickerDialog;
+/**
+ * Factory class for running dialogs asynchronously
+ *
+ */
public class DialogRunnableFactory {
- public static Runnable getDateTimeRunnable( String type, String value, String min, String max, ScriptableFunction callback,
- Object thiz ) {
+ /**
+ * Factory method for running an asynchronous DateTime dialog
+ * @param type the dialog's type
+ * @param value the value
+ * @param min the minimum value
+ * @param max the maximum value
+ * @param callback the callback function
+ * @return the Runnable responsible for opening the dialog
+ */
+ public static Runnable getDateTimeRunnable( String type, String value, String min, String max, ScriptableFunction callback) {
IWebWorksDialog d = new DateTimeDialog( type, value, min, max );
- return new DialogRunnable( d, callback, thiz );
+ return new DialogRunnable( d, callback );
}
- public static Runnable getColorPickerRunnable( int initialColor, ScriptableFunction callback, Object thiz ) {
+ /**
+ * Factory method for running an asynchronous ColorPicker dialog
+ * @param initialColor the initial color
+ * @param callback the callback function
+ * @return the Runnable responsible for opening the dialog
+ */
+ public static Runnable getColorPickerRunnable( int initialColor, ScriptableFunction callback ) {
ColorPickerDialog d = new ColorPickerDialog( initialColor );
- return new DialogRunnable( d, callback, thiz );
+ return new DialogRunnable( d, callback );
}
- public static Runnable getSelectRunnable(boolean allowMultiple, String[] labels, boolean[] enabled, boolean[] selected, int[] types, ScriptableFunction callback, Object thiz) {
+ /**
+ * Factory method for running an asynchronous Select dialog
+ * @param allowMultiple flag indicating whether multiple values are allowed
+ * @param labels the labels
+ * @param enabled the enabled values
+ * @param selected the selected values
+ * @param types the types of the values
+ * @param callback the callback function
+ * @return the Runnable responsible for opening the dialog
+ */
+ public static Runnable getSelectRunnable(boolean allowMultiple, String[] labels, boolean[] enabled, boolean[] selected, int[] types, ScriptableFunction callback) {
IWebWorksDialog d = new SelectDialog(allowMultiple, labels, enabled, selected, types);
- return new DialogRunnable(d, callback, thiz);
+ return new DialogRunnable(d, callback);
+ }
+
+ /**
+ * Factory method for running an asynchronous CustomAsk dialog
+ * @param message the message to be displayed in the dialog
+ * @param buttons the choices presented as buttons
+ * @param values the values of the choices
+ * @param defaultChoice the default choice
+ * @param global the global status
+ * @param callback the callback function
+ * @return the Runnable responsible for opening the dialog
+ */
+ public static Runnable getCustomAskRunnable(String message, String[] buttons, int defaultChoice, boolean global /* style, false */, ScriptableFunction callback) {
+ Dialog d = new Dialog( message, buttons, null, defaultChoice, null /* bitmap */, global ? Dialog.GLOBAL_STATUS : 0 /* style */);
+ return new DialogAsyncRunnable( d, callback );
+ }
+
+ /**
+ * Factory method for running an asynchronous StandardAsk dialog
+ * @param message the message to be displayed in the dialog
+ * @param type the dialog's type
+ * @param defaultChoice
+ * @param global the global status
+ * @param callback the callback function
+ * @return the Runnable responsible for running the dialog
+ */
+ public static Runnable getStandardAskRunnable(String message, int type, int defaultChoice, boolean global /* style, false */, ScriptableFunction callback) {
+ Dialog d = new Dialog( type, message, defaultChoice, null /* bitmap */, global ? Dialog.GLOBAL_STATUS : 0 /* style */);
+ return new DialogAsyncRunnable(d, callback);
}
private static class DialogRunnable implements Runnable {
private IWebWorksDialog _dialog;
private ScriptableFunction _callback;
- private Object _context;
/**
* Constructs a <code>DialogRunnable</code> object.
@@ -57,13 +112,10 @@ public static Runnable getSelectRunnable(boolean allowMultiple, String[] labels,
* The dialog
* @param callback
* The callback
- * @param context
- * The context in which the callback executes (its "this" object)
*/
- DialogRunnable( IWebWorksDialog dialog, ScriptableFunction callback, Object context ) {
+ DialogRunnable( IWebWorksDialog dialog, ScriptableFunction callback ) {
_dialog = dialog;
_callback = callback;
- _context = context;
}
@@ -106,4 +158,39 @@ public void run() {
}
}
}
+
+ private static class DialogAsyncRunnable implements Runnable {
+ private Dialog _dialog;
+ private ScriptableFunction _callback;
+ private Integer _dialogValue;
+
+ /**
+ * Constructs a <code>DialogRunnable</code> object.
+ *
+ * @param dialog
+ * The dialog
+ * @param callback
+ * The callback
+ */
+ DialogAsyncRunnable( Dialog dialog, ScriptableFunction callback ) {
+ _dialog = dialog;
+ _callback = callback;
+ }
+
+ /**
+ * Run the dialog.
+ *
+ * @see java.lang.Runnable#run()
+ */
+ public void run() {
+ _dialogValue = new Integer( _dialog.doModal() );
+ // get object's string as all ecma primitives will return a valid string representation of themselves
+ Object retVal = _dialogValue.toString();
+ try {
+ _callback.invoke( null, new Object[] { retVal } );
+ } catch( Exception e ) {
+ throw new RuntimeException( "Invoke callback failed: " + e.getMessage() );
+ }
+ }
+ }
}
@@ -15,17 +15,12 @@
*/
package blackberry.ui.dialog;
-import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.script.Scriptable;
import net.rim.device.api.script.ScriptableFunction;
import net.rim.device.api.ui.UiApplication;
-import blackberry.common.util.json4j.JSONArray;
import blackberry.core.FunctionSignature;
import blackberry.core.ScriptableFunctionBase;
-import blackberry.ui.dialog.DialogRunnableFactory;
-import blackberry.ui.dialog.select.SelectDialog;
-
/**
* Implementation of asynchronous selection dialog
*
@@ -56,7 +51,7 @@ public Object execute( Object thiz, Object[] args ) throws Exception {
populateChoiceStateArrays( choices, labels, enabled, selected, types, allowMultiple );
- Runnable dr = DialogRunnableFactory.getSelectRunnable(allowMultiple, labels, enabled, selected, types, callback, thiz);
+ Runnable dr = DialogRunnableFactory.getSelectRunnable(allowMultiple, labels, enabled, selected, types, callback);
// queue
UiApplication.getUiApplication().invokeLater(dr);
@@ -0,0 +1,77 @@
+/*
+* Copyright 2010-2011 Research In Motion Limited.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package blackberry.ui.dialog;
+
+import net.rim.device.api.script.Scriptable;
+import net.rim.device.api.script.ScriptableFunction;
+import net.rim.device.api.ui.UiApplication;
+import net.rim.device.api.ui.component.Dialog;
+import blackberry.core.FunctionSignature;
+import blackberry.core.ScriptableFunctionBase;
+
+/**
+ * Implementation of asynchronous standard ask function (blackberry.ui.dialog.standardAskAsync)
+ *
+ * @author dmateescu
+ *
+ */
+public class StandardAskAsyncFunction extends ScriptableFunctionBase {
+
+ public static final String NAME = "standardAskAsync";
+
+ /**
+ * @see blackberry.core.ScriptableFunctionBase#execute(Object, Object[])
+ */
+ public Object execute( Object thiz, Object[] args ) throws Exception {
+ String message;
+ int type;
+ final boolean global = false;
+
+ // message
+ message = (String) args[ 0 ];
+ // type
+ type = ( (Integer) args[ 1 ] ).intValue();
+ if( type < Dialog.D_OK || type > Dialog.D_OK_CANCEL ) {
+ throw new IllegalArgumentException();
+ }
+ // default choice
+ final int defaultChoice = DialogNamespace.getDefaultChoice( type );
+ //callback function
+ ScriptableFunction callback = (ScriptableFunction) args[ 2 ];
+
+ Runnable dr = DialogRunnableFactory.getStandardAskRunnable(message, type, defaultChoice, global, callback);
+ // queue
+ UiApplication.getUiApplication().invokeLater(dr);
+ // return value
+ return Scriptable.UNDEFINED;
+ }
+
+ /**
+ * @see blackberry.core.ScriptableFunctionBase#getFunctionSignatures()
+ */
+ protected FunctionSignature[] getFunctionSignatures() {
+ FunctionSignature fs = new FunctionSignature( 3 );
+ // message
+ fs.addParam( String.class, true );
+ // type
+ fs.addParam( Integer.class, true );
+ // callback
+ fs.addParam( ScriptableFunction.class, true );
+ // filler
+ fs.addParam( Object.class, false );
+ return new FunctionSignature[] { fs };
+ }
+}
Oops, something went wrong.

0 comments on commit c5c4048

Please sign in to comment.