1 14 package wingset; 15 16 import org.wings.*; 17 import org.wings.style.CSSProperty; 18 import org.wings.script.JavaScriptEvent; 19 import org.wings.script.JavaScriptListener; 20 21 import java.awt.*; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.text.DecimalFormat ; 25 import java.text.DecimalFormatSymbols ; 26 import java.text.NumberFormat ; 27 import java.util.Locale ; 28 29 37 public class JavaScriptListenerExample 38 extends WingSetPane { 39 44 private final static String JS_ADD_SCRIPT = 45 "document.getElementById('{2}').getElementsByTagName('input')[0].value" + 46 " = ((1.0 * document.getElementById('{0}').getElementsByTagName('input')[0].value)" + 47 " + (1.0 * document.getElementById('{1}').getElementsByTagName('input')[0].value));"; 48 49 private final static DecimalFormatSymbols DSYM 50 = new DecimalFormatSymbols (Locale.US); 52 public SComponent createExample() { 53 SPanel p = new SPanel(); 54 p.add(new SLabel("The client side can handle simple events by JavaScript listeners. In this example, numbers are added locally.")); 55 56 final STextField firstField = createNumberField(); 57 final STextField secondField = createNumberField(); 58 final STextField sumField = createNumberField(); 59 SButton serverCalcButton = new SButton("sum"); 60 61 firstField.setFocusTraversalIndex(1); 62 secondField.setFocusTraversalIndex(2); 63 64 SGridLayout gridLayout = new SGridLayout(2); 65 gridLayout.setHgap(4); 66 gridLayout.setVgap(4); 67 68 SForm form = new SForm(gridLayout); 69 form.add(new SLabel("Value #1")); 70 form.add(firstField); 71 form.add(new SLabel("Value #2")); 72 form.add(secondField); 73 form.add(new SLabel("Sum")); 74 form.add(sumField); 75 form.add(new SLabel("Server calculation")); 76 form.add(serverCalcButton); 77 78 81 serverCalcButton.addActionListener(new ActionListener () { 82 public void actionPerformed(ActionEvent ev) { 83 doCalculation(firstField, secondField, sumField); 84 } 85 }); 86 87 91 SComponent[] jsParams = new SComponent[]{firstField, secondField, 92 sumField}; 93 JavaScriptListener jsListener; 94 jsListener = new JavaScriptListener(JavaScriptEvent.ON_CHANGE, 95 JS_ADD_SCRIPT, 96 jsParams); 97 98 firstField.addScriptListener(jsListener); 99 secondField.addScriptListener(jsListener); 100 101 sumField.addScriptListener(jsListener); 103 104 p.add(form); 105 return p; 106 } 107 108 111 private void doCalculation(STextField a, STextField b, STextField sum) { 112 double aNum = parseNumber(a); 113 double bNum = parseNumber(b); 114 if (Double.isNaN(aNum) || Double.isNaN(bNum)) { 115 sum.setBackground(Color.RED); 116 sum.setText("?"); 117 } else { 118 sum.setBackground(null); 119 123 int decimalsNeeded = Math.max(fractionDecimals(aNum), 124 fractionDecimals(bNum)); 125 NumberFormat fmt; 126 fmt = new DecimalFormat ("#.#", DSYM); 127 fmt.setMinimumFractionDigits(decimalsNeeded); 128 129 a.setText(fmt.format(aNum)); 130 b.setText(fmt.format(bNum)); 131 sum.setText(fmt.format(aNum + bNum)); 132 } 133 } 134 135 139 private int fractionDecimals(double number) { 140 NumberFormat fmt = new DecimalFormat ("#.########"); 142 String fractionStr = fmt.format(Math.IEEEremainder(number, 1.0)); 143 return fractionStr.length() - 2; 144 } 145 146 150 private double parseNumber(STextField field) { 151 String text = field.getText().trim(); 152 if (text.length() == 0) { 153 text = "0"; 154 } 155 double result = Double.NaN; 156 try { 157 result = Double.parseDouble(text); 158 field.setBackground(null); 159 } catch (Exception e) { 160 field.setBackground(Color.RED); 161 } 162 return result; 163 } 164 165 private STextField createNumberField() { 166 STextField field = new STextField(); 167 field.setAttribute(CSSProperty.TEXT_ALIGN, "right"); 168 return field; 169 } 170 } 171 172 173 | Popular Tags |