📄 visualtreepanel.java
字号:
package ai.decision.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import ai.decision.algorithm.*;
import ai.common.*;
/**
* A panel that displays a visual representation of a decision
* tree.
*
* <p>
* <b>Change History:</b>
*
* <p><pre>
* Name: Date: Change:
* =============================================================
* J. Kelly Jun-18-2000 Created.
* J. Kelly Jun-27-2000 Added new positioning
* algorithm.
* J. Kelly Oct-11-2000 Updated to reflect
* changes to GUI classes.
* J. Kelly Nov-15-2000 Moved formatting code to
* TreeLayoutPanel.
* </pre>
*
* Copyright 2000 University of Alberta.
*
* <!--
* This file is part of the Decision Tree Applet.
*
* The Decision Tree Applet 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.
*
* Foobar 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 the Decision Tree Applet; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* -->
*/
public class VisualTreePanel
extends TreeLayoutPanel
implements ActionListener,
MouseListener,
MouseMotionListener
{
// Class data members
private static final String SHOW_TRAINING_RESULT = "Show Training Results";
private static final String SHOW_TESTING_RESULT = "Show Testing Results";
private static final int TRAINING = 0;
private static final int TESTING = 1;
// Instance data members
GradientBar m_gradBar; // Gradient bar (node colors).
PieChart m_chart; // Performance chart.
Object m_drag; // Object being dragged.
AbstractTreeNode m_tempNode; // Temporary node.
VisualTreeNode m_tempParent; // Temporary parent node.
int m_tempArcNum; // Temporary arc number.
AttributeMask m_tempMask; // Temporary mask.
int[] m_tempConcl; // Temporary conclusion.
int[] m_tempTrainingCounts; // Temporary counts.
int[] m_tempTestingCounts; // Temporary counts.
// View control.
JCheckBoxMenuItem m_infoOn; // Show/hide graphic stats
JCheckBoxMenuItem m_fullSizeView; // Full size tree display.
JRadioButtonMenuItem m_zoom100; // 100% (full-size) zoom.
JRadioButtonMenuItem m_zoom75; // 75% (3/4-size) zoom.
JRadioButtonMenuItem m_zoom50; // 50% (half-size) zoom.
JRadioButtonMenuItem m_zoom25; // 25% (1/4-size) zoom.
JMenuItem m_showExampleResult; // Show training, or testing
// examples.
JPopupMenu m_viewMenu; // View control popup.
JPopupMenu m_attMenu; // Menu of attributes
int m_resultToShow; // Determines whether the panel shows
// training or testing results.
// Constructors
/**
* Builds a new VisualTreePanel. The panel has a white
* background by default.
*
* @param manager The global component manager.
*
* @throws NullPointerException If the supplied
* ComponentManager is null.
*/
public VisualTreePanel( ComponentManager manager )
{
super( manager );
m_resultToShow = TRAINING;
// Build the panel structure.
buildPanel();
}
// Public methods
/**
* ActionListener interface implementation. The panel
* updates itself, and sends messages to other panels
* as required.
*/
public void actionPerformed( ActionEvent e )
{
Object source = e.getSource();
// Was it a view action?
if( source == m_infoOn )
// Simply repaint to update the display.
repaint();
else if( source == m_showExampleResult )
handleShowResult();
else if( source == m_fullSizeView )
handlePanelResize();
else if( source == m_zoom100 )
handleZoom( m_zoom100 );
else if( source == m_zoom75 )
handleZoom( m_zoom75 );
else if( source == m_zoom50 )
handleZoom( m_zoom50 );
else if( source == m_zoom25 )
handleZoom( m_zoom25 );
else if( source instanceof AttributeMenuItem ||
source instanceof AttributeValueMenuItem )
// An attribute or value was selected, so we'll
// be adding an internal or leaf node to the tree.
handleAttributeOrValueSelected( (JMenuItem)source );
}
/**
* MouseListener interface implementation. The panel
* displays various popup menus in response to mouse
* clicks.
*/
public void mouseClicked( MouseEvent e )
{
// If the right mouse button is clicked,
// the zoom menu pops up.
if( SwingUtilities.isRightMouseButton( e ) )
m_viewMenu.show( e.getComponent(), e.getX(), e.getY() );
// If the left button is clicked:
// - find out if it was clicked on a node
// and display appropriate info or menu, or
// - do nothing.
else if( SwingUtilities.isLeftMouseButton( e ) )
if( handleNodeClickDetect( e.getX(), e.getY() ) )
m_attMenu.show( e.getComponent(), e.getX(), e.getY() );
}
public void mouseEntered( MouseEvent e ) {}
public void mouseExited( MouseEvent e ) {}
public void mousePressed( MouseEvent e )
{
// Determine if the user clicked on the pie chart or gradient bar.
if( SwingUtilities.isLeftMouseButton( e ) &&
m_infoOn.isSelected() ) {
if( m_gradBar.hitDetect( e.getX(), e.getY() ) ) {
m_drag = m_gradBar;
addMouseMotionListener( this );
}
else if( m_chart.hitDetect( e.getX(), e.getY() ) ) {
m_drag = m_chart;
addMouseMotionListener( this );
}
else {
m_drag = null;
}
}
}
public void mouseReleased( MouseEvent e )
{
removeMouseMotionListener( this );
}
/**
* MouseMotionListener interface implementation. A user
* can click and drag any of the statistical displays
* to move them out of the way.
*/
public void mouseDragged( MouseEvent e )
{
if( m_drag == m_gradBar )
m_gradBar.move( e.getX(), e.getY() );
else if( m_drag == m_chart )
m_chart.move( e.getX(), e.getY() );
repaint();
}
public void mouseMoved( MouseEvent e ) {}
public void setViewport( JViewport viewport )
{
super.setViewport( viewport );
// Now that we have a viewport, we can
// setup a pie chart and gradient color bar
m_gradBar = new GradientBar( this, viewport );
m_chart = new PieChart( this );
if( m_manager.getAlgorithm() != null ) {
m_chart.updateTrainingPerformance(
m_manager.getAlgorithm().getTree().getNumTrainingEgCorrectClass(),
m_manager.getAlgorithm().getDataset().getNumTrainingExamples() );
m_chart.updateTestingPerformance(
m_manager.getAlgorithm().getTree().getNumTestingEgCorrectClass(),
m_manager.getAlgorithm().getDataset().getNumTestingExamples() );
}
}
/**
* Notifies the panel that a new tree has been
* created. The panel will clear itself in response.
*/
public void notifyNewTree()
{
super.notifyNewTree();
// Update performance chart.
if( m_chart != null ) {
m_chart.reset();
m_chart.updateTrainingPerformance( 0,
m_manager.getAlgorithm().getDataset().getNumTrainingExamples() );
m_chart.updateTestingPerformance( 0,
m_manager.getAlgorithm().getDataset().getNumTestingExamples() );
}
if( m_gradBar != null ) m_gradBar.reset();
// Repaint the panel.
repaint();
}
/**
* Notifies the panel that a node has been added to
* the tree. The panel recomputes the optimal display
* configuration for the tree with the new node attached
* and then repaints the display.
*
* @param node The most recently added node.
*/
public void notifyNodeAdded( DecisionTreeNode node )
{
super.notifyNodeAdded( node );
refresh();
}
/**
* Notifies the panel that a node had been removed
* from the tree. The panel recomputes the optimal
* display configuration for the tree with the
* node removed and then repaints the display.
*
* <p>
* This method can be used to remove <i>any</i> visual
* node within the tree - the structure of the entire
* tree is rebuilt before any painting occurs.
*
* @param node The most recently removed node.
*/
public void notifyNodeRemoved( DecisionTreeNode node )
{
super.notifyNodeRemoved( node );
refresh();
}
/**
* A utility method that refreshes and updates the
* display (including the pie chart).
*/
public void refresh()
{
// Update the state of the pie chart tracking tree performance.
if( m_chart != null ) {
m_chart.updateNumNodes(
m_manager.getAlgorithm().getTree().getNumNodes(),
m_manager.getAlgorithm().getTree().getNumInternalNodes() );
m_chart.updateTrainingPerformance(
m_manager.getAlgorithm().getTree().getNumTrainingEgCorrectClass(),
m_manager.getAlgorithm().getDataset().getNumTrainingExamples() );
m_chart.updateTestingPerformance(
m_manager.getAlgorithm().getTree().getNumTestingEgCorrectClass(),
m_manager.getAlgorithm().getDataset().getNumTestingExamples() );
}
repaint();
}
/**
* Paints a visual representation of the current tree.
*/
public void paintComponent( Graphics g )
{
super.paintComponent( g );
// Paint additional graphic objects - the tree
// will appear underneath.
if( m_infoOn.isSelected() ) {
// Draw pie chart, gradient bar etc.
if( m_chart != null )
m_chart.paintChart( g );
if( m_gradBar != null )
m_gradBar.paintBar( g );
}
}
// Private methods
/**
* Toggles the full-size tree view on and off. The
* dataset and algorithm panels are hidden when the
* tree is viewed at full-size.
*/
private void handlePanelResize()
{
if( m_fullSizeView.isSelected() ) {
if( m_manager.getAlgorithmPanel() != null )
m_manager.getAlgorithmPanel().setVisible( false );
if( m_manager.getDatasetPanel() != null )
m_manager.getDatasetPanel().setVisible( false );
}
else {
if( m_manager.getAlgorithmPanel() != null )
m_manager.getAlgorithmPanel().setVisible( true );
if( m_manager.getDatasetPanel() != null )
m_manager.getDatasetPanel().setVisible( true );
}
revalidate();
if( m_chart != null ) m_chart.adjust();
if( m_gradBar != null ) m_gradBar.adjust();
}
/**
* Sets the current zoom state, based on the
* selected zoom level. The panel is resized and
* repainted if the zoom level has changed.
*/
private void handleZoom( JRadioButtonMenuItem zoomLevel )
{
boolean changed = false;
if( zoomLevel == m_zoom100 && SCALING_FACTOR != 1.0 ) {
changed = true;
// Set the global zoom level.
SCALING_FACTOR = 1.0;
}
else if( zoomLevel == m_zoom75 && SCALING_FACTOR != 0.75 ) {
changed = true;
// Set the global zoom level.
SCALING_FACTOR = 0.75;
}
else if( zoomLevel == m_zoom50 && SCALING_FACTOR != 0.50 ) {
changed = true;
// Set the global zoom level.
SCALING_FACTOR = 0.50;
}
else if( zoomLevel == m_zoom25 && SCALING_FACTOR != 0.25 ) {
changed = true;
// Set the global zoom level.
SCALING_FACTOR = 0.25;
}
if( changed ) {
// Resize the panel and repaint.
handlePanelAdjust( null );
repaint();
}
}
/**
* Handles and coordinates operation when the user
* selects the 'show training / testing results' menu item.
*/
private void handleShowResult()
{
m_resultToShow = (m_resultToShow == TRAINING ? TESTING : TRAINING );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -