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

import hibergui.Form;
import hibergui.I18n;
import hibergui.Util;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * @author espakm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public abstract class ListEdit_List<T> extends M2MEdit_List<T, java.util.List<T>> {
    
    private Button btnUp, btnDown;
    
    public ListEdit_List(Form<?> form, Composite cptParent, int style, String prop, String name, String editProp) {
        super(form, cptParent, style, prop, name, editProp);
        
		btnUp.addKeyListener(lsn_WIDGET_KEY);
		btnUp.addFocusListener(lsn_WIDGET_FOCUS);
		btnDown.addKeyListener(lsn_WIDGET_KEY);
		btnDown.addFocusListener(lsn_WIDGET_FOCUS);

		btnUp.setEnabled(false);
        btnDown.setEnabled(false);
    }
    
	@Override
    protected Control createContents(final Composite cptParent) {
        Composite cpt = new Composite(cptParent, SWT.NONE);
        
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        gridLayout.verticalSpacing = 5;
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        cpt.setLayout(gridLayout);
        
        GridData data;
        
        lst = createList(cpt);	
        data = new GridData(GridData.FILL_BOTH);
        data.verticalSpan = 4;
        data.heightHint = 120;
        data.grabExcessHorizontalSpace = true;
        lst.setLayoutData(data);
        
        Button btnAdd = createAssocDialogButton(cpt);
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.grabExcessHorizontalSpace = false;
        btnAdd.setLayoutData(data);
        
        Button btnRemove = createRemoveButton(cpt);
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.grabExcessHorizontalSpace = false;
        btnRemove.setLayoutData(data);
        
        btnUp = new Button(cpt, SWT.PUSH);
        btnUp.setText(I18n.get("up"));
        btnUp.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	if (lst.getSelectionIndices().length != 1)
            		return;
                int idx = lst.getSelectionIndex();
                T upper = value.get(idx - 1);
                T lower = value.get(idx);
                value.set(idx - 1, lower);
                value.set(idx, upper);
                lsv.refresh();
                btnUp.setEnabled(idx != 1);
                btnDown.setEnabled(true);
                markDirty();
                set(extract());
            }
        });
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.grabExcessHorizontalSpace = false;
        btnUp.setLayoutData(data);
        
        btnDown= new Button(cpt, SWT.PUSH);
        btnDown.setText(I18n.get("down"));
        btnDown.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
            	if (lst.getSelectionIndices().length != 1)
            		return;
                int idx = lst.getSelectionIndex();
                T upper = value.get(idx);
                T lower = value.get(idx + 1);
                value.set(idx, lower);
                value.set(idx + 1, upper);
                lsv.refresh();
                btnUp.setEnabled(true);
                btnDown.setEnabled(idx != value.size() - 2);
                markDirty();
                set(extract());
            }
        });
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.grabExcessHorizontalSpace = false;
        btnDown.setLayoutData(data);
        
        return cpt;
    }
    
	@Override
	protected void selectionChanged() {
		super.selectionChanged();
        int idx = lst.getSelectionIndex();
        if (idx == -1) {
            btnUp.setEnabled(false);
            btnDown.setEnabled(false);
            return;
        }
        btnUp.setEnabled(idx != 0);
        btnDown.setEnabled(idx != value.size() - 1);
    }
    
    @Override
    public void clear() {
        set(new ArrayList<T>());
    }
    
    @Override
    public void fill() {
        lsv.setInput(Util.ensure(value));
    }

    @Override
    public void setEnabled(boolean b) {
    	super.setEnabled(b);
    	if (b) {
    		if (lst.getSelectionCount() == 1) {
    			int idx = lst.getSelectionIndex();
    			if (idx != 0)
    				btnUp.setEnabled(true);
    			if (idx != value.size() -1)
    				btnDown.setEnabled(true);
    		}
    	}
    	else {
    		btnUp.setEnabled(false);
    		btnDown.setEnabled(false);
    	}
    }
}
