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