⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpletableeditui.java

📁 《j2ee开发全程实录》随书源码
💻 JAVA
字号:
package com.cownew.PIS.ui.commonUI;

import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JToolBar;

import com.cownew.PIS.framework.common.IValueObject;
import com.cownew.PIS.framework.common.metaDataMgr.MetaDataConstant;
import com.cownew.PIS.ui.commonUI.databind.TableDataBinder;
import com.cownew.PIS.ui.utils.PISAbstractAction;
import com.cownew.PIS.ui.utils.UIUtils;
import com.cownew.ctk.common.PropertyUtils;
import javax.swing.table.DefaultTableModel;

abstract public class SimpleTableEditUI extends EditUI
{
	protected TableDataBinder tableDataBinder;

	private JTable table;

	private ActionAddTableLine actionAddTableLine;

	private ActionRemoveTableLine actionRemoveTableLine;

	public SimpleTableEditUI() throws Exception
	{
		super();		
	}

	protected void initTable(JTable table)
	{
		
	}

	protected JTable getTable()
	{
		if (table == null)
		{
			table = new JTable();
			table.setVisible(true);
			initTable(table);
			tableDataBinder = new TableDataBinder(table, getTableDataClass());
		}
		return table;
	}

	protected void initDataBind()
	{
		super.initDataBind();
		//默认所有的一对多属性类型都为Set
		Set valueObjectSet = (Set) PropertyUtils.getProperty(modelVO,
				getTableDataProperty());
		tableDataBinder.setValueObjectSet(valueObjectSet);
	}

	public void loadToUI()
	{
		super.loadToUI();
		tableDataBinder.loadToTable();
	}

	public void storeToVO() throws Exception
	{
		super.storeToVO();
		tableDataBinder.storeToVOSet();
		IValueObject headVO = dataBinder.getValueObject();
		Set detailSet = tableDataBinder.getValueObjectSet();
		Iterator it = detailSet.iterator();
		while (it.hasNext())
		{
			Object detailInfo = it.next();

			PropertyUtils.setProperty(detailInfo, MetaDataConstant.PROPHEAD, headVO);
		}
		PropertyUtils.setProperty(headVO, getTableDataProperty(), detailSet);
	}

	/**
	 * 返回表格对应的VO的属性名,比如entrys details
	 * @return
	 */
	abstract protected String getTableDataProperty();

	/**
	 *返回表格对应的VO的属性的类型,比如SaleBillDetail.class
	 * @return
	 */
	abstract protected Class getTableDataClass();

	protected void initAction()
	{
		super.initAction();
		actionAddTableLine = new ActionAddTableLine("新增行");
		actionRemoveTableLine = new ActionRemoveTableLine("删除行");
	}

	protected void initToolBar(JToolBar tBar)
	{
		super.initToolBar(tBar);
		JButton btnAddLine = new JButton();
		btnAddLine.setAction(actionAddTableLine);
		tBar.add(btnAddLine);

		JButton btnRemoveLine = new JButton();
		btnRemoveLine.setAction(actionRemoveTableLine);
		tBar.add(btnRemoveLine);
	}

	class ActionAddTableLine extends PISAbstractAction
	{

		public ActionAddTableLine(String name, Icon icon)
		{
			super(name, icon);

		}

		public ActionAddTableLine(String name)
		{
			super(name);

		}

		public void onActionPerformed(ActionEvent e) throws Exception
		{
			DefaultTableModel tableModel = (DefaultTableModel) getTable()
					.getModel();
			Vector vector = new Vector();
			tableModel.addRow(vector);
		}

	}

	class ActionRemoveTableLine extends PISAbstractAction
	{

		public void onActionPerformed(ActionEvent e) throws Exception
		{
			UIUtils.checkSelected(getTable());
			DefaultTableModel tableModel = (DefaultTableModel) getTable()
					.getModel();
			tableModel.removeRow(getTable().getSelectedRow());

		}

		public ActionRemoveTableLine(String name, Icon icon)
		{
			super(name, icon);

		}

		public ActionRemoveTableLine(String name)
		{
			super(name);

		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -