📄 dataview.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/12
*
* $Author$
* $Date$
* $Revision$
*
* This class extends Result View to work as a common pane to
* visualize Xelopes MiningStoredData object and export to excel file
*
*/
package eti.bi.alphaminer.operation.result.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Core.OrdinalAttribute;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Input.MiningVector;
import eti.bi.alphaminer.operation.result.OperatorResult;
import eti.bi.alphaminer.operation.result.ResultView;
import eti.bi.alphaminer.operation.result.datamodel.MetaDataGridModel;
import eti.bi.alphaminer.operation.result.datamodel.SortingDataGridModel;
import eti.bi.alphaminer.operation.result.export.ExcelExporter;
import eti.bi.alphaminer.operation.result.renderer.DataCellRenderer;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
/**
* @author kelvinj
*
*/
public class DataView extends ResultView {
/**
*
*/
private static final long serialVersionUID = 3478038758570482114L;
private JTabbedPane m_DataSourceTabbedPane = new JTabbedPane(JTabbedPane.TOP);
private JScrollPane m_AttributeScrollPane = new JScrollPane();
private JScrollPane m_DataScrollPane = new JScrollPane();
private JTable m_AttributeTable = new JTable();
private JTable m_DataTable = new JTable();
private IBIData m_BIData;
public final static String TILTLE_EXPORT = Resource.srcStr("FileExport");
public final static String VIEW_NAME = Resource.srcStr("Data");
public DataView(IBIData a_BIData, OperatorResult a_OperatorResult)
{
super(a_OperatorResult);
m_ViewType = TYPE_DATA;
m_BIData = a_BIData;
if (m_BIData != null)
{
setName(a_BIData.getDataSourceName());
setViewName(a_BIData.getDataSourceName());
}else
{
setName("Data Source");
setViewName("Data Source");
}
add(m_DataSourceTabbedPane, BorderLayout.CENTER);
m_DataSourceTabbedPane.add(m_AttributeScrollPane, Resource.srcStr("Variables"));
m_DataSourceTabbedPane.add(m_DataScrollPane, Resource.srcStr("Data"));
//add(m_AttributeScrollPane, BorderLayout.SOUTH);
m_DataScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
m_AttributeScrollPane.getViewport().setBackground(Color.white);
m_AttributeScrollPane.getViewport().add(m_AttributeTable, null);
m_DataScrollPane.getViewport().setBackground(Color.white);
m_DataScrollPane.getViewport().add(m_DataTable, null);
//JTableHeader tableHeader = m_AttributeTable.getTableHeader();
//tableHeader = m_DataTable.getTableHeader();
//tableHeader.setReorderingAllowed(false);
// m_AttributeTable.setAutoscrolls(true);
m_AttributeTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// m_DataTable.setAutoscrolls(true);
m_DataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}
public void showData()
{
if (m_BIData==null)
{
return;
}
MiningStoredData aMiningStoredData = null;
MiningDataSpecification aMetaData = null;
String target = null;
aMiningStoredData = m_BIData.getMiningStoredData();
aMetaData = m_BIData.getMetaData();
MiningAttribute aAttr = m_BIData.getTargetAttribute();
if (aAttr!=null)
{
target = aAttr.getName();
}
if (aMetaData==null || aMiningStoredData ==null)
{
System.out.println("No output data available.");
return;
}
int m_Column = aMetaData.getAttributesNumber();
if (m_Column==0)
{
return;
}
MiningAttribute[] miningAttributes = aMetaData.getAttributesArray();
MiningAttribute attribute = null;
String[] metaDataTableHeader = { Resource.srcStr("m_AttributeLabel"), Resource.srcStr("TypeTableHeader"), Resource.srcStr("ValueRange"), Resource.srcStr("DataTypeTableHeader"), Resource.srcStr("DescriptionMessageTab")};
Object[][] metaDataTableContent;
String[] dataTableHeader;
Object[][] dataTableContent;
Class[] dataTableType;
dataTableType = new Class[m_Column+1];
dataTableHeader = new String[m_Column+1];
dataTableType[0] = Integer.class;
dataTableHeader[0] = Resource.srcStr("Data_Index");
Object[][] content = new Object[m_Column][metaDataTableHeader.length];
Object[] row = null;
int temp_index=1;
for (int i=0; i<m_Column; i++,temp_index++)
{
row = new Object[metaDataTableHeader.length];
attribute = miningAttributes[i];
dataTableHeader[temp_index] = attribute.getName();
row[0] = attribute.getName();
if (target!=null && target.equals(row[0]))
row[0] = (String) row[0] + "[Target]";
dataTableType[temp_index] = String.class;
if (attribute instanceof NumericAttribute)
{
row[1] = "Numeric";
row[2] = ((NumericAttribute) attribute).getInterval().toString();
int dataType = ((NumericAttribute) attribute).getDataType();
if (dataType == MiningAttribute.UNDEFINED) row[3] = "undefined";
else if (dataType == NumericAttribute.DOUBLE) {row[3] = "double"; dataTableType[temp_index] = Double.class;}
else if (dataType == NumericAttribute.FLOAT) {row[3] = "float"; dataTableType[temp_index] = Float.class;}
else if (dataType == NumericAttribute.INTEGER) {row[3] = "integer"; dataTableType[temp_index] = Integer.class;}
else if (dataType == NumericAttribute.BOOLEAN) {row[3] = "boolean"; dataTableType[temp_index] = Boolean.class;}
else if (dataType == NumericAttribute.DATETIME_PRUDSYS) row[3] = "datePrudsys";
String des = null;
if (((NumericAttribute) attribute).isCyclic())
des = "cyclic";
if (((NumericAttribute) attribute).isDiscrete())
des = (des==null?"":", ") + "discrete";
if (((NumericAttribute) attribute).isTime())
des = (des==null?"":", ") + "time";
row[4] = des;
}
else if (attribute instanceof CategoricalAttribute)
{
row[1] = "Categorical";
String cat = "";
ArrayList values = ((CategoricalAttribute) attribute).getValues();
CategoricalAttribute catAttribute = (CategoricalAttribute) attribute;
if (catAttribute.isUnstoredCategories())
{
cat = "[No value stored]";
}else
{
for( int j = 0; j < values.size(); j++ )
{
cat += values.get( j );
if (j<values.size()-1)
cat += ", ";
}
}
row[2] = cat;
int dataType = ((CategoricalAttribute) attribute).getDataType();
if (dataType == MiningAttribute.UNDEFINED) row[3] = "undefined";
else if (dataType == CategoricalAttribute.STRING) row[3] = "string";
else if (dataType == CategoricalAttribute.BOOLEAN) {row[3] = "boolean"; dataTableType[i] = Boolean.class;}
String des = null;
if (attribute instanceof OrdinalAttribute)
{
row[1] = (String) row[1] + " ordinal";
if (((OrdinalAttribute) attribute).isCyclic())
des = "cyclic";
}
if (((CategoricalAttribute) attribute).getTaxonomy()!= null)
des = (des==null?"taxonomy":des+", taxonomy");
if (((CategoricalAttribute) attribute).isUnboundedCategories())
des = (des==null?"unboundedCategories":des+", unboundedCategories");
if (((CategoricalAttribute) attribute).isUnstoredCategories())
des = (des==null?"unstoredCategories":des+", unstoredCategories");
row[4] = des;
}
content[i] = row;
}
metaDataTableContent = content;
ArrayList list = m_BIData.getMiningStoredData().getMiningVectors();
content = new Object[list.size()][m_Column+1];
MiningVector vec = null;
int j;
Object[] temp;
for (int i=0; i<list.size(); i++)
{
vec = (MiningVector) list.get(i);
temp = vec.toVector().toArray();
content[i][0] = i+1;
for(j=1;j<=m_Column;j++) {
content[i][j] = temp[j-1];
}
}
dataTableContent = content;
m_AttributeTable.setModel(new MetaDataGridModel(metaDataTableContent, metaDataTableHeader));
SortingDataGridModel tableModel = new SortingDataGridModel(dataTableContent, dataTableHeader, dataTableType);
m_DataTable.setModel(tableModel);
m_DataTable.setDefaultRenderer(String.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Short.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Long.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Integer.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Double.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Float.class, new DataCellRenderer(true));
tableModel.addMouseListenerToHeader(m_DataTable);
setColumnWidth(m_DataTable);
setColumnWidth(m_AttributeTable);
}
public void setColumnWidth(JTable a_Table) {
final int widthOffset = 20;
TableColumnModel tcm = a_Table.getColumnModel();
TableCellRenderer headerRenderer = a_Table.getTableHeader().getDefaultRenderer();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int count = tcm.getColumnCount();
for (int i = 0; i < count; i++) {
column = tcm.getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(
null, column.getHeaderValue(),
false, false, 0, 0);
headerWidth = comp.getPreferredSize().width + widthOffset;
column.setPreferredWidth(headerWidth);
}
tcm.getColumn(0).setCellRenderer(a_Table.getTableHeader().getDefaultRenderer());
}
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(TILTLE_EXPORT));
chooser.setFileFilter(ExcelExporter.FILTER);
chooser.setSelectedFile(ExcelExporter.DEFAULT_FILE);
chooser.setName(Resource.srcStr(TILTLE_EXPORT));
// 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();
// Create the excel exporter with the excel file extension enforced to be .xls
if (m_BIData!=null)
{
MiningStoredData aMiningStoredData = m_BIData.getMiningStoredData();
ExcelExporter aExporter = new ExcelExporter(aMiningStoredData, exportFile, true);
aExporter.export();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -