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

📄 listui.java

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

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Vector;

import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import org.apache.commons.beanutils.NestedNullException;
import org.apache.commons.beanutils.PropertyUtils;

import com.cownew.PIS.framework.client.ClientMetaDataLoaderFactory;
import com.cownew.PIS.framework.client.RemoteServiceLocator;
import com.cownew.PIS.framework.common.IBaseDAO;
import com.cownew.PIS.framework.common.IValueObject;
import com.cownew.PIS.framework.common.PagingQueryParam;
import com.cownew.PIS.framework.common.metaDataMgr.EntityModelInfo;
import com.cownew.PIS.framework.common.metaDataMgr.IMetaDataLoader;
import com.cownew.PIS.ui.base.IUIContainer;
import com.cownew.PIS.ui.base.UIFactory;
import com.cownew.PIS.ui.commonUI.filterUI.FilterUI;
import com.cownew.PIS.ui.ctrl.query.QueryColumnProperty;
import com.cownew.PIS.ui.ctrl.query.QueryColumnPropertyList;
import com.cownew.PIS.ui.ctrl.query.QueryExecutor;
import com.cownew.PIS.ui.ctrl.table.ExcelTableExporter;
import com.cownew.PIS.ui.utils.UIUtils;
import com.cownew.ctk.ui.swing.MsgBox;
import com.cownew.ctk.ui.swing.SuffixFileFilter;
import com.cownew.ctk.ui.swing.SwingUtils;
import com.cownew.ctk.ui.swing.TableUtils;

public abstract class ListUI extends AbstractListUI
{

	// 默认每页的条数
	private static final int DEFAULTPAGESIZE = 100;

	// 分页参数
	private PagingQueryParam pagingQueryParam;

	private QueryExecutor queryExe;

	private EntityModelInfo entityInfo;

	public ListUI() throws Exception
	{
		super();
		IMetaDataLoader metaLoader = ClientMetaDataLoaderFactory.getLoader();
		entityInfo = metaLoader.loadEntityByEntityPath(getRemoteService()
				.getEntityPath());
		pagingQueryParam = new PagingQueryParam(getPageSize());
		refreshData();
	}

	public abstract Class getServiceIntfClass();

	public abstract Class getEditUIClass();

	public abstract IValueObject generateNewVO() throws Exception;

	protected IBaseDAO getRemoteService()
	{
		return (IBaseDAO) RemoteServiceLocator
				.getRemoteService(getServiceIntfClass());
	}

	public String getUITitle()
	{
		return entityInfo.getAlias();

	}

	public String getSelectedId()
	{
		String pkName = entityInfo.getPrimaryKey();
		Object obj = TableUtils.getSelectedColValue(getTableMain(), pkName);
		if (obj == null)
		{
			return null;
		}
		return obj.toString();
	}

	protected QueryExecutor getQueryExecutor()
	{
		if (queryExe == null)
		{
			IBaseDAO baseDAO = getRemoteService();
			queryExe = new QueryExecutor(baseDAO);
		}

		return queryExe;
	}

	/**
	 * 用当前queryexecutor来刷新数据
	 */
	protected void refreshData() throws Exception
	{
		refreshData(getQueryExecutor());
	}

	protected void refreshData(QueryExecutor queryExecutor) throws Exception
	{
		List list = queryExecutor.executeQuery(pagingQueryParam);
		JTable tblMain = getTableMain();
		QueryColumnPropertyList columnList = queryExecutor.getColumnList();
		Vector tableHead = new Vector(columnList.size());
		for (int i = 0, n = columnList.size(); i < n; i++)
		{
			QueryColumnProperty qcProperty = columnList
					.getQueryColumnProperty(i);
			tableHead.add(qcProperty.getDisplayName());
		}
		DefaultTableModel tableModel = new DefaultTableModel(tableHead, 0);

		for (int i = 0, n = list.size(); i < n; i++)
		{
			Vector rowData = new Vector(columnList.size());
			Object info = list.get(i);
			for (int j = 0, m = columnList.size(); j < m; j++)
			{
				QueryColumnProperty qcProperty = columnList
						.getQueryColumnProperty(j);
				String column = qcProperty.getColumn();
				Object property = null;
				property = getPropertySilent(info, column);
				rowData.add(property);
			}

			tableModel.addRow(rowData);
		}

		tblMain.setModel(tableModel);

		for (int i = 0, n = columnList.size(); i < n; i++)
		{
			TableColumnModel columnModel = tblMain.getTableHeader()
					.getColumnModel();
			TableColumn column = columnModel.getColumn(i);
			QueryColumnProperty columnProperty = columnList
					.getQueryColumnProperty(i);
			String columnId = columnProperty.getColumn();
			column.setIdentifier(columnId);
			if (!columnProperty.isVisible())
			{
				TableUtils.hideColumn(tblMain, column.getModelIndex());
			}
		}
	}

	private Object getPropertySilent(Object info, String column)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException
	{
		Object property;
		try
		{
			property = PropertyUtils.getProperty(info, column);

		} catch (NestedNullException ne)
		{
			// 当表达式的中间某个嵌套值为null的时候就会抛出此异常,因此对于其值按照null处理
			property = null;
		}
		return property;
	}

	protected void actionAddNew_actionPerformed(ActionEvent e) throws Exception
	{
		IValueObject newInfo = generateNewVO();
		if (newInfo == null)
		{
			return;
		}
		Window window = SwingUtilities.getWindowAncestor(this);
		IUIContainer editUICont = UIFactory.getUIDialogContainer(
				getEditUIClass().getName(), window);
		EditUI editUI = (EditUI) editUICont.getUIPanel();
		editUI.setMode(EditUIModeEnum.UNSAVED);
		editUI.setModelVO(newInfo);
		editUI.loadToUI();
		editUICont.show();
		refreshData();
	}

	protected void actionDelete_actionPerformed(ActionEvent e) throws Exception
	{
		super.actionDelete_actionPerformed(e);
		UIUtils.checkSelected(getTableMain());
		IBaseDAO baseDAO = getRemoteService();
		baseDAO.delete(getSelectedId());
		refreshData();
	}

	protected void actionEdit_actionPerformed(ActionEvent e) throws Exception
	{
		super.actionEdit_actionPerformed(e);
		UIUtils.checkSelected(getTableMain());
		Window window = SwingUtilities.getWindowAncestor(this);
		IUIContainer editUICont = UIFactory.getUIDialogContainer(
				getEditUIClass().getName(), window);
		EditUI editUI = (EditUI) editUICont.getUIPanel();
		editUI.setMode(EditUIModeEnum.EDIT);
		editUI.loadById(getSelectedId());
		editUICont.show();
		refreshData();
	}

	protected void actionView_actionPerformed(ActionEvent e) throws Exception
	{
		super.actionView_actionPerformed(e);
		UIUtils.checkSelected(getTableMain());
		Window window = SwingUtilities.getWindowAncestor(this);
		IUIContainer editUICont = UIFactory.getUIDialogContainer(
				getEditUIClass().getName(), window);
		EditUI editUI = (EditUI) editUICont.getUIPanel();
		editUI.setMode(EditUIModeEnum.VIEW);
		editUI.loadById(getSelectedId());
		editUICont.show();
	}

	protected void actionRefresh_actionPerformed(ActionEvent e)
			throws Exception
	{
		refreshData();
	}

	protected void actionFilter_actionPerformed(ActionEvent e) throws Exception
	{
		FilterUI fiterUI = getFilterUI();
		if (fiterUI == null)
		{
			return;
		}
		fiterUI.setSolutionId(getClass().getName());
		SwingUtils.centerAtScreen(fiterUI);
		fiterUI.show();
		if (fiterUI.isOK())
		{
			QueryExecutor qe = getQueryExecutor();
			qe.setOQL(fiterUI.getSQLWithSort());
			qe.setSqlParams(fiterUI.getParams());
			refreshData(qe);
		}
	}

	/**
	 * 得到过滤界面
	 * 
	 * @return
	 */
	protected FilterUI getFilterUI()
	{
		return null;
	}

	protected void actionExportToExcel_actionPerformed(ActionEvent e)
			throws Exception
	{
		JFileChooser fileChooser = new JFileChooser();

		fileChooser.setDialogTitle("导出Excel");
		fileChooser.setFileFilter(new SuffixFileFilter("xls"));
		int ret = fileChooser.showSaveDialog(this);
		if (ret != JFileChooser.APPROVE_OPTION)
		{
			return;
		}

		File file = fileChooser.getSelectedFile();
		if (file != null)
		{
			ExcelTableExporter exporter = new ExcelTableExporter(getTableMain());
			short defValue = ExcelTableExporter.DEFAULTVALUE;
			// 第一列是id列,所以不导出它
			exporter.setBounds((short) 1, defValue, defValue, defValue);
			exporter.export(file);
		}
	}

	public int getCurrentPageIndex()
	{
		return pagingQueryParam.getPageIndex();
	}

	public void setCurrentPageIndex(int currentPageIndex)
	{
		pagingQueryParam.setPageIndex(currentPageIndex);
		labelPageInfo.setText("当前为第" + (currentPageIndex + 1) + "页");
	}

	/**
	 * 每页显示的条数
	 */
	protected int getPageSize()
	{
		return DEFAULTPAGESIZE;
	}

	protected void firstPage_actionPerformed() throws Exception
	{
		if (getCurrentPageIndex() == 0)
		{
			return;
		}
		setCurrentPageIndex(0);
		refreshData();
	}

	protected void nextPage_actionPerformed() throws Exception
	{
		int rowCount = getTableMain().getModel().getRowCount();
		if (rowCount < getPageSize())
		{
			// 如果当前行数小于页大小,则说明有可能是最后一页
			//(有可能别的用户在自从上次活动以来新增数据了,所以要刷新一下)
			refreshData();
			rowCount = getTableMain().getModel().getRowCount();
			// 如果刷新以后还是当前行数小于页大小,则提示"当前已经是最后一页"
			if (rowCount < getPageSize())
			{
				MsgBox.showError(this, "当前已经是最后一页");
				return;
			}
		}
		setCurrentPageIndex(getCurrentPageIndex() + 1);
		refreshData();
	}

	protected void priorPage_actionPerformed() throws Exception
	{
		if (getCurrentPageIndex() == 0)
		{
			MsgBox.showError(this, "当前已经是第一页");
			return;
		}
		setCurrentPageIndex(getCurrentPageIndex() - 1);
		refreshData();
	}

}

⌨️ 快捷键说明

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