[4] | 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 | /* |
---|
| 18 | * AbstractTrainAndTestSetProducer.java |
---|
| 19 | * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui.beans; |
---|
| 24 | |
---|
| 25 | import java.awt.BorderLayout; |
---|
| 26 | import java.beans.EventSetDescriptor; |
---|
| 27 | import java.io.Serializable; |
---|
| 28 | import java.util.Vector; |
---|
| 29 | |
---|
| 30 | import javax.swing.JPanel; |
---|
| 31 | |
---|
| 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.5 $ |
---|
| 39 | */ |
---|
| 40 | public abstract class AbstractTrainAndTestSetProducer |
---|
| 41 | extends JPanel |
---|
| 42 | implements Visible, TrainingSetProducer, TestSetProducer, |
---|
| 43 | BeanCommon, Serializable, DataSourceListener { |
---|
| 44 | |
---|
| 45 | /** for serialization */ |
---|
| 46 | private static final long serialVersionUID = -1809339823613492037L; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Objects listening for trainin set events |
---|
| 50 | */ |
---|
| 51 | protected Vector m_trainingListeners = new Vector(); |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Objects listening for test set events |
---|
| 55 | */ |
---|
| 56 | protected Vector m_testListeners = new Vector(); |
---|
| 57 | |
---|
| 58 | protected BeanVisual m_visual = |
---|
| 59 | new BeanVisual("AbstractTrainingSetProducer", |
---|
| 60 | BeanVisual.ICON_PATH+"DefaultTrainTest.gif", |
---|
| 61 | BeanVisual.ICON_PATH+"DefaultTrainTest_animated.gif"); |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * non null if this object is a target for any events. |
---|
| 65 | */ |
---|
| 66 | protected Object m_listenee = null; |
---|
| 67 | |
---|
| 68 | protected transient weka.gui.Logger m_logger = null; |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * Creates a new <code>AbstractTrainAndTestSetProducer</code> instance. |
---|
| 72 | */ |
---|
| 73 | public AbstractTrainAndTestSetProducer() { |
---|
| 74 | setLayout(new BorderLayout()); |
---|
| 75 | add(m_visual, BorderLayout.CENTER); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Subclass must implement |
---|
| 80 | * |
---|
| 81 | * @param e a <code>DataSetEvent</code> value |
---|
| 82 | */ |
---|
| 83 | public abstract void acceptDataSet(DataSetEvent e); |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Add a training set listener |
---|
| 87 | * |
---|
| 88 | * @param tsl a <code>TrainingSetListener</code> value |
---|
| 89 | */ |
---|
| 90 | public synchronized void addTrainingSetListener(TrainingSetListener tsl) { |
---|
| 91 | m_trainingListeners.addElement(tsl); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * Remove a training set listener |
---|
| 96 | * |
---|
| 97 | * @param tsl a <code>TrainingSetListener</code> value |
---|
| 98 | */ |
---|
| 99 | public synchronized void removeTrainingSetListener(TrainingSetListener tsl) { |
---|
| 100 | m_trainingListeners.removeElement(tsl); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Add a test set listener |
---|
| 105 | * |
---|
| 106 | * @param tsl a <code>TestSetListener</code> value |
---|
| 107 | */ |
---|
| 108 | public synchronized void addTestSetListener(TestSetListener tsl) { |
---|
| 109 | m_testListeners.addElement(tsl); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Remove a test set listener |
---|
| 114 | * |
---|
| 115 | * @param tsl a <code>TestSetListener</code> value |
---|
| 116 | */ |
---|
| 117 | public synchronized void removeTestSetListener(TestSetListener tsl) { |
---|
| 118 | m_testListeners.removeElement(tsl); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * Set the visual for this bean |
---|
| 123 | * |
---|
| 124 | * @param newVisual a <code>BeanVisual</code> value |
---|
| 125 | */ |
---|
| 126 | public void setVisual(BeanVisual newVisual) { |
---|
| 127 | m_visual = newVisual; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Get the visual for this bean |
---|
| 132 | * |
---|
| 133 | * @return a <code>BeanVisual</code> value |
---|
| 134 | */ |
---|
| 135 | public BeanVisual getVisual() { |
---|
| 136 | return m_visual; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Use the default visual for this bean |
---|
| 141 | */ |
---|
| 142 | public void useDefaultVisual() { |
---|
| 143 | m_visual.loadIcons(BeanVisual.ICON_PATH+"DefaultTrainTest.gif", |
---|
| 144 | BeanVisual.ICON_PATH+"DefaultTrainTest_animated.gif"); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * Returns true if, at this time, |
---|
| 149 | * the object will accept a connection according to the supplied |
---|
| 150 | * event name |
---|
| 151 | * |
---|
| 152 | * @param eventName the event |
---|
| 153 | * @return true if the object will accept a connection |
---|
| 154 | */ |
---|
| 155 | public boolean connectionAllowed(String eventName) { |
---|
| 156 | return (m_listenee == null); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * Returns true if, at this time, |
---|
| 161 | * the object will accept a connection according to the supplied |
---|
| 162 | * EventSetDescriptor |
---|
| 163 | * |
---|
| 164 | * @param esd the EventSetDescriptor |
---|
| 165 | * @return true if the object will accept a connection |
---|
| 166 | */ |
---|
| 167 | public boolean connectionAllowed(EventSetDescriptor esd) { |
---|
| 168 | return connectionAllowed(esd.getName()); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * Notify this object that it has been registered as a listener with |
---|
| 173 | * a source with respect to the supplied event name |
---|
| 174 | * |
---|
| 175 | * @param eventName the event |
---|
| 176 | * @param source the source with which this object has been registered as |
---|
| 177 | * a listener |
---|
| 178 | */ |
---|
| 179 | public synchronized void connectionNotification(String eventName, |
---|
| 180 | Object source) { |
---|
| 181 | if (connectionAllowed(eventName)) { |
---|
| 182 | m_listenee = source; |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * Notify this object that it has been deregistered as a listener with |
---|
| 188 | * a source with respect to the supplied event name |
---|
| 189 | * |
---|
| 190 | * @param eventName the event |
---|
| 191 | * @param source the source with which this object has been registered as |
---|
| 192 | * a listener |
---|
| 193 | */ |
---|
| 194 | public synchronized void disconnectionNotification(String eventName, |
---|
| 195 | Object source) { |
---|
| 196 | if (m_listenee == source) { |
---|
| 197 | m_listenee = null; |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
| 202 | * Set a log for this bean |
---|
| 203 | * |
---|
| 204 | * @param logger a <code>weka.gui.Logger</code> value |
---|
| 205 | */ |
---|
| 206 | public void setLog(weka.gui.Logger logger) { |
---|
| 207 | m_logger = logger; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * Stop any processing that the bean might be doing. |
---|
| 212 | * Subclass must implement |
---|
| 213 | */ |
---|
| 214 | public abstract void stop(); |
---|
| 215 | } |
---|
| 216 | |
---|