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

📄 filterui.java

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

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.table.DefaultTableModel;

import com.cownew.PIS.base.uiService.common.FilterSolutionInfo;
import com.cownew.PIS.base.uiService.common.IFilterSolutionDAO;
import com.cownew.PIS.framework.client.RemoteServiceLocator;
import com.cownew.PIS.framework.common.IValueObject;
import com.cownew.PIS.framework.common.utils.KeyValueList;
import com.cownew.PIS.ui.ctrl.table.ComboBoxCellEditor;
import com.cownew.ctk.common.AssertUtils;
import com.cownew.ctk.common.StringUtils;
import com.cownew.ctk.ui.swing.MsgBox;

public class FilterUI extends AbstractFilterUI
{
	private String solutionId;

	private FilterSortItem[] sortItems;

	/**
	 * @param sysDefFilterComponent
	 *            系统预设条件的控件,通常是JPanel等面板,并实现ISysDefFilter接口
	 * @param sortItems
	 *            预设待选择的排序属性
	 */
	public FilterUI(Component sysDefFilterComponent, FilterSortItem[] sortItems)
	{
		super(sysDefFilterComponent);
		this.sortItems = sortItems;
		initSortTable();

		if (!(sysDefFilterComponent instanceof ISysDefFilter))
		{
			throw new IllegalArgumentException("系统预设面板必须实现ISysDefFilter接口");
		}
	}

	protected void loadConfig()
	{
		super.loadConfig();
		loadAllSolution();
	}

	// 加载此过滤界面相关的过滤方案
	protected void loadAllSolution()
	{
		AssertUtils.assertNotNull(solutionId);

		DefaultListModel listModel = new DefaultListModel();

		IFilterSolutionDAO filterDAO = (IFilterSolutionDAO) RemoteServiceLocator
				.getRemoteService(IFilterSolutionDAO.class);
		List solList = filterDAO.loadBySolutionId(getSolutionId());
		if (solList != null && solList.size() > 0)
		{
			for (int i = 0, n = solList.size(); i < n; i++)
			{
				listModel.addElement(solList.get(i));
			}
		}

		listSolution.setModel(listModel);
	}

	public void actionAddNewSolution_actionPerformed(ActionEvent e)
			throws Exception
	{
		String name = MsgBox.showInput(this, "请输入方案的名称");
		if (StringUtils.isEmpty(name))
		{
			return;
		}

		DefaultListModel listModel = (DefaultListModel) listSolution.getModel();
		Enumeration en = listModel.elements();
		while (en.hasMoreElements())
		{
			FilterSolutionInfo info = (FilterSolutionInfo) en.nextElement();
			if (info.getName().equals(name))
			{
				MsgBox.showError(this, "方案名称重复");
				return;
			}
		}

		FilterSolutionInfo info = new FilterSolutionInfo();
		info.setName(name);
		info.setSolutionId(getSolutionId());

		IFilterSolutionDAO filterDAO = (IFilterSolutionDAO) RemoteServiceLocator
				.getRemoteService(IFilterSolutionDAO.class);
		info.setId(filterDAO.save(info));

		listModel.addElement(info);
	}

	public void actionDelSolution_actionPerformed(ActionEvent e)
			throws Exception
	{
		IValueObject vo = (IValueObject) listSolution.getSelectedValue();
		if (vo == null)
		{
			MsgBox.showError(this, "请选择要删除的方案!");
			return;
		}
		IFilterSolutionDAO filterDAO = (IFilterSolutionDAO) RemoteServiceLocator
				.getRemoteService(IFilterSolutionDAO.class);
		filterDAO.delete(vo.getId());
		DefaultListModel listModel = (DefaultListModel) listSolution.getModel();
		listModel.removeElement(vo);
		MsgBox.showInfo(this, "删除成功!");
	}

	public void actionSaveSolution_actionPerformed(ActionEvent e)
			throws Exception
	{
		FilterSolutionInfo solutionInfo = (FilterSolutionInfo) listSolution
				.getSelectedValue();
		if (solutionInfo == null)
		{
			MsgBox.showError(this, "请选择要保存的方案!");
			return;
		}

		ISysDefFilter sysDefFilter = (ISysDefFilter) getSysDefFilterComponent();
		String params = sysDefFilter.getParamsForSolution();

		solutionInfo.setParams(params);
		String sortPropsStr = FilterUIUtils
				.filterSortInfosToString(getSortInfos());
		solutionInfo.setSortRule(sortPropsStr);

		IFilterSolutionDAO filterDAO = (IFilterSolutionDAO) RemoteServiceLocator
				.getRemoteService(IFilterSolutionDAO.class);
		filterDAO.update(solutionInfo);
		MsgBox.showInfo(this, "保存成功!");

	}

	/**
	 * 得到用户设定的排序选项
	 * 
	 * @return
	 */
	public FilterSortInfo[] getSortInfos()
	{
		DefaultTableModel tableMode = (DefaultTableModel) tableSort.getModel();
		FilterSortInfo[] infos = new FilterSortInfo[tableMode.getRowCount()];
		for (int i = 0, n = tableMode.getRowCount(); i < n; i++)
		{
			FilterSortItem prop = (FilterSortItem) tableMode.getValueAt(i, 0);
			FilterSortTypEnum sortType = (FilterSortTypEnum) tableMode
					.getValueAt(i, 1);
			FilterSortInfo info = new FilterSortInfo(prop.getProperty(),
					sortType);
			infos[i] = info;
		}
		return infos;
	}

	protected void listSolution_SelectChanged() throws Exception
	{
		FilterSolutionInfo solutionInfo = (FilterSolutionInfo) listSolution
				.getSelectedValue();
		String params = solutionInfo.getParams();
		String sortRule = solutionInfo.getSortRule();
		ISysDefFilter sysDefFilter = (ISysDefFilter) getSysDefFilterComponent();
		sysDefFilter.setParamsFromSolution(params);
		setSelectedSortProps(FilterUIUtils.filterSortInfosFromString(sortRule));
	}

	private void setSelectedSortProps(FilterSortInfo[] infos)
	{
		DefaultTableModel tableMode = (DefaultTableModel) tableSort.getModel();
		tableMode.getDataVector().clear();
		tableMode.fireTableDataChanged();
		for (int i = 0, n = infos.length; i < n; i++)
		{
			FilterSortInfo info = infos[i];

			FilterSortItem sortItem = findSortItem(info.getProperty());
			if (sortItem == null)
			{
				throw new IllegalArgumentException("sortItem not found!");
			}
			Object[] rowData = new Object[] { sortItem, info.getSortType() };
			tableMode.addRow(rowData);
		}
	}

	private FilterSortItem findSortItem(String property)
	{
		for (int i = 0, n = sortItems.length; i < n; i++)
		{
			FilterSortItem prop = sortItems[i];
			if (prop.getProperty().equals(property))
			{
				return prop;
			}
		}
		return null;
	}

	private void initSortTable()
	{
		Vector headVector = new Vector();
		headVector.add(COL_PROPERTY);
		headVector.add(COL_SORTTYPE);

		DefaultTableModel tableMode = new DefaultTableModel(headVector, 0);

		tableSort.setModel(tableMode);

		Vector propsVector = new Vector();
		for (int i = 0, n = sortItems.length; i < n; i++)
		{
			FilterSortItem sortProp = sortItems[i];
			propsVector.add(sortProp);
		}
		tableSort.getColumn(COL_PROPERTY).setCellEditor(
				new ComboBoxCellEditor(propsVector));

		Vector sortTypeVector = new Vector(3);
		sortTypeVector.add(FilterSortTypEnum.DEFAULT);
		sortTypeVector.add(FilterSortTypEnum.ASC);
		sortTypeVector.add(FilterSortTypEnum.DESC);
		tableSort.getColumn(COL_SORTTYPE).setCellEditor(
				new ComboBoxCellEditor(sortTypeVector));
	}

	protected void btnSortAddNew_actionPerformed(ActionEvent e)
	{
		DefaultTableModel tableMode = (DefaultTableModel) tableSort.getModel();
		tableMode.addRow(new Vector());
	}

	protected void btnSortDelete_actionPerformed(ActionEvent e)
	{
		int row = tableSort.getSelectedRow();
		if (row < 0)
		{
			MsgBox.showError(this, "请选择要删除的行!");
			return;
		}
		DefaultTableModel tableMode = (DefaultTableModel) tableSort.getModel();

		tableMode.removeRow(row);
	}

	public String getSolutionId()
	{
		return solutionId;
	}

	/**
	 * 设定此过滤方案的唯一标识,用ListUI的类名,VO类名,元数据路径等一般都可以保证唯一性
	 * 
	 * @return
	 */
	public void setSolutionId(String solutionId)
	{
		this.solutionId = solutionId;
	}

	/**
	 * 得到过滤条件的SQL语句
	 * 
	 * @return
	 */
	public String getSQL()
	{
		ISysDefFilter filter = (ISysDefFilter) getSysDefFilterComponent();
		return filter.getSQL();
	}

	/**
	 * 得到过滤条件SQL对应的参数列表
	 * 
	 * @return
	 */
	public KeyValueList getParams()
	{
		ISysDefFilter filter = (ISysDefFilter) getSysDefFilterComponent();
		return filter.getParams();
	}

	/**
	 * 得到过滤条件的SQL语句(将orderBy语句融合进去了)
	 * 
	 * @return
	 */
	public String getSQLWithSort()
	{
		FilterSortInfo[] sortInfos = getSortInfos();

		StringBuffer sbSort = new StringBuffer();
		for (int i = 0, n = sortInfos.length; i < n; i++)
		{
			FilterSortInfo sortInfo = sortInfos[i];
			sbSort.append(" ").append(sortInfo.getProperty()).append(" ");
			FilterSortTypEnum sortType = sortInfo.getSortType();
			if (sortType == FilterSortTypEnum.ASC)
			{
				sbSort.append("asc");
			} else if (sortType == FilterSortTypEnum.DESC)
			{
				sbSort.append("desc");
			}
			if (i < n - 1)
			{
				sbSort.append(",");
			}
		}

		if (StringUtils.isEmpty(sbSort))
		{
			return getSQL();
		}

		return getSQL() + " order by " + sbSort.toString();
	}

}

⌨️ 快捷键说明

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