/*
 * Created on Aug 9, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package zemplen.ui;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import static hibergui.HiberGUI.*;

import hibergui.Form;
import hibergui.assoc.ListEdit_List;
import hibergui.assoc.SetEdit_List;
import hibergui.hibernate.HibernateForm;
import hibergui.property.IntegerEdit;
import hibergui.property.StringEdit;
import hibergui.property.TimeEdit1;

import zemplen.model.Allomas;
import zemplen.model.Kategoria;
import zemplen.model.Tav;
import zemplen.model.Verseny;

/**
 * @author espakm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TavForm extends HibernateForm<Tav> {
    
    VersenyCombo eVerseny;
    StringEdit eElnevezes;
    ListEdit_List<Allomas> eAllomasok;
    SetEdit_List<Kategoria> eKategoriak;
    IntegerEdit eHossz;
    TimeEdit1 eSzintido;
    TimeEdit1 eRajtido;
    
    public TavForm(Composite cptParent, int mode) {
        super(cptParent, mode);
        
        setWindowTitle("Távok");
    }
    
    @Override
    protected void save(Tav tav) {
        super.save(tav);
        Verseny v = eVerseny.get();
        Set<Tav> tavok = v.getTavok();
        if (tavok == null)
        	v.setTavok(tavok = new HashSet<Tav>());
        tavok.add(tav);
    }
    
    @Override
    protected void delete(Tav tav) {
        super.delete(tav);
        eVerseny.get().getTavok().remove(tav);
    }
    
    @Override
    protected Composite createFields(Composite cptParent) {
        Composite cpt = new Composite(cptParent, SWT.BORDER);
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        cpt.setLayout(gridLayout);
        
        GridData data;
        
        new Label(cpt, SWT.NONE).setText("Verseny:");
        eVerseny = new VersenyCombo(this, cpt, NOT_NULL);
        data = new GridData(GridData.FILL_HORIZONTAL);
//        data.horizontalSpan = 2;
        eVerseny.setLayoutData(data);
        
        new Label(cpt, SWT.NONE).setText("Elnevezés:");
        eElnevezes = new StringEdit(this, cpt, NOT_NULL, "elnevezes", "elnevezés");
        data = new GridData(GridData.FILL_HORIZONTAL);
        eElnevezes.setLayoutData(data);
        
        Label lblAllomasok = new Label(cpt, SWT.NONE);
        lblAllomasok.setText("Állomások:");
        data = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
        lblAllomasok.setLayoutData(data);
        
        eAllomasok = new ListEdit_List<Allomas>(this, cpt, NONE, "allomasok", "állomások", "elnevezes") {
            @Override
            protected Form<Allomas> createForm(Composite cptParent, final int formStyle) {
            	AllomasForm f = new AllomasForm(cptParent, formStyle);
            	f.eVerseny.set(eVerseny.get());
				f.eVerseny.fix(true);
            	return f;
            }
        };
        
        data = new GridData(GridData.FILL_HORIZONTAL);
        eAllomasok.setLayoutData(data);
        
        Label lblKategoriak= new Label(cpt, SWT.NONE);
        lblKategoriak.setText("Kategóriák:");
        data = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
        lblKategoriak.setLayoutData(data);
        
        eKategoriak = new SetEdit_List<Kategoria>(this, cpt, NONE, "kategoriak", "kategóriák", "elnevezes") {                   
            protected Form<Kategoria> createForm(Composite cptParent, final int formStyle) {
            	KategoriaForm f = new KategoriaForm(cptParent, formStyle);
            	f.eVerseny.set(eVerseny.get());
				f.eVerseny.fix(true);
            	return f;
            }
        };
        data = new GridData(GridData.FILL_HORIZONTAL);
        eKategoriak.setLayoutData(data);
        
        new Label(cpt, SWT.NONE).setText("Hossz:");
        eHossz = new IntegerEdit(this, cpt, NONE, "hossz", "hossz");
        data = new GridData(GridData.FILL_HORIZONTAL);
        eHossz.setLayoutData(data);

        new Label(cpt, SWT.NONE).setText("Szintidő:");
        eSzintido = new TimeEdit1(this, cpt, NONE, "szintido", "szintidő");
        data = new GridData(GridData.FILL_HORIZONTAL);
        eSzintido.setLayoutData(data);

        new Label(cpt, SWT.NONE).setText("Rajtidő:");
        eRajtido = new TimeEdit1(this, cpt, NONE, "rajtido", "rajtidő");
        data = new GridData(GridData.FILL_HORIZONTAL);
        eRajtido.setLayoutData(data);

        return cpt;
    }
    
    @Override
    protected void createColumnsForTable() {
        addColumn(SWT.LEFT, "elnevezes", "Elnevezés", 200);
        addColumn(SWT.LEFT, "hossz", "Hossz", 100);
    }
}
