[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 | * DataVisualizer.java |
---|
| 19 | * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui.beans; |
---|
| 24 | |
---|
| 25 | import weka.core.Instances; |
---|
| 26 | import weka.gui.visualize.PlotData2D; |
---|
| 27 | import weka.gui.visualize.VisualizePanel; |
---|
| 28 | |
---|
| 29 | import java.awt.BorderLayout; |
---|
| 30 | import java.beans.PropertyChangeListener; |
---|
| 31 | import java.beans.VetoableChangeListener; |
---|
| 32 | import java.beans.beancontext.BeanContext; |
---|
| 33 | import java.beans.beancontext.BeanContextChild; |
---|
| 34 | import java.beans.beancontext.BeanContextChildSupport; |
---|
| 35 | import java.io.Serializable; |
---|
| 36 | import java.util.Enumeration; |
---|
| 37 | import java.util.Vector; |
---|
| 38 | |
---|
| 39 | import javax.swing.JFrame; |
---|
| 40 | import javax.swing.JPanel; |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Bean that encapsulates weka.gui.visualize.VisualizePanel |
---|
| 44 | * |
---|
| 45 | * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a> |
---|
| 46 | * @version $Revision: 5284 $ |
---|
| 47 | */ |
---|
| 48 | public class DataVisualizer extends JPanel |
---|
| 49 | implements DataSourceListener, TrainingSetListener, |
---|
| 50 | TestSetListener, Visible, UserRequestAcceptor, Serializable, |
---|
| 51 | BeanContextChild { |
---|
| 52 | |
---|
| 53 | /** for serialization */ |
---|
| 54 | private static final long serialVersionUID = 1949062132560159028L; |
---|
| 55 | |
---|
| 56 | protected BeanVisual m_visual; |
---|
| 57 | |
---|
| 58 | protected transient Instances m_visualizeDataSet; |
---|
| 59 | |
---|
| 60 | protected transient JFrame m_popupFrame; |
---|
| 61 | |
---|
| 62 | protected boolean m_framePoppedUp = false; |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * True if this bean's appearance is the design mode appearance |
---|
| 66 | */ |
---|
| 67 | protected boolean m_design; |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * BeanContex that this bean might be contained within |
---|
| 71 | */ |
---|
| 72 | protected transient BeanContext m_beanContext = null; |
---|
| 73 | |
---|
| 74 | private VisualizePanel m_visPanel; |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * Objects listening for data set events |
---|
| 78 | */ |
---|
| 79 | private Vector m_dataSetListeners = new Vector(); |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * BeanContextChild support |
---|
| 83 | */ |
---|
| 84 | protected BeanContextChildSupport m_bcSupport = |
---|
| 85 | new BeanContextChildSupport(this); |
---|
| 86 | |
---|
| 87 | public DataVisualizer() { |
---|
| 88 | java.awt.GraphicsEnvironment ge = |
---|
| 89 | java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
| 90 | if (!ge.isHeadless()) { |
---|
| 91 | appearanceFinal(); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Global info for this bean |
---|
| 97 | * |
---|
| 98 | * @return a <code>String</code> value |
---|
| 99 | */ |
---|
| 100 | public String globalInfo() { |
---|
| 101 | return "Visualize incoming data/training/test sets in a 2D scatter plot."; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | protected void appearanceDesign() { |
---|
| 105 | m_visPanel = null; |
---|
| 106 | removeAll(); |
---|
| 107 | m_visual = new BeanVisual("DataVisualizer", |
---|
| 108 | BeanVisual.ICON_PATH+"DefaultDataVisualizer.gif", |
---|
| 109 | BeanVisual.ICON_PATH |
---|
| 110 | +"DefaultDataVisualizer_animated.gif"); |
---|
| 111 | setLayout(new BorderLayout()); |
---|
| 112 | add(m_visual, BorderLayout.CENTER); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | protected void appearanceFinal() { |
---|
| 116 | java.awt.GraphicsEnvironment ge = |
---|
| 117 | java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
| 118 | |
---|
| 119 | removeAll(); |
---|
| 120 | if (!ge.isHeadless()) { |
---|
| 121 | setLayout(new BorderLayout()); |
---|
| 122 | setUpFinal(); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | protected void setUpFinal() { |
---|
| 127 | if (m_visPanel == null) { |
---|
| 128 | m_visPanel = new VisualizePanel(); |
---|
| 129 | } |
---|
| 130 | add(m_visPanel, BorderLayout.CENTER); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * Accept a training set |
---|
| 135 | * |
---|
| 136 | * @param e a <code>TrainingSetEvent</code> value |
---|
| 137 | */ |
---|
| 138 | public void acceptTrainingSet(TrainingSetEvent e) { |
---|
| 139 | Instances trainingSet = e.getTrainingSet(); |
---|
| 140 | DataSetEvent dse = new DataSetEvent(this, trainingSet); |
---|
| 141 | acceptDataSet(dse); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * Accept a test set |
---|
| 146 | * |
---|
| 147 | * @param e a <code>TestSetEvent</code> value |
---|
| 148 | */ |
---|
| 149 | public void acceptTestSet(TestSetEvent e) { |
---|
| 150 | Instances testSet = e.getTestSet(); |
---|
| 151 | DataSetEvent dse = new DataSetEvent(this, testSet); |
---|
| 152 | acceptDataSet(dse); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * Accept a data set |
---|
| 157 | * |
---|
| 158 | * @param e a <code>DataSetEvent</code> value |
---|
| 159 | */ |
---|
| 160 | public synchronized void acceptDataSet(DataSetEvent e) { |
---|
| 161 | // ignore structure only events |
---|
| 162 | if (e.isStructureOnly()) { |
---|
| 163 | return; |
---|
| 164 | } |
---|
| 165 | m_visualizeDataSet = new Instances(e.getDataSet()); |
---|
| 166 | if (m_visualizeDataSet.classIndex() < 0) { |
---|
| 167 | m_visualizeDataSet.setClassIndex(m_visualizeDataSet.numAttributes()-1); |
---|
| 168 | } |
---|
| 169 | if (!m_design) { |
---|
| 170 | try { |
---|
| 171 | setInstances(m_visualizeDataSet); |
---|
| 172 | } catch (Exception ex) { |
---|
| 173 | ex.printStackTrace(); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | // pass on the event to any listeners |
---|
| 178 | notifyDataSetListeners(e); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * Set the visual appearance of this bean |
---|
| 183 | * |
---|
| 184 | * @param newVisual a <code>BeanVisual</code> value |
---|
| 185 | */ |
---|
| 186 | public void setVisual(BeanVisual newVisual) { |
---|
| 187 | m_visual = newVisual; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * Return the visual appearance of this bean |
---|
| 192 | */ |
---|
| 193 | public BeanVisual getVisual() { |
---|
| 194 | return m_visual; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * Use the default appearance for this bean |
---|
| 199 | */ |
---|
| 200 | public void useDefaultVisual() { |
---|
| 201 | m_visual.loadIcons(BeanVisual.ICON_PATH+"DefaultDataVisualizer.gif", |
---|
| 202 | BeanVisual.ICON_PATH+"DefaultDataVisualizer_animated.gif"); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * Describe <code>enumerateRequests</code> method here. |
---|
| 207 | * |
---|
| 208 | * @return an <code>Enumeration</code> value |
---|
| 209 | */ |
---|
| 210 | public Enumeration enumerateRequests() { |
---|
| 211 | Vector newVector = new Vector(0); |
---|
| 212 | if (m_visualizeDataSet != null) { |
---|
| 213 | newVector.addElement("Show plot"); |
---|
| 214 | } |
---|
| 215 | return newVector.elements(); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /** |
---|
| 219 | * Add a property change listener to this bean |
---|
| 220 | * |
---|
| 221 | * @param name the name of the property of interest |
---|
| 222 | * @param pcl a <code>PropertyChangeListener</code> value |
---|
| 223 | */ |
---|
| 224 | public void addPropertyChangeListener(String name, |
---|
| 225 | PropertyChangeListener pcl) { |
---|
| 226 | m_bcSupport.addPropertyChangeListener(name, pcl); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * Remove a property change listener from this bean |
---|
| 231 | * |
---|
| 232 | * @param name the name of the property of interest |
---|
| 233 | * @param pcl a <code>PropertyChangeListener</code> value |
---|
| 234 | */ |
---|
| 235 | public void removePropertyChangeListener(String name, |
---|
| 236 | PropertyChangeListener pcl) { |
---|
| 237 | m_bcSupport.removePropertyChangeListener(name, pcl); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | * Add a vetoable change listener to this bean |
---|
| 242 | * |
---|
| 243 | * @param name the name of the property of interest |
---|
| 244 | * @param vcl a <code>VetoableChangeListener</code> value |
---|
| 245 | */ |
---|
| 246 | public void addVetoableChangeListener(String name, |
---|
| 247 | VetoableChangeListener vcl) { |
---|
| 248 | m_bcSupport.addVetoableChangeListener(name, vcl); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | * Remove a vetoable change listener from this bean |
---|
| 253 | * |
---|
| 254 | * @param name the name of the property of interest |
---|
| 255 | * @param vcl a <code>VetoableChangeListener</code> value |
---|
| 256 | */ |
---|
| 257 | public void removeVetoableChangeListener(String name, |
---|
| 258 | VetoableChangeListener vcl) { |
---|
| 259 | m_bcSupport.removeVetoableChangeListener(name, vcl); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * Set a bean context for this bean |
---|
| 264 | * |
---|
| 265 | * @param bc a <code>BeanContext</code> value |
---|
| 266 | */ |
---|
| 267 | public void setBeanContext(BeanContext bc) { |
---|
| 268 | m_beanContext = bc; |
---|
| 269 | m_design = m_beanContext.isDesignTime(); |
---|
| 270 | if (m_design) { |
---|
| 271 | appearanceDesign(); |
---|
| 272 | } else { |
---|
| 273 | java.awt.GraphicsEnvironment ge = |
---|
| 274 | java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
| 275 | if (!ge.isHeadless()) { |
---|
| 276 | appearanceFinal(); |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | /** |
---|
| 282 | * Return the bean context (if any) that this bean is embedded in |
---|
| 283 | * |
---|
| 284 | * @return a <code>BeanContext</code> value |
---|
| 285 | */ |
---|
| 286 | public BeanContext getBeanContext() { |
---|
| 287 | return m_beanContext; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | /** |
---|
| 291 | * Set instances for this bean. This method is a convenience method |
---|
| 292 | * for clients who use this component programatically |
---|
| 293 | * |
---|
| 294 | * @param inst an <code>Instances</code> value |
---|
| 295 | * @exception Exception if an error occurs |
---|
| 296 | */ |
---|
| 297 | public void setInstances(Instances inst) throws Exception { |
---|
| 298 | if (m_design) { |
---|
| 299 | throw new Exception("This method is not to be used during design " |
---|
| 300 | +"time. It is meant to be used if this " |
---|
| 301 | +"bean is being used programatically as as " |
---|
| 302 | +"stand alone component."); |
---|
| 303 | } |
---|
| 304 | m_visualizeDataSet = inst; |
---|
| 305 | PlotData2D pd1 = new PlotData2D(m_visualizeDataSet); |
---|
| 306 | String relationName = m_visualizeDataSet.relationName(); |
---|
| 307 | pd1.setPlotName(relationName); |
---|
| 308 | try { |
---|
| 309 | m_visPanel.setMasterPlot(pd1); |
---|
| 310 | } catch (Exception ex) { |
---|
| 311 | System.err.println("Problem setting up " |
---|
| 312 | +"visualization (DataVisualizer)"); |
---|
| 313 | ex.printStackTrace(); |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | /** |
---|
| 318 | * Notify all data set listeners of a data set event |
---|
| 319 | * |
---|
| 320 | * @param ge a <code>DataSetEvent</code> value |
---|
| 321 | */ |
---|
| 322 | private void notifyDataSetListeners(DataSetEvent ge) { |
---|
| 323 | Vector l; |
---|
| 324 | synchronized (this) { |
---|
| 325 | l = (Vector)m_dataSetListeners.clone(); |
---|
| 326 | } |
---|
| 327 | if (l.size() > 0) { |
---|
| 328 | for(int i = 0; i < l.size(); i++) { |
---|
| 329 | ((DataSourceListener)l.elementAt(i)).acceptDataSet(ge); |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | /** |
---|
| 335 | * Describe <code>performRequest</code> method here. |
---|
| 336 | * |
---|
| 337 | * @param request a <code>String</code> value |
---|
| 338 | * @exception IllegalArgumentException if an error occurs |
---|
| 339 | */ |
---|
| 340 | public void performRequest(String request) { |
---|
| 341 | if (request.compareTo("Show plot") == 0) { |
---|
| 342 | try { |
---|
| 343 | // popup visualize panel |
---|
| 344 | if (!m_framePoppedUp) { |
---|
| 345 | m_framePoppedUp = true; |
---|
| 346 | final VisualizePanel vis = new VisualizePanel(); |
---|
| 347 | PlotData2D pd1 = new PlotData2D(m_visualizeDataSet); |
---|
| 348 | |
---|
| 349 | String relationName = m_visualizeDataSet.relationName(); |
---|
| 350 | |
---|
| 351 | // A bit of a nasty hack. Allows producers of instances-based |
---|
| 352 | // events to specify that the points should be connected |
---|
| 353 | if (relationName.startsWith("__")) { |
---|
| 354 | boolean[] connect = new boolean[m_visualizeDataSet.numInstances()]; |
---|
| 355 | for (int i = 1; i < connect.length; i++) { connect[i] = true; } |
---|
| 356 | pd1.setConnectPoints(connect); |
---|
| 357 | relationName = relationName.substring(2); |
---|
| 358 | } |
---|
| 359 | pd1.setPlotName(relationName); |
---|
| 360 | try { |
---|
| 361 | vis.setMasterPlot(pd1); |
---|
| 362 | } catch (Exception ex) { |
---|
| 363 | System.err.println("Problem setting up " |
---|
| 364 | +"visualization (DataVisualizer)"); |
---|
| 365 | ex.printStackTrace(); |
---|
| 366 | } |
---|
| 367 | final JFrame jf = new JFrame("Visualize"); |
---|
| 368 | jf.setSize(800,600); |
---|
| 369 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
| 370 | jf.getContentPane().add(vis, BorderLayout.CENTER); |
---|
| 371 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
| 372 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
| 373 | jf.dispose(); |
---|
| 374 | m_framePoppedUp = false; |
---|
| 375 | } |
---|
| 376 | }); |
---|
| 377 | jf.setVisible(true); |
---|
| 378 | m_popupFrame = jf; |
---|
| 379 | } else { |
---|
| 380 | m_popupFrame.toFront(); |
---|
| 381 | } |
---|
| 382 | } catch (Exception ex) { |
---|
| 383 | ex.printStackTrace(); |
---|
| 384 | m_framePoppedUp = false; |
---|
| 385 | } |
---|
| 386 | } else { |
---|
| 387 | throw new IllegalArgumentException(request |
---|
| 388 | + " not supported (DataVisualizer)"); |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | /** |
---|
| 393 | * Add a listener |
---|
| 394 | * |
---|
| 395 | * @param dsl a <code>DataSourceListener</code> value |
---|
| 396 | */ |
---|
| 397 | public synchronized void addDataSourceListener(DataSourceListener dsl) { |
---|
| 398 | m_dataSetListeners.addElement(dsl); |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | /** |
---|
| 402 | * Remove a listener |
---|
| 403 | * |
---|
| 404 | * @param dsl a <code>DataSourceListener</code> value |
---|
| 405 | */ |
---|
| 406 | public synchronized void removeDataSourceListener(DataSourceListener dsl) { |
---|
| 407 | m_dataSetListeners.remove(dsl); |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | public static void main(String [] args) { |
---|
| 411 | try { |
---|
| 412 | if (args.length != 1) { |
---|
| 413 | System.err.println("Usage: DataVisualizer <dataset>"); |
---|
| 414 | System.exit(1); |
---|
| 415 | } |
---|
| 416 | java.io.Reader r = new java.io.BufferedReader( |
---|
| 417 | new java.io.FileReader(args[0])); |
---|
| 418 | Instances inst = new Instances(r); |
---|
| 419 | final javax.swing.JFrame jf = new javax.swing.JFrame(); |
---|
| 420 | jf.getContentPane().setLayout(new java.awt.BorderLayout()); |
---|
| 421 | final DataVisualizer as = new DataVisualizer(); |
---|
| 422 | as.setInstances(inst); |
---|
| 423 | |
---|
| 424 | jf.getContentPane().add(as, java.awt.BorderLayout.CENTER); |
---|
| 425 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
| 426 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
| 427 | jf.dispose(); |
---|
| 428 | System.exit(0); |
---|
| 429 | } |
---|
| 430 | }); |
---|
| 431 | jf.setSize(800,600); |
---|
| 432 | jf.setVisible(true); |
---|
| 433 | } catch (Exception ex) { |
---|
| 434 | ex.printStackTrace(); |
---|
| 435 | System.err.println(ex.getMessage()); |
---|
| 436 | } |
---|
| 437 | } |
---|
| 438 | } |
---|