1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.Dimension ; 22 import java.lang.reflect.Constructor ; 23 import javax.swing.JPasswordField ; 24 import org.objectweb.jac.aspects.gui.FieldEditor; 25 import org.objectweb.jac.aspects.gui.GuiAC; 26 import org.objectweb.jac.aspects.gui.Length; 27 import org.objectweb.jac.core.rtti.FieldItem; 28 29 32 33 public class PrimitiveFieldEditor extends TextFieldEditor 34 implements FieldEditor 35 { 36 39 public PrimitiveFieldEditor(Object substance, FieldItem field, 40 boolean password) 41 { 42 super(substance,field); 43 init(password); 44 checkType(); 45 } 46 47 public PrimitiveFieldEditor(Object substance, FieldItem field) 48 { 49 super(substance,field); 50 init(false); 51 checkType(); 52 } 53 54 protected void checkType() { 55 if (field!=null && field.getType().isArray()) 56 throw new RuntimeException ("PrimitiveFieldEditor cannot handle arrays"); 57 } 58 59 protected void init(boolean password) 60 { 61 if (password) 62 textField = new JPasswordField (); 63 else 64 textField = new JTextField(); 65 setSize(new Length("15ex"),null); 66 textField.addFocusListener(this); 67 add(textField); 68 } 69 70 72 public Object getValue() { 73 Class cl = type.getActualClass(); 74 if (cl == int.class || cl == Integer .class) { 75 return new Integer (textField.getText()); 76 } else if (cl == boolean.class || cl == Boolean .class) { 77 return( Boolean.valueOf(textField.getText())); 78 } else if (cl == long.class || cl == Long .class) { 79 return new Long (textField.getText()); 80 } else if (cl == float.class || cl == Float .class) { 81 return new Float (textField.getText()); 82 } else if (cl == double.class || cl == Double .class) { 83 return new Double (textField.getText()); 84 } else if (cl == short.class || cl == Short .class) { 85 return new Short (textField.getText()); 86 } else if (cl == byte.class || cl == Byte .class) { 87 return new Byte (textField.getText()); 88 } else if (cl == char.class || cl == Character .class) { 89 return new Character (textField.getText().charAt(0)); 90 } else if (cl == String .class) { 91 return textField.getText(); 92 } else if (!cl.isArray()) { 93 try { 94 Constructor c = cl.getConstructor(new Class [] {String .class}); 99 return c.newInstance( new Object [] {textField.getText()} ); 100 } catch( Exception e ) { 101 logger.error("Failed to instantiate "+cl.getName(),e); 102 throw new RuntimeException ("Unhandled type "+type.getName()); 103 } 104 } else { 105 throw new RuntimeException ("Unhandled type "+type.getName()); 106 } 107 } 108 109 public void setValue(Object value) { 110 super.setValue(value); 111 if( value == null ) value=""; 112 textField.setText(GuiAC.toString(value)); 113 Dimension minSize = textField.getPreferredSize(); 114 minSize.width = 100; 115 textField.setMinimumSize(minSize); 116 } 117 118 public void setAutoUpdate(boolean autoUpdate) { 119 } 121 } 122 | Popular Tags |