ResultHistoryPanel.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 12k
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.  *    ResultHistoryPanel.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui;
  22. import java.util.Hashtable;
  23. import java.awt.BorderLayout;
  24. import java.awt.Font;
  25. import java.awt.Point;
  26. import java.awt.event.WindowAdapter;
  27. import java.awt.event.WindowEvent;
  28. import java.awt.event.MouseAdapter;
  29. import java.awt.event.MouseEvent;
  30. import java.awt.event.MouseListener;
  31. import java.awt.event.InputEvent;
  32. import java.awt.event.KeyAdapter;
  33. import java.awt.event.KeyEvent;
  34. import javax.swing.JPanel;
  35. import javax.swing.DefaultListModel;
  36. import javax.swing.JScrollPane;
  37. import javax.swing.JList;
  38. import javax.swing.JFrame;
  39. import javax.swing.JTextArea;
  40. import javax.swing.JViewport;
  41. import javax.swing.ListSelectionModel;
  42. import javax.swing.text.JTextComponent;
  43. import javax.swing.event.ChangeListener;
  44. import javax.swing.event.ChangeEvent;
  45. import javax.swing.event.ListSelectionEvent;
  46. import javax.swing.event.ListSelectionListener;
  47. import javax.swing.BorderFactory;
  48. import java.io.Serializable;
  49. /** 
  50.  * A component that accepts named stringbuffers and displays the name in a list
  51.  * box. When a name is right-clicked, a frame is popped up that contains
  52.  * the string held by the stringbuffer. Optionally a text component may be
  53.  * provided that will have it's text set to the named result text on a
  54.  * left-click.
  55.  *
  56.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  57.  * @version $Revision: 1.17 $
  58.  */
  59. public class ResultHistoryPanel extends JPanel {
  60.   
  61.   /** An optional component for single-click display */
  62.   protected JTextComponent m_SingleText;
  63.   /** The named result being viewed in the single-click display */
  64.   protected String m_SingleName;
  65.   
  66.   /** The list model */
  67.   protected DefaultListModel m_Model = new DefaultListModel();
  68.   /** The list component */
  69.   protected JList m_List = new JList(m_Model);
  70.   
  71.   /** A Hashtable mapping names to result buffers */
  72.   protected Hashtable m_Results = new Hashtable();
  73.   /** A Hashtable mapping names to output text components */
  74.   protected Hashtable m_FramedOutput = new Hashtable();
  75.   /** A hashtable mapping names to arbitrary objects */
  76.   protected Hashtable m_Objs = new Hashtable();
  77.   /** Let the result history list handle right clicks in the default
  78.       manner---ie, pop up a window displaying the buffer */
  79.   protected boolean m_HandleRightClicks = true;
  80.   /**
  81.    * Extension of MouseAdapter that implements Serializable.
  82.    */
  83.   public static class RMouseAdapter 
  84.     extends MouseAdapter implements Serializable {}
  85.  
  86.   
  87.   /**
  88.    * Extension of KeyAdapter that implements Serializable.
  89.    */
  90.   public static class RKeyAdapter 
  91.     extends KeyAdapter implements Serializable {}
  92.   /**
  93.    * Create the result history object
  94.    *
  95.    * @param text the optional text component for single-click display
  96.    */
  97.   public ResultHistoryPanel(JTextComponent text) {
  98.     
  99.     m_SingleText = text;
  100.     m_List.addMouseListener(new RMouseAdapter() {
  101.       public void mouseClicked(MouseEvent e) {
  102. if ((e.getModifiers() & InputEvent.BUTTON1_MASK)
  103.     == InputEvent.BUTTON1_MASK) {
  104.   int index = m_List.locationToIndex(e.getPoint());
  105.   if ((index != -1) && (m_SingleText != null)) {
  106.     setSingle((String)m_Model.elementAt(index));
  107.   }
  108. } else {
  109.   // if there are stored objects then assume that the storer
  110.   // will handle popping up the text in a seperate frame
  111.   if (m_HandleRightClicks) {
  112.     int index = m_List.locationToIndex(e.getPoint());
  113.     if (index != -1) {
  114.       String name = (String)m_Model.elementAt(index);
  115.       openFrame(name);
  116.     }
  117.   }
  118. }
  119.       }
  120.     });
  121.     m_List.addKeyListener(new RKeyAdapter() {
  122.       public void keyReleased(KeyEvent e) {
  123.         if (e.getKeyCode() == KeyEvent.VK_DELETE) {
  124.           int selected = m_List.getSelectedIndex();
  125.           if (selected != -1) {
  126.             removeResult((String)m_Model.elementAt(selected));
  127.           }
  128.         }
  129.       }
  130.     });
  131.     m_List.getSelectionModel()
  132.       .addListSelectionListener(new ListSelectionListener() {
  133.       public void valueChanged(ListSelectionEvent e) {
  134. if (!e.getValueIsAdjusting()) {
  135.   ListSelectionModel lm = (ListSelectionModel) e.getSource();
  136.   for (int i = e.getFirstIndex(); i <= e.getLastIndex(); i++) {
  137.     if (lm.isSelectedIndex(i)) {
  138.       //m_AttSummaryPanel.setAttribute(i);
  139.        if ((i != -1) && (m_SingleText != null)) {
  140.  setSingle((String)m_Model.elementAt(i));
  141.        }
  142.       break;
  143.     }
  144.   }
  145. }
  146.       }
  147.     });
  148.     setLayout(new BorderLayout());
  149.     //    setBorder(BorderFactory.createTitledBorder("Result history"));
  150.     final JScrollPane js = new JScrollPane(m_List);
  151.     js.getViewport().addChangeListener(new ChangeListener() {
  152.       private int lastHeight;
  153.       public void stateChanged(ChangeEvent e) {
  154. JViewport vp = (JViewport)e.getSource();
  155. int h = vp.getViewSize().height; 
  156. if (h != lastHeight) { // i.e. an addition not just a user scrolling
  157.   lastHeight = h;
  158.   int x = h - vp.getExtentSize().height;
  159.   vp.setViewPosition(new Point(0, x));
  160. }
  161.       }
  162.     });
  163.     add(js, BorderLayout.CENTER);
  164.   }
  165.   /**
  166.    * Adds a new result to the result list.
  167.    *
  168.    * @param name the name to associate with the result
  169.    * @param result the StringBuffer that contains the result text
  170.    */
  171.   public void addResult(String name, StringBuffer result) {
  172.     
  173.     m_Model.addElement(name);
  174.     m_Results.put(name, result);
  175.   }
  176.   /**
  177.    * Removes one of the result buffers from the history. Any windows currently
  178.    * displaying the contents of the buffer are not affected.
  179.    *
  180.    * @param name the name of the buffer to remove.
  181.    */
  182.   public void removeResult(String name) {
  183.     StringBuffer buff = (StringBuffer) m_Results.get(name);
  184.     JTextComponent currentText = (JTextComponent) m_FramedOutput.get(name);
  185.     if (buff != null) {
  186.       m_Results.remove(name);
  187.       m_Model.removeElement(name);
  188.     } 
  189.   }
  190.   /**
  191.    * Adds an object to the results list
  192.    * @param name the name to associate with the object
  193.    * @param o the object
  194.    */
  195.   public void addObject(String name, Object o) {
  196.     m_Objs.put(name, o);
  197.   }
  198.   /**
  199.    * Get the named object from the list
  200.    * @param index the index of the item to retrieve the stored object
  201.    * for
  202.    * @return the object or null if there is no object at this index
  203.    */
  204.   public Object getNamedObject(String name) {
  205.     Object v = null;
  206.     v = m_Objs.get(name);
  207.     return v;
  208.   }
  209.   /**
  210.    * Gets the object associated with the currently
  211.    * selected item in the list.
  212.    * @return the object or null if there is no
  213.    * object corresponding to the current selection in
  214.    * the list
  215.    */
  216.   public Object getSelectedObject() {
  217.     Object v = null;
  218.     int index = m_List.getSelectedIndex();
  219.     if (index != -1) {
  220.       String name = (String)(m_Model.elementAt(index));
  221.       v = m_Objs.get(name);
  222.     }
  223.     
  224.     return v;
  225.   }
  226.   /**
  227.    * Gets the named buffer
  228.    * @return the buffer or null if there are no items in
  229.    * the list
  230.    */
  231.   public StringBuffer getNamedBuffer(String name) {
  232.     StringBuffer b = null;
  233.     b = (StringBuffer)(m_Results.get(name));
  234.     return b;
  235.   }
  236.   /**
  237.    * Gets the buffer associated with the currently
  238.    * selected item in the list.
  239.    * @return the buffer or null if there are no items in
  240.    * the list
  241.    */
  242.   public StringBuffer getSelectedBuffer() {
  243.     StringBuffer b = null;
  244.     int index = m_List.getSelectedIndex();
  245.     if (index != -1) {
  246.       String name = (String)(m_Model.elementAt(index));
  247.       b = (StringBuffer)(m_Results.get(name));
  248.     }
  249.     return b;
  250.   }
  251.   /**
  252.    * Get the name of the currently selected item in the list
  253.    * @return the name of the currently selected item or null if no
  254.    * item selected
  255.    */
  256.   public String getSelectedName() {
  257.     int index = m_List.getSelectedIndex();
  258.     if (index != -1) {
  259.       return (String)(m_Model.elementAt(index));
  260.     }
  261.     return null;
  262.   }
  263.   /**
  264.    * Gets the name of theitem in the list at the specified index
  265.    * @return the name of item or null if there is no item at that index
  266.    */
  267.   public String getNameAtIndex(int index) {
  268.     if (index != -1) {
  269.       return (String)(m_Model.elementAt(index));
  270.     }
  271.     return null;
  272.   }
  273.   /**
  274.    * Sets the single-click display to view the named result.
  275.    *
  276.    * @param name the name of the result to display.
  277.    */
  278.   public void setSingle(String name) {
  279.     StringBuffer buff = (StringBuffer) m_Results.get(name);
  280.     if (buff != null) {
  281.       m_SingleName = name;
  282.       m_SingleText.setText(buff.toString());
  283.       m_List.setSelectedValue(name, true);
  284.     }
  285.   }
  286.   
  287.   /**
  288.    * Opens the named result in a separate frame.
  289.    *
  290.    * @param name the name of the result to open.
  291.    */
  292.   public void openFrame(String name) {
  293.     StringBuffer buff = (StringBuffer) m_Results.get(name);
  294.     JTextComponent currentText = (JTextComponent) m_FramedOutput.get(name);
  295.     if ((buff != null) && (currentText == null)) {
  296.       // Open the frame.
  297.       JTextArea ta = new JTextArea();
  298.       ta.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  299.       ta.setFont(new Font("Monospaced", Font.PLAIN, 12));
  300.       ta.setEditable(false);
  301.       ta.setText(buff.toString());
  302.       m_FramedOutput.put(name, ta);
  303.       final JFrame jf = new JFrame(name);
  304.       jf.addWindowListener(new WindowAdapter() {
  305. public void windowClosing(WindowEvent e) {
  306.   m_FramedOutput.remove(jf.getTitle());
  307.   jf.dispose();
  308. }
  309.       });
  310.       jf.getContentPane().setLayout(new BorderLayout());
  311.       jf.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER);
  312.       jf.pack();
  313.       jf.setSize(450, 350);
  314.       jf.setVisible(true);
  315.     }
  316.   }
  317.   /**
  318.    * Tells any component currently displaying the named result that the
  319.    * contents of the result text in the StringBuffer have been updated.
  320.    *
  321.    * @param name the name of the result that has been updated.
  322.    */
  323.   public void updateResult(String name) {
  324.     StringBuffer buff = (StringBuffer) m_Results.get(name);
  325.     if (buff == null) {
  326.       return;
  327.     }
  328.     if (m_SingleName == name) {
  329.       m_SingleText.setText(buff.toString());
  330.     }
  331.     JTextComponent currentText = (JTextComponent) m_FramedOutput.get(name);
  332.     if (currentText != null) {
  333.       currentText.setText(buff.toString());
  334.     }
  335.   }
  336.   /**
  337.    * Gets the selection model used by the results list.
  338.    *
  339.    * @return a value of type 'ListSelectionModel'
  340.    */
  341.   public ListSelectionModel getSelectionModel() {
  342.     
  343.     return m_List.getSelectionModel();
  344.   }
  345.   /**
  346.    * Gets the JList used by the results list
  347.    * @return the JList
  348.    */
  349.   public JList getList() {
  350.     return m_List;
  351.   }
  352.   /**
  353.    * Set whether the result history list should handle right clicks
  354.    * or whether the parent object will handle them.
  355.    * @param tf false if parent object will handle right clicks
  356.    */
  357.   public void setHandleRightClicks(boolean tf) {
  358.     m_HandleRightClicks = tf;
  359.   }
  360.   /**
  361.    * Tests out the result history from the command line.
  362.    *
  363.    * @param args ignored
  364.    */
  365.   public static void main(String [] args) {
  366.     try {
  367.       final javax.swing.JFrame jf =
  368. new javax.swing.JFrame("Weka Knowledge Explorer: Classifier");
  369.       jf.getContentPane().setLayout(new BorderLayout());
  370.       final ResultHistoryPanel jd = new ResultHistoryPanel(null);
  371.       jd.addResult("blah", new StringBuffer("Nothing to see here"));
  372.       jd.addResult("blah1", new StringBuffer("Nothing to see here1"));
  373.       jd.addResult("blah2", new StringBuffer("Nothing to see here2"));
  374.       jd.addResult("blah3", new StringBuffer("Nothing to see here3"));
  375.       jf.getContentPane().add(jd, BorderLayout.CENTER);
  376.       jf.addWindowListener(new java.awt.event.WindowAdapter() {
  377. public void windowClosing(java.awt.event.WindowEvent e) {
  378.   jf.dispose();
  379.   System.exit(0);
  380. }
  381.       });
  382.       jf.pack();
  383.       jf.setVisible(true);
  384.     } catch (Exception ex) {
  385.       ex.printStackTrace();
  386.       System.err.println(ex.getMessage());
  387.     }
  388.   }
  389. }