Package

Source Code of GraphPanel

// $Id: GraphPanel.java,v 1.1 2007/01/30 16:21:23 jtrent Exp $

import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.Dimension;
import java.util.HashMap;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.Box;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import javax.swing.RepaintManager;

/**
* GraphPanel is a JPanel that organizes two StringRadarGraphs, one for costs,
* and one for benefits. It also handles clicks on labels and passes the information
* upward.
*
* @author Jason R Trent - jtrent@sandia.gov - Sandia National Laboratories
* @version $Revision: 1.1 $
*/
public class GraphPanel extends JPanel implements MouseListener, Printable
{
  private MainFrame mainFrameRef;

  private SmoothJLabel evalNameLabel,
            evalProductLabel,
            evalDateLabel;
 
  private final static int POINTS_PER_INCH = 72;

  public GraphPanel( MainFrame mainFrameRef )
  {
    super();
    setLayout( new BorderLayout( 10, 30 ) );

    this.mainFrameRef = mainFrameRef;
  }

  public void updateGraphs()
  {
    if( mainFrameRef == null )
      return;

    removeAll();

    JPanel graphPanel = new JPanel();
    graphPanel.setLayout( new BoxLayout( graphPanel, BoxLayout.Y_AXIS ) );

    if( mainFrameRef.getEvalProductName() != "" )
    {
          evalNameLabel = new SmoothJLabel( "<html><font size=+1>" + mainFrameRef.getEvalProductName() + "</font></html>", SwingConstants.CENTER );
      evalNameLabel.setAlignmentX( Component.CENTER_ALIGNMENT );
      evalNameLabel.addMouseListener( this );
          graphPanel.add( evalNameLabel );
    }
    if( mainFrameRef.getEvaluatorName() != "" )
    {
          evalProductLabel = new SmoothJLabel( "<html>Evaluated by " + mainFrameRef.getEvaluatorName() + "</html>", SwingConstants.CENTER );
      evalProductLabel.setAlignmentX( Component.CENTER_ALIGNMENT );
      evalProductLabel.addMouseListener( this );
          graphPanel.add( evalProductLabel );
    }

    String evalDataString = new SimpleDateFormat( "MMMMM dd, yyyy" ).format(new Date());
        evalDateLabel = new SmoothJLabel( "<html>Evaluated " + evalDataString + "</html>", SwingConstants.CENTER );
    evalDateLabel.setAlignmentX( Component.CENTER_ALIGNMENT );
    evalDateLabel.addMouseListener( this );
         graphPanel.add( evalDateLabel );

        graphPanel.add( Box.createRigidArea( new Dimension( 0, 5 ) ) );

        SmoothJLabel costLabel = new SmoothJLabel( "<html><font size=+1>Costs</font></html>", SwingConstants.CENTER );
    costLabel.setAlignmentX( Component.CENTER_ALIGNMENT );
        graphPanel.add( costLabel );
    StringRadarGraph costGraph = new StringRadarGraph( this, mainFrameRef.getCostMap(), -1 );
        costGraph.setMinimumSize( new Dimension( 300, 300 ) );
    costGraph.setSize( new Dimension( 300, 300 ) );
    costGraph.setTickCount( 5 );
    costGraph.setFillColor( StringRadarGraph.REDFILL );
    costGraph.setAlignmentX( Component.CENTER_ALIGNMENT );
    costGraph.setAlignmentY( Component.CENTER_ALIGNMENT );
    costGraph.setXOffset( 100 );
    graphPanel.add( costGraph );

        graphPanel.add( Box.createRigidArea( new Dimension( 0, 10 ) ) );

        SmoothJLabel benefitLabel = new SmoothJLabel( "<html><font size=+1>Benefits</font></html>", SwingConstants.CENTER );
    benefitLabel.setAlignmentX( Component.CENTER_ALIGNMENT );
        graphPanel.add( benefitLabel );
       StringRadarGraph benefitGraph = new StringRadarGraph( this, mainFrameRef.getBenefitMap(), -1 );
        benefitGraph.setMinimumSize( new Dimension( 300, 300 ) );
    benefitGraph.setSize( new Dimension( 300, 300 ) );
    benefitGraph.setTickCount( 5 );
    benefitGraph.setFillColor( StringRadarGraph.GREENFILL );
    benefitGraph.setAlignmentX( Component.CENTER_ALIGNMENT );
    benefitGraph.setAlignmentY( Component.CENTER_ALIGNMENT );
    benefitGraph.setXOffset( 100 );
    graphPanel.add( benefitGraph );

    add( graphPanel, BorderLayout.CENTER );

    return;
  }
 
  public int print( Graphics g, PageFormat pageFormat, int pageIndex )
  {
    if( pageIndex > 0 )
    {
          return NO_SUCH_PAGE;
      }
    else
    {
      Graphics2D g2d = (Graphics2D) g;
      g2d.translate( pageFormat.getImageableX (), pageFormat.getImageableY () );

            RepaintManager currentManager = RepaintManager.currentManager( this);
      currentManager.setDoubleBufferingEnabled( false );

      paint( g2d );

      currentManager.setDoubleBufferingEnabled( true );

      return PAGE_EXISTS;
    }
  }

  public void printGraphs()
  {
    PrinterJob printJob = PrinterJob.getPrinterJob();

      printJob.setPrintable( this );
      if( printJob.printDialog() )
      {
      try
      {
            printJob.print();
      }
        catch( PrinterException pe )
      {
            System.out.println("Error printing: " + pe);
          }
       }

    return;
  }

  public void mouseClicked( MouseEvent event )
  {
    mainFrameRef.popupEvalInfoPanel();
    return;
  }

    public void graphLabelClicked( String labelClicked )
    {
         mainFrameRef.popupValueSelection( labelClicked );
         return;
  }

  public void mouseEntered( MouseEvent event ) {}
  public void mouseExited( MouseEvent event ) {}
  public void mousePressed( MouseEvent event ) {}
  public void mouseReleased( MouseEvent event ) {}
};
TOP

Related Classes of GraphPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.