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

📄 assessmentoperatorconfusionmatrixview.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program 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 General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Created on 2005-1-27 
 */
package eti.bi.alphaminer.patch.standard.operation.result.view;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

import eti.bi.alphaminer.operation.result.ResultView;
import eti.bi.alphaminer.operation.result.datamodel.DataGridModel;
import eti.bi.alphaminer.operation.result.export.ExcelExporter;
import eti.bi.alphaminer.patch.standard.operation.operator.EvaluationMatrix;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;


/**
 * Input an ConfusionMatrix ArrayList. Output all matrix into JTables.
 * Each JTable has some description words at the top. Can export all 
 * these JTables into one worksheet of an EXCEL file. 
 * Note that the JTable description is also exported. Modifications are
 * done also in ExcelExporter.exportMultiTables() method. 
 * 
 * By TWang. Jan 28, 2005.
 */
public class AssessmentOperatorConfusionMatrixView extends ResultView {
	 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// JScrollPane that contains JTable
	private JScrollPane[] m_ScrollPanes;
	private JScrollPane m_WholePane;
	private JPanel m_Jpanel;
	private JLabel[] m_JLabels;
  
	private ArrayList m_ConfusionMatrixLists;
	private ArrayList<JTable> m_JTableLists;  
	private ArrayList<Object> m_JTableNameLists;
 	
	
	/**
	 * @param confusionMatrixLists
	 * @throws AppException
	 */
	public AssessmentOperatorConfusionMatrixView(ArrayList a_ConfusionMatrixLists) throws AppException {  
	 
			super(Resource.srcStr("ConfusionMatrixView"));	 
			m_ViewType = ResultView.TYPE_DATA;  
			
			m_ConfusionMatrixLists = a_ConfusionMatrixLists;
			m_JTableLists = new ArrayList<JTable>(); 
			m_JTableNameLists = new ArrayList<Object>();
			
			int numOfTables = m_ConfusionMatrixLists.size() * 2;
			m_Jpanel = new JPanel();
			m_WholePane = new JScrollPane();
			m_ScrollPanes = new JScrollPane[numOfTables];
			m_JLabels = new JLabel[numOfTables];
			
			GridBagConstraints c = new GridBagConstraints();
			
			m_Jpanel.setLayout(new GridBagLayout()); 
			m_Jpanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));  
 				
			for (int i=0; i<numOfTables; i++){
				m_ScrollPanes[i] = new JScrollPane();
				m_JLabels[i] = new JLabel();
				
				c.gridx = 0;
				c.gridy = i*2; 
			 	c.weightx = 1.0;
			 	c.weighty = 0;
			 	c.ipadx = 100;
			 	c.ipady = 0;
			 	c.fill = GridBagConstraints.HORIZONTAL;
				c.anchor = GridBagConstraints.WEST;
				c.insets = new Insets(0, 2, 0, 0);  
				m_Jpanel.add(m_JLabels[i], c);
				
				c.gridx = 0;
				c.gridy = i*2 + 1; 
		 	    c.weightx = 1.0; 
		 	    c.weighty = 1.0;
		 		c.ipadx = 100;
 			    c.ipady = 50; // Important, otherwise, the ScrollPane containing the JTable will clapse.
			    c.fill = GridBagConstraints.HORIZONTAL;
				c.anchor = GridBagConstraints.WEST;
				c.insets = new Insets(0, 0, 0, 0);
				m_Jpanel.add(m_ScrollPanes[i], c);
			}
			
			this.setLayout(new BorderLayout()); 
			this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 
			this.add(m_WholePane, BorderLayout.CENTER);  
 			
			m_WholePane.setViewportView(m_Jpanel);
			
			for (int i=0; i<m_ConfusionMatrixLists.size(); i++){
				Object object = m_ConfusionMatrixLists.get(i);
				if ( !(object instanceof EvaluationMatrix) ) {
					throw new AppException("The input is not of type JTable");
				}
				EvaluationMatrix eMatrix = (EvaluationMatrix) m_ConfusionMatrixLists.get(i);
				createDataTable(eMatrix, i);
			}    
 	 	} 
		
		/**
		 * 
		 * Create the JTable and attach it in the ScrollPane.   
		 *  
		 * @param a_ScrollPane
		 * @throws AppException
		 */
		private void createDataTable(EvaluationMatrix a_EMatrix, int a_index) throws AppException { 
		 
			int row = 0;
			int column = 0;
			double[][] tableContent;
			
			String[] classNameStrings = a_EMatrix.getM_ClassNames();
			String[] classMatrixTableHeader = new String[classNameStrings.length + 1];
			// One column more to display class infor. The size of the table content
			// does not count the table header as a row.
			column = classMatrixTableHeader.length;
			row = classNameStrings.length;
			
			Class[] classMatrixTableType = new Class[column]; 
			Object[][] classMatrixTableContent = new Object[row][column]; 
			tableContent = a_EMatrix.getM_ConfusionMatrixElements(); 
	 
			// Create JTable header and JTable class type.
			for (int coloumIndex = 0; coloumIndex < column; coloumIndex++) {
				if (coloumIndex == 0){ 
					classMatrixTableType[coloumIndex] = String.class;
					classMatrixTableHeader[coloumIndex] = Resource.srcStr("ASSESSMENT_CLASS");
					continue;  
				}  
				classMatrixTableType[coloumIndex] = Double.class;  
				classMatrixTableHeader[coloumIndex] = classNameStrings[coloumIndex-1];
			}
			 
			// Fill the JTable content.
			if (tableContent != null) {
				double[] rowContent = tableContent[0];
				if( tableContent.length != row || rowContent.length != column-1){
					throw new AppException("The size of the input Table is not Correct.");
				}
			}

			for(int rowIndex=0; rowIndex<row; rowIndex++){
				classMatrixTableContent[rowIndex][0] = classNameStrings[rowIndex];
				for(int columnIndex=1; columnIndex<column; columnIndex++){
					classMatrixTableContent[rowIndex][columnIndex] = new Double(tableContent[rowIndex][columnIndex-1]);
				}
			}
			
			// Create Table			
			JTable classTable = new JTable();
			classTable.setModel(new DataGridModel(classMatrixTableContent, classMatrixTableHeader, classMatrixTableType));
 			setColumnWidth(classTable);  
 			
			m_JTableLists.add(classTable); 
			
			m_ScrollPanes[a_index*2].setPreferredSize(new Dimension(200, 100)); 
			m_ScrollPanes[a_index*2].getViewport().add(classTable);
			m_ScrollPanes[a_index*2].getViewport().setBackground(Color.WHITE);   
			
			String tableName = new String(a_EMatrix.getM_ModelName() + " " + a_EMatrix.getM_DataName() + Resource.srcStr("ASSESSMENT_CLASSDATA"));
			m_JTableNameLists.add(tableName);
			m_JLabels[a_index*2].setText(tableName);
					  
			/** Create the Statistic table */ 
 			
			String[] tempHeader = a_EMatrix.getM_StatisticsNames();
			String[] statMatrixTableHeader = new String[tempHeader.length + 1];
			// One column more to display class infor. The size of the table content
			// does not count the table header as a row.
			column = statMatrixTableHeader.length;
			row = classNameStrings.length; 
			 
			Class[] statMatrixTableType = new Class[column]; 
			Object[][] statMatrixTableContent = new Object[row][column]; 
			tableContent = a_EMatrix.getM_StatisticsMatrixElements(); 
	 
			// Create JTable header and JTable class type.
			for (int coloumIndex = 0; coloumIndex < column; coloumIndex++) {
				if (coloumIndex == 0){ 
					statMatrixTableType[coloumIndex] = String.class;
					statMatrixTableHeader[coloumIndex] = Resource.srcStr("ASSESSMENT_CLASS");
					continue;  
				}  
				statMatrixTableType[coloumIndex] = Double.class;				
				statMatrixTableHeader[coloumIndex] = tempHeader[coloumIndex-1];
			}
			 
			// Fill the JTable content.
			if (tableContent != null) {
				double[] rowContent = tableContent[0];
				if( tableContent.length != row || rowContent.length != column-1){
					throw new AppException("The size of the input Table is not Correct.");
				}
			}

			for(int rowIndex=0; rowIndex<row; rowIndex++){
				statMatrixTableContent[rowIndex][0] = classNameStrings[rowIndex];
				for(int columnIndex=1; columnIndex<column; columnIndex++){
						statMatrixTableContent[rowIndex][columnIndex] = new Double(100*tableContent[rowIndex][columnIndex-1]);
				}
			}
			
			// Create Statistics Table			
			JTable statTable = new JTable();
			statTable.setModel(new DataGridModel(statMatrixTableContent, statMatrixTableHeader, statMatrixTableType));
 			setColumnWidth(statTable); 
 		 
 			m_JTableLists.add(statTable); 
			
 			m_ScrollPanes[a_index*2 + 1].setPreferredSize(new Dimension(200, 100)); 
 			m_ScrollPanes[a_index*2 + 1].getViewport().add(statTable);
 			m_ScrollPanes[a_index*2 + 1].getViewport().setBackground(Color.WHITE);  
 			
 			StringBuffer statTableName = new StringBuffer(a_EMatrix.getM_ModelName() + " " + a_EMatrix.getM_DataName() + Resource.srcStr("ASSESSMENT_STATISTICS"));
  			statTableName.append(Math.round(100*a_EMatrix.getM_OverallPrecision())+"%):");
 			
			m_JTableNameLists.add(statTableName.toString());
 			m_JLabels[a_index*2 + 1].setText(statTableName.toString());
		} 
		
		public void setColumnWidth(JTable a_JTable) {
				TableColumnModel tcm = a_JTable.getColumnModel();
				if (tcm.getColumn(0).getWidth()<60)
				{
					for (int i=1; i<tcm.getColumnCount(); i++){
						tcm.getColumn(i).setMinWidth(80); 
					}
					a_JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
				}
				tcm.getColumn(0).setMaxWidth(80);
				tcm.getColumn(0).setCellRenderer(a_JTable.getTableHeader().getDefaultRenderer()); 
		}
		
		/**
		 * Exort the data table into an excel file.
		 * 
		 * Called by the OperatorResult class. The subclass of OperatorResult must 
		 * call m_SelectedView.export() explictly if it overwirte the export() function 
		 * of OperatorResult.
		 */
		public void export() throws AppException, SysException {
			// Use user home directory 
			File directory = new File(System.getProperty("user.dir"));

			// Create and initialize file chooser for excel
			JFileChooser chooser = new JFileChooser(directory);
			chooser.setDialogTitle(Resource.srcStr("FileExport"));
			chooser.setFileFilter(ExcelExporter.FILTER);
			chooser.setSelectedFile(ExcelExporter.DEFAULT_FILE);

	  		// pop up the file chooser dialog and return the file value
			int returnVal = chooser.showSaveDialog(this);	  
			if (returnVal == JFileChooser.APPROVE_OPTION) 
			{
				File exportFile = chooser.getSelectedFile();
				//<<tyleung 20/4/2005
				if (exportFile.exists()) {
					int option = JOptionPane
							.showConfirmDialog(
									(Component) this,
									"The file  \""
											+ exportFile.getName()
											+ "\""
											+ " already exists. Do you want to replace the existing file?",//						
									"AlphaMiner", JOptionPane.YES_NO_OPTION,
									JOptionPane.QUESTION_MESSAGE);
					if (option != JOptionPane.CANCEL_OPTION) {
						if (option == JOptionPane.YES_OPTION) {
							// Create the excel exporter with the excel file extension enforced to be .xls
						  	ExcelExporter aExporter = new ExcelExporter(exportFile, true);
							aExporter.exportMultiTables(m_JTableLists, m_JTableNameLists);
							
						}else{
							returnVal = chooser.showSaveDialog(this);
						}
					}
				}else {

					// Create the excel exporter with the excel file extension enforced to be .xls
				  	ExcelExporter aExporter = new ExcelExporter(exportFile, true);
					aExporter.exportMultiTables(m_JTableLists, m_JTableNameLists);
				}
			} 
			//tyleung 20/4/2005>>
		}

	}

⌨️ 快捷键说明

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