weka.gui.visualize.plugins.GraphVizTreeVisualizationPlugin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphviz-treevisualize-weka-package Show documentation
Show all versions of graphviz-treevisualize-weka-package Show documentation
Contains TreeVisualize plugin for the Explorer for visualization trees (eg J48) using GraphViz.
The newest version!
/*
* 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-2022 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;
import java.io.File;
/**
* 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)
*/
public class GraphVizTreeVisualizationPlugin
implements TreeVisualizePlugin {
/** the filechooser for saving the dotty graph. */
protected JFileChooser m_FileChooserDotty;
/** the filechooser for saving the image. */
protected JFileChooser m_FileChooserImage;
/** the filechooser for exporting the grqpah. */
protected JFileChooser m_FileChooserExport;
/**
* 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[]{".dot", ".dotty"},
"GraphViz dot file (.dot, .dotty)");
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);
for (String ext: GraphVizTreeVisualization.getSingleton().getImageExtension()) {
filter = new ExtensionFileFilter(new String[]{"." + ext}, ext.toUpperCase() + " file");
m_FileChooserImage.addChoosableFileFilter(filter);
if (ext.toLowerCase().equals("png"))
m_FileChooserImage.setFileFilter(filter);
}
}
return m_FileChooserImage;
}
/**
* Returns the filechooser to use for exporting the graph.
*
* @return the filechooser
*/
protected JFileChooser getFileChooserExport() {
ExtensionFileFilter filterPS;
ExtensionFileFilter filterEPS;
ExtensionFileFilter filterPDF;
if (m_FileChooserExport == null) {
filterPS = new ExtensionFileFilter(
new String[]{".ps"},
"Postscript (.ps)");
filterEPS = new ExtensionFileFilter(
new String[]{".eps"},
"Encapsulated postscript (.eps)");
filterPDF = new ExtensionFileFilter(
new String[]{".pdf"},
"PDF (.pdf)");
m_FileChooserExport = new JFileChooser();
m_FileChooserExport.addChoosableFileFilter(filterPDF);
m_FileChooserExport.addChoosableFileFilter(filterPS);
m_FileChooserExport.addChoosableFileFilter(filterEPS);
m_FileChooserExport.setFileFilter(filterPDF);
m_FileChooserExport.setFileSelectionMode(JFileChooser.FILES_ONLY);
m_FileChooserExport.setAcceptAllFileFilterUsed(false);
}
return m_FileChooserExport;
}
/**
* Adds the extension of the filter, if file has none.
*
* @param file the file to check
* @param filter the filter to get the extension from
* @return the potentially updated file
*/
protected File fixExtension(File file, ExtensionFileFilter filter) {
String[] exts;
boolean ok;
if ((filter == null) || (file == null))
return file;
exts = filter.getExtensions();
ok = false;
for (String ext: exts) {
if (file.getName().toLowerCase().endsWith(ext)) {
ok = true;
break;
}
}
if (ok)
return file;
else
return new File(file.getAbsolutePath() + exts[0]);
}
/**
* 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;
ExtensionFileFilter filter = (ExtensionFileFilter) fileChooser.getFileFilter();
String msg = GraphVizTreeVisualization.getSingleton().saveDotty(
dotty,
"" + fixExtension(fileChooser.getSelectedFile(), filter));
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;
ExtensionFileFilter filter = (ExtensionFileFilter) fileChooser.getFileFilter();
String msg = GraphVizTreeVisualization.getSingleton().saveImage(
dotty,
filter.getExtensions()[0].substring(1),
"" + fixExtension(fileChooser.getSelectedFile(), filter));
if (msg != null)
JOptionPane.showMessageDialog(null, msg, "Error saving graph image", JOptionPane.ERROR_MESSAGE);
}
});
result.add(menuitem);
menuitem = new JMenuItem("Export graph...");
menuitem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = getFileChooserExport();
int retVal = fileChooser.showSaveDialog(null);
if (retVal != JFileChooser.APPROVE_OPTION)
return;
ExtensionFileFilter filter = (ExtensionFileFilter) fileChooser.getFileFilter();
String msg = GraphVizTreeVisualization.getSingleton().export(
dotty,
filter.getExtensions()[0].substring(1),
"" + fixExtension(fileChooser.getSelectedFile(), filter));
if (msg != null)
JOptionPane.showMessageDialog(null, msg, "Error exporting graph", 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 "4.0.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";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy