1 6 7 20 package org.wings; 21 22 import org.wings.text.SAbstractFormatter; 23 import org.wings.text.SDefaultFormatter; 24 import org.wings.script.JavaScriptListener; 25 26 import java.awt.event.ActionListener ; 27 import java.text.ParseException ; 28 29 30 33 public class SFormattedTextField 34 extends STextField 35 { 36 private SAbstractFormatter formatter = null; 37 private static final SAbstractFormatter NO_FORMATTER = new SAbstractFormatter() { 38 public JavaScriptListener generateJavaScript(SFormattedTextField field, boolean actionListener) { 39 return null; 40 } 41 42 public Object stringToValue(String text) throws ParseException { 43 return null; 44 } 45 46 public String valueToString(Object value) throws ParseException { 47 return null; 48 } 49 }; 50 51 public SFormattedTextField() { 52 this(NO_FORMATTER); 53 } 54 55 public SFormattedTextField(SAbstractFormatter formatter) { 56 this.formatter = formatter; 57 } 58 59 public void setValue(Object object) { 60 String string = null; 61 if (formatter != null) 62 try { 63 string = this.formatter.valueToString(object); 64 } 65 catch (ParseException e) { 66 e.printStackTrace(); 67 } 68 super.setText(string); 69 } 70 71 public Object getValue() { 72 Object returnValue = null; 73 try { 74 returnValue = this.formatter.stringToValue(this.getText()); 75 } 76 catch (ParseException e) { 77 e.printStackTrace(); 78 } 79 return returnValue; 80 } 81 82 public SFormattedTextField(Object value) { 84 setValue(value); 85 } 86 87 public SAbstractFormatter getFormatter() { 88 return formatter; 89 } 90 91 public void setFormatter(SAbstractFormatter formatter) { 92 if (this.formatter != null) { 93 this.formatter.uninstall(this); 94 } 95 this.formatter = formatter; 96 formatter.install(this); 97 } 98 99 102 public void addActionListener(ActionListener listener) { 103 super.addActionListener(listener); 104 if (formatter == null) { 105 formatter = new SDefaultFormatter(); 106 formatter.install(this); 107 } else { 108 formatter.updateFormatter(); 109 } 110 } 111 112 protected void setParentFrame(SFrame parentFrame) { 113 if (this.parentFrame != null) 114 formatter.uninstall(this); 115 super.setParentFrame(parentFrame); 116 if (this.parentFrame != null) 117 formatter.install(this); 118 } 119 } | Popular Tags |