📄 arfftablemodel.java
字号:
/** * returns the most specific superclass for all the cell values in the * column (always String) * * @param columnIndex the column index * @return the class of the column */ public Class getColumnClass(int columnIndex) { Class result; result = null; if ( (columnIndex >= 0) && (columnIndex < getColumnCount()) ) { if (columnIndex == 0) result = Integer.class; else if (getType(columnIndex) == Attribute.NUMERIC) result = Double.class; else result = String.class; // otherwise no input of "?"!!! } return result; } /** * returns the number of columns in the model * * @return the number of columns */ public int getColumnCount() { int result; result = 1; if (m_Data != null) result += m_Data.numAttributes(); return result; } /** * checks whether the column represents the class or not * * @param columnIndex the index of the column * @return true if the column is the class attribute */ private boolean isClassIndex(int columnIndex) { boolean result; int index; index = m_Data.classIndex(); result = ((index == - 1) && (m_Data.numAttributes() == columnIndex)) || (index == columnIndex - 1); return result; } /** * returns the name of the column at columnIndex * * @param columnIndex the index of the column * @return the name of the column */ public String getColumnName(int columnIndex) { String result; result = ""; if ( (columnIndex >= 0) && (columnIndex < getColumnCount()) ) { if (columnIndex == 0) { result = "<html><center>No.<br><font size=\"-2\"> </font></center></html>"; } else { if (m_Data != null) { if ( (columnIndex - 1 < m_Data.numAttributes()) ) { result = "<html><center>"; // name if (isClassIndex(columnIndex)) result += "<b>" + m_Data.attribute(columnIndex - 1).name() + "</b>"; else result += m_Data.attribute(columnIndex - 1).name(); // attribute type switch (getType(columnIndex)) { case Attribute.DATE: result += "<br><font size=\"-2\">Date</font>"; break; case Attribute.NOMINAL: result += "<br><font size=\"-2\">Nominal</font>"; break; case Attribute.STRING: result += "<br><font size=\"-2\">String</font>"; break; case Attribute.NUMERIC: result += "<br><font size=\"-2\">Numeric</font>"; break; case Attribute.RELATIONAL: result += "<br><font size=\"-2\">Relational</font>"; break; default: result += "<br><font size=\"-2\">???</font>"; } result += "</center></html>"; } } } } return result; } /** * returns the number of rows in the model * * @return the number of rows */ public int getRowCount() { if (m_Data == null) return 0; else return m_Data.numInstances(); } /** * checks whether the value at the given position is missing * * @param rowIndex the row index * @param columnIndex the column index * @return true if the value at the position is missing */ public boolean isMissingAt(int rowIndex, int columnIndex) { boolean result; result = false; if ( (rowIndex >= 0) && (rowIndex < getRowCount()) && (columnIndex > 0) && (columnIndex < getColumnCount()) ) result = (m_Data.instance(rowIndex).isMissing(columnIndex - 1)); return result; } /** * returns the double value of the underlying Instances object at the * given position, -1 if out of bounds * * @param rowIndex the row index * @param columnIndex the column index * @return the underlying value in the Instances object */ public double getInstancesValueAt(int rowIndex, int columnIndex) { double result; result = -1; if ( (rowIndex >= 0) && (rowIndex < getRowCount()) && (columnIndex > 0) && (columnIndex < getColumnCount()) ) result = m_Data.instance(rowIndex).value(columnIndex - 1); return result; } /** * returns the value for the cell at columnindex and rowIndex * * @param rowIndex the row index * @param columnIndex the column index * @return the value at the position */ public Object getValueAt(int rowIndex, int columnIndex) { Object result; String tmp; result = null; if ( (rowIndex >= 0) && (rowIndex < getRowCount()) && (columnIndex >= 0) && (columnIndex < getColumnCount()) ) { if (columnIndex == 0) { result = new Integer(rowIndex + 1); } else { if (isMissingAt(rowIndex, columnIndex)) { result = null; } else { switch (getType(columnIndex)) { case Attribute.DATE: case Attribute.NOMINAL: case Attribute.STRING: case Attribute.RELATIONAL: result = m_Data.instance(rowIndex).stringValue(columnIndex - 1); break; case Attribute.NUMERIC: result = new Double(m_Data.instance(rowIndex).value(columnIndex - 1)); break; default: result = "-can't display-"; } } } } if (getType(columnIndex) != Attribute.NUMERIC) { if (result != null) { // does it contain "\n" or "\r"? -> replace with red html tag tmp = result.toString(); if ( (tmp.indexOf("\n") > -1) || (tmp.indexOf("\r") > -1) ) { tmp = tmp.replaceAll("\\r\\n", "<font color=\"red\"><b>\\\\r\\\\n</b></font>"); tmp = tmp.replaceAll("\\r", "<font color=\"red\"><b>\\\\r</b></font>"); tmp = tmp.replaceAll("\\n", "<font color=\"red\"><b>\\\\n</b></font>"); result = "<html>" + tmp + "</html>"; } } } return result; } /** * returns true if the cell at rowindex and columnindexis editable * * @param rowIndex the index of the row * @param columnIndex the index of the column * @return true if the cell is editable */ public boolean isCellEditable(int rowIndex, int columnIndex) { return (columnIndex > 0) && !isReadOnly(); } /** * sets the value in the cell at columnIndex and rowIndex to aValue. * but only the value and the value can be changed * * @param aValue the new value * @param rowIndex the row index * @param columnIndex the column index */ public void setValueAt(Object aValue, int rowIndex, int columnIndex) { setValueAt(aValue, rowIndex, columnIndex, true); } /** * sets the value in the cell at columnIndex and rowIndex to aValue. * but only the value and the value can be changed * * @param aValue the new value * @param rowIndex the row index * @param columnIndex the column index * @param notify whether to notify the listeners */ public void setValueAt(Object aValue, int rowIndex, int columnIndex, boolean notify) { int type; int index; String tmp; Instance inst; Attribute att; Object oldValue; if (!m_IgnoreChanges) addUndoPoint(); oldValue = getValueAt(rowIndex, columnIndex); type = getType(rowIndex, columnIndex); index = columnIndex - 1; inst = m_Data.instance(rowIndex); att = inst.attribute(index); // missing? if (aValue == null) { inst.setValue(index, Instance.missingValue()); } else { tmp = aValue.toString(); switch (type) { case Attribute.DATE: try { att.parseDate(tmp); inst.setValue(index, att.parseDate(tmp)); } catch (Exception e) { // ignore } break; case Attribute.NOMINAL: if (att.indexOfValue(tmp) > -1) inst.setValue(index, att.indexOfValue(tmp)); break; case Attribute.STRING: inst.setValue(index, tmp); break; case Attribute.NUMERIC: try { Double.parseDouble(tmp); inst.setValue(index, Double.parseDouble(tmp)); } catch (Exception e) { // ignore } break; case Attribute.RELATIONAL: try { inst.setValue(index, inst.attribute(index).addRelation((Instances) aValue)); } catch (Exception e) { // ignore } break; default: throw new IllegalArgumentException("Unsupported Attribute type: " + type + "!"); } } // notify only if the value has changed! if (notify && (!("" + oldValue).equals("" + aValue)) ) notifyListener(new TableModelEvent(this, rowIndex, columnIndex)); } /** * adds a listener to the list that is notified each time a change to data * model occurs * * @param l the listener to add */ public void addTableModelListener(TableModelListener l) { m_Listeners.add(l); } /** * removes a listener from the list that is notified each time a change to * the data model occurs * * @param l the listener to remove */ public void removeTableModelListener(TableModelListener l) { m_Listeners.remove(l); } /** * notfies all listener of the change of the model * * @param e the event to send to the listeners */ public void notifyListener(TableModelEvent e) { Iterator iter; TableModelListener l; // is notification enabled? if (!isNotificationEnabled()) return; iter = m_Listeners.iterator(); while (iter.hasNext()) { l = (TableModelListener) iter.next(); l.tableChanged(e); } } /** * removes the undo history */ public void clearUndo() { m_UndoList = new Vector(); } /** * returns whether an undo is possible, i.e. whether there are any undo points * saved so far * * @return returns TRUE if there is an undo possible */ public boolean canUndo() { return !m_UndoList.isEmpty(); } /** * undoes the last action */ public void undo() { File tempFile; Instances inst; ObjectInputStream ooi; if (canUndo()) { // load file tempFile = (File) m_UndoList.get(m_UndoList.size() - 1); try { // read serialized data ooi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(tempFile))); inst = (Instances) ooi.readObject(); ooi.close(); // set instances setInstances(inst); notifyListener(new TableModelEvent(this, TableModelEvent.HEADER_ROW)); notifyListener(new TableModelEvent(this)); } catch (Exception e) { e.printStackTrace(); } tempFile.delete(); // remove from undo m_UndoList.remove(m_UndoList.size() - 1); } } /** * adds an undo point to the undo history, if the undo support is enabled * @see #isUndoEnabled() * @see #setUndoEnabled(boolean) */ public void addUndoPoint() { File tempFile; ObjectOutputStream oos; // undo support currently on? if (!isUndoEnabled()) return; if (getInstances() != null) { try { // temp. filename tempFile = File.createTempFile("arffviewer", null); tempFile.deleteOnExit(); // serialize instances oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(tempFile))); oos.writeObject(getInstances()); oos.flush(); oos.close(); // add to undo list m_UndoList.add(tempFile); } catch (Exception e) { e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -