PropertySelectorDialog.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 9k
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.  *    PropertySelectorDialog.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui;
  22. import weka.experiment.PropertyNode;
  23. import java.beans.PropertyDescriptor;
  24. import java.beans.BeanInfo;
  25. import java.beans.IntrospectionException;
  26. import java.beans.Introspector;
  27. import java.beans.PropertyEditor;
  28. import java.beans.PropertyEditorManager;
  29. import java.lang.reflect.Method;
  30. import java.lang.reflect.InvocationTargetException;
  31. import java.awt.Component;
  32. import java.awt.BorderLayout;
  33. import java.awt.GridLayout;
  34. import java.awt.Frame;
  35. import java.awt.FlowLayout;
  36. import java.awt.Container;
  37. import java.awt.event.ActionEvent;
  38. import java.awt.event.WindowAdapter;
  39. import java.awt.event.WindowEvent;
  40. import java.awt.event.ActionListener;
  41. import javax.swing.JPanel;
  42. import javax.swing.JLabel;
  43. import javax.swing.JFrame;
  44. import javax.swing.SwingConstants;
  45. import javax.swing.JTextField;
  46. import javax.swing.BorderFactory;
  47. import javax.swing.DefaultListModel;
  48. import javax.swing.JScrollPane;
  49. import javax.swing.JButton;
  50. import javax.swing.Box;
  51. import javax.swing.BoxLayout;
  52. import javax.swing.JTree;
  53. import javax.swing.JDialog;
  54. import javax.swing.tree.TreeNode;
  55. import javax.swing.tree.TreePath;
  56. import javax.swing.tree.DefaultMutableTreeNode;
  57. import javax.swing.tree.TreeSelectionModel;
  58. /** 
  59.  * Allows the user to select any (supported) property of an object, including
  60.  * properties that any of it's property values may have.
  61.  *
  62.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  63.  * @version $Revision: 1.5 $
  64.  */
  65. public class PropertySelectorDialog extends JDialog {
  66.   
  67.   /** Click to choose the currently selected property */
  68.   protected JButton m_SelectBut = new JButton("Select");
  69.   /** Click to cancel the property selection */
  70.   protected JButton m_CancelBut = new JButton("Cancel");
  71.   /** The root of the property tree */
  72.   protected DefaultMutableTreeNode m_Root;
  73.   /** The object at the root of the tree */
  74.   protected Object m_RootObject;
  75.   /** Whether the selection was made or cancelled */
  76.   protected int m_Result;
  77.   /** Stores the path to the selected property */
  78.   protected Object [] m_ResultPath;
  79.   /** The component displaying the property tree */
  80.   protected JTree m_Tree;
  81.   /** Signifies an OK property selection */
  82.   public static final int APPROVE_OPTION = 0;
  83.   /** Signifies a cancelled property selection */
  84.   public static final int CANCEL_OPTION = 1;
  85.   
  86.   /**
  87.    * Create the property selection dialog.
  88.    *
  89.    * @param parentFrame the parent frame of the dialog
  90.    * @param rootObject the object containing properties to select from
  91.    */
  92.   public PropertySelectorDialog(Frame parentFrame, Object rootObject) {
  93.     
  94.     super(parentFrame, "Select a property", true);
  95.     m_CancelBut.addActionListener(new ActionListener() {
  96.       public void actionPerformed(ActionEvent e) {
  97. m_Result = CANCEL_OPTION;
  98. setVisible(false);
  99.       }
  100.     });
  101.     m_SelectBut.addActionListener(new ActionListener() {
  102.       public void actionPerformed(ActionEvent e) {
  103. // value = path from root to selected;
  104. TreePath tPath = m_Tree.getSelectionPath();
  105. if (tPath == null) {
  106.   m_Result = CANCEL_OPTION;
  107. } else {
  108.   m_ResultPath = tPath.getPath();
  109.   if ((m_ResultPath == null) || (m_ResultPath.length < 2)) {
  110.     m_Result = CANCEL_OPTION;
  111.   } else {
  112.     m_Result = APPROVE_OPTION;
  113.   }
  114. setVisible(false);
  115.       }
  116.     });
  117.     m_RootObject = rootObject;
  118.     m_Root = new DefaultMutableTreeNode(
  119.      new PropertyNode(m_RootObject));
  120.     createNodes(m_Root);
  121.     
  122.     Container c = getContentPane();
  123.     c.setLayout(new BorderLayout());
  124.     //    setBorder(BorderFactory.createTitledBorder("Select a property"));
  125.     Box b1 = new Box(BoxLayout.X_AXIS);
  126.     b1.add(m_SelectBut);
  127.     b1.add(Box.createHorizontalStrut(10));
  128.     b1.add(m_CancelBut);
  129.     c.add(b1, BorderLayout.SOUTH);
  130.     m_Tree = new JTree(m_Root);
  131.     m_Tree.getSelectionModel()
  132.       .setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  133.     c.add(new JScrollPane(m_Tree), BorderLayout.CENTER);
  134.     pack();
  135.   }
  136.   /**
  137.    * Pops up the modal dialog and waits for cancel or a selection.
  138.    *
  139.    * @return either APPROVE_OPTION, or CANCEL_OPTION
  140.    */
  141.   public int showDialog() {
  142.     m_Result = CANCEL_OPTION;
  143.     setVisible(true);
  144.     return m_Result;
  145.   }
  146.   /**
  147.    * Gets the path of property nodes to the selected property.
  148.    *
  149.    * @return an array of PropertyNodes
  150.    */
  151.   public PropertyNode [] getPath() {
  152.     PropertyNode [] result = new PropertyNode [m_ResultPath.length - 1];
  153.     for (int i = 0; i < result.length; i++) {
  154.       result[i] = (PropertyNode) ((DefaultMutableTreeNode) m_ResultPath[i + 1])
  155. .getUserObject();
  156.     }
  157.     return result;
  158.   }
  159.   /**
  160.    * Creates the property tree below the current node.
  161.    *
  162.    * @param localNode a value of type 'DefaultMutableTreeNode'
  163.    */
  164.   protected void createNodes(DefaultMutableTreeNode localNode) {
  165.     PropertyNode pNode = (PropertyNode)localNode.getUserObject();
  166.     Object localObject = pNode.value;
  167.     // Find all the properties of the object in the root node
  168.     PropertyDescriptor localProperties[];
  169.     try {
  170.       BeanInfo bi = Introspector.getBeanInfo(localObject.getClass());
  171.       localProperties = bi.getPropertyDescriptors();
  172.     } catch (IntrospectionException ex) {
  173.       System.err.println("PropertySelectorDialog: Couldn't introspect");
  174.       return;
  175.     }
  176.     // Put their values into child nodes.
  177.     for (int i = 0; i < localProperties.length; i++) {
  178.       // Don't display hidden or expert properties.
  179.       if (localProperties[i].isHidden() || localProperties[i].isExpert()) {
  180. continue;
  181.       }
  182.       String name = localProperties[i].getDisplayName();
  183.       Class type = localProperties[i].getPropertyType();
  184.       Method getter = localProperties[i].getReadMethod();
  185.       Method setter = localProperties[i].getWriteMethod();
  186.       Object value = null;
  187.       // Only display read/write properties.
  188.       if (getter == null || setter == null) {
  189. continue;
  190.       }
  191.       try {
  192. Object args[] = { };
  193. value = getter.invoke(localObject, args);
  194. PropertyEditor editor = null;
  195. Class pec = localProperties[i].getPropertyEditorClass();
  196. if (pec != null) {
  197.   try {
  198.     editor = (PropertyEditor)pec.newInstance();
  199.   } catch (Exception ex) {
  200.   }
  201. }
  202. if (editor == null) {
  203.   editor = PropertyEditorManager.findEditor(type);
  204. }
  205. if ((editor == null) || (value == null)) {
  206.   continue;
  207. }
  208.       } catch (InvocationTargetException ex) {
  209. System.err.println("Skipping property " + name
  210.    + " ; exception on target: "
  211.    + ex.getTargetException());
  212. ex.getTargetException().printStackTrace();
  213. continue;
  214.       } catch (Exception ex) {
  215. System.err.println("Skipping property " + name
  216.    + " ; exception: " + ex);
  217. ex.printStackTrace();
  218. continue;
  219.       }
  220.       // Make a child node
  221.       DefaultMutableTreeNode child = new DefaultMutableTreeNode(
  222.      new PropertyNode(value,
  223.       localProperties[i],
  224.       localObject.getClass()));
  225.       localNode.add(child);
  226.       createNodes(child);
  227.     }
  228.   }
  229.   
  230.   /**
  231.    * Tests out the property selector from the command line.
  232.    *
  233.    * @param args ignored
  234.    */
  235.   public static void main(String [] args) {
  236.     try {
  237.       System.err.println("---Registering Weka Editors---");
  238.       java.beans.PropertyEditorManager
  239. .registerEditor(weka.experiment.ResultProducer.class,
  240. weka.gui.GenericObjectEditor.class);
  241.       java.beans.PropertyEditorManager
  242. .registerEditor(weka.experiment.SplitEvaluator.class,
  243. weka.gui.GenericObjectEditor.class);
  244.       java.beans.PropertyEditorManager
  245. .registerEditor(weka.classifiers.Classifier.class,
  246. weka.gui.GenericObjectEditor.class);
  247.       java.beans.PropertyEditorManager
  248. .registerEditor(weka.core.SelectedTag.class,
  249. weka.gui.SelectedTagEditor.class);
  250.       Object rp
  251. = new weka.experiment.AveragingResultProducer();
  252.       final PropertySelectorDialog jd = new PropertySelectorDialog(null, rp);
  253.       int result = jd.showDialog();
  254.       if (result == PropertySelectorDialog.APPROVE_OPTION) {
  255. System.err.println("Property Selected");
  256. PropertyNode [] path = jd.getPath();
  257. for (int i = 0; i < path.length; i++) {
  258.   PropertyNode pn = path[i];
  259.   System.err.println("" + (i + 1) + "  " + pn.toString()
  260.      + " " + pn.value.toString());
  261. }
  262.       } else {
  263. System.err.println("Cancelled");
  264.       }
  265.       System.exit(0);
  266.     } catch (Exception ex) {
  267.       ex.printStackTrace();
  268.       System.err.println(ex.getMessage());
  269.     }
  270.   }
  271. }