📄 attributeeditor.java
字号:
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(); } 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(); JMenuItem autoType = new JMenuItem("Guess type"); autoType.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { autoSetValueType(column); } }); menu.add(autoType); REMOVE_COLUMN.setColumn(column); menu.add(REMOVE_COLUMN); return menu; } public void removeColumn(int column) { sourceList.remove(column); dataColumnVector.removeElementAt(column); model.fireTableStructureChanged(); } 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; sourceList.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(); for (int i = 0; i < example.getNumberOfAttributes(); i++) { setDatum(row, i, example.getValueAsString(i)); } row++; } model.fireTableStructureChanged(); } public void saveAttributeFile() { 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 + -