📄 attributeeditor.java.old
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * 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.table.DefaultTableModel;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.List;import java.util.Set;import java.util.HashSet;import java.util.Iterator;/** 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. */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 int column = -1; private RemoveColumnAction() { super("Remove column"); putValue(SHORT_DESCRIPTION, "Remove the current column"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_R)); } public void setColumn(int column) { this.column = column; } public void actionPerformed(ActionEvent e) { removeColumn(column); column = -1; } } private RemoveColumnAction REMOVE_COLUMN = new RemoveColumnAction(); 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 FIRST_DATA_ROW = 5; private ArrayList[] cellEditors; private ArrayList[] cellRenderers; private TableCellRenderer dataRenderer = new DataCellRenderer(); { cellEditors = new ArrayList[FIRST_DATA_ROW]; cellRenderers = new ArrayList[FIRST_DATA_ROW]; for (int i = 0; i < FIRST_DATA_ROW; i++) { cellEditors[i] = new ArrayList(); cellRenderers[i] = new ArrayList(); } } private ArrayList sourceList = new ArrayList(); private File file; private Operator exampleSource; public AttributeEditor(Operator exampleSource) { super(0,0); this.exampleSource = exampleSource; setRowHeight(getRowHeight()+4); getTableHeader().setReorderingAllowed(false); setAutoResizeMode(AUTO_RESIZE_OFF); //setPreferredScrollableViewportSize(new java.awt.Dimension(600,400)); addMouseListener(this); DefaultTableModel model = (DefaultTableModel)getModel(); for (int i = 0; i < FIRST_DATA_ROW; i++) { model.addRow(new Object[0]); } } private void addColumn(DefaultTableModel model, AttributeDataSource source) { final int col = model.getColumnCount(); Attribute attribute = source.getAttribute(); model.addColumn(source.getFile().getName()+" ("+(source.getColumn()+1)+")"); model.setValueAt(source.getType(), TYPE_ROW, col); model.setValueAt(attribute.getName(), NAME_ROW, col); String unitString = attribute.unitToString(); if (unitString.length() == 0) unitString = "[unit]"; model.setValueAt(unitString, UNIT_ROW, col); model.setValueAt(Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(attribute.getValueType()), VALUE_TYPE_ROW, col); model.setValueAt(Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(attribute.getBlockType()), BLOCK_TYPE_ROW, col); 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))); } } 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"); addColumn((DefaultTableModel)getModel(), source); sourceList.add(source); getColumnModel().getColumn(getColumnCount()-1).setMinWidth(COLUMN_WIDTH); } protected void clear() { sourceList.clear(); updateModel(); } public void setModel(TableModel model) { super.setModel(model); for (int i = 0; i < getColumnCount(); i++) { getColumnModel().getColumn(i).setMinWidth(COLUMN_WIDTH); } } public void readFile(File file, boolean series) throws IOException { DefaultTableModel model = (DefaultTableModel)getModel(); int columnOffset = model.getColumnCount(); int numberOfNewColumns = 0; int currentColumn = 0; int currentRow = -1; StreamTokenizer tokenizer = FileDataRowReader.makeTokenizer(file, exampleSource.getParameterAsString("separator_chars").toCharArray(), exampleSource.getParameterAsString("comment_chars").toCharArray(), exampleSource.getParameterAsString("ignore_chars").toCharArray()); ArrayList valueTypes = new ArrayList(); tokenizer.eolIsSignificant(true); while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) { String value = null; int valueType = Ontology.INTEGER; switch (tokenizer.ttype) { case StreamTokenizer.TT_WORD: value = tokenizer.sval.trim(); if (!value.equals("?")) { try { double d = Double.parseDouble(value); if (d == (int)d) { valueType = Ontology.INTEGER; } else { valueType = Ontology.REAL; } } catch (NumberFormatException e) { valueType = Ontology.NOMINAL; } } if (currentColumn == 0) currentRow++; if (currentRow+FIRST_DATA_ROW >= model.getRowCount()) { model.addRow(new Object[model.getColumnCount()]); } if (currentColumn >= numberOfNewColumns) { addColumn(file, currentColumn); numberOfNewColumns++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -