⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arffpanel.java

📁 MacroWeka扩展了著名数据挖掘工具weka
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    if (model != null)
      labelName.setText("Relation: " + model.getInstances().relationName());
    else
      labelName.setText("");
  }
  
  /**
   * loads the specified file into the table
   */
  private void loadFile(String filename) {
    ArffTableSorter         model;
    
    this.filename = filename;
    
    createTitle();
    
    if (filename.equals(""))   
      model = null;
    else
      model = new ArffTableSorter(filename);
    
    tableArff.setModel(model);
    setChanged(false);
    createName();
  }
  
  /**
   * calculates the mean of the given numeric column
   */
  private void calcMean() {
    ArffTableSorter   model;
    int               i;
    double            mean;
    
    // no column selected?
    if (currentCol == -1)
      return;
    
    model = (ArffTableSorter) tableArff.getModel();
    
    // not numeric?
    if (!model.getAttributeAt(currentCol).isNumeric())
      return;
    
    mean = 0;
    for (i = 0; i < model.getRowCount(); i++)
      mean += model.getInstances().instance(i).value(currentCol - 1);
    mean = mean / model.getRowCount();
    
    // show result
    ComponentHelper.showMessageBox(
        getParent(), 
        "Mean for attribute...",
        "Mean for attribute '" 
        + tableArff.getPlainColumnName(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
   */
  private void setValues(Object o) {
    String                     msg;
    String                     title;
    String                     value;
    String                     valueNew;
    int                        i;
    ArffTableSorter      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(tableArff.getParent(), title, msg, lastSearch);
    
    // cancelled?
    if (value == null)
      return;

    lastSearch = value;
    
    // replacement
    if (o == menuItemReplaceValues) {
      valueNew = ComponentHelper.showInputBox(tableArff.getParent(), title, "New value", lastReplace);
      if (valueNew == null)
        return;
      lastReplace = valueNew;
    }
    
    model = (ArffTableSorter) tableArff.getModel();
    model.setNotificationEnabled(false);

    // undo
    addUndoPoint();
    model.setUndoEnabled(false);
    
    // set value
    for (i = 0; i < tableArff.getRowCount(); i++) {
      if (o == menuItemSetAllValues)
        model.setValueAt(value, i, currentCol);
      else
        if ( (o == menuItemSetMissingValues) 
            && model.isMissingAt(i, currentCol) )
          model.setValueAt(value, i, currentCol);
        else if ( (o == menuItemReplaceValues) 
            && model.getValueAt(i, currentCol).toString().equals(value) )
          model.setValueAt(valueNew, i, currentCol);
    }
    model.setUndoEnabled(true);
    model.setNotificationEnabled(true);
    model.notifyListener(new TableModelEvent(model, 0, model.getRowCount(), currentCol, TableModelEvent.UPDATE));
    
    // refresh
    tableArff.repaint();
  }
  
  /**
   * deletes the currently selected attribute
   */
  public void deleteAttribute() {
    ArffTableSorter   model;
    
    // no column selected?
    if (currentCol == -1)
      return;
    
    model = (ArffTableSorter) tableArff.getModel();
    
    // really an attribute column?
    if (model.getAttributeAt(currentCol) == null)
      return;
    
    // really?
    if (ComponentHelper.showMessageBox(
        getParent(), 
        "Confirm...",
        "Do you really want to delete the attribute '" 
        + model.getAttributeAt(currentCol).name() + "'?",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE) != JOptionPane.YES_OPTION)
      return;
    
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    model.deleteAttributeAt(currentCol);
    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  }
  
  /**
   * deletes the chosen attributes
   */
  public void deleteAttributes() {
    ListSelectorDialog    dialog;
    ArffTableSorter       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   = (ArffTableSorter) 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));
  }
  
  /**
   * renames the current attribute
   */
  public void renameAttribute() {
    ArffTableSorter   model;
    String            newName;
    
    // no column selected?
    if (currentCol == -1)
      return;

    model   = (ArffTableSorter) tableArff.getModel();

    // really an attribute column?
    if (model.getAttributeAt(currentCol) == null)
      return;
    
    newName = ComponentHelper.showInputBox(getParent(), "Rename attribute...", "Enter new Attribute name", model.getAttributeAt(currentCol).name());
    if (newName == null)
      return;
    
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    model.renameAttributeAt(currentCol, newName);
    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  }
  
  /**
   * deletes the currently selected instance
   */
  public void deleteInstance() {
    int               index;
    
    index = tableArff.getSelectedRow();
    if (index == -1)
      return;
    
    ((ArffTableSorter) tableArff.getModel()).deleteInstanceAt(index);
  }
  
  /**
   * deletes all the currently selected instances
   */
  public void deleteInstances() {
    int[]             indices;
    
    if (tableArff.getSelectedRow() == -1)
      return;
    
    indices = tableArff.getSelectedRows();
    ((ArffTableSorter) tableArff.getModel()).deleteInstances(indices);
  }
  
  /**
   * sorts the instances via the currently selected column
   */
  public void sortInstances() {
    if (currentCol == -1)
      return;
    
    ((ArffTableSorter) tableArff.getModel()).sortInstances(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() {
    ArffTable            table;
    String               searchString;
    
    // display dialog
    searchString = ComponentHelper.showInputBox(getParent(), "Search...", "Enter the string to search for", lastSearch);
    if (searchString != null)
      lastSearch = searchString;
    
    getTable().setSearchString(searchString);
  }
  
  /**
   * clears the search, i.e. resets the found cells
   */
  public void clearSearch() {
    getTable().setSearchString("");
  }
  
  /**
   * invoked when an action occurs
   */
  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 == 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();
  }
  
  /**
   * Invoked when a mouse button has been pressed and released on a component
   */
  public void mouseClicked(MouseEvent e) {
    int         col;
    boolean	popup;
    
    col   = 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());
    
    if (e.getSource() == tableArff.getTableHeader()) {
      currentCol = col;
      
      // Popup-Menu
      if (popup) {
        e.consume();
        setMenu();
        popupHeader.show(e.getComponent(), e.getX(), e.getY());
      }
    }
    else if (e.getSource() == tableArff) {
      // Popup-Menu
      if (popup) {
        e.consume();
        setMenu();
        popupRows.show(e.getComponent(), e.getX(), e.getY());
      }
    }
    
    // highlihgt column
    if (    (e.getButton() == MouseEvent.BUTTON1)  
        && (e.getClickCount() == 1) 
        && (!e.isAltDown())
        && (col > -1) ) {
      tableArff.setSelectedColumn(col);
    }
  }
  
  /**
   * Invoked when the mouse enters a component.
   */
  public void mouseEntered(MouseEvent e) {
  }
  
  /**
   * Invoked when the mouse exits a component
   */
  public void mouseExited(MouseEvent e) {
  }
  
  /**
   * Invoked when a mouse button has been pressed on a component
   */
  public void mousePressed(MouseEvent e) {
  }
  
  /**
   * Invoked when a mouse button has been released on a component.
   */
  public void mouseReleased(MouseEvent e) {
  }
  
  /**
   * Invoked when the target of the listener has changed its state.
   */
  public void stateChanged(ChangeEvent e) {
    changed = true;
    createTitle();
    notifyListener();
  }
  
  /**
   * notfies all listener of the change
   */
  public void notifyListener() {
    Iterator                iter;
    
    iter = changeListeners.iterator();
    while (iter.hasNext())
      ((ChangeListener) iter.next()).stateChanged(new ChangeEvent(this));
  }
  
  /**
   * Adds a ChangeListener to the panel
   */
  public void addChangeListener(ChangeListener l) {
    changeListeners.add(l);
  }
  
  /**
   * Removes a ChangeListener from the panel
   */
  public void removeChangeListener(ChangeListener l) {
    changeListeners.remove(l);
  }
}

⌨️ 快捷键说明

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