/*
 * Copyright 2013, TengJianfa , and other individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.free_erp.client.ui.forms.system;

import com.jdatabeans.beans.data.IDataRow;
import com.jdatabeans.beans.data.IDbSupport;
import com.jdatabeans.beans.data.ObjectDataRow;
import com.jdatabeans.beans.data.table.ITableColumnModel;
import com.jdatabeans.beans.data.table.ITableModel;
import com.jdatabeans.beans.data.table.JDataTableColumn;
import com.jdatabeans.util.MessageBox;
import org.free_erp.client.ui.core.ObjectsPool;
import org.free_erp.client.ui.forms.CBaseListManageDialog;
import org.free_erp.client.ui.main.Main;
import org.free_erp.service.entity.system.EmployeeDegree;
import org.free_erp.service.logic.IOptionSetService;
import java.awt.Frame;
import java.util.Collection;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author TengJianfa mobile:086-13003311398 qq:575633370 www.free-erp.com
 */
public class CEmpDegreeListDialog extends CBaseListManageDialog
{
    protected CEmpDegreeAddDialog dialog;
    public CEmpDegreeListDialog(Frame parent)
    {
        super(parent);
        this.createDbSupport();
        this.initCs();
        this.initDatas();
    }
    private void initCs()
    {
        this.setTitle("Ô±¹¤Ö°³ÆÏÂÀ­¿òÉèÖÃ");
    }

    @Override
    protected void initColumns()
    {
        ITableColumnModel columnModel = dataTable.getTableColumnModel();
		JDataTableColumn column = new JDataTableColumn();
		column.setHeaderText("Ô±¹¤Ö°³ÆÃû³Æ");
		column.setColumnName("empDegree");
		column.setWidth(165);
		columnModel.addColumn(column);
    }

    @Override
    public void initDatas()
    {
        ITableModel model = this.dataTable.getTableModel();
		List<EmployeeDegree> employeeDegrees = service.getListEmployeeDegrees(Main.getMainFrame().getCompany());
		for(EmployeeDegree po:employeeDegrees)
		{
			IDataRow dataRow = new ObjectDataRow(po, "id",dbSupport);
			model.insertDataRow(dataRow);
		}
    }

    @Override
    public void doAdd()
    {
        if (this.dialog == null)
		{
			this.dialog = new CEmpDegreeAddDialog(this, this.dataSource);
		}
		dialog.newDataRow();
        dialog.setTitle("Ìí¼ÓÔ±¹¤Ö°³Æ");
		dialog.setVisible(true);
		this.dataSource.clearTempDataRows();
    }

    @Override
    public void doEdit()
    {
        if (this.dialog == null)
		{
			this.dialog = new CEmpDegreeAddDialog(this, this.dataSource);
		}
		if (this.dataTable.getSelectedRow() < 0)
		{
			MessageBox.showErrorDialog(Main.getMainFrame(), "Ã»ÓÐÈÎºÎÊý¾ÝÐÐ±»Ñ¡ÖÐ!");
			return;
		}
		this.dataSource.setCurrentDataRow(this.dataTable.getSelectedDataRow());
        dialog.setTitle("ÐÞ¸ÄÔ±¹¤Ö°³Æ");
		dialog.setVisible(true);
    }

    public void createDbSupport()
    {
		dbSupport = new IDbSupport()
        {
			ObjectsPool pool = Main.getMainFrame().getObjectsPool();
			IOptionSetService optionSetService = Main.getServiceManager().getOptionSetService();

			public void delete(Object obj)
            {
                if (obj instanceof EmployeeDegree)
                {
					EmployeeDegree po = (EmployeeDegree) obj;
					optionSetService.deleteEmployeeDegree(po);
					pool.refreshEmployeeDegrees();
				}
                else
                {
					throw new IllegalArgumentException("²ÎÊýÀàÐÍ²»·ûºÏ!´«ÈëµÄÀàÐÍÎª" + obj.getClass().getName());
				}
			}

			public void save(Object obj)
            {
                if (obj instanceof EmployeeDegree)
                {
					EmployeeDegree po = (EmployeeDegree) obj;
					optionSetService.saveEmployeeDegree(po);
					pool.refreshEmployeeDegrees();
				}
                else
                {
					throw new IllegalArgumentException("²ÎÊýÀàÐÍ²»·ûºÏ!´«ÈëµÄÀàÐÍÎª" + obj.getClass().getName());
				}
			}

			public void deleteAll(Collection<Object> objs)
            {
				JOptionPane.showMessageDialog(null, "ÔÝÎ´ÊµÏÖ!");
			}

			public void saveAll(Collection<Object> objs)
            {
				JOptionPane.showMessageDialog(null, "ÔÝÎ´ÊµÏÖ!");
			}
		};
	}
}
