/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
 *
 * @author Csubák Péter
 */
public class WindowFieldSetter {

    public static <E> void setFields(JFrame win, E obj){
        for (Method m : win.getClass().getMethods()){
            if (m.getName().startsWith("get")){
                Object field = null;
                try {
                    field = m.invoke(win);
                } catch (IllegalAccessException ex) {
                  //  Logger.getLogger(WindowFieldSetter.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalArgumentException ex) {
                   // Logger.getLogger(WindowFieldSetter.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InvocationTargetException ex) {
                  //  Logger.getLogger(WindowFieldSetter.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (field instanceof JTextField){
                    JTextField tf = (JTextField) field;
                    try {
                        tf.setText( (AnnotationExtractor.getFieldByGuiName(tf.getName(), obj).toString()));
                    } catch (Exception ex) {

                       // Logger.getLogger(WindowFieldSetter.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                if (field instanceof JComboBox){
                    JComboBox jc = (JComboBox) field;
                    try {
                        jc.setSelectedItem(AnnotationExtractor.getFieldByGuiName(jc.getName(), obj));
                    } catch (Exception ex) {
                        Logger.getLogger(WindowFieldSetter.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                if (field instanceof JCheckBox){
                    JCheckBox jck = (JCheckBox) field;
                    try {
                        jck.setSelected(((Boolean) AnnotationExtractor.getFieldByGuiName(jck.getName(), obj)).booleanValue());
                    } catch (Exception ex) {
                        Logger.getLogger(WindowFieldSetter.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

}
