Re: [Xswt-developer] Javascript support
Brought to you by:
dvorme
|
From: David J. O. <dj...@co...> - 2007-04-18 15:32:37
|
Excellent work, Hallvard!=20 Dave=20 ----- Original Message -----=20 From: Hallvard Tr=C3=A6tteberg <ha...@id...>=20 To: xsw...@li...=20 Sent: Wednesday, April 18, 2007 3:27:42 AM GMT-0800=20 Subject: [Xswt-developer] Javascript support=20 Hi,=20 As some of you may recall, I have been working on scripting support i.e.=20 <script> tags, e.g. to be able to handle events. The first language I=20 managed to get working was pnuts, which was the one I knew best=20 (technically). However, I've now been able to get GNUs JavaScript engine=20 Rhino working, too. Since JavaScript is something a lot of people already= =20 know and Rhino I believe is bundled with with Java 6 (?), this should be=20 good for XSWT. Below is a calculator example; the script tags implement the= =20 calculator logic and the selectionListener attributes calls the functions= =20 defined in the script tags. Note how all the widgets (with ids) are=20 accessible to the script. There's still some small things to do before=20 checking it in, but I thought I'd mention it.=20 Hallvard=20 <?xml version=3D"1.0" encoding=3D"UTF-8"?>=20 <xswt xmlns:x=3D"http://sweet_swt.sf.net/xswt">=20 <x:import>=20 <package name=3D"java.lang"/>=20 <package name=3D"org.eclipse.swt.widgets"/>=20 <package name=3D"com.swtworkbench.community.xswt.scripting"/>=20 <package name=3D"org.eclipse.swt.layout"/>=20 </x:import>=20 <composite>=20 <layout x:class=3D"gridLayout" numColumns=3D"5"/>=20 <script x:id=3D"math" lang=3D"js">=20 function plus (n1, n2) { return (+n1) + (+n2);}=20 function minus(n1, n2) { return n1 - n2;}=20 function mult (n1, n2) { return n1 * n2;}=20 function div (n1, n2) { return n1 / n2;}=20 </script>=20 <text x:id=3D"result">=20 <layoutData x:class=3D"gridData" horizontalSpan=3D"5"=20 horizontalAlignment=3D"FILL" grabExcessHorizontalSpace=3D"true"/>=20 </text>=20 <result text=3D" 0"/>=20 <script x:id=3D"calc" lang=3D"js">=20 var n1 =3D 0;=20 var n2 =3D null;=20 var mem =3D 0;=20 var op =3D null;=20 function setOp(sop) {=20 setN1(getN(n1));=20 if (sop =3D=3D "-") {=20 op =3D math.minus;=20 } else if (sop =3D=3D "+") {=20 op =3D math.plus;=20 } else if (sop =3D=3D "*") {=20 op =3D math.mult;=20 } else if (sop =3D=3D "/") {=20 op =3D math.div;=20 } else {=20 op =3D sop;=20 }=20 n2 =3D "";=20 }=20 function getN(n) {=20 return (+n);=20 }=20 function updateDigits() {=20 var digits =3D (typeof(n2) =3D=3D "string") ? n2 : n1;=20 result.text =3D digits;=20 }=20 function appendDigit(d) {=20 if (typeof(n2) =3D=3D "string") {=20 n2 =3D n2 + d;=20 } else if (typeof(n1) =3D=3D "string") {=20 n1 =3D n1 + d;=20 } else {=20 n1 =3D d;=20 }=20 updateDigits();=20 }=20 function setN1(n) {=20 n1 =3D n;=20 n2 =3D null;=20 op =3D null;=20 return n;=20 }=20 function setN2(n) {=20 n2 =3D n;=20 return n;=20 }=20 function calculate() {=20 if (op !=3D null) {=20 setN2(getN(n2));=20 setN1(op(n1,n2));=20 }=20 updateDigits();=20 }=20 function setMem(n) {=20 mem =3D n;=20 }=20 </script>=20 <button text=3D"MC" selectionListener=3D"handlers.memClear"/>=20 <button text=3D"7" selectionListener=3D"handlers.digit"/>=20 <button text=3D"8" selectionListener=3D"handlers.digit"/>=20 <button text=3D"9" selectionListener=3D"handlers.digit"/>=20 <button text=3D"/" selectionListener=3D"handlers.op"/>=20 <button text=3D"MR" selectionListener=3D"handlers.memRecall"/>=20 <button text=3D"4" selectionListener=3D"handlers.digit"/>=20 <button text=3D"5" selectionListener=3D"handlers.digit"/>=20 <button text=3D"6" selectionListener=3D"handlers.digit"/>=20 <button text=3D"*" selectionListener=3D"handlers.op"/>=20 <button text=3D"M+" selectionListener=3D"handlers.memPlus"/>=20 <button text=3D"1" selectionListener=3D"handlers.digit"/>=20 <button text=3D"2" selectionListener=3D"handlers.digit"/>=20 <button text=3D"3" selectionListener=3D"handlers.digit"/>=20 <button text=3D"-" selectionListener=3D"handlers.op"/>=20 <button text=3D"C" selectionListener=3D"handlers.clear"/>=20 <button text=3D"0" selectionListener=3D"handlers.digit"/>=20 <button text=3D","/>=20 <button text=3D"=3D" selectionListener=3D"handlers.calculate"/>=20 <button text=3D"+" selectionListener=3D"handlers.op"/>=20 <script x:id=3D"handlers" lang=3D"js">=20 function digit(event) {=20 calc.appendDigit("" + event.widget.text); // force=20 conversion to JavaScript string=20 }=20 function op(event) {=20 calc.setOp("" + event.widget.text); // force=20 conversion to JavaScript string=20 }=20 function clear(event) {=20 calc.setN1(0);=20 calc.updateDigits();=20 }=20 function memClear(event) {=20 calc.setMem(0);=20 }=20 function memRecall(event) {=20 calc.setN1(mem);=20 }=20 function memPlus(event) {=20 calc.setMem(mem + calc.setN1());=20 }=20 function calculate(event) {=20 calc.calculate();=20 }=20 </script>=20 </composite>=20 </xswt>=20 ---=20 Hallvard Tr=C3=A6tteberg (ha...@id..., http://www.idi.ntnu.no/~hal)=20 Associate Professor, IS group, Dept. of Computer and Information Sciences a= t=20 the Norwegian Univ. of Science and Technology=20 -------------------------------------------------------------------------= =20 This SF.net email is sponsored by DB2 Express=20 Download DB2 Express C - the FREE version of DB2 express and take=20 control of your XML. No limits. Just data. Click to get it now.=20 http://sourceforge.net/powerbar/db2/=20 _______________________________________________=20 Xswt-developer mailing list=20 Xsw...@li...=20 https://lists.sourceforge.net/lists/listinfo/xswt-developer=20 |