SetInstancesPanel.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 8k
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.  *    SetInstancesPanel.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui;
  22. import weka.core.Instances;
  23. import java.net.URL;
  24. import java.io.File;
  25. import java.io.Reader;
  26. import java.io.BufferedReader;
  27. import java.io.FileReader;
  28. import java.io.InputStreamReader;
  29. import java.awt.GridLayout;
  30. import java.awt.BorderLayout;
  31. import java.awt.event.ActionEvent;
  32. import java.awt.event.ActionListener;
  33. import java.beans.PropertyChangeListener;
  34. import java.beans.PropertyChangeEvent;
  35. import java.beans.PropertyChangeSupport;
  36. import javax.swing.JPanel;
  37. import javax.swing.JFrame;
  38. import javax.swing.JButton;
  39. import javax.swing.filechooser.FileFilter;
  40. import javax.swing.JFileChooser;
  41. import javax.swing.JOptionPane;
  42. import javax.swing.BorderFactory;
  43. /** 
  44.  * A panel that displays an instance summary for a set of instances and
  45.  * lets the user open a set of instances from either a file or URL.
  46.  *
  47.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  48.  * @version $Revision: 1.7 $
  49.  */
  50. public class SetInstancesPanel extends JPanel {
  51.   
  52.   /** Click to open instances from a file */
  53.   protected JButton m_OpenFileBut = new JButton("Open file...");
  54.   /** Click to open instances from a URL */
  55.   protected JButton m_OpenURLBut = new JButton("Open URL...");
  56.   /** The instance summary component */
  57.   protected InstancesSummaryPanel m_Summary = new InstancesSummaryPanel();
  58.   
  59.   /** Filter to ensure only arff files are selected */  
  60.   protected FileFilter m_ArffFilter =
  61.     new ExtensionFileFilter(Instances.FILE_EXTENSION, "Arff data files");
  62.   /** The file chooser for selecting arff files */
  63.   protected JFileChooser m_FileChooser
  64.     = new JFileChooser(new File(System.getProperty("user.dir")));
  65.   /** Stores the last URL that instances were loaded from */
  66.   protected String m_LastURL = "http://";
  67.   /** The thread we do loading in */
  68.   protected Thread m_IOThread;
  69.   /**
  70.    * Manages sending notifications to people when we change the set of
  71.    * working instances.
  72.    */
  73.   protected PropertyChangeSupport m_Support = new PropertyChangeSupport(this);
  74.   /** The current set of instances loaded */
  75.   protected Instances m_Instances;
  76.   
  77.   /**
  78.    * Create the panel.
  79.    */
  80.   public SetInstancesPanel() {
  81.     m_OpenFileBut.setToolTipText("Open a set of instances from a file");
  82.     m_OpenURLBut.setToolTipText("Open a set of instances from a URL");
  83.     m_FileChooser.setFileFilter(m_ArffFilter);
  84.     m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  85.     m_OpenURLBut.addActionListener(new ActionListener() {
  86.       public void actionPerformed(ActionEvent e) {
  87. setInstancesFromURLQ();
  88.       }
  89.     });
  90.     m_OpenFileBut.addActionListener(new ActionListener() {
  91.       public void actionPerformed(ActionEvent e) {
  92. setInstancesFromFileQ();
  93.       }
  94.     });
  95.     m_Summary.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
  96.     JPanel buttons = new JPanel();
  97.     buttons.setLayout(new GridLayout(1, 2));
  98.     buttons.add(m_OpenFileBut);
  99.     buttons.add(m_OpenURLBut);
  100.     
  101.     setLayout(new BorderLayout());
  102.     add(m_Summary, BorderLayout.CENTER);
  103.     add(buttons, BorderLayout.SOUTH);
  104.   }
  105.   /**
  106.    * Queries the user for a file to load instances from, then loads the
  107.    * instances in a background process. This is done in the IO
  108.    * thread, and an error message is popped up if the IO thread is busy.
  109.    */
  110.   public void setInstancesFromFileQ() {
  111.     
  112.     if (m_IOThread == null) {
  113.       int returnVal = m_FileChooser.showOpenDialog(this);
  114.       if (returnVal == JFileChooser.APPROVE_OPTION) {
  115. final File selected = m_FileChooser.getSelectedFile();
  116. m_IOThread = new Thread() {
  117.   public void run() {
  118.     setInstancesFromFile(selected);
  119.     m_IOThread = null;
  120.   }
  121. };
  122. m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority
  123. m_IOThread.start();
  124.       }
  125.     } else {
  126.       JOptionPane.showMessageDialog(this,
  127.     "Can't load at this time,n"
  128.     + "currently busy with other IO",
  129.     "Load Instances",
  130.     JOptionPane.WARNING_MESSAGE);
  131.     }
  132.   }
  133.     
  134.   /**
  135.    * Queries the user for a URL to load instances from, then loads the
  136.    * instances in a background process. This is done in the IO
  137.    * thread, and an error message is popped up if the IO thread is busy.
  138.    */
  139.   public void setInstancesFromURLQ() {
  140.     
  141.     if (m_IOThread == null) {
  142.       try {
  143. String urlName = (String) JOptionPane.showInputDialog(this,
  144. "Enter the source URL",
  145. "Load Instances",
  146. JOptionPane.QUESTION_MESSAGE,
  147. null,
  148. null,
  149. m_LastURL);
  150. if (urlName != null) {
  151.   m_LastURL = urlName;
  152.   final URL url = new URL(urlName);
  153.   m_IOThread = new Thread() {
  154.     public void run() {
  155.       setInstancesFromURL(url);
  156.       m_IOThread = null;
  157.     }
  158.   };
  159.   m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority
  160.   m_IOThread.start();
  161. }
  162.       } catch (Exception ex) {
  163. JOptionPane.showMessageDialog(this,
  164.       "Problem with URL:n"
  165.       + ex.getMessage(),
  166.       "Load Instances",
  167.       JOptionPane.ERROR_MESSAGE);
  168.       }
  169.     } else {
  170.       JOptionPane.showMessageDialog(this,
  171.     "Can't load at this time,n"
  172.     + "currently busy with other IO",
  173.     "Load Instances",
  174.     JOptionPane.WARNING_MESSAGE);
  175.     }
  176.   }
  177.   
  178.   /**
  179.    * Loads results from a set of instances contained in the supplied
  180.    * file.
  181.    *
  182.    * @param f a value of type 'File'
  183.    */
  184.   protected void setInstancesFromFile(File f) {
  185.       
  186.     try {
  187.       Reader r = new BufferedReader(new FileReader(f));
  188.       setInstances(new Instances(r));
  189.       r.close();
  190.     } catch (Exception ex) {
  191.       JOptionPane.showMessageDialog(this,
  192.     "Couldn't read from file:n"
  193.     + f.getName(),
  194.     "Load Instances",
  195.     JOptionPane.ERROR_MESSAGE);
  196.     }
  197.   }
  198.   /**
  199.    * Loads instances from a URL.
  200.    *
  201.    * @param u the URL to load from.
  202.    */
  203.   protected void setInstancesFromURL(URL u) {
  204.     try {
  205.       Reader r = new BufferedReader(new InputStreamReader(u.openStream()));
  206.       setInstances(new Instances(r));
  207.       r.close();
  208.     } catch (Exception ex) {
  209.       JOptionPane.showMessageDialog(this,
  210.     "Couldn't read from URL:n"
  211.     + u,
  212.     "Load Instances",
  213.     JOptionPane.ERROR_MESSAGE);
  214.     }
  215.   }
  216.   /**
  217.    * Updates the set of instances that is currently held by the panel
  218.    *
  219.    * @param i a value of type 'Instances'
  220.    */
  221.   public void setInstances(Instances i) {
  222.     m_Instances = i;
  223.     m_Summary.setInstances(m_Instances);
  224.     // Fire property change event for those interested.
  225.     m_Support.firePropertyChange("", null, null);
  226.   }
  227.   /**
  228.    * Gets the set of instances currently held by the panel
  229.    *
  230.    * @return a value of type 'Instances'
  231.    */
  232.   public Instances getInstances() {
  233.     
  234.     return m_Instances;
  235.   }
  236.   /**
  237.    * Gets the instances summary panel associated with
  238.    * this panel
  239.    * @return the instances summary panel
  240.    */
  241.   public InstancesSummaryPanel getSummary() {
  242.     return m_Summary;
  243.   }
  244.   
  245.   /**
  246.    * Adds a PropertyChangeListener who will be notified of value changes.
  247.    *
  248.    * @param l a value of type 'PropertyChangeListener'
  249.    */
  250.   public void addPropertyChangeListener(PropertyChangeListener l) {
  251.     m_Support.addPropertyChangeListener(l);
  252.   }
  253.   /**
  254.    * Removes a PropertyChangeListener.
  255.    *
  256.    * @param l a value of type 'PropertyChangeListener'
  257.    */
  258.   public void removePropertyChangeListener(PropertyChangeListener l) {
  259.     m_Support.removePropertyChangeListener(l);
  260.   }
  261. }