📄 attributeeditor.java.old
字号:
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)); } } } model.setValueAt(value, currentRow+FIRST_DATA_ROW, currentColumn+columnOffset); currentColumn++; break; case StreamTokenizer.TT_EOL: currentColumn = 0; break; case StreamTokenizer.TT_EOF: break; } } for (int i = 0; i < valueTypes.size(); i++) { model.setValueAt(Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(((Integer)valueTypes.get(i)).intValue()), VALUE_TYPE_ROW, i+columnOffset); } if (series) { model.setValueAt(Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(Ontology.VALUE_SERIES_START), BLOCK_TYPE_ROW, columnOffset); model.setValueAt(Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(Ontology.VALUE_SERIES_END), BLOCK_TYPE_ROW, columnOffset+numberOfNewColumns-1); for (int i = 1; i < numberOfNewColumns-1; i++) { model.setValueAt(Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(Ontology.VALUE_SERIES), BLOCK_TYPE_ROW, columnOffset+i); } } } private void autoSetValueType(int column) { int valueType = Ontology.INTEGER; DefaultTableModel model = (DefaultTableModel)getModel(); for (int i = FIRST_DATA_ROW; i < model.getRowCount(); i++) { Object value = model.getValueAt(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) { model.setValueAt(Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(Ontology.NOMINAL), VALUE_TYPE_ROW, column); return; // 1 nominal => exit } } } model.setValueAt(Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(valueType), VALUE_TYPE_ROW, column); } public boolean isCellEditable(int row, int col) { return true; } public TableCellEditor getCellEditor(int row, int column) { if (row >= FIRST_DATA_ROW) { return super.getCellEditor(row, column); } else { return (TableCellEditor)cellEditors[row].get(column); } } public TableCellRenderer getCellRenderer(int row, int column) { if (row >= FIRST_DATA_ROW) { 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) { TableColumnModel cm = getColumnModel(); cm.removeColumn(cm.getColumn(column)); sourceList.remove(column); } private void setAttributeData() { DefaultTableModel model = (DefaultTableModel)getModel(); TableColumnModel cm = getColumnModel(); for (int j = 0; j < cm.getColumnCount(); j++) { int column = cm.getColumn(j).getModelIndex(); AttributeDataSource source = (AttributeDataSource)sourceList.get(column); Attribute attribute = source.getAttribute(); attribute.setName(model.getValueAt(NAME_ROW, column).toString()); source.setType(model.getValueAt(TYPE_ROW, column).toString()); attribute.setValueType(Ontology.ATTRIBUTE_VALUE_TYPE.mapName(model.getValueAt(VALUE_TYPE_ROW, column).toString())); attribute.setBlockType(Ontology.ATTRIBUTE_BLOCK_TYPE.mapName(model.getValueAt(BLOCK_TYPE_ROW, column).toString())); attribute.parseUnits(model.getValueAt(UNIT_ROW, column).toString()); if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.NOMINAL)) { for (int i = FIRST_DATA_ROW; i < model.getRowCount(); i++) { Object value = model.getValueAt(i, column); if ((value != null) && (!value.equals("?"))) attribute.mapString(value.toString()); } } } } public void writeXML(PrintWriter out) throws IOException { if (sourceList.size() == 0) return; setAttributeData(); File defaultSource = ((AttributeDataSource)sourceList.get(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); } DefaultTableModel model = (DefaultTableModel)getModel(); for (int row = FIRST_DATA_ROW; row < model.getRowCount(); row++) { for (int col = 0; col < model.getColumnCount(); col++) { if (col != 0) out.print("\t"); out.print(model.getValueAt(row, col)); } out.println(); } out.close(); } private void updateModel() { DefaultTableModel model = new DefaultTableModel(FIRST_DATA_ROW, 0); for (int i = 0; i < cellEditors.length; i++) { cellEditors[i].clear(); cellRenderers[i].clear(); } Iterator i = sourceList.iterator(); while (i.hasNext()) { AttributeDataSource source = (AttributeDataSource)i.next(); addColumn(model, source); } setModel(model); } 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()); updateModel(); DefaultTableModel model = (DefaultTableModel)getModel(); ExampleTable table = new MemoryExampleTable(new AttributeSet(attributeDataSources).getAllAttributes(), reader); ExampleReader e = table.createCompleteExampleSet(null, null, null, null).getExampleReader(); while (e.hasNext()) { Example example = e.next(); Object[] row = new Object[example.getNumberOfAttributes()]; for (int i = 0; i < example.getNumberOfAttributes(); i++) { row[i] = example.getValueAsString(i); } model.addRow(row); } } 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 + -