package nc.ui.wa.ruleex.action;

import java.awt.event.ActionEvent;

import javax.swing.Action;

import nc.hr.utils.ResHelper;
import nc.itf.hr.wa.IHRWAActionCode;
import nc.ui.hr.uif2.action.BodyTabAction;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UITable;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pub.bill.BillModel;
import nc.ui.uif2.editor.IEditor;
import nc.ui.wa.ruleex.view.WaRuleForm;
import nc.uitheme.ui.ThemeResourceCenter;
import nc.vo.hrwa.waruleex.WageformdetExVO;

/**
 * WaRuleDownRowAction<BR>
 * <BR>
 *
 * @author: xuhw
 * @date: 2009-11-10
 * @since: eHR V6.0
 * @走查人:
 * @走查日期:
 * @修改人:
 * @修改日期:
 */
public class WaRuleDownLineAction extends BodyTabAction
{
	private IEditor editor;

	/**
	 * 初始化按钮
	 */
	public WaRuleDownLineAction()
	{
		super();
		setBtnName(ResHelper.getString("60130payrule","060130payrule0002")/*@res "下移行"*/);
		setCode(IHRWAActionCode.Down);
		// putValue(Action.ACCELERATOR_KEY,
		// KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK));
		putValue(Action.SHORT_DESCRIPTION, ResHelper.getString("60130payrule","060130payrule0002")/*@res "下移行"*/);
		putValue(Action.SMALL_ICON, 
				ThemeResourceCenter.getInstance().getImage("themeres/ui/toolbaricons/move_down.png"));
	}

	@Override
	public void doAction(ActionEvent e) throws Exception
	{
		BillCardPanel billCardPanel = ((WaRuleForm) this.getEditor()).getBillCardPanel();
		BillModel billModel = billCardPanel.getBillModel();
		UITable table = billCardPanel.getBillTable();
		((WaRuleForm) this.getEditor()).getBillCardPanel().stopEditing();
		// 当前选中的子表的行数
		int intSelectedIndex = table.getSelectedRow();
		// 取得薪资规则表所有的行数
		int intAllNumbers = table.getRowCount();
		table.editingStopped(null);
		if (intSelectedIndex < 0)
		{ // 用户未选中行
			MessageDialog.showHintDlg(getModel().getContext().getEntranceUI(), null, ResHelper.getString("60130payrule","060130payrule0003")/*@res "请先选中要移动的行。"*/);
			return;
		}
		if (intSelectedIndex == intAllNumbers - 1)
		{
			// 如果已是最后一行
			MessageDialog.showHintDlg(getModel().getContext().getEntranceUI(), null, ResHelper.getString("60130payrule","060130payrule0004")/*@res "已经是最后一行。"*/);
			return;
		}

		// 交换位置
		// 取得选中的子表的vo
		WageformdetExVO wageFormDetVO = (WageformdetExVO) billModel.getBodyValueRowVO(intSelectedIndex, WageformdetExVO.class.getName());
		// 取得子表所有的VO
		WageformdetExVO[] wageFormDetVOs = (WageformdetExVO[]) billModel.getBodyValueVOs(WageformdetExVO.class.getName());

		// 交换位置
		wageFormDetVOs[intSelectedIndex] = wageFormDetVOs[intSelectedIndex + 1];
		wageFormDetVOs[intSelectedIndex + 1] = wageFormDetVO;
		wageFormDetVOs[intSelectedIndex].setIlevel(Integer.valueOf(intSelectedIndex + 1));
		wageFormDetVOs[intSelectedIndex + 1].setIlevel(Integer.valueOf(intSelectedIndex + 2));

		billModel.setBodyDataVO(wageFormDetVOs);
		table.setRowSelectionInterval(intSelectedIndex + 1, intSelectedIndex + 1);
	}

	public IEditor getEditor()
	{
		return editor;
	}

	public void setEditor(IEditor editor)
	{
		this.editor = editor;
	}
}