/*
 * Created on Aug 16, 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.O2MEdit_Button;
import hibergui.hibernate.HibernateForm;
import hibergui.property.StringEdit;

import zemplen.model.Allomas;
import zemplen.model.Reszido;
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 AllomasForm extends HibernateForm<Allomas> {
    
    VersenyCombo eVerseny;
    StringEdit eElnevezes;
    O2MEdit_Button<Reszido> eReszidok;
    
    public AllomasForm(Composite cptParent, int mode) {
        super(cptParent, mode);
        
        setWindowTitle("Állomások");
    }
    
    @Override
    protected void save(Allomas allomas) {
        super.save(allomas);
        Verseny v = eVerseny.get();
        Set<Allomas> allomasok = v.getAllomasok();
        if (allomasok == null)
        	v.setAllomasok(allomasok = new HashSet<Allomas>());
        allomasok.add(allomas);
    }
    
    @Override
    protected void delete(Allomas allomas) {
    	super.delete(allomas);
        eVerseny.get().getAllomasok().remove(allomas);
    }
    
    @Override
    protected Composite createFields(Composite cptParent) {
        final 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);
        
        eReszidok = new O2MEdit_Button<Reszido>(this, cpt, NONE, "reszidok", "Részidők") {
			@Override
            protected Form<Reszido> createForm(Composite cpt, int style) {
				ReszidoForm f = new ReszidoForm(cpt, style);
//				f.eVerseny.set(eVerseny.get());
				f.fixVerseny(eVerseny.get());
				f.eAllomas.set(AllomasForm.this.get());
				f.eAllomas.fix(true);
				f.refresh();
				return f;
            }
        };

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