[29] | 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 | * GraphViewer.java |
---|
| 19 | * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui.beans; |
---|
| 24 | |
---|
| 25 | import weka.core.Drawable; |
---|
| 26 | import weka.core.FastVector; |
---|
| 27 | import weka.gui.ResultHistoryPanel; |
---|
| 28 | import weka.gui.graphvisualizer.BIFFormatException; |
---|
| 29 | import weka.gui.graphvisualizer.GraphVisualizer; |
---|
| 30 | import weka.gui.treevisualizer.PlaceNode2; |
---|
| 31 | import weka.gui.treevisualizer.TreeVisualizer; |
---|
| 32 | |
---|
| 33 | import java.awt.BorderLayout; |
---|
| 34 | import java.awt.event.MouseEvent; |
---|
| 35 | import java.io.Serializable; |
---|
| 36 | import java.text.SimpleDateFormat; |
---|
| 37 | import java.util.Date; |
---|
| 38 | import java.util.Enumeration; |
---|
| 39 | import java.util.Vector; |
---|
| 40 | import java.beans.beancontext.BeanContext; |
---|
| 41 | import java.beans.beancontext.BeanContextChild; |
---|
| 42 | import java.beans.beancontext.BeanContextChildSupport; |
---|
| 43 | import java.beans.PropertyChangeListener; |
---|
| 44 | import java.beans.VetoableChangeListener; |
---|
| 45 | |
---|
| 46 | import javax.swing.BorderFactory; |
---|
| 47 | import javax.swing.JFrame; |
---|
| 48 | import javax.swing.JPanel; |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * A bean encapsulating weka.gui.treevisualize.TreeVisualizer |
---|
| 52 | * |
---|
| 53 | * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a> |
---|
| 54 | * @version $Revision: 1.9 $ |
---|
| 55 | */ |
---|
| 56 | public class GraphViewer |
---|
| 57 | extends JPanel |
---|
| 58 | implements Visible, GraphListener, |
---|
| 59 | UserRequestAcceptor, |
---|
| 60 | Serializable, BeanContextChild { |
---|
| 61 | |
---|
| 62 | /** for serialization */ |
---|
| 63 | private static final long serialVersionUID = -5183121972114900617L; |
---|
| 64 | |
---|
| 65 | protected BeanVisual m_visual; |
---|
| 66 | |
---|
| 67 | private transient JFrame m_resultsFrame = null; |
---|
| 68 | |
---|
| 69 | protected transient ResultHistoryPanel m_history; |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * BeanContex that this bean might be contained within |
---|
| 73 | */ |
---|
| 74 | protected transient BeanContext m_beanContext = null; |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * BeanContextChild support |
---|
| 78 | */ |
---|
| 79 | protected BeanContextChildSupport m_bcSupport = |
---|
| 80 | new BeanContextChildSupport(this); |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * True if this bean's appearance is the design mode appearance |
---|
| 84 | */ |
---|
| 85 | protected boolean m_design; |
---|
| 86 | |
---|
| 87 | public GraphViewer() { |
---|
| 88 | /* setUpResultHistory(); |
---|
| 89 | setLayout(new BorderLayout()); |
---|
| 90 | add(m_visual, BorderLayout.CENTER); */ |
---|
| 91 | |
---|
| 92 | java.awt.GraphicsEnvironment ge = |
---|
| 93 | java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
| 94 | if (!ge.isHeadless()) { |
---|
| 95 | appearanceFinal(); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | protected void appearanceDesign() { |
---|
| 100 | setUpResultHistory(); |
---|
| 101 | removeAll(); |
---|
| 102 | m_visual = |
---|
| 103 | new BeanVisual("GraphViewer", |
---|
| 104 | BeanVisual.ICON_PATH+"DefaultGraph.gif", |
---|
| 105 | BeanVisual.ICON_PATH+"DefaultGraph_animated.gif"); |
---|
| 106 | setLayout(new BorderLayout()); |
---|
| 107 | add(m_visual, BorderLayout.CENTER); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | protected void appearanceFinal() { |
---|
| 111 | removeAll(); |
---|
| 112 | setLayout(new BorderLayout()); |
---|
| 113 | setUpFinal(); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | protected void setUpFinal() { |
---|
| 117 | setUpResultHistory(); |
---|
| 118 | add(m_history, BorderLayout.CENTER); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * Global info for this bean |
---|
| 123 | * |
---|
| 124 | * @return a <code>String</code> value |
---|
| 125 | */ |
---|
| 126 | public String globalInfo() { |
---|
| 127 | return "Graphically visualize trees or graphs produced by classifiers/clusterers."; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | private void setUpResultHistory() { |
---|
| 131 | if (m_history == null) { |
---|
| 132 | m_history = new ResultHistoryPanel(null); |
---|
| 133 | } |
---|
| 134 | m_history.setBorder(BorderFactory.createTitledBorder("Graph list")); |
---|
| 135 | m_history.setHandleRightClicks(false); |
---|
| 136 | m_history.getList(). |
---|
| 137 | addMouseListener(new ResultHistoryPanel.RMouseAdapter() { |
---|
| 138 | /** for serialization */ |
---|
| 139 | private static final long serialVersionUID = -4984130887963944249L; |
---|
| 140 | |
---|
| 141 | public void mouseClicked(MouseEvent e) { |
---|
| 142 | int index = m_history.getList().locationToIndex(e.getPoint()); |
---|
| 143 | if (index != -1) { |
---|
| 144 | String name = m_history.getNameAtIndex(index); |
---|
| 145 | doPopup(name); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | }); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Set a bean context for this bean |
---|
| 153 | * |
---|
| 154 | * @param bc a <code>BeanContext</code> value |
---|
| 155 | */ |
---|
| 156 | public void setBeanContext(BeanContext bc) { |
---|
| 157 | m_beanContext = bc; |
---|
| 158 | m_design = m_beanContext.isDesignTime(); |
---|
| 159 | if (m_design) { |
---|
| 160 | appearanceDesign(); |
---|
| 161 | } else { |
---|
| 162 | java.awt.GraphicsEnvironment ge = |
---|
| 163 | java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
| 164 | if (!ge.isHeadless()){ |
---|
| 165 | appearanceFinal(); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * Return the bean context (if any) that this bean is embedded in |
---|
| 172 | * |
---|
| 173 | * @return a <code>BeanContext</code> value |
---|
| 174 | */ |
---|
| 175 | public BeanContext getBeanContext() { |
---|
| 176 | return m_beanContext; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * Add a vetoable change listener to this bean |
---|
| 181 | * |
---|
| 182 | * @param name the name of the property of interest |
---|
| 183 | * @param vcl a <code>VetoableChangeListener</code> value |
---|
| 184 | */ |
---|
| 185 | public void addVetoableChangeListener(String name, |
---|
| 186 | VetoableChangeListener vcl) { |
---|
| 187 | m_bcSupport.addVetoableChangeListener(name, vcl); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * Remove a vetoable change listener from this bean |
---|
| 192 | * |
---|
| 193 | * @param name the name of the property of interest |
---|
| 194 | * @param vcl a <code>VetoableChangeListener</code> value |
---|
| 195 | */ |
---|
| 196 | public void removeVetoableChangeListener(String name, |
---|
| 197 | VetoableChangeListener vcl) { |
---|
| 198 | m_bcSupport.removeVetoableChangeListener(name, vcl); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
| 202 | * Accept a graph |
---|
| 203 | * |
---|
| 204 | * @param e a <code>GraphEvent</code> value |
---|
| 205 | */ |
---|
| 206 | public synchronized void acceptGraph(GraphEvent e) { |
---|
| 207 | |
---|
| 208 | FastVector graphInfo = new FastVector(); |
---|
| 209 | |
---|
| 210 | if (m_history == null) { |
---|
| 211 | setUpResultHistory(); |
---|
| 212 | } |
---|
| 213 | String name = (new SimpleDateFormat("HH:mm:ss - ")).format(new Date()); |
---|
| 214 | |
---|
| 215 | name += e.getGraphTitle(); |
---|
| 216 | graphInfo.addElement(new Integer(e.getGraphType())); |
---|
| 217 | graphInfo.addElement(e.getGraphString()); |
---|
| 218 | m_history.addResult(name, new StringBuffer()); |
---|
| 219 | m_history.addObject(name, graphInfo); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * Set the visual appearance of this bean |
---|
| 224 | * |
---|
| 225 | * @param newVisual a <code>BeanVisual</code> value |
---|
| 226 | */ |
---|
| 227 | public void setVisual(BeanVisual newVisual) { |
---|
| 228 | m_visual = newVisual; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | * Get the visual appearance of this bean |
---|
| 233 | * |
---|
| 234 | */ |
---|
| 235 | public BeanVisual getVisual() { |
---|
| 236 | return m_visual; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Use the default visual appearance |
---|
| 241 | * |
---|
| 242 | */ |
---|
| 243 | public void useDefaultVisual() { |
---|
| 244 | m_visual.loadIcons(BeanVisual.ICON_PATH+"DefaultGraph.gif", |
---|
| 245 | BeanVisual.ICON_PATH+"DefaultGraph_animated.gif"); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * Popup a result list from which the user can select a graph to view |
---|
| 250 | * |
---|
| 251 | */ |
---|
| 252 | public void showResults() { |
---|
| 253 | if (m_resultsFrame == null) { |
---|
| 254 | if (m_history == null) { |
---|
| 255 | setUpResultHistory(); |
---|
| 256 | } |
---|
| 257 | m_resultsFrame = new JFrame("Graph Viewer"); |
---|
| 258 | m_resultsFrame.getContentPane().setLayout(new BorderLayout()); |
---|
| 259 | m_resultsFrame.getContentPane().add(m_history, BorderLayout.CENTER); |
---|
| 260 | m_resultsFrame.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
| 261 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
| 262 | m_resultsFrame.dispose(); |
---|
| 263 | m_resultsFrame = null; |
---|
| 264 | } |
---|
| 265 | }); |
---|
| 266 | m_resultsFrame.pack(); |
---|
| 267 | m_resultsFrame.setVisible(true); |
---|
| 268 | } else { |
---|
| 269 | m_resultsFrame.toFront(); |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | private void doPopup(String name) { |
---|
| 274 | |
---|
| 275 | FastVector graph; |
---|
| 276 | String grphString; |
---|
| 277 | int grphType; |
---|
| 278 | |
---|
| 279 | graph = (FastVector)m_history.getNamedObject(name); |
---|
| 280 | grphType = ((Integer)graph.firstElement()).intValue(); |
---|
| 281 | grphString = (String)graph.lastElement(); |
---|
| 282 | |
---|
| 283 | if(grphType == Drawable.TREE){ |
---|
| 284 | final javax.swing.JFrame jf = |
---|
| 285 | new javax.swing.JFrame("Weka Classifier Tree Visualizer: "+name); |
---|
| 286 | jf.setSize(500,400); |
---|
| 287 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
| 288 | TreeVisualizer tv = |
---|
| 289 | new TreeVisualizer(null, |
---|
| 290 | grphString, |
---|
| 291 | new PlaceNode2()); |
---|
| 292 | jf.getContentPane().add(tv, BorderLayout.CENTER); |
---|
| 293 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
| 294 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
| 295 | jf.dispose(); |
---|
| 296 | } |
---|
| 297 | }); |
---|
| 298 | |
---|
| 299 | jf.setVisible(true); |
---|
| 300 | } |
---|
| 301 | if(grphType == Drawable.BayesNet) { |
---|
| 302 | final javax.swing.JFrame jf = |
---|
| 303 | new javax.swing.JFrame("Weka Classifier Graph Visualizer: "+name); |
---|
| 304 | jf.setSize(500,400); |
---|
| 305 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
| 306 | GraphVisualizer gv = |
---|
| 307 | new GraphVisualizer(); |
---|
| 308 | try { |
---|
| 309 | gv.readBIF(grphString); |
---|
| 310 | } |
---|
| 311 | catch (BIFFormatException be) { |
---|
| 312 | System.err.println("unable to visualize BayesNet"); be.printStackTrace(); |
---|
| 313 | } |
---|
| 314 | gv.layoutGraph(); |
---|
| 315 | jf.getContentPane().add(gv, BorderLayout.CENTER); |
---|
| 316 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
| 317 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
| 318 | jf.dispose(); |
---|
| 319 | } |
---|
| 320 | }); |
---|
| 321 | |
---|
| 322 | jf.setVisible(true); |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | /** |
---|
| 327 | * Return an enumeration of user requests |
---|
| 328 | * |
---|
| 329 | * @return an <code>Enumeration</code> value |
---|
| 330 | */ |
---|
| 331 | public Enumeration enumerateRequests() { |
---|
| 332 | Vector newVector = new Vector(0); |
---|
| 333 | newVector.addElement("Show results"); |
---|
| 334 | |
---|
| 335 | return newVector.elements(); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | /** |
---|
| 339 | * Perform the named request |
---|
| 340 | * |
---|
| 341 | * @param request a <code>String</code> value |
---|
| 342 | * @exception IllegalArgumentException if an error occurs |
---|
| 343 | */ |
---|
| 344 | public void performRequest(String request) |
---|
| 345 | { |
---|
| 346 | if (request.compareTo("Show results") == 0) { |
---|
| 347 | showResults(); |
---|
| 348 | } else { |
---|
| 349 | throw new |
---|
| 350 | IllegalArgumentException(request |
---|
| 351 | + " not supported (GraphViewer)"); |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | } |
---|