Anzeige

JavaFx TableView Dynamisches Modell


#1
hallo,

ich habe keine lust jedesmal von Hand für jede TableView ein Modell zu schreiben. Also habe ich das Automatisiert zumindest Teilweise. Das Programm schreibt einen String den ich als File speichern kann. Ist das Sinnvoll oder sollte man das ganz anderst angehen. Wenn es sinnvoll ist. Wie Integriere ich das File möglichst Automatisch in mein Eclipse Projekt?(Eclipse Java EE IDE for Web Developers Version: 2018-09 (4.9.0)).

Java:
public class TVModellClassSynthesis
{

    private String classString;
    private final Set<Field> fields;
    
    public TVModellClassSynthesis(String className, String packageName, Set<Field> fields)
    {
        
        this.fields = fields;
        //ClassHeader
        classString = "package "+ packageName+";\n" +
                "\n" +
                "import javafx.beans.property.SimpleStringProperty;\n" +
                "\n" +
                "*******************\n" +
                "**@author blazing**\n" +
                "*******************\n" +
                "\n" +
                "public class "+className+" \n" +
                "{\n\n"+
                "    private SimpleStringProperty ";
        
        int counter = 0;
        for(Field f: fields)//Global Fields
        {
            if(counter<1)classString += f.getFieldName();
            else classString += ", "+f.getFieldName();
            counter++;
        }
        
        classString += ";\n\n";
        classString += "    public "+className+"(";//Constructor Head
        
        counter = 0;
        for(Field f: fields)//Constructor parameter(Fields)
        {
            if(counter<1)classString += f.getFieldType()+" "+f.getFieldName();
            else classString += ", "+f.getFieldType()+" "+f.getFieldName();
            counter++;
        }
        
        classString += ")\n"
                + "    {\n\n";

        for(Field f: fields)//Constructor Body
        {
            if(f.getFieldType().equals(f.stringType))classString += "        this."+f.getFieldName()+" = new SimpleStringProperty("+f.getFieldName()+");\n";
            if(f.getFieldType().equals(f.integerType))classString += "        this."+f.getFieldName()+" = new SimpleStringProperty(String.valueOf("+f.getFieldName()+"));\n";       
        }
        
        classString += "    }\n\n";//Constructor End
        
        for(Field f: fields)//GetterNSetter
        {
            classString += createGetterNSetterForField(f);
        }   
        
        classString  += "}\n";//class End
        
        System.out.println("Done! classString:\n\n"+classString);
    }
    
    private String createGetterNSetterForField(Field f)
    {
        String firstLetter = f.getFieldName().substring(0, 1).toUpperCase();//This letter  to uppercase
        String restOfMethodName = firstLetter+f.getFieldName().substring(1);
        String getterMethodString = "    public String get"+restOfMethodName+"()\n";//Head of getter Method
        getterMethodString += "    {\n"+                                            //Body
                              "        return "+f.getFieldName()+".get();\n"+
                              "    }\n";

        String setterMethodString = "    public void set"+restOfMethodName+"(String s)\n";//Head of setter Method
        setterMethodString += "    {\n"+                                                  //Body
                              "         "+f.getFieldName()+".set(s)\n"+
                              "    }\n\n";
        
        return getterMethodString+"\n"+setterMethodString;
    }
    
    public String getClassString()
    {
        return classString;
    }
}

public class Field
{
    
    private final String fieldName;
    
    private static final List<String> keyWords =Arrays.asList("abstract",
    "continue", "for", "new", "switch", "assert", "default", "goto", "package",
    "synchronized", "boolean", "do", "if", "private", "this", "break", "double",
    "implements", "protected", "throw", "byte", "else", "import", "public", 
    "throws", "case", "enum", "instanceof", "return", "transient", "catch",
    "extends", "int", "short", "try", "char", "final", "interface", "static",
    "void", "class", "finally", "long", "strictfp", "volatile", "const", 
    "float", "native", "super", "while");

    private final String fieldType;
    public static final String stringType = "String";
    public static final String integerType = "int";
    public static final List<String> fieldTypeList = Arrays.asList(stringType
            , integerType);
    
    
    public Field(String fieldName, String fieldType)
    {
        
        if(fieldTypeList.contains(fieldType))this.fieldType = fieldType;
        else throw new IllegalArgumentException("unknown type: " + fieldType);
        
        if(keyWords.contains(fieldName))throw new IllegalArgumentException("cant use keyWord for Field name: "+fieldName);
        else this.fieldName =fieldName;
    }
    
    public String getFieldName()
    {
        return fieldName;
    }
    
    public String getFieldType()
    {
        return fieldType;
    }
 

ComFreek

Mod | @comfreek
Moderator
#2
Eclipse (wie viele IDEs) hat doch sowas wie Templates und Dialoge für Klassenerstellung, wo du deine Felder auch angeben kannst. Eventuell könntest du es dort als Eclipse Plugin integrieren und dann auch weltweit teilen? :)
 
Anzeige
Anzeige