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

📄 attributeeditor.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  YALE - Yet Another Learning Environment
 *  Copyright (C) 2001-2004
 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, 
 *          Katharina Morik, Oliver Ritthoff
 *      Artificial Intelligence Unit
 *      Computer Science Department
 *      University of Dortmund
 *      44221 Dortmund,  Germany
 *  email: yale-team@lists.sourceforge.net
 *  web:   http://yale.cs.uni-dortmund.de/
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */
package edu.udo.cs.yale.gui;

import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.DataRowReader;
import edu.udo.cs.yale.example.FileDataRowReader;
import edu.udo.cs.yale.example.DataRowFactory;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.example.ExampleTable;
import edu.udo.cs.yale.example.MemoryExampleTable;
import edu.udo.cs.yale.tools.att.AttributeSet;
import edu.udo.cs.yale.tools.att.AttributeDataSource;
import edu.udo.cs.yale.tools.att.AttributeDataSources;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultCellEditor;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.io.File;
import java.io.FileReader;
import java.io.StreamTokenizer;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;

/** A table for creating an attribute description file. 
 *  Data can be read from files as single columns or as a value series. The value types are guessed
 *  and can be edited by the user. 
 *
 *  @version $Id: AttributeEditor.java,v 2.13 2004/08/27 11:57:33 ingomierswa Exp $
 */
public class AttributeEditor extends JTable implements MouseListener {
    
    private class DataCellRenderer extends DefaultTableCellRenderer {
	public Component getTableCellRendererComponent(JTable table, 
						       Object value, 
						       boolean isSelected, boolean hasFocus, 
						       int row, int column) {
	    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
	    if (!checkData(value, row, column)) {
		c.setBackground(java.awt.Color.red);
	    } else {
		c.setBackground(java.awt.Color.white);
	    }
	    return c;
	}
    }

    private class RemoveColumnAction extends AbstractAction {
	private RemoveColumnAction() { 
	    super("Remove column"); 
   	    putValue(SHORT_DESCRIPTION, "Remove the current column");
	    putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_R));
	}
	public void actionPerformed(ActionEvent e) {
	    int column = getSelectedColumn();
	    if (column != -1)
		removeColumn(column);
	}
    }

    private class GuessTypeAction extends AbstractAction {
	private int column = -1;
	private GuessTypeAction() { 
	    super("Guess value type"); 
   	    putValue(SHORT_DESCRIPTION, "Guess the value type of the current column based on the data.");
	    putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_G));
	}
	public void actionPerformed(ActionEvent e) {
	    int column = getSelectedColumn();
	    if (column != -1)
		autoSetValueType(column);
	}
    }

    protected Action REMOVE_COLUMN_ACTION = new RemoveColumnAction();
    protected Action GUESS_TYPE_ACTION = new GuessTypeAction();

    private static final int COLUMN_WIDTH = 100;

    private static final int NAME_ROW           = 0;
    private static final int TYPE_ROW           = 1;
    private static final int UNIT_ROW           = 2;
    private static final int VALUE_TYPE_ROW     = 3;
    private static final int BLOCK_TYPE_ROW     = 4;
    private static final int NUM_OF_HEADER_ROWS = 5;


    private class AttributeTableModel extends AbstractTableModel {
	public int getColumnCount() {
	    return sourceList.size();
	}
	public int getRowCount() {
	    return getNumberOfDataRows() + NUM_OF_HEADER_ROWS;
	}
	public String getColumnName(int column) {
	    AttributeDataSource source = getDataSource(column);
	    return source.getFile().getName()+" ("+(source.getColumn()+1)+")";
	}
	public Object getValueAt(int row, int column) {
	    if (row < NUM_OF_HEADER_ROWS) {
		AttributeDataSource source = getDataSource(column);
		switch (row) {
		case TYPE_ROW: return source.getType();
		case NAME_ROW: return source.getAttribute().getName();
		case UNIT_ROW:
		    String unitString = source.getAttribute().unitToString();
		    if (unitString.length() == 0) return "[unit]";
		    else return unitString;
		case VALUE_TYPE_ROW:
		    return Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(source.getAttribute().getValueType());
		case BLOCK_TYPE_ROW:
		    return Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(source.getAttribute().getBlockType());
		default:
		    return "This cannot happen!";
		}
	    } else {
		return getDatum(row - NUM_OF_HEADER_ROWS, column);
	    }
	}
	public void setValueAt(Object value, int row, int column) {
	    if (row < NUM_OF_HEADER_ROWS) {
		AttributeDataSource source = getDataSource(column);
		switch (row) {
		case TYPE_ROW: 
		    source.setType((String)value); 
		    break;
		case NAME_ROW: source.getAttribute().setName((String)value); break;
		case UNIT_ROW:
		    if (!value.equals("[unit]")) {
			source.getAttribute().parseUnits((String)value);
		    }
		    break;
		case VALUE_TYPE_ROW:
		    source.getAttribute().setValueType(Ontology.ATTRIBUTE_VALUE_TYPE.mapName((String)value));
		    break;
		case BLOCK_TYPE_ROW:
		    source.getAttribute().setBlockType(Ontology.ATTRIBUTE_BLOCK_TYPE.mapName((String)value));
		    break;
		}
	    } else {
		setDatum(row-NUM_OF_HEADER_ROWS, column, (String)value);
	    }
	}
    }
        
    private ArrayList[] cellEditors;
    private ArrayList[] cellRenderers;
    private TableCellRenderer dataRenderer = new DataCellRenderer();
    {
	cellEditors   = new ArrayList[NUM_OF_HEADER_ROWS];
	cellRenderers = new ArrayList[NUM_OF_HEADER_ROWS];
	for (int i = 0; i < NUM_OF_HEADER_ROWS; i++) {
	    cellEditors[i]   = new ArrayList();
	    cellRenderers[i] = new ArrayList();
	}
    }

    private ArrayList sourceList = new ArrayList();

    private File file;    
    private Operator exampleSource;
    private AttributeTableModel model;
    private int rowCount = 0;

    private Vector dataColumnVector = new Vector();

    public AttributeEditor(Operator exampleSource) {
	super();
	setModel(model = new AttributeTableModel());
	this.exampleSource = exampleSource;
	setRowHeight(getRowHeight()+4);
	getTableHeader().setReorderingAllowed(false);
	setAutoResizeMode(AUTO_RESIZE_OFF);
	addMouseListener(this);
    }

    private AttributeDataSource getDataSource(int i) {
	return (AttributeDataSource)sourceList.get(i);
    }

    private String getDatum(int row, int column) {
	Vector col = (Vector)dataColumnVector.get(column);
	if (row >= col.size()) return "?";
	return (String)col.get(row);
    }

    private void setDatum(int row, int column, String value) {
	Vector col = (Vector)dataColumnVector.get(column);
	if (row >= col.size()) {
	    col.addElement(value);
	    if (row >= rowCount) rowCount = row+1;
	} else {
	    col.setElementAt(value, row);
	}
    }

    private int getNumberOfDataRows() { 
  	String max = System.getProperty("yale.gui.attributeeditor.rowlimit");
  	if (max != null) {
	    int rowLimit = Integer.MAX_VALUE;
  	    try {
  		rowLimit = Integer.parseInt(max);
  		if (rowLimit == -1) return rowCount;
		else if (rowCount <= rowLimit) return rowCount;
		else return rowLimit;
  	    } catch (NumberFormatException e) {
  		System.err.println("Value of yale.gui.attributeeditor.rowlimit must be an integer!");
		return rowCount;
  	    }
  	} else {
	    return rowCount;
	}
    }

    private void createNewColumn() {
	cellEditors[NAME_ROW].add(new DefaultCellEditor(new JTextField()));
	JComboBox typeBox = new JComboBox(AttributeDataSource.KNOWN_TYPES);
	typeBox.setEditable(true);
	cellEditors[TYPE_ROW].add(new DefaultCellEditor(typeBox));
	cellEditors[UNIT_ROW].add(new DefaultCellEditor(new JTextField()));
	cellEditors[VALUE_TYPE_ROW].add(new DefaultCellEditor(new JComboBox(Ontology.ATTRIBUTE_VALUE_TYPE.getNames())));
	cellEditors[BLOCK_TYPE_ROW].add(new DefaultCellEditor(new JComboBox(Ontology.ATTRIBUTE_BLOCK_TYPE.getNames())));
	for (int i = 0; i < cellRenderers.length; i++) {
	    cellRenderers[i].add(new EditorCellRenderer((TableCellEditor)cellEditors[i].get(cellEditors[i].size()-1)));
	}
	dataColumnVector.add(new Vector());
    }

    private void addColumn(File file, int index) {
	String name = file.getName()+" ("+(index+1)+")";
	AttributeDataSource source 
	    = new AttributeDataSource(new Attribute(name, Ontology.VALUE_TYPE, 
						    Ontology.SINGLE_VALUE, 
						    Attribute.UNDEFINED_BLOCK_NR, 
						    null), 
				      file, 
				      index,
				      "attribute");
	createNewColumn();
	sourceList.add(source);
    }

    protected void clear() {
	sourceList.clear();

⌨️ 快捷键说明

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