genericobjecteditor.java
来自「Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等」· Java 代码 · 共 1,764 行 · 第 1/4 页
JAVA
1,764 行
/* * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * GenericObjectEditor.java * Copyright (C) 2002 Len Trigg, Xin Xu, Richard Kirkby * */package weka.gui;import weka.core.Capabilities;import weka.core.CapabilitiesHandler;import weka.core.ClassDiscovery;import weka.core.OptionHandler;import weka.core.SerializedObject;import weka.core.Utils;import weka.core.Capabilities.Capability;import weka.gui.CheckBoxList.CheckBoxListModel;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.FontMetrics;import java.awt.GridLayout;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;import java.beans.PropertyEditor;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.Enumeration;import java.util.Hashtable;import java.util.Properties;import java.util.StringTokenizer;import java.util.Vector;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JTree;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.TreePath;import javax.swing.tree.TreeSelectionModel;/** * A PropertyEditor for objects. It can be used either in a static or a dynamic * way. <br> * <br> * In the <b>static</b> way (<code>USE_DYNAMIC</code> is <code>false</code>) the * objects have been defined as editable in the GenericObjectEditor * configuration file, which lists possible values that can be selected from, * and themselves configured. The configuration file is called * "GenericObjectEditor.props" and may live in either the location given by * "user.home" or the current directory (this last will take precedence), and a * default properties file is read from the weka distribution. For speed, the * properties file is read only once when the class is first loaded -- this may * need to be changed if we ever end up running in a Java OS ;-). <br> * <br> * If it is used in a <b>dynamic</b> way (<code>USE_DYNAMIC</code> is * <code>true</code>) then the classes to list are discovered by the * <code>GenericPropertiesCreator</code> class (it checks the complete classpath). * * @see #USE_DYNAMIC * @see GenericPropertiesCreator * @see GenericPropertiesCreator#CREATOR_FILE * @see weka.core.ClassDiscovery * * @author Len Trigg (trigg@cs.waikato.ac.nz) * @author Xin Xu (xx5@cs.waikato.ac.nz) * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz) * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.52 $ */public class GenericObjectEditor implements PropertyEditor, CustomPanelSupplier { /** The object being configured */ protected Object m_Object; /** Holds a copy of the current object that can be reverted to if the user decides to cancel */ protected Object m_Backup; /** Handles property change notification */ protected PropertyChangeSupport m_Support = new PropertyChangeSupport(this); /** The Class of objects being edited */ protected Class m_ClassType; /** The model containing the list of names to select from */ protected Hashtable m_ObjectNames; /** The GUI component for editing values, created when needed */ protected GOEPanel m_EditorComponent; /** True if the GUI component is needed */ protected boolean m_Enabled = true; /** The name of the properties file */ protected static String PROPERTY_FILE = "weka/gui/GenericObjectEditor.props"; /** Contains the editor properties */ protected static Properties EDITOR_PROPERTIES; /** The tree node of the current object so we can re-select it for the user */ protected GOETreeNode m_treeNodeOfCurrentObject; /** The property panel created for the objects */ protected PropertyPanel m_ObjectPropertyPanel; /** whether the class can be changed */ protected boolean m_canChangeClassInDialog; /** whether to generate the properties dynamically or use the static props-file */ protected final static boolean USE_DYNAMIC = true; /** whether the Weka Editors were already registered */ protected static boolean m_EditorsRegistered; /** for filtering the tree based on the Capabilities of the leaves */ protected Capabilities m_CapabilitiesFilter = null; /** * Loads the configuration property file (USE_DYNAMIC is FALSE) or determines * the classes dynamically (USE_DYNAMIC is TRUE) * @see #USE_DYNAMIC * @see GenericPropertiesCreator */ static { if (USE_DYNAMIC) { try { GenericPropertiesCreator creator = new GenericPropertiesCreator(); creator.execute(false); EDITOR_PROPERTIES = creator.getOutputProperties(); } catch (Exception e) { JOptionPane.showMessageDialog( null, "Could not determine the properties for the generic object\n" + "editor. This exception was produced:\n" + e.toString(), "GenericObjectEditor", JOptionPane.ERROR_MESSAGE); } } else { // Allow a properties file in the current directory to override try { EDITOR_PROPERTIES = Utils.readProperties(PROPERTY_FILE); java.util.Enumeration keys = (java.util.Enumeration)EDITOR_PROPERTIES.propertyNames(); if (!keys.hasMoreElements()) { throw new Exception("Failed to read a property file for the " +"generic object editor"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Could not read a configuration file for the generic object\n" +"editor. An example file is included with the Weka distribution.\n" +"This file should be named \"" + PROPERTY_FILE + "\" and\n" +"should be placed either in your user home (which is set\n" + "to \"" + System.getProperties().getProperty("user.home") + "\")\n" + "or the directory that java was started from\n", "GenericObjectEditor", JOptionPane.ERROR_MESSAGE); } } } /** * A specialized TreeNode for supporting filtering via Capabilities */ public class GOETreeNode extends DefaultMutableTreeNode { /** for serialization */ static final long serialVersionUID = -1707872446682150133L; /** color for "no support" */ public final static String NO_SUPPORT = "red"; /** color for "maybe support" */ public final static String MAYBE_SUPPORT = "blue"; /** the Capabilities object to use for filtering */ protected Capabilities m_Capabilities = null; /** * Creates a tree node that has no parent and no children, but which * allows children. */ public GOETreeNode() { super(); } /** * Creates a tree node with no parent, no children, but which allows * children, and initializes it with the specified user object. * * @param userObject an Object provided by the user that constitutes * the node's data */ public GOETreeNode(Object userObject) { super(userObject); } /** * Creates a tree node with no parent, no children, initialized with the * specified user object, and that allows children only if specified. * @param userObject an Object provided by the user that constitutes * the node's data * @param allowsChildren if true, the node is allowed to have child nodes * -- otherwise, it is always a leaf node */ public GOETreeNode(Object userObject, boolean allowsChildren) { super(userObject, allowsChildren); } /** * generates if necessary a Capabilities object for the given leaf */ protected void initCapabilities() { String classname; Class cls; Object obj; if (m_Capabilities != null) return; if (!isLeaf()) return; classname = getClassnameFromPath(new TreePath(getPath())); try { cls = Class.forName(classname); if (!ClassDiscovery.hasInterface(CapabilitiesHandler.class, cls)) return; obj = cls.newInstance(); m_Capabilities = ((CapabilitiesHandler) obj).getCapabilities(); } catch (Exception e) { // ignore it } } /** * returns a string representation of this treenode * * @return the text to display */ public String toString() { String result; result = super.toString(); if (m_CapabilitiesFilter != null) { initCapabilities(); if (m_Capabilities != null) { if (m_Capabilities.supportsMaybe(m_CapabilitiesFilter) && !m_Capabilities.supports(m_CapabilitiesFilter)) result = "<html><font color=\"" + MAYBE_SUPPORT + "\">" + result + "</font></i><html>"; else if (!m_Capabilities.supports(m_CapabilitiesFilter)) result = "<html><font color=\"" + NO_SUPPORT + "\">" + result + "</font></i><html>"; } } return result; } } /** * A dialog for selecting Capabilities to look for in the GOE tree. */ public class CapabilitiesFilterDialog extends JDialog { /** for serialization */ static final long serialVersionUID = -7845503345689646266L; /** the dialog itself */ protected JDialog m_Self; /** the popup to display again */ protected JPopupMenu m_Popup = null; /** the capabilities used for initializing the dialog */ protected Capabilities m_Capabilities = new Capabilities(null); /** the label, listing the name of the superclass */ protected JLabel m_InfoLabel = new JLabel(); /** the list with all the capabilities */ protected CheckBoxList m_List = new CheckBoxList(); /** the OK button */ protected JButton m_OkButton = new JButton("OK"); /** the Cancel button */ protected JButton m_CancelButton = new JButton("Cancel"); /** * creates a dialog to choose Capabilities from */ public CapabilitiesFilterDialog() { super(); m_Self = this; initGUI(); } /** * sets up the GUI */ protected void initGUI() { JPanel panel; CheckBoxListModel model; setTitle("Filtering Capabilities..."); setLayout(new BorderLayout()); panel = new JPanel(new BorderLayout()); panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); getContentPane().add(panel, BorderLayout.NORTH); m_InfoLabel.setText( "<html>" + m_ClassType.getName().replaceAll(".*\\.", "") + "s" + " have to support <i>at least</i> the following capabilities <br>" + "(the ones highlighted <font color=\"" + GOETreeNode.NO_SUPPORT + "\">" + GOETreeNode.NO_SUPPORT + "</font> don't meet these requirements <br>" + "the ones highlighted <font color=\"" + GOETreeNode.MAYBE_SUPPORT + "\">" + GOETreeNode.MAYBE_SUPPORT + "</font> possibly meet them):" + "</html>"); panel.add(m_InfoLabel, BorderLayout.CENTER); // list getContentPane().add(new JScrollPane(m_List), BorderLayout.CENTER); model = (CheckBoxListModel) m_List.getModel(); for (Capability cap: Capability.values()) model.addElement(cap); // buttons panel = new JPanel(new FlowLayout(FlowLayout.CENTER)); getContentPane().add(panel, BorderLayout.SOUTH); m_OkButton.setMnemonic('O'); m_OkButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { updateCapabilities(); if (m_CapabilitiesFilter == null) m_CapabilitiesFilter = new Capabilities(null); m_CapabilitiesFilter.assign(m_Capabilities); m_Self.setVisible(false); showPopup(); } }); panel.add(m_OkButton); m_CancelButton.setMnemonic('C'); m_CancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { m_Self.setVisible(false); showPopup(); } }); panel.add(m_CancelButton); pack(); } /** * transfers the Capabilities object to the JList * * @see #m_Capabilities * @see #m_List */ protected void updateList() { CheckBoxListModel model; model = (CheckBoxListModel) m_List.getModel(); for (Capability cap: Capability.values()) model.setChecked(model.indexOf(cap), m_Capabilities.handles(cap)); } /** * transfers the selected Capabilities from the JList to the * Capabilities object * * @see #m_Capabilities * @see #m_List */ protected void updateCapabilities() { CheckBoxListModel model; model = (CheckBoxListModel) m_List.getModel(); for (Capability cap: Capability.values()) { if (model.getChecked(model.indexOf(cap))) m_Capabilities.enable(cap); else m_Capabilities.disable(cap); } } /** * sets the initial capabilities * * @param value the capabilities to use */ public void setCapabilities(Capabilities value) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?