weka.gui.visualize.plugins.GraphVizTreeVisualizationPlugin Maven / Gradle / Ivy
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* GraphVizTreeVisualizationPlugin.java
* Copyright (C) 2014-2017 University of Waikato, Hamilton, New Zealand
*/
package weka.gui.visualize.plugins;
import weka.gui.ComponentHelper;
import weka.gui.ExtensionFileFilter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Displays a graph image generated by GraphViz from the dotty notation that
* we get from the Explorer.
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision$
*/
public class GraphVizTreeVisualizationPlugin
implements TreeVisualizePlugin {
/** the filechooser for saving the dotty graph. */
protected JFileChooser m_FileChooserDotty;
/** the filechooser for saving the iimage. */
protected JFileChooser m_FileChooserImage;
/**
* Returns the filechooser to use for saving the dotty graph.
*
* @return the filechooser
*/
protected JFileChooser getFileChooserDotty() {
ExtensionFileFilter filter;
if (m_FileChooserDotty == null) {
filter = new ExtensionFileFilter(
new String[]{".dotty", ".dot"},
"GraphViz dot file");
m_FileChooserDotty = new JFileChooser();
m_FileChooserDotty.addChoosableFileFilter(filter);
m_FileChooserDotty.setFileFilter(filter);
m_FileChooserDotty.setFileSelectionMode(JFileChooser.FILES_ONLY);
m_FileChooserDotty.setAcceptAllFileFilterUsed(false);
}
return m_FileChooserDotty;
}
/**
* Returns the filechooser to use for saving the image.
*
* @return the filechooser
*/
protected JFileChooser getFileChooserImage() {
ExtensionFileFilter filter;
if (m_FileChooserImage == null) {
m_FileChooserImage = new JFileChooser();
m_FileChooserImage.setFileSelectionMode(JFileChooser.FILES_ONLY);
m_FileChooserImage.setAcceptAllFileFilterUsed(false);
filter = new ExtensionFileFilter(
new String[]{"." + GraphVizTreeVisualization.getSingleton().getImageExtension()},
GraphVizTreeVisualization.getSingleton().getImageExtension().toUpperCase() + " file");
m_FileChooserImage.addChoosableFileFilter(filter);
m_FileChooserImage.setFileFilter(filter);
}
return m_FileChooserImage;
}
/**
* Get a JMenu or JMenuItem which contain action listeners
* that perform the visualization of the tree in GraphViz's dotty format.
* Exceptions thrown because of changes in Weka since compilation need to
* be caught by the implementer.
*
* @see NoClassDefFoundError
* @see IncompatibleClassChangeError
*
* @param dotty the tree in dotty format
* @param name the name of the item (in the Explorer's history list)
* @return menuitem for opening visualization(s), or null
* to indicate no visualization is applicable for the input
*/
public JMenuItem getVisualizeMenuItem(final String dotty, final String name) {
JMenu result;
JMenuItem menuitem;
result = new JMenu("Visualize tree (GraphViz)");
menuitem = new JMenuItem("Show graph...");
menuitem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final GraphVizPanel panel = new GraphVizPanel();
panel.setDotty(dotty);
JScrollPane pane = new JScrollPane(panel);
pane.getVerticalScrollBar().setUnitIncrement(20);
pane.getHorizontalScrollBar().setUnitIncrement(20);
pane.getVerticalScrollBar().setBlockIncrement(100);
pane.getHorizontalScrollBar().setBlockIncrement(100);
pane.setWheelScrollingEnabled(true);
final JFrame jf = new JFrame("Tree for " + name);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setSize(GraphVizTreeVisualization.getSingleton().getDialogWidth(), GraphVizTreeVisualization.getSingleton().getDialogHeight());
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(pane, BorderLayout.CENTER);
jf.setLocationRelativeTo(null);
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("File");
menubar.add(menu);
JMenuItem menuitem = new JMenuItem("Save as...", ComponentHelper.getImageIcon("save.gif"));
menuitem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.saveComponent();
}
});
menu.add(menuitem);
menu.addSeparator();
menuitem = new JMenuItem("Close", ComponentHelper.getImageIcon("delete.gif"));
menuitem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jf.setVisible(false);
jf.dispose();
}
});
menu.add(menuitem);
jf.setJMenuBar(menubar);
jf.setVisible(true);
}
});
result.add(menuitem);
menuitem = new JMenuItem("Save graph data...");
menuitem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = getFileChooserDotty();
int retVal = fileChooser.showSaveDialog(null);
if (retVal != JFileChooser.APPROVE_OPTION)
return;
String msg = GraphVizTreeVisualization.getSingleton().saveDotty(dotty, "" + fileChooser.getSelectedFile());
if (msg != null)
JOptionPane.showMessageDialog(null, msg, "Error saving graph", JOptionPane.ERROR_MESSAGE);
}
});
result.add(menuitem);
menuitem = new JMenuItem("Save graph image...");
menuitem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = getFileChooserImage();
int retVal = fileChooser.showSaveDialog(null);
if (retVal != JFileChooser.APPROVE_OPTION)
return;
String msg = GraphVizTreeVisualization.getSingleton().saveImage(dotty, "" + fileChooser.getSelectedFile());
if (msg != null)
JOptionPane.showMessageDialog(null, msg, "Error saving graph image", JOptionPane.ERROR_MESSAGE);
}
});
result.add(menuitem);
return result;
}
/**
* Get the minimum version of Weka, inclusive, the class
* is designed to work with. eg: 3.5.0
*
* @return the minimum version
*/
public String getMinVersion() {
return "3.7.9";
}
/**
* Get the maximum version of Weka, exclusive, the class
* is designed to work with. eg: 3.6.0
*
* @return the maximum version
*/
public String getMaxVersion() {
return "3.10.0";
}
/**
* Get the specific version of Weka the class is designed for.
* eg: 3.5.1
*
* @return the version the plugin was designed for
*/
public String getDesignVersion() {
return "3.7.11";
}
}