1 33 package net.sf.jga.swing; 34 35 import java.awt.Color ; 36 import java.text.Format ; 37 import java.text.NumberFormat ; 38 import javax.swing.DefaultCellEditor ; 39 import javax.swing.JComponent ; 40 import javax.swing.JLabel ; 41 import javax.swing.JTextField ; 42 import javax.swing.border.LineBorder ; 43 import net.sf.jga.fn.UnaryFunctor; 44 import net.sf.jga.fn.arithmetic.Arithmetic; 45 import net.sf.jga.fn.arithmetic.ArithmeticFactory; 46 import net.sf.jga.fn.property.ConstructUnary; 47 import net.sf.jga.fn.string.DefaultFormat; 48 import net.sf.jga.fn.string.FormatValue; 49 import net.sf.jga.fn.string.ParseFormat; 50 import net.sf.jga.fn.EvaluationException; 51 52 61 62 public class GenericCellEditor<T> extends DefaultCellEditor { 63 64 static final long serialVersionUID = -6265710748312156901L; 65 66 protected JTextField _component; 68 69 private T _defaultValue; 71 72 73 78 public GenericCellEditor (Class <T> type) { 79 this(new DefaultFormat<T>(), new ConstructUnary<String ,T>(String .class, type)); 80 } 81 82 85 public GenericCellEditor (UnaryFunctor<T,String > formatter, UnaryFunctor<String ,T> parser) { 86 this(formatter, parser, null); 87 } 88 89 93 public GenericCellEditor (UnaryFunctor<T,String > formatter, UnaryFunctor<String ,T> parser, 94 T defaultValue) 95 { 96 super(new JTextField ()); 97 if (formatter == null) { 98 String msg = "Formatter (functor) is required"; 99 throw new IllegalArgumentException (msg); 100 } 101 102 if (parser == null) { 103 String msg = "Parser (functor) is required"; 104 throw new IllegalArgumentException (msg); 105 } 106 107 _component = (JTextField ) editorComponent; 108 _defaultValue = defaultValue; 109 110 delegate = new FormattedDelegate(formatter, parser); 112 } 113 114 118 119 public boolean stopCellEditing() { 120 try { 121 T value = (T) getCellEditorValue(); 125 } 126 catch (Exception e) { 127 _component.setBorder(new LineBorder (Color.red)); 128 return false; 129 } 130 131 return super.stopCellEditing(); 132 } 133 134 137 public int getHorizontalAlignment() { 138 return _component.getHorizontalAlignment(); 139 } 140 141 144 public void setHorizontalAlignment(int alignment) { 145 _component.setHorizontalAlignment(alignment); 146 } 147 148 public String toString() { 149 return "GenericTableCellEditor["+delegate+"]"; 150 } 151 152 157 protected class FormattedDelegate extends EditorDelegate { 158 159 static final long serialVersionUID = -3670848351305705031L; 160 161 private UnaryFunctor<T,String > _formatter; 162 private UnaryFunctor<String ,T> _parser; 163 164 public FormattedDelegate(UnaryFunctor<T,String > formatter, 165 UnaryFunctor<String ,T> parser) 166 { 167 _formatter = formatter; 168 _parser = parser; 169 } 170 171 176 public void setValue(Object value) { 177 if (value != null) { 178 try { 181 _component.setText(_formatter.fn((T)value)); 182 return; 183 } 184 catch (ClassCastException x) { 185 } 188 } 189 190 if (_defaultValue != null) { 191 _component.setText(_formatter.fn(_defaultValue)); 192 return; 193 } 194 195 _component.setText(""); 196 } 197 198 203 public Object getCellEditorValue() { 204 String txt = _component.getText(); 205 if (txt == null || txt.trim().length() == 0) { 206 return _defaultValue; 207 } 208 209 try { 210 Object val = _parser.fn(txt); 211 return val; 212 } 213 catch (EvaluationException x) { 214 return _defaultValue; 215 } 216 } 217 218 public String toString() { return "FormattedDelegate["+_formatter+","+_parser+"]"; } 219 } 220 } 221 | Popular Tags |