AbstractTrainAndTestSetProducer.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
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.  *    AbstractTrainAndTestSetProducer.java
  18.  *    Copyright (C) 2002 Mark Hall
  19.  *
  20.  */
  21. package weka.gui.beans;
  22. import java.io.Serializable;
  23. import java.util.Vector;
  24. import javax.swing.JPanel;
  25. import javax.swing.JLabel;
  26. import javax.swing.JTextField;
  27. import java.awt.BorderLayout;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.SwingConstants;
  30. import java.awt.*;
  31. import java.beans.EventSetDescriptor;
  32. /**
  33.  * Abstract base class for TrainAndTestSetProducers that contains default
  34.  * implementations of add/remove listener methods and defualt
  35.  * visual representation.
  36.  *
  37.  * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>
  38.  * @version $Revision: 1.2 $
  39.  */
  40. public abstract class AbstractTrainAndTestSetProducer 
  41.   extends JPanel
  42.   implements Visible, TrainingSetProducer, TestSetProducer, 
  43.      BeanCommon, Serializable, DataSourceListener {
  44.   /**
  45.    * Objects listening for trainin set events
  46.    */
  47.   protected Vector m_trainingListeners = new Vector();
  48.   /**
  49.    * Objects listening for test set events
  50.    */
  51.   protected Vector m_testListeners = new Vector();
  52.   protected BeanVisual m_visual = 
  53.     new BeanVisual("AbstractTrainingSetProducer", 
  54.    BeanVisual.ICON_PATH+"DefaultTrainTest.gif",
  55.    BeanVisual.ICON_PATH+"DefaultTrainTest_animated.gif");
  56.   /**
  57.    * non null if this object is a target for any events.
  58.    */
  59.   protected Object m_listenee = null;
  60.   protected transient weka.gui.Logger m_logger = null;
  61.   /**
  62.    * Creates a new <code>AbstractTrainAndTestSetProducer</code> instance.
  63.    */
  64.   public AbstractTrainAndTestSetProducer() {
  65.     setLayout(new BorderLayout());
  66.     add(m_visual, BorderLayout.CENTER);
  67.   }
  68.   /**
  69.    * Subclass must implement
  70.    *
  71.    * @param e a <code>DataSetEvent</code> value
  72.    */
  73.   public abstract void acceptDataSet(DataSetEvent e);
  74.   /**
  75.    * Add a training set listener
  76.    *
  77.    * @param tsl a <code>TrainingSetListener</code> value
  78.    */
  79.   public synchronized void addTrainingSetListener(TrainingSetListener tsl) {
  80.     m_trainingListeners.addElement(tsl);
  81.   }
  82.   /**
  83.    * Remove a training set listener
  84.    *
  85.    * @param tsl a <code>TrainingSetListener</code> value
  86.    */
  87.   public synchronized void removeTrainingSetListener(TrainingSetListener tsl) {
  88.     m_trainingListeners.removeElement(tsl);
  89.   }
  90.   /**
  91.    * Add a test set listener
  92.    *
  93.    * @param tsl a <code>TestSetListener</code> value
  94.    */
  95.   public synchronized void addTestSetListener(TestSetListener tsl) {
  96.     m_testListeners.addElement(tsl);
  97.   }
  98.   /**
  99.    * Remove a test set listener
  100.    *
  101.    * @param tsl a <code>TestSetListener</code> value
  102.    */
  103.   public synchronized void removeTestSetListener(TestSetListener tsl) {
  104.     m_testListeners.removeElement(tsl);
  105.   }
  106.   /**
  107.    * Set the visual for this bean
  108.    *
  109.    * @param newVisual a <code>BeanVisual</code> value
  110.    */
  111.   public void setVisual(BeanVisual newVisual) {
  112.     m_visual = newVisual;
  113.   }
  114.   /**
  115.    * Get the visual for this bean
  116.    *
  117.    * @return a <code>BeanVisual</code> value
  118.    */
  119.   public BeanVisual getVisual() {
  120.     return m_visual;
  121.   }
  122.   
  123.   /**
  124.    * Use the default visual for this bean
  125.    */
  126.   public void useDefaultVisual() {
  127.     m_visual.loadIcons(BeanVisual.ICON_PATH+"DefaultTrainTest.gif",
  128.        BeanVisual.ICON_PATH+"DefaultTrainTest_animated.gif");
  129.   }
  130.   /**
  131.    * Returns true if, at this time, 
  132.    * the object will accept a connection according to the supplied
  133.    * event name
  134.    *
  135.    * @param eventName the event
  136.    * @return true if the object will accept a connection
  137.    */
  138.   public boolean connectionAllowed(String eventName) {
  139.     return (m_listenee == null);
  140.   }
  141.   /**
  142.    * Notify this object that it has been registered as a listener with
  143.    * a source with respect to the supplied event name
  144.    *
  145.    * @param eventName the event
  146.    * @param source the source with which this object has been registered as
  147.    * a listener
  148.    */
  149.   public synchronized void connectionNotification(String eventName,
  150.   Object source) {
  151.     if (connectionAllowed(eventName)) {
  152.       m_listenee = source;
  153.     }
  154.   }
  155.   /**
  156.    * Notify this object that it has been deregistered as a listener with
  157.    * a source with respect to the supplied event name
  158.    *
  159.    * @param eventName the event
  160.    * @param source the source with which this object has been registered as
  161.    * a listener
  162.    */
  163.   public synchronized void disconnectionNotification(String eventName,
  164.      Object source) {
  165.     if (m_listenee == source) {
  166.       m_listenee = null;
  167.     }
  168.   }
  169.   
  170.   /**
  171.    * Set a log for this bean
  172.    *
  173.    * @param logger a <code>weka.gui.Logger</code> value
  174.    */
  175.   public void setLog(weka.gui.Logger logger) {
  176.     m_logger = logger;
  177.   }
  178.   /**
  179.    * Stop any processing that the bean might be doing.
  180.    * Subclass must implement
  181.    */
  182.   public abstract void stop();
  183. }