1 4 package dwr; 5 6 import org.wings.session.Session; 7 import org.wings.session.SessionManager; 8 import org.wings.externalizer.ExternalizedResource; 9 import org.wings.event.SRequestEvent; 10 import org.wings.event.SRequestListener; 11 import org.wings.*; 12 import org.wings.resource.DefaultURLResource; 13 import org.wings.header.Script; 14 import org.wings.script.JavaScriptListener; 15 import org.wings.text.SAbstractFormatter; 16 17 import javax.servlet.http.HttpServletResponse ; 18 import javax.servlet.http.HttpServletRequest ; 19 import java.text.ParseException ; 20 import java.text.DateFormat ; 21 import java.text.NumberFormat ; 22 import java.io.IOException ; 23 24 28 public class DWRExample 29 { 30 private SFormattedTextField dateTextField; 31 private SFormattedTextField numberTextField; 32 private SButton button; 33 private SFrame frame; 34 35 public DWRExample() throws IOException { 36 dateTextField = new SFormattedTextField(new DateFormatter()); 37 dateTextField.setName("dateTextField"); 38 39 numberTextField = new SFormattedTextField(new NumberFormatter()); 40 numberTextField.setName("numberTextField"); 41 42 button = new SButton("submit"); 43 44 SForm form = new SForm(); 45 form.setLayout(new STemplateLayout(SessionManager.getSession().getServletContext().getRealPath("/template/content.thtml"))); 46 form.add(dateTextField, "dateTextField"); 47 form.add(numberTextField, "numberTextField"); 48 form.add(button, "submit"); 49 50 frame = new SFrame("DWRExample example"); 51 frame.getContentPane().add(form); 52 frame.show(); 53 frame.addHeader(new Script("text/javascript", new DefaultURLResource("../dwr/engine.js"))); 54 } 55 56 public static class DateFormatter extends DWRFormatter { 57 DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, SessionManager.getSession().getLocale()); 58 59 public Object stringToValue(String text) throws ParseException { 60 System.out.println("text = '" + text + "'"); 61 if (text == null || text.trim().length() == 0) 62 return null; 63 else 64 return format.parse(text.trim()); 65 } 66 67 public String valueToString(Object value) throws ParseException { 68 if (value == null) 69 return ""; 70 else 71 return format.format(value); 72 } 73 74 public String validate(String text) { 75 try { 76 String s = valueToString(stringToValue(text)); 77 System.out.println("return = '" + s + "'"); 78 return s; 79 } 80 catch (ParseException e) { 81 return null; 82 } 83 } 84 } 85 86 public static class NumberFormatter extends DWRFormatter { 87 NumberFormat format = NumberFormat.getNumberInstance(SessionManager.getSession().getLocale()); 88 89 public Object stringToValue(String text) throws ParseException { 90 if (text == null || text.trim().length() == 0) 91 return null; 92 else 93 return format.parse(text.trim()); 94 } 95 96 public String valueToString(Object value) throws ParseException { 97 if (value == null) 98 return ""; 99 else 100 return format.format(value); 101 } 102 103 public String validate(String text) { 104 try { 105 return valueToString(stringToValue(text)); 106 } 107 catch (ParseException e) { 108 return null; 109 } 110 } 111 } 112 } 113 | Popular Tags |