📄 treevisualizer.java
字号:
/*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* TreeVisualizer.java
* Copyright (C) 1999 Malcolm Ware
*
*/
package weka.gui.treevisualizer;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.Timer;
import weka.core.Instances;
import weka.gui.visualize.VisualizePanel;
/**
* Class for displaying a Node structure in Swing. <p>
*
* To work this class simply create an instance of it.<p>
*
* Assign it to a window or other such object.<p>
*
* Resize it to the desired size.<p>
*
*
* When using the Displayer hold the left mouse button to drag the
* tree around. <p>
*
* Click the left mouse button with ctrl to shrink the size of the tree
* by half. <p>
*
* Click and drag with the left mouse button and shift to draw a box,
* when the left mouse button is released the contents of the box
* will be magnified
* to fill the screen. <p> <p>
*
* Click the right mouse button to bring up a menu. <p>
* Most options are self explanatory.<p>
*
* Select Auto Scale to set the tree to it's optimal display size.
*
* @author Malcolm Ware (mfw4@cs.waikato.ac.nz)
* @version $Revision$
*/
public class TreeVisualizer extends JPanel implements MouseMotionListener,
MouseListener,ActionListener,ItemListener {
/**
*
*/
private static final long serialVersionUID = -8668637962504080749L;
/** The placement algorithm for the Node structure. */
private NodePlace m_placer;
/** The top Node. */
private Node m_topNode;
/** The postion of the view relative to the tree. */
private Dimension m_viewPos;
/** The size of the tree in pixels. */
private Dimension m_viewSize;
/** The font used to display the tree. */
private Font m_currentFont;
/** The size information for the current font. */
private FontMetrics m_fontSize;
/** The number of Nodes in the tree. */
private int m_numNodes;
/** The number of levels in the tree. */
private int m_numLevels;
/** An array with the Nodes sorted into it and display information
* about the Nodes. */
private NodeInfo[] m_nodes;
/** An array with the Edges sorted into it and display information
* about the Edges. */
private EdgeInfo[] m_edges;
/** A timer to keep the frame rate constant. */
private Timer m_frameLimiter;
/** Describes the action the user is performing. */
private int m_mouseState;
/** A variable used to tag the start pos of a user action. */
private Dimension m_oldMousePos;
/** A variable used to tag the most current point of a user action. */
private Dimension m_newMousePos;
/** A variable used to determine for the clicked method if any other
* mouse state has already taken place. */
private boolean m_clickAvailable;
/** A variable used to remember the desired view pos. */
private Dimension m_nViewPos;
/** A variable used to remember the desired tree size. */
private Dimension m_nViewSize;
/** The number of frames left to calculate. */
private int m_scaling;
/** A right (or middle) click popup menu. */
private JPopupMenu m_winMenu;
/** An option on the win_menu */
private JMenuItem m_topN;
/** An option on the win_menu*/
private JMenuItem m_fitToScreen;
/** An option on the win_menu */
private JMenuItem m_autoScale;
/** A ub group on the win_menu */
private JMenu m_selectFont;
/** A grouping for the font choices */
private ButtonGroup m_selectFontGroup;
/** A font choice. */
private JRadioButtonMenuItem m_size24;
/** A font choice. */
private JRadioButtonMenuItem m_size22;
/** A font choice. */
private JRadioButtonMenuItem m_size20;
/** A font choice. */
private JRadioButtonMenuItem m_size18;
/** A font choice. */
private JRadioButtonMenuItem m_size16;
/** A font choice. */
private JRadioButtonMenuItem m_size14;
/** A font choice. */
private JRadioButtonMenuItem m_size12;
/** A font choice. */
private JRadioButtonMenuItem m_size10;
/** A font choice. */
private JRadioButtonMenuItem m_size8;
/** A font choice. */
private JRadioButtonMenuItem m_size6;
/** A font choice. */
private JRadioButtonMenuItem m_size4;
/** A font choice. */
private JRadioButtonMenuItem m_size2;
/** A font choice. */
private JRadioButtonMenuItem m_size1;
/** An option on the win menu. */
private JMenuItem m_accept;
/** A right or middle click popup menu for nodes. */
private JPopupMenu m_nodeMenu;
/** A visualize choice for the node, may not be available. */
private JMenuItem m_visualise;
/** Similar to add children but now it removes children. */
private JMenuItem m_remChildren;
/** Use this to have J48 classify this node. */
private JMenuItem m_classifyChild;
/** Use this to dump the instances from this node to the vis panel. */
private JMenuItem m_sendInstances;
/** The subscript for the currently selected node (this is an internal
* thing, so the user is unaware of this). */
private int m_focusNode;
/**
* The Node the user is currently focused on , this is similar to
* focus node except that it is used by other
* classes rather than this one.
*/
private int m_highlightNode;
/* A pointer to this tree's classifier if a classifier is using it. */
//private UserClassifier classer;
private TreeDisplayListener m_listener;
/**
* Constructs Displayer to display a tree provided in a dot format.
* Uses the NodePlacer to place the Nodes.
* @param tdl listener
* @param dot string containing the dot representation of the tree to
* display
* @param p the algorithm to be used to position the nodes.
*/
public TreeVisualizer(TreeDisplayListener tdl, String dot, NodePlace p) {
//generate the node structure in here
setBorder(BorderFactory.createTitledBorder("Tree View"));
m_listener = tdl;
TreeBuild builder = new TreeBuild();
Node n = null;
n = builder.create(new StringReader(dot));
// System.out.println(n.getCount(n, 0));
//if the size needs to be automatically alocated I will do it here
m_highlightNode = 5;
m_topNode = n;
m_placer = p;
m_placer.place(m_topNode);
m_viewPos = new Dimension(0, 0); //will be adjusted
m_viewSize = new Dimension(800, 600); //I allocate this now so that
//the tree will be visible
//when the panel is enlarged
m_nViewPos = new Dimension(0, 0);
m_nViewSize = new Dimension(800, 600);
m_scaling = 0;
m_numNodes = Node.getCount(m_topNode,0); //note the second
//argument must be a zero, this is a
//recursive function
m_numLevels = Node.getHeight(m_topNode,0);
m_nodes = new NodeInfo[m_numNodes];
m_edges = new EdgeInfo[m_numNodes-1];
arrayFill(m_topNode, m_nodes, m_edges);
changeFontSize(12);
m_mouseState = 0;
m_oldMousePos = new Dimension(0, 0);
m_newMousePos = new Dimension(0, 0);
m_frameLimiter = new Timer(120, this);
m_winMenu = new JPopupMenu();
m_topN = new JMenuItem("Center on Top Node"); //note to change
//language change this line
m_topN.setActionCommand("Center on Top Node"); //but not this one,
//same for all menu items
m_fitToScreen = new JMenuItem("Fit to Screen");
m_fitToScreen.setActionCommand("Fit to Screen");
//unhide = new JMenuItem("Unhide all Nodes");
m_selectFont = new JMenu("Select Font");
m_selectFont.setActionCommand("Select Font");
m_autoScale = new JMenuItem("Auto Scale");
m_autoScale.setActionCommand("Auto Scale");
m_selectFontGroup = new ButtonGroup();
m_accept = new JMenuItem("Accept The Tree");
m_accept.setActionCommand("Accept The Tree");
m_winMenu.add(m_topN);
m_winMenu.addSeparator();
m_winMenu.add(m_fitToScreen);
m_winMenu.add(m_autoScale);
m_winMenu.addSeparator();
//m_winMenu.add(unhide);
m_winMenu.addSeparator();
m_winMenu.add(m_selectFont);
m_winMenu.addSeparator();
if (m_listener != null) {
m_winMenu.add(m_accept);
}
m_topN.addActionListener(this);
m_fitToScreen.addActionListener(this);
//unhide.addActionListener(this);
m_autoScale.addActionListener(this);
m_accept.addActionListener(this);
m_size24 = new JRadioButtonMenuItem("Size 24",false);//,select_font_group);
m_size22 = new JRadioButtonMenuItem("Size 22",false);//,select_font_group);
m_size20 = new JRadioButtonMenuItem("Size 20",false);//,select_font_group);
m_size18 = new JRadioButtonMenuItem("Size 18",false);//,select_font_group);
m_size16 = new JRadioButtonMenuItem("Size 16",false);//,select_font_group);
m_size14 = new JRadioButtonMenuItem("Size 14",false);//,select_font_group);
m_size12 = new JRadioButtonMenuItem("Size 12",true);//,select_font_group);
m_size10 = new JRadioButtonMenuItem("Size 10",false);//,select_font_group);
m_size8 = new JRadioButtonMenuItem("Size 8",false);//,select_font_group);
m_size6 = new JRadioButtonMenuItem("Size 6",false);//,select_font_group);
m_size4 = new JRadioButtonMenuItem("Size 4",false);//,select_font_group);
m_size2 = new JRadioButtonMenuItem("Size 2",false);//,select_font_group);
m_size1 = new JRadioButtonMenuItem("Size 1",false);//,select_font_group);
m_size24.setActionCommand("Size 24");//,select_font_group);
m_size22.setActionCommand("Size 22");//,select_font_group);
m_size20.setActionCommand("Size 20");//,select_font_group);
m_size18.setActionCommand("Size 18");//,select_font_group);
m_size16.setActionCommand("Size 16");//,select_font_group);
m_size14.setActionCommand("Size 14");//,select_font_group);
m_size12.setActionCommand("Size 12");//,select_font_group);
m_size10.setActionCommand("Size 10");//,select_font_group);
m_size8.setActionCommand("Size 8");//,select_font_group);
m_size6.setActionCommand("Size 6");//,select_font_group);
m_size4.setActionCommand("Size 4");//,select_font_group);
m_size2.setActionCommand("Size 2");//,select_font_group);
m_size1.setActionCommand("Size 1");//,select_font_group);
m_selectFontGroup.add(m_size24);
m_selectFontGroup.add(m_size22);
m_selectFontGroup.add(m_size20);
m_selectFontGroup.add(m_size18);
m_selectFontGroup.add(m_size16);
m_selectFontGroup.add(m_size14);
m_selectFontGroup.add(m_size12);
m_selectFontGroup.add(m_size10);
m_selectFontGroup.add(m_size8);
m_selectFontGroup.add(m_size6);
m_selectFontGroup.add(m_size4);
m_selectFontGroup.add(m_size2);
m_selectFontGroup.add(m_size1);
m_selectFont.add(m_size24);
m_selectFont.add(m_size22);
m_selectFont.add(m_size20);
m_selectFont.add(m_size18);
m_selectFont.add(m_size16);
m_selectFont.add(m_size14);
m_selectFont.add(m_size12);
m_selectFont.add(m_size10);
m_selectFont.add(m_size8);
m_selectFont.add(m_size6);
m_selectFont.add(m_size4);
m_selectFont.add(m_size2);
m_selectFont.add(m_size1);
m_size24.addItemListener(this);
m_size22.addItemListener(this);
m_size20.addItemListener(this);
m_size18.addItemListener(this);
m_size16.addItemListener(this);
m_size14.addItemListener(this);
m_size12.addItemListener(this);
m_size10.addItemListener(this);
m_size8.addItemListener(this);
m_size6.addItemListener(this);
m_size4.addItemListener(this);
m_size2.addItemListener(this);
m_size1.addItemListener(this);
/*
search_string = new JTextField(22);
search_win = new JDialog();
case_sen = new JRadioButton("Case Sensitive");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -