arffpanel.java

来自「Weka」· Java 代码 · 共 1,037 行 · 第 1/2 页

JAVA
1,037
字号
  }    /**   * sets the relation name   */  private void createName() {    ArffSortedTableModel         model;        model = (ArffSortedTableModel) m_TableArff.getModel();    if ((model != null) && (model.getInstances() != null))      m_LabelName.setText("Relation: " + model.getInstances().relationName());    else      m_LabelName.setText("");  }    /**   * loads the specified file into the table   *    * @param filename		the file to load   */  private void loadFile(String filename) {    ArffSortedTableModel         model;        this.m_Filename = filename;        createTitle();        if (filename.equals(""))         model = null;    else      model = new ArffSortedTableModel(filename);        m_TableArff.setModel(model);    setChanged(false);    createName();  }    /**   * calculates the mean of the given numeric column   */  private void calcMean() {    ArffSortedTableModel   model;    int               i;    double            mean;        // no column selected?    if (m_CurrentCol == -1)      return;        model = (ArffSortedTableModel) m_TableArff.getModel();        // not numeric?    if (!model.getAttributeAt(m_CurrentCol).isNumeric())      return;        mean = 0;    for (i = 0; i < model.getRowCount(); i++)      mean += model.getInstances().instance(i).value(m_CurrentCol - 1);    mean = mean / model.getRowCount();        // show result    ComponentHelper.showMessageBox(        getParent(),         "Mean for attribute...",        "Mean for attribute '"         + m_TableArff.getPlainColumnName(m_CurrentCol)         + "':\n\t" + Utils.doubleToString(mean, 3),        JOptionPane.OK_CANCEL_OPTION,        JOptionPane.PLAIN_MESSAGE);  }    /**   * sets the specified values in a column to a new value   *    * @param o		the menu item   */  private void setValues(Object o) {    String                     msg;    String                     title;    String                     value;    String                     valueNew;    int                        i;    ArffSortedTableModel      model;        value    = "";    valueNew = "";        if (o == menuItemSetMissingValues) {      title = "Replace missing values...";       msg   = "New value for MISSING values";    }    else if (o == menuItemSetAllValues) {      title = "Set all values...";       msg   = "New value for ALL values";    }    else if (o == menuItemReplaceValues) {      title = "Replace values...";       msg   = "Old value";    }    else      return;        value = ComponentHelper.showInputBox(m_TableArff.getParent(), title, msg, m_LastSearch);        // cancelled?    if (value == null)      return;    m_LastSearch = value;        // replacement    if (o == menuItemReplaceValues) {      valueNew = ComponentHelper.showInputBox(m_TableArff.getParent(), title, "New value", m_LastReplace);      if (valueNew == null)        return;      m_LastReplace = valueNew;    }        model = (ArffSortedTableModel) m_TableArff.getModel();    model.setNotificationEnabled(false);    // undo    addUndoPoint();    model.setUndoEnabled(false);        // set value    for (i = 0; i < m_TableArff.getRowCount(); i++) {      if (o == menuItemSetAllValues)        model.setValueAt(value, i, m_CurrentCol);      else        if ( (o == menuItemSetMissingValues)             && model.isMissingAt(i, m_CurrentCol) )          model.setValueAt(value, i, m_CurrentCol);        else if ( (o == menuItemReplaceValues)             && model.getValueAt(i, m_CurrentCol).toString().equals(value) )          model.setValueAt(valueNew, i, m_CurrentCol);    }    model.setUndoEnabled(true);    model.setNotificationEnabled(true);    model.notifyListener(new TableModelEvent(model, 0, model.getRowCount(), m_CurrentCol, TableModelEvent.UPDATE));        // refresh    m_TableArff.repaint();  }    /**   * deletes the currently selected attribute   */  public void deleteAttribute() {    ArffSortedTableModel   model;        // no column selected?    if (m_CurrentCol == -1)      return;        model = (ArffSortedTableModel) m_TableArff.getModel();    // really an attribute column?    if (model.getAttributeAt(m_CurrentCol) == null)      return;        // really?    if (ComponentHelper.showMessageBox(        getParent(),         "Confirm...",        "Do you really want to delete the attribute '"         + model.getAttributeAt(m_CurrentCol).name() + "'?",        JOptionPane.YES_NO_OPTION,        JOptionPane.QUESTION_MESSAGE) != JOptionPane.YES_OPTION)      return;        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));    model.deleteAttributeAt(m_CurrentCol);    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));  }    /**   * deletes the chosen attributes   */  public void deleteAttributes() {    ListSelectorDialog    dialog;    ArffSortedTableModel       model;    Object[]              atts;    int[]                 indices;    int                   i;    JList                 list;    int                   result;        list   = new JList(getAttributes());    dialog = new ListSelectorDialog(null, list);    result = dialog.showDialog();        if (result != ListSelectorDialog.APPROVE_OPTION)      return;        atts = list.getSelectedValues();        // really?    if (ComponentHelper.showMessageBox(        getParent(),         "Confirm...",        "Do you really want to delete these "         + atts.length + " attributes?",        JOptionPane.YES_NO_OPTION,        JOptionPane.QUESTION_MESSAGE) != JOptionPane.YES_OPTION)      return;        model   = (ArffSortedTableModel) m_TableArff.getModel();    indices = new int[atts.length];    for (i = 0; i < atts.length; i++)      indices[i] = model.getAttributeColumn(atts[i].toString());        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));    model.deleteAttributes(indices);    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));  }    /**   * sets the current attribute as class attribute, i.e. it moves it to the end   * of the attributes   */  public void attributeAsClass() {    ArffSortedTableModel   model;        // no column selected?    if (m_CurrentCol == -1)      return;        model   = (ArffSortedTableModel) m_TableArff.getModel();    // really an attribute column?    if (model.getAttributeAt(m_CurrentCol) == null)      return;        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));    model.attributeAsClassAt(m_CurrentCol);    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));  }    /**   * renames the current attribute   */  public void renameAttribute() {    ArffSortedTableModel   model;    String            newName;        // no column selected?    if (m_CurrentCol == -1)      return;        model   = (ArffSortedTableModel) m_TableArff.getModel();    // really an attribute column?    if (model.getAttributeAt(m_CurrentCol) == null)      return;        newName = ComponentHelper.showInputBox(getParent(), "Rename attribute...", "Enter new Attribute name", model.getAttributeAt(m_CurrentCol).name());    if (newName == null)      return;        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));    model.renameAttributeAt(m_CurrentCol, newName);    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));  }    /**   * deletes the currently selected instance   */  public void deleteInstance() {    int               index;        index = m_TableArff.getSelectedRow();    if (index == -1)      return;        ((ArffSortedTableModel) m_TableArff.getModel()).deleteInstanceAt(index);  }    /**   * deletes all the currently selected instances   */  public void deleteInstances() {    int[]             indices;        if (m_TableArff.getSelectedRow() == -1)      return;        indices = m_TableArff.getSelectedRows();    ((ArffSortedTableModel) m_TableArff.getModel()).deleteInstances(indices);  }    /**   * sorts the instances via the currently selected column   */  public void sortInstances() {    if (m_CurrentCol == -1)      return;        ((ArffSortedTableModel) m_TableArff.getModel()).sortInstances(m_CurrentCol);  }    /**   * copies the content of the selection to the clipboard   */  public void copyContent() {    StringSelection      selection;    Clipboard            clipboard;        selection = getTable().getStringSelection();    if (selection == null)      return;        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();    clipboard.setContents(selection, selection);  }    /**   * searches for a string in the cells   */  public void search() {    String              searchString;        // display dialog    searchString = ComponentHelper.showInputBox(getParent(), "Search...", "Enter the string to search for", m_LastSearch);    if (searchString != null)      m_LastSearch = searchString;        getTable().setSearchString(searchString);  }    /**   * clears the search, i.e. resets the found cells   */  public void clearSearch() {    getTable().setSearchString("");  }    /**   * calculates the optimal column width for the current column   */  public void setOptimalColWidth() {    // no column selected?    if (m_CurrentCol == -1)      return;    JTableHelper.setOptimalColumnWidth(getTable(), m_CurrentCol);  }    /**   * calculates the optimal column widths for all columns   */  public void setOptimalColWidths() {    JTableHelper.setOptimalColumnWidth(getTable());  }    /**   * invoked when an action occurs   *    * @param e		the action event   */  public void actionPerformed(ActionEvent e) {    Object          o;        o = e.getSource();        if (o == menuItemMean)      calcMean();    else if (o == menuItemSetAllValues)      setValues(menuItemSetAllValues);    else if (o == menuItemSetMissingValues)      setValues(menuItemSetMissingValues);    else if (o == menuItemReplaceValues)      setValues(menuItemReplaceValues);    else if (o == menuItemRenameAttribute)      renameAttribute();    else if (o == menuItemAttributeAsClass)      attributeAsClass();    else if (o == menuItemDeleteAttribute)      deleteAttribute();    else if (o == menuItemDeleteAttributes)      deleteAttributes();    else if (o == menuItemDeleteSelectedInstance)      deleteInstance();    else if (o == menuItemDeleteAllSelectedInstances)      deleteInstances();    else if (o == menuItemSortInstances)      sortInstances();    else if (o == menuItemSearch)      search();    else if (o == menuItemClearSearch)      clearSearch();    else if (o == menuItemUndo)      undo();    else if (o == menuItemCopy)      copyContent();    else if (o == menuItemOptimalColWidth)      setOptimalColWidth();    else if (o == menuItemOptimalColWidths)      setOptimalColWidths();  }    /**   * Invoked when a mouse button has been pressed and released on a component   *    * @param e		the mouse event   */  public void mouseClicked(MouseEvent e) {    int		col;    boolean	popup;        col   = m_TableArff.columnAtPoint(e.getPoint());    popup =    ((e.getButton() == MouseEvent.BUTTON3) && (e.getClickCount() == 1))            || ((e.getButton() == MouseEvent.BUTTON1) && (e.getClickCount() == 1) && e.isAltDown() && !e.isControlDown() && !e.isShiftDown());    popup = popup && (getInstances() != null);        if (e.getSource() == m_TableArff.getTableHeader()) {      m_CurrentCol = col;            // Popup-Menu      if (popup) {        e.consume();        setMenu();        initPopupMenus();        m_PopupHeader.show(e.getComponent(), e.getX(), e.getY());      }    }    else if (e.getSource() == m_TableArff) {      // Popup-Menu      if (popup) {        e.consume();        setMenu();        initPopupMenus();        m_PopupRows.show(e.getComponent(), e.getX(), e.getY());      }    }        // highlihgt column    if (    (e.getButton() == MouseEvent.BUTTON1)           && (e.getClickCount() == 1)          && (!e.isAltDown())         && (col > -1) ) {      m_TableArff.setSelectedColumn(col);    }  }    /**   * Invoked when the mouse enters a component.   *    * @param e		the mouse event   */  public void mouseEntered(MouseEvent e) {  }    /**   * Invoked when the mouse exits a component   *    * @param e		the mouse event   */  public void mouseExited(MouseEvent e) {  }    /**   * Invoked when a mouse button has been pressed on a component   *    * @param e		the mouse event   */  public void mousePressed(MouseEvent e) {  }    /**   * Invoked when a mouse button has been released on a component.   *    * @param e		the mouse event   */  public void mouseReleased(MouseEvent e) {  }    /**   * Invoked when the target of the listener has changed its state.   *    * @param e		the change event   */  public void stateChanged(ChangeEvent e) {    m_Changed = true;    createTitle();    notifyListener();  }    /**   * notfies all listener of the change   */  public void notifyListener() {    Iterator                iter;        iter = m_ChangeListeners.iterator();    while (iter.hasNext())      ((ChangeListener) iter.next()).stateChanged(new ChangeEvent(this));  }    /**   * Adds a ChangeListener to the panel   *    * @param l		the listener to add   */  public void addChangeListener(ChangeListener l) {    m_ChangeListeners.add(l);  }    /**   * Removes a ChangeListener from the panel   *    * @param l		the listener to remove   */  public void removeChangeListener(ChangeListener l) {    m_ChangeListeners.remove(l);  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?