LegendPanel.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 8k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    LegendPanel.java
  18.  *    Copyright (C) 2000 Mark Hall
  19.  *
  20.  */
  21. package weka.gui.visualize;
  22. import weka.core.FastVector;
  23. import weka.core.Utils;
  24. import weka.core.Instances;
  25. import java.util.Random;
  26. import java.awt.BorderLayout;
  27. import java.awt.GridLayout;
  28. import java.awt.GridBagLayout;
  29. import java.awt.GridBagConstraints;
  30. import java.awt.Insets;
  31. import java.awt.event.WindowAdapter;
  32. import java.awt.event.WindowEvent;
  33. import java.awt.event.MouseAdapter;
  34. import java.awt.event.MouseEvent;
  35. import java.awt.Dimension;
  36. import javax.swing.JPanel;
  37. import javax.swing.JLabel;
  38. import javax.swing.JScrollPane;
  39. import javax.swing.JSlider;
  40. import javax.swing.JColorChooser;
  41. import java.awt.Color;
  42. import java.awt.FontMetrics;
  43. import java.awt.Graphics;
  44. import java.awt.Component;
  45. /**
  46.  * This panel displays legends for a list of plots. If a given plot
  47.  * has a custom colour defined then this panel allows the colour to
  48.  * be changed.
  49.  *
  50.  * @author Mark Hall (mhall@cs.waikato.ac.nz)
  51.  * @version $Revision: 1.3 $
  52.  */
  53. public class LegendPanel extends JScrollPane {
  54.   /** the list of plot elements */
  55.   protected FastVector m_plots;
  56.   /** the panel that contains the legend entries */
  57.   protected JPanel m_span=null;
  58.   /** a list of components that need to be repainted when a colour is
  59.       changed */
  60.   protected FastVector m_Repainters = new FastVector();
  61.   /**
  62.    * Inner class for handling legend entries
  63.    */
  64.   protected class LegendEntry extends JPanel {
  65.     /** the data for this legend entry */
  66.     private PlotData2D m_plotData=null;
  67.     /** the index (in the list of plots) of the data for this legend---
  68. used to draw the correct shape for this data */
  69.     private int m_dataIndex;
  70.     /** the text part of this legend */
  71.     private JLabel m_legendText;
  72.     /** displays the point shape associated with this legend entry */
  73.     private JPanel m_pointShape;
  74.     public LegendEntry(PlotData2D data, int dataIndex) {
  75.       m_plotData = data;
  76.       m_dataIndex = dataIndex;
  77.       //      this.setBackground(Color.black);
  78.       /*      this.setPreferredSize(new Dimension(0, 20));
  79.       this.setMinimumSize(new Dimension(0, 20)); */
  80.       if (m_plotData.m_useCustomColour) {
  81. this.addMouseListener(new MouseAdapter() {
  82.     public void mouseClicked(MouseEvent e) {
  83.       
  84.       if ((e.getModifiers() & e.BUTTON1_MASK) == e.BUTTON1_MASK) {
  85. Color tmp = JColorChooser.showDialog
  86.   (LegendPanel.this, "Select new Color", 
  87.    m_plotData.m_customColour);
  88. if (tmp != null) {
  89.   m_plotData.m_customColour = tmp;
  90.   m_legendText.setForeground(tmp);
  91.   if (m_Repainters.size() > 0) {
  92.     for (int i=0;i<m_Repainters.size();i++) {
  93.       ((Component)(m_Repainters.elementAt(i))).repaint();
  94.     }
  95.   }
  96.   LegendPanel.this.repaint();
  97. }
  98.       }
  99.     }
  100.   });
  101.       }
  102.       m_legendText = new JLabel(m_plotData.m_plotName);
  103.       if (m_plotData.m_useCustomColour) {
  104. m_legendText.setForeground(m_plotData.m_customColour);
  105.       }
  106.       this.setLayout(new BorderLayout());
  107.       this.add(m_legendText, BorderLayout.CENTER);
  108.       /*      GridBagLayout gb = new GridBagLayout();
  109.       GridBagConstraints constraints = new GridBagConstraints();
  110.       constraints.fill = GridBagConstraints.HORIZONTAL;
  111.       constraints.gridx=0;constraints.gridy=0;constraints.weightx=5; */
  112.       m_pointShape = new JPanel() {
  113. public void paintComponent(Graphics gx) {
  114.   super.paintComponent(gx);
  115.   if (!m_plotData.m_useCustomColour) {
  116.     gx.setColor(Color.white);
  117.   } else {
  118.     gx.setColor(m_plotData.m_customColour);
  119.   }
  120.   Plot2D.drawDataPoint(10,10,3,m_dataIndex,gx);
  121. }
  122.       };
  123.       //      m_pointShape.setBackground(Color.black);
  124.       m_pointShape.setPreferredSize(new Dimension(20, 20));
  125.       m_pointShape.setMinimumSize(new Dimension(20, 20));
  126.       this.add(m_pointShape, BorderLayout.WEST);
  127.     }
  128.   }
  129.   /**
  130.    * Constructor
  131.    */
  132.   public LegendPanel() {
  133.     this.setBackground(Color.blue);
  134.     setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
  135.   }
  136.   /**
  137.    * Set the list of plots to generate legend entries for
  138.    * @param pl a list of plots
  139.    */
  140.   public void setPlotList(FastVector pl) {
  141.     m_plots = pl;
  142.     updateLegends();
  143.   }
  144.   /**
  145.    * Adds a component that will need to be repainted if the user
  146.    * changes the colour of a label.
  147.    * @param c the component to be repainted
  148.    */
  149.   public void addRepaintNotify(Component c) {
  150.     m_Repainters.addElement(c);
  151.   }
  152.   /**
  153.    * Redraw the panel with the legend entries
  154.    */
  155.   private void updateLegends() {
  156.     if (m_span == null) {
  157.       m_span = new JPanel();
  158.     }
  159.       
  160.     JPanel padder = new JPanel();
  161.     JPanel padd2 = new JPanel();
  162.     m_span.setPreferredSize(new Dimension(m_span.getPreferredSize().width, 
  163.   (m_plots.size() + 1) * 20));
  164.     m_span.setMaximumSize(new Dimension(m_span.getPreferredSize().width, 
  165.   (m_plots.size() + 1) * 20));
  166.     LegendEntry tmp;
  167.     GridBagLayout gb = new GridBagLayout();
  168.     GridBagLayout gb2 = new GridBagLayout();
  169.     GridBagConstraints constraints = new GridBagConstraints();
  170.       
  171.     m_span.removeAll();
  172.     padder.setLayout(gb);
  173.     m_span.setLayout(gb2);
  174.     constraints.anchor = GridBagConstraints.CENTER;
  175.     constraints.gridx=0;constraints.gridy=0;constraints.weightx=5;
  176.     constraints.fill = GridBagConstraints.HORIZONTAL;
  177.     constraints.gridwidth=1;constraints.gridheight=1;
  178.     constraints.insets = new Insets(0, 0, 0, 0);
  179.     padder.add(m_span, constraints);
  180.     constraints.gridx=0;constraints.gridy=1;constraints.weightx=5;
  181.     constraints.fill = GridBagConstraints.BOTH;
  182.     constraints.gridwidth=1;constraints.gridheight=1;constraints.weighty=5;
  183.     constraints.insets = new Insets(0, 0, 0, 0);
  184.     padder.add(padd2, constraints);
  185.     constraints.weighty=0;
  186.     setViewportView(padder);
  187.     constraints.anchor = GridBagConstraints.CENTER;
  188.     constraints.gridx=0;constraints.gridy=0;constraints.weightx=5;
  189.     constraints.fill = GridBagConstraints.HORIZONTAL;
  190.     constraints.gridwidth=1;constraints.gridheight=1;constraints.weighty=5;
  191.     constraints.insets = new Insets(2,4,2,4);
  192.      for (int i=0;i<m_plots.size();i++) {
  193.        tmp = new LegendEntry((PlotData2D)m_plots.elementAt(i),i);
  194.        constraints.gridy = i;
  195.        m_span.add(tmp, constraints);
  196.      }
  197.   }
  198.   /**
  199.    * Main method for testing this class
  200.    * @param args a list of arff files
  201.    */
  202.   public static void main(String [] args) {
  203.     try {
  204.       if (args.length < 1) {
  205. System.err.println("Usage : weka.gui.visualize.LegendPanel "
  206.    +"<dataset> [dataset2], [dataset3],...");
  207. System.exit(1);
  208.       }
  209.       final javax.swing.JFrame jf = 
  210. new javax.swing.JFrame("Weka Knowledge Explorer: Legend");
  211.       jf.setSize(100,100);
  212.       jf.getContentPane().setLayout(new BorderLayout());
  213.       final LegendPanel p2 = new LegendPanel();
  214.       jf.getContentPane().add(p2, BorderLayout.CENTER);
  215.       jf.addWindowListener(new java.awt.event.WindowAdapter() {
  216. public void windowClosing(java.awt.event.WindowEvent e) {
  217.   jf.dispose();
  218.   System.exit(0);
  219. }
  220.       });
  221.       FastVector plotList = new FastVector();
  222.       for (int j=0;j<args.length;j++) {
  223. System.err.println("Loading instances from " + args[j]);
  224. java.io.Reader r = new java.io.BufferedReader(
  225.    new java.io.FileReader(args[j]));
  226. Instances i = new Instances(r);
  227. PlotData2D tmp = new PlotData2D(i);
  228. if (j != 1) {
  229.   tmp.m_useCustomColour = true;
  230.   tmp.m_customColour = Color.red;
  231. }
  232. tmp.setPlotName(i.relationName());
  233. plotList.addElement(tmp);
  234.       }
  235.       
  236.       p2.setPlotList(plotList);
  237.       jf.setVisible(true);
  238.     } catch (Exception ex) {
  239.       System.err.println(ex.getMessage());
  240.       ex.printStackTrace();
  241.     }
  242.   }
  243. }