// JTEM - Java Tools for Experimental Mathematics
// GPL Copyright (C) 2002 JTEM-Group
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

String beanInfoMaker(){ return beanInfoMaker(self);}


String beanInfoMaker(String fullClassName){
     import java.beans.BeanInfo;
     import java.beans.MethodDescriptor;
     import java.beans.PropertyDescriptor;
     import java.beans.Introspector;
  
     final int lastP =  fullClassName.lastIndexOf('.');
     final String packageName = fullClassName.substring(0,lastP);    
     final String myClassName = fullClassName.substring
	 (lastP+((lastP==0)?0:1));
    
//   final	String  myclassName = myClassName.substring(0,1).toLowerCase() 
// 	+  myClassName.substring(1);

     final     BeanInfo bI= Introspector.getBeanInfo
	(Class.forName(fullClassName));

//   final MethodDescriptor[] mDA = bI.getMethodDescriptors();	    
     final PropertyDescriptor[] pDA = bI.getPropertyDescriptors();	
     final StringBuffer pDS = new StringBuffer(500*pDA.length);

     for (int i=0; i<pDA.length; i++) {
	final PropertyDescriptor pDAi = pDA[i];
	final String mGetName = pDAi.getReadMethod().getName();
// 	final java.lang.reflect.Method mSetMethod = pDAig.getWriteMethod();
// 	final String mSetName = (mSetMethod==null)?"":mSetMethod.getName();
	final String pName = mGetName.substring(3);
	final String pname = pName.substring(0,1).toLowerCase() 
	    +  pName.substring(1);
// 	final String pTypeUnreadable = pDAi.getPropertyType().getName();

	pDS.append
("\n"+
 "      pD=new PropertyDescriptor(\""+pname+"\",myClass);\n"+
 "      pD.setDisplayName(\""+pName+"\");\n"+
 "      pD.setPropertyEditorClass("+pName+"Editor.class);\n"+
 "      pD.setBound(false);\n"+
 "      pD.setConstrained(false);\n"+
 "      pD.setHidden(false);\n"+
 "      pD.setPreferred(true);\n"+
 "      pD.setShortDescription(\""+pName+"\");\n"+
 "      pD.setExpert(false);\n"+
 "      properties[i++]=pD;\n");
    }

     return (// Sorry not to respect usual identation here.
 "// Brought to you by JTEM - JavaTtools for Experimental Mathematics\n"+
 "// http://www.jtem.de\n"+
 "// BeanInfo Maker GPL Copyright (C) 2002 JTEM-Group\n"+
 "//\n"+
 "// This program is free software; you can redistribute it and/or modify\n"+
 "// it under the terms of the GNU General Public License as published by\n"+
 "// the Free Software Foundation; either version 2 of the License, or\n"+
 "// any later version.\n"+
 "//\n"+
 "// This program is distributed in the hope that it will be useful,\n"+
 "// but WITHOUT ANY WARRANTY; without even the implied warranty of\n"+
 "// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"+
 "// GNU General Public License for more details.\n"+
 "//\n"+
 "// You should have received a copy of the GNU General Public License\n"+
 "// along with this program; if not, write to the Free Software\n"+
 "// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n"+
 "\n"+
 ((packageName=="")?"": // Unknown package.
 "package "+packageName+";\n")+
 "\n"+
 "\n"+
 "import  java.beans.*;\n"+
 "\n"+
 "/**\n"+
 " * Bean support class for {@link "+myClassName+"}\n"+
 " * components.\n"+
 " * @see BeanInfo\n"+
 " */\n"+
 "public class "+myClassName+"BeanInfo implements BeanInfo\n"+
 "{\n"+
 "  private static final BeanDescriptor BD\n"+
 "                       =new BeanDescriptor("+myClassName+".class);\n"+
 "  private static final PropertyDescriptor[] properties\n"+
 "                       =new PropertyDescriptor["+pDA.length+"];\n"+
 "  private static IntrospectionException initialError=null;\n"+
 "  static\n"+
 "  {\n"+
 "    try\n"+
 "    {\n"+
 "      Class myClass="+myClassName+".class;\n"+
 "      BD.setDisplayName(\""+myClassName+" nice name\");\n"+
 "      PropertyDescriptor pD;\n"+
 "      int i=0;\n"+
 pDS+
 "    }\n"+
 "    catch(IntrospectionException ex)\n"+
 "    {\n"+
 "      initialError=ex;\n"+
 "      ex.printStackTrace();\n"+
 "    }\n"+
 "  }\n"+
 "  public "+myClassName+"BeanInfo() throws IntrospectionException\n"+
 "  {\n"+
 "    if(initialError!=null) throw initialError;\n"+
 "  }\n"+
 "  public BeanDescriptor getBeanDescriptor()\n"+
 "  {\n"+
 "    return BD;\n"+
 "  }\n"+
 "  public EventSetDescriptor[] getEventSetDescriptors()\n"+
 "  {\n"+
 "    return null;\n"+
 "  }\n"+
 "  public int getDefaultEventIndex()\n"+
 "  {\n"+
 "    return -1;\n"+
 "  }\n"+
 "  public PropertyDescriptor[] getPropertyDescriptors()\n"+
 "  {\n"+
 "//  System.out.println(\"providing good PropertyDescriptors\");\n"+
 "    return properties;\n"+
 "  }\n"+
 "  public int getDefaultPropertyIndex()\n"+
 "  {\n"+
 "    return -1;\n"+
 "  }\n"+
 "  public MethodDescriptor[] getMethodDescriptors()\n"+
 "  {\n"+
 "    return null;\n"+
 "  }\n"+
 "  public BeanInfo[] getAdditionalBeanInfo()\n"+
 "  {\n"+
 "    return null;\n"+
 "  }\n"+
 "  public java.awt.Image getIcon(int iconKind)\n"+
 "  {\n"+
 "    return null;\n"+
 "  }\n"+
 "}\n"
);


}

String beanInfoMaker(Object o){

    final	    String fullClassName;
    if (o.getClass()==Class) {
	fullClassName = o.getName();	
    } else {
	fullClassName = o.getClass().getName();
    }
    return beanInfoMaker(fullClassName);
}

void beanInfoMaker(Object o, String fileName) // throws IOException 
{
    
    File outputFile = new File(fileName);

    FileWriter out = new FileWriter(outputFile);
    out.write( beanInfoMaker(o));
 
    if (!fileName.endsWith("BeanInfo.java")) {
	System.out.println("Name it \"MyClassBeanInfo.java\"");
    }

    out.close();
}

