AttributeSelectionPanel.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 10k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    AttributeSelectionPanel.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui;
  22. import weka.core.Instances;
  23. import java.awt.Dimension;
  24. import java.awt.GridLayout;
  25. import java.awt.BorderLayout;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.ActionEvent;
  28. import javax.swing.JPanel;
  29. import javax.swing.JButton;
  30. import javax.swing.JTable;
  31. import javax.swing.JScrollPane;
  32. import javax.swing.ListSelectionModel;
  33. import javax.swing.table.TableColumnModel;
  34. import javax.swing.table.AbstractTableModel;
  35. import javax.swing.BorderFactory;
  36. /**
  37.  * Creates a panel that displays the attributes contained in a set of
  38.  * instances, letting the user toggle whether each attribute is selected
  39.  * or not (eg: so that unselected attributes can be removed before
  40.  * classification).
  41.  *
  42.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  43.  * @version $Revision: 1.5 $
  44.  */
  45. public class AttributeSelectionPanel extends JPanel {
  46.   /**
  47.    * A table model that looks at the names of attributes and maintains
  48.    * a list of attributes that have been "selected".
  49.    */
  50.   class AttributeTableModel extends AbstractTableModel {
  51.     /** The instances who's attribute structure we are reporting */
  52.     protected Instances m_Instances;
  53.     
  54.     /** The flag for whether the instance will be included */
  55.     protected boolean [] m_Selected;
  56.     
  57.     /**
  58.      * Creates the tablemodel with the given set of instances.
  59.      *
  60.      * @param instances the initial set of Instances
  61.      */
  62.     public AttributeTableModel(Instances instances) {
  63.       setInstances(instances);
  64.     }
  65.     /**
  66.      * Sets the tablemodel to look at a new set of instances.
  67.      *
  68.      * @param instances the new set of Instances.
  69.      */
  70.     public void setInstances(Instances instances) {
  71.       m_Instances = instances;
  72.       m_Selected = new boolean [m_Instances.numAttributes()];
  73.       includeAll();
  74.     }
  75.     
  76.     /**
  77.      * Gets the number of attributes.
  78.      *
  79.      * @return the number of attributes.
  80.      */
  81.     public int getRowCount() {
  82.       
  83.       return m_Selected.length;
  84.     }
  85.     
  86.     /**
  87.      * Gets the number of columns: 3
  88.      *
  89.      * @return 3
  90.      */
  91.     public int getColumnCount() {
  92.       
  93.       return 3;
  94.     }
  95.     
  96.     /**
  97.      * Gets a table cell
  98.      *
  99.      * @param row the row index
  100.      * @param column the column index
  101.      * @return the value at row, column
  102.      */
  103.     public Object getValueAt(int row, int column) {
  104.       
  105.       switch (column) {
  106.       case 0:
  107. return new Integer(row + 1);
  108.       case 1:
  109. return new Boolean(m_Selected[row]);
  110.       case 2:
  111. return m_Instances.attribute(row).name();
  112.       default:
  113. return null;
  114.       }
  115.     }
  116.     
  117.     /**
  118.      * Gets the name for a column.
  119.      *
  120.      * @param column the column index.
  121.      * @return the name of the column.
  122.      */
  123.     public String getColumnName(int column) {
  124.       
  125.       switch (column) {
  126.       case 0:
  127. return new String("No.");
  128.       case 1:
  129. return new String("");
  130.       case 2:
  131. return new String("Name");
  132.       default:
  133. return null;
  134.       }
  135.     }
  136.     
  137.     /**
  138.      * Sets the value at a cell.
  139.      *
  140.      * @param value the new value.
  141.      * @param row the row index.
  142.      * @param col the column index.
  143.      */
  144.     public void setValueAt(Object value, int row, int col) {
  145.       
  146.       if (col == 1) {
  147. m_Selected[row] = ((Boolean) value).booleanValue(); 
  148.       }
  149.     }
  150.     
  151.     /**
  152.      * Gets the class of elements in a column.
  153.      *
  154.      * @param col the column index.
  155.      * @return the class of elements in the column.
  156.      */
  157.     public Class getColumnClass(int col) {
  158.       return getValueAt(0, col).getClass();
  159.     }
  160.     /**
  161.      * Returns true if the column is the "selected" column.
  162.      *
  163.      * @param row ignored
  164.      * @param col the column index.
  165.      * @return true if col == 1.
  166.      */
  167.     public boolean isCellEditable(int row, int col) {
  168.       if (col == 1) { 
  169. return true;
  170.       }
  171.       return false;
  172.     }
  173.     
  174.     /**
  175.      * Gets an array containing the indices of all selected attributes.
  176.      *
  177.      * @return the array of selected indices.
  178.      */
  179.     public int [] getSelectedAttributes() {
  180.       
  181.       int [] r1 = new int[getRowCount()];
  182.       int selCount = 0;
  183.       for (int i = 0; i < getRowCount(); i++) {
  184. if (m_Selected[i]) {
  185.   r1[selCount++] = i;
  186. }
  187.       }
  188.       int [] result = new int[selCount];
  189.       System.arraycopy(r1, 0, result, 0, selCount);
  190.       return result;
  191.     }
  192.     
  193.     /**
  194.      * Sets the state of all attributes to selected.
  195.      */
  196.     public void includeAll() {
  197.       
  198.       for (int i = 0; i < m_Selected.length; i++) {
  199. m_Selected[i] = true;
  200.       }
  201.       fireTableRowsUpdated(0, m_Selected.length);
  202.     }
  203.     
  204.     /**
  205.      * Deselects all attributes.
  206.      */
  207.     public void removeAll() {
  208.       
  209.       for (int i = 0; i < m_Selected.length; i++) {
  210. m_Selected[i] = false;
  211.       }
  212.       fireTableRowsUpdated(0, m_Selected.length);
  213.     }
  214.     /**
  215.      * Inverts the selected status of each attribute.
  216.      */
  217.     public void invert() {
  218.       for (int i = 0; i < m_Selected.length; i++) {
  219. m_Selected[i] = !m_Selected[i];
  220.       }
  221.       fireTableRowsUpdated(0, m_Selected.length);
  222.     }
  223.   }
  224.   /** Press to select all attributes */  
  225.   protected JButton m_IncludeAll = new JButton("All");
  226.   /** Press to deselect all attributes */
  227.   protected JButton m_RemoveAll = new JButton("None");
  228.   /** Press to invert the current selection */
  229.   protected JButton m_Invert = new JButton("Invert");
  230.   /** The table displaying attribute names and selection status */
  231.   protected JTable m_Table = new JTable();
  232.   /** The table model containingn attribute names and selection status */
  233.   protected AttributeTableModel m_Model;
  234.   
  235.   /**
  236.    * Creates the attribute selection panel with no initial instances.
  237.    */
  238.   public AttributeSelectionPanel() {
  239.     m_IncludeAll.setToolTipText("Selects all attributes");
  240.     m_IncludeAll.setEnabled(false);
  241.     m_IncludeAll.addActionListener(new ActionListener() {
  242.       public void actionPerformed(ActionEvent e) {
  243. m_Model.includeAll();
  244.       }
  245.     });
  246.     m_RemoveAll.setToolTipText("Unselects all attributes");
  247.     m_RemoveAll.setEnabled(false);
  248.     m_RemoveAll.addActionListener(new ActionListener() {
  249.       public void actionPerformed(ActionEvent e) {
  250. m_Model.removeAll();
  251.       }
  252.     });
  253.     m_Invert.setToolTipText("Inverts the current attribute selection");
  254.     m_Invert.setEnabled(false);
  255.     m_Invert.addActionListener(new ActionListener() {
  256.       public void actionPerformed(ActionEvent e) {
  257. m_Model.invert();
  258.       }
  259.     });
  260.     m_Table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  261.     m_Table.setColumnSelectionAllowed(false); 
  262.     m_Table.setPreferredScrollableViewportSize(new Dimension(250, 150));
  263.     // Set up the layout
  264.     JPanel p1 = new JPanel();
  265.     p1.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
  266.     p1.setLayout(new GridLayout(1, 3, 5, 5));
  267.     p1.add(m_IncludeAll);
  268.     p1.add(m_RemoveAll);
  269.     p1.add(m_Invert);
  270.     setLayout(new BorderLayout());
  271.     add(p1, BorderLayout.NORTH);
  272.     add(new JScrollPane(m_Table), BorderLayout.CENTER);
  273.   }
  274.   /**
  275.    * Sets the instances who's attribute names will be displayed.
  276.    *
  277.    * @param newInstances the new set of instances
  278.    */
  279.   public void setInstances(Instances newInstances) {
  280.     if (m_Model == null) {
  281.       m_Model = new AttributeTableModel(newInstances);
  282.       m_Table.setModel(m_Model);
  283.       TableColumnModel tcm = m_Table.getColumnModel();
  284.       tcm.getColumn(0).setMaxWidth(60);
  285.       tcm.getColumn(1).setMaxWidth(tcm.getColumn(1).getMinWidth());
  286.       tcm.getColumn(2).setMinWidth(100);
  287.     } else {
  288.       m_Model.setInstances(newInstances);
  289.       m_Table.clearSelection();
  290.     }
  291.     m_IncludeAll.setEnabled(true);
  292.     m_RemoveAll.setEnabled(true);
  293.     m_Invert.setEnabled(true);
  294.     m_Table.sizeColumnsToFit(2);
  295.     m_Table.revalidate();
  296.     m_Table.repaint();
  297.   }
  298.   /**
  299.    * Gets an array containing the indices of all selected attributes.
  300.    *
  301.    * @return the array of selected indices.
  302.    */
  303.   public int [] getSelectedAttributes() {
  304.     
  305.     return m_Model.getSelectedAttributes();
  306.   }
  307.   
  308.   /**
  309.    * Gets the selection model used by the table.
  310.    *
  311.    * @return a value of type 'ListSelectionModel'
  312.    */
  313.   public ListSelectionModel getSelectionModel() {
  314.     return m_Table.getSelectionModel();
  315.   }
  316.   
  317.   /**
  318.    * Tests the attribute selection panel from the command line.
  319.    *
  320.    * @param args must contain the name of an arff file to load.
  321.    */
  322.   public static void main(String[] args) {
  323.     try {
  324.       if (args.length == 0) {
  325. throw new Exception("supply the name of an arff file");
  326.       }
  327.       Instances i = new Instances(new java.io.BufferedReader(
  328.   new java.io.FileReader(args[0])));
  329.       AttributeSelectionPanel asp = new AttributeSelectionPanel();
  330.       final javax.swing.JFrame jf =
  331. new javax.swing.JFrame("Attribute Selection Panel");
  332.       jf.getContentPane().setLayout(new BorderLayout());
  333.       jf.getContentPane().add(asp, BorderLayout.CENTER);
  334.       jf.addWindowListener(new java.awt.event.WindowAdapter() {
  335. public void windowClosing(java.awt.event.WindowEvent e) {
  336.   jf.dispose();
  337.   System.exit(0);
  338. }
  339.       });
  340.       jf.pack();
  341.       jf.setVisible(true);
  342.       asp.setInstances(i);
  343.     } catch (Exception ex) {
  344.       ex.printStackTrace();
  345.       System.err.println(ex.getMessage());
  346.     }
  347.   }
  348.   
  349. } // AttributeSelectionPanel