📄 attributeeditor.java
字号:
dataColumnVector.clear();
rowCount = 0;
model.fireTableStructureChanged();
}
public void readFile(File file) throws IOException {
int columnOffset = sourceList.size();
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++;
// TODO: Add data
// if (currentRow+FIRST_DATA_ROW >= model.getRowCount()) {
// model.addRow(new Object[model.getColumnCount()]);
// }
if (currentColumn >= numberOfNewColumns) {
addColumn(file, currentColumn);
numberOfNewColumns++;
valueTypes.add(new Integer(valueType));
} else {
int soFar = ((Integer)valueTypes.get(currentColumn)).intValue();
if (soFar != valueType) {
if ((soFar == Ontology.NOMINAL) || (valueType == Ontology.NOMINAL)) {
valueTypes.set(currentColumn, new Integer(Ontology.NOMINAL));
} else { // 1 real, 1 integer
valueTypes.set(currentColumn, new Integer(Ontology.REAL));
}
}
}
setDatum(currentRow, currentColumn+columnOffset, value);
currentColumn++;
break;
case StreamTokenizer.TT_EOL:
currentColumn = 0;
break;
case StreamTokenizer.TT_EOF:
break;
}
}
for (int i = 0; i < valueTypes.size(); i++) {
getDataSource(i+columnOffset).getAttribute().setValueType(((Integer)valueTypes.get(i)).intValue());
}
model.fireTableStructureChanged();
}
private void autoSetValueType(int column) {
int valueType = Ontology.INTEGER;
AttributeDataSource source = getDataSource(column);
for (int i = 0; i < getNumberOfDataRows(); i++) {
String value = getDatum(i, column);
if ((value != null) && (!value.equals("?"))) {
try {
double d = Double.parseDouble(value.toString());
if ((valueType == Ontology.INTEGER) && ((int)d != d)) { // 1 real => real
valueType = Ontology.REAL;
}
} catch (NumberFormatException e) {
valueType = Ontology.NOMINAL;
break;
}
}
}
source.getAttribute().setValueType(valueType);
model.fireTableCellUpdated(VALUE_TYPE_ROW, column);
}
public boolean isCellEditable(int row, int col) {
return true;
}
public TableCellEditor getCellEditor(int row, int column) {
if (row >= NUM_OF_HEADER_ROWS) {
return super.getCellEditor(row, column);
} else {
return (TableCellEditor)cellEditors[row].get(column);
}
}
public TableCellRenderer getCellRenderer(int row, int column) {
if (row >= NUM_OF_HEADER_ROWS) {
return dataRenderer;
} else {
return (TableCellRenderer)cellRenderers[row].get(column);
}
}
private boolean checkData(Object value, int row, int column) {
return true;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
createPopupMenu(columnAtPoint(e.getPoint())).show(this,e.getX(), e.getY());
}
}
public JPopupMenu createPopupMenu(final int column) {
JPopupMenu menu = new JPopupMenu();
menu.add(GUESS_TYPE_ACTION);
menu.add(REMOVE_COLUMN_ACTION);
return menu;
}
public void removeColumn(int column) {
sourceList.remove(column);
dataColumnVector.removeElementAt(column);
model.fireTableStructureChanged();
}
private void ensureAttributeTypeIsUnique(String type) {
List columns = new LinkedList();
List columnNumbers = new LinkedList();
Iterator i = sourceList.iterator();
int j = 0;
while (i.hasNext()) {
AttributeDataSource source = (AttributeDataSource)i.next();
if ((source.getType() != null) &&
source.getType().equals(type)) {
columns.add(source);
columnNumbers.add(new Integer(j));
}
j++;
}
if (columns.size() > 1) {
String[] names = new String[columns.size()];
i = columns.iterator();
j = 0;
while (i.hasNext()) {
names[j++] = ((AttributeDataSource)i.next()).getAttribute().getName();
}
javax.swing.JTextArea message = new javax.swing.JTextArea("The special attribute "+type+" is multiply defined. Please select one of the data columns (others will be changed to regular attributes). Press \"Cancel\" to ignore.",4,40);
message.setEditable(false);
message.setLineWrap(true);
message.setWrapStyleWord(true);
message.setBackground(new javax.swing.JLabel("").getBackground());
String selection = (String)JOptionPane.showInputDialog(this,
message,
type + " multiply defined",
JOptionPane.WARNING_MESSAGE, null,
names,
names[0]);
if (selection != null) {
i = columns.iterator();
Iterator k = columnNumbers.iterator();
while (i.hasNext()) {
AttributeDataSource source = (AttributeDataSource)i.next();
Integer number = (Integer)k.next();
if (!source.getAttribute().getName().equals(selection)) {
source.setType("attribute");
model.fireTableCellUpdated(TYPE_ROW, number.intValue());
}
}
}
}
}
public void writeXML(PrintWriter out) throws IOException {
if (sourceList.size() == 0) return;
File defaultSource = getDataSource(0).getFile();
out.println("<attributeset default_source=\""+defaultSource.getAbsolutePath()+"\">");
Iterator i = sourceList.iterator();
while (i.hasNext()) {
((AttributeDataSource)i.next()).writeXML(out, defaultSource);
}
out.println("</attributeset>");
}
public void writeData(File file) throws IOException {
PrintWriter out = new PrintWriter(new FileWriter(file));
for (int i = 0; i < sourceList.size(); i++) {
AttributeDataSource source = (AttributeDataSource)sourceList.get(i);
source.setSource(file, i);
}
for (int row = 0; row < getNumberOfDataRows(); row++) {
for (int col = 0; col < sourceList.size(); col++) {
if (col != 0) out.print("\t");
out.print(getDatum(row, col));
}
out.println();
}
out.close();
}
public void openAttributeFile() {
File file = SwingTools.chooseFile(this, null, true);
if (file != null) {
openAttributeFile(file);
}
}
public void openAttributeFile(File file) {
AttributeDataSources attributeDataSources = null;
try {
attributeDataSources = AttributeDataSource.createAttributeDataSources(file, true);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Could not open '"+file+"':\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
return;
}
this.file = file;
clear();
DataRowReader reader = null;
try {
reader = new FileDataRowReader(new DataRowFactory(DataRowFactory.TYPE_DOUBLE_ARRAY),
attributeDataSources.getDataSources(),
-1,
exampleSource.getParameterAsString("separator_chars").toCharArray(),
exampleSource.getParameterAsString("comment_chars").toCharArray(),
exampleSource.getParameterAsString("ignore_chars").toCharArray());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Cannot open data file: "+e, "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
sourceList.addAll(attributeDataSources.getDataSources());
for (int j = 0; j < attributeDataSources.getDataSources().size(); j++)
createNewColumn();
ExampleTable table = new MemoryExampleTable(new AttributeSet(attributeDataSources).getAllAttributes(), reader);
ExampleReader e = table.createCompleteExampleSet(null, null, null, null).getExampleReader();
int row = 0;
while (e.hasNext()) {
Example example = e.next();
Iterator adsIterator = sourceList.iterator();
int n = 0;
while (adsIterator.hasNext()) {
AttributeDataSource ads = (AttributeDataSource)adsIterator.next();
setDatum(row, n++, example.getValueAsString(ads.getAttribute()));
}
row++;
}
model.fireTableStructureChanged();
}
public void saveAttributeFile() {
for (int i = 1; i < AttributeDataSource.KNOWN_TYPES.length; i++)
ensureAttributeTypeIsUnique(AttributeDataSource.KNOWN_TYPES[i]);
File file = SwingTools.chooseFile(this, null, false);
if (file != null) {
this.file = file;
try {
PrintWriter out = new PrintWriter(new FileWriter(file));
writeXML(out);
out.close();
} catch (java.io.IOException e) {
JOptionPane.showMessageDialog(this, e.toString(), "Error saving attribute file "+file, JOptionPane.ERROR_MESSAGE);
}
}
}
public File getFile() {
return file;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -