001 /* 002 Copyright (C) 2001-2003 Renaud Pawlak, Laurent Martelli 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, but 010 WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public 015 License along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 017 USA */ 018 019 package org.objectweb.jac.aspects.gui.swing; 020 021 import org.objectweb.jac.aspects.gui.FieldEditor; 022 import org.objectweb.jac.aspects.gui.GuiAC; 023 import org.objectweb.jac.core.rtti.FieldItem; 024 import java.awt.Dimension; 025 import java.lang.reflect.Constructor; 026 import javax.swing.JPasswordField; 027 import javax.swing.JTextField; 028 029 /** 030 * A Swing editor component for fields values (primitive types). 031 */ 032 033 public class PrimitiveFieldEditor extends AbstractFieldEditor 034 implements FieldEditor 035 { 036 JTextField textField; 037 038 /** 039 * Constructs a new primitive field editor. 040 */ 041 public PrimitiveFieldEditor(Object substance, FieldItem field, 042 boolean password) 043 { 044 super(substance,field); 045 init(password); 046 checkType(); 047 } 048 049 public PrimitiveFieldEditor(Object substance, FieldItem field) 050 { 051 super(substance,field); 052 init(false); 053 checkType(); 054 } 055 056 protected void checkType() { 057 if (field!=null && field.getType().isArray()) 058 throw new RuntimeException("PrimitiveFieldEditor cannot handle arrays"); 059 } 060 061 protected void init(boolean password) 062 { 063 if (password) 064 textField = new JPasswordField(); 065 else 066 textField = new org.objectweb.jac.aspects.gui.swing.JTextField(); 067 // swing "bug" workaround : prevent the textField from extending 068 // vertically. 069 Dimension minSize = textField.getPreferredSize(); 070 Dimension maxSize = textField.getMaximumSize(); 071 textField.setMaximumSize(new Dimension(maxSize.width,minSize.height)); 072 073 textField.addFocusListener(this); 074 add(textField); 075 } 076 077 // FieldEditor interface 078 079 public Object getValue() { 080 Class cl = type.getActualClass(); 081 if (cl == int.class || cl == Integer.class) { 082 return new Integer(textField.getText()); 083 } else if (cl == boolean.class || cl == Boolean.class) { 084 return( Boolean.valueOf(textField.getText())); 085 } else if (cl == long.class || cl == Long.class) { 086 return new Long(textField.getText()); 087 } else if (cl == float.class || cl == Float.class) { 088 return new Float(textField.getText()); 089 } else if (cl == double.class || cl == Double.class) { 090 return new Double(textField.getText()); 091 } else if (cl == short.class || cl == Short.class) { 092 return new Short (textField.getText()); 093 } else if (cl == byte.class || cl == Byte.class) { 094 return new Byte(textField.getText()); 095 } else if (cl == char.class || cl == Character.class) { 096 return new Character(textField.getText().charAt(0)); 097 } else if (cl == String.class) { 098 return textField.getText(); 099 } else if (!cl.isArray()) { 100 try { 101 // trying to construct the object from its textual 102 // representation (I think that any class should have 103 // a constructor taking a string... this is so helpful...) 104 // of course, this will raise an exception most of the time :-( 105 Constructor c = cl.getConstructor(new Class[] {String.class}); 106 return c.newInstance( new Object[] {textField.getText()} ); 107 } catch( Exception e ) { 108 logger.error("Failed to instantiate "+cl.getName(),e); 109 throw new RuntimeException("Unhandled type "+type.getName()); 110 } 111 } else { 112 throw new RuntimeException("Unhandled type "+type.getName()); 113 } 114 } 115 116 public void setValue(Object value) { 117 super.setValue(value); 118 if( value == null ) value=""; 119 textField.setText(GuiAC.toString(value)); 120 Dimension minSize = textField.getPreferredSize(); 121 minSize.width = 100; 122 textField.setMinimumSize(minSize); 123 } 124 125 public void setWidth(int width) { 126 textField.setColumns(width); 127 } 128 129 public void setAutoUpdate(boolean autoUpdate) { 130 // TODO 131 } 132 133 /** 134 * Set the focus on the textField 135 */ 136 public void requestFocus() { 137 textField.requestFocus(); 138 } 139 140 }