📄 propertysheetpanel.java
字号:
/* * 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. *//* * PropertySheet.java * Copyright (C) 1999 Len Trigg * */package weka.gui;import weka.core.Capabilities;import weka.core.CapabilitiesHandler;import weka.core.MultiInstanceCapabilitiesHandler;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.Font;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.beans.BeanInfo;import java.beans.Beans;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.MethodDescriptor;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;import java.beans.PropertyDescriptor;import java.beans.PropertyEditor;import java.beans.PropertyEditorManager;import java.beans.PropertyVetoException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Iterator;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.SwingConstants;/** * Displays a property sheet where (supported) properties of the target * object may be edited. * * @author Len Trigg (trigg@cs.waikato.ac.nz) * @version $Revision: 1.16 $ */public class PropertySheetPanel extends JPanel implements PropertyChangeListener { /** for serialization */ static final long serialVersionUID = -557854258929870536L; /** * A specialized frame for displaying the capabilities */ protected class CapabilitiesHelpFrame extends JFrame implements PropertyChangeListener { /** for serialization */ private static final long serialVersionUID = 3591124039176891020L; /** the frame itself */ private CapabilitiesHelpFrame m_Self; /** * default constructor */ public CapabilitiesHelpFrame() { super("Information about Capabilities"); m_Self = this; m_CapabilitiesText = new JTextArea(); m_CapabilitiesText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); m_CapabilitiesText.setLineWrap(true); m_CapabilitiesText.setWrapStyleWord(true); m_CapabilitiesText.setEditable(false); updateText(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { m_Self.dispose(); if (m_CapabilitiesFrame == m_Self) { m_CapabilitiesBut.setEnabled(true); } } }); getContentPane().setLayout(new BorderLayout()); getContentPane().add(new JScrollPane(m_CapabilitiesText), BorderLayout.CENTER); pack(); } /** * returns a comma-separated list of all the capabilities * * @param c the capabilities to get a string representation from * @return the string describing the capabilities */ protected String listCapabilities(Capabilities c) { String result; Iterator iter; result = ""; iter = c.capabilities(); while (iter.hasNext()) { if (result.length() != 0) result += ", "; result += iter.next().toString(); } return result; } /** * generates a string from the capapbilities, suitable to add to the help * text. * * @param title the title for the capabilities * @param c the capabilities * @return a string describing the capabilities */ protected String addCapabilities(String title, Capabilities c) { String result; String caps; result = title + "\n"; // class caps = listCapabilities(c.getClassCapabilities()); if (caps.length() != 0) { result += "Class -- "; result += caps; result += "\n\n"; } // attribute caps = listCapabilities(c.getAttributeCapabilities()); if (caps.length() != 0) { result += "Attributes -- "; result += caps; result += "\n\n"; } // other capabilities caps = listCapabilities(c.getOtherCapabilities()); if (caps.length() != 0) { result += "Other -- "; result += caps; result += "\n\n"; } // additional stuff result += "Additional\n"; result += "min # of instances: " + c.getMinimumNumberInstances() + "\n"; result += "\n"; return result; } /** * updates the content of the capabilities help frame */ protected void updateText() { StringBuffer helpText = new StringBuffer(); if (m_Target instanceof CapabilitiesHandler) helpText.append( addCapabilities( "CAPABILITIES", ((CapabilitiesHandler) m_Target).getCapabilities())); if (m_Target instanceof MultiInstanceCapabilitiesHandler) helpText.append( addCapabilities( "MI CAPABILITIES", ((MultiInstanceCapabilitiesHandler) m_Target).getMultiInstanceCapabilities())); m_CapabilitiesText.setText(helpText.toString()); m_CapabilitiesText.setCaretPosition(0); } /** * This method gets called when a bound property is changed. * * @param evt the change event */ public void propertyChange(PropertyChangeEvent evt) { updateText(); } } /** The target object being edited */ private Object m_Target; /** Holds properties of the target */ private PropertyDescriptor m_Properties[]; /** Holds the methods of the target */ private MethodDescriptor m_Methods[]; /** Holds property editors of the object */ private PropertyEditor m_Editors[]; /** Holds current object values for each property */ private Object m_Values[]; /** Stores GUI components containing each editing component */ private JComponent m_Views[]; /** The labels for each property */ private JLabel m_Labels[]; /** The tool tip text for each property */ private String m_TipTexts[]; /** StringBuffer containing help text for the object being edited */ private StringBuffer m_HelpText; /** Help frame */ private JFrame m_HelpFrame; /** Capabilities Help frame */ private CapabilitiesHelpFrame m_CapabilitiesFrame; /** Button to pop up the full help text in a separate frame */ private JButton m_HelpBut; /** Button to pop up the capabilities in a separate frame */ private JButton m_CapabilitiesBut; /** the TextArea of the Capabilities help frame */ private JTextArea m_CapabilitiesText; /** A count of the number of properties we have an editor for */ private int m_NumEditable = 0; /** The panel holding global info and help, if provided by the object being editied */ private JPanel m_aboutPanel; /** * Creates the property sheet panel. */ public PropertySheetPanel() { // setBorder(BorderFactory.createLineBorder(Color.red)); setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); } /** * Return the panel containing global info and help for * the object being edited. May return null if the edited * object provides no global info or tip text. * * @return the about panel. */ public JPanel getAboutPanel() { return m_aboutPanel; } /** A support object for handling property change listeners */ private PropertyChangeSupport support = new PropertyChangeSupport(this); /** * Updates the property sheet panel with a changed property and also passed * the event along. * * @param evt a value of type 'PropertyChangeEvent' */ public void propertyChange(PropertyChangeEvent evt) { wasModified(evt); // Let our panel update before guys downstream support.firePropertyChange("", null, null); } /** * Adds a PropertyChangeListener. * * @param l a value of type 'PropertyChangeListener' */ public void addPropertyChangeListener(PropertyChangeListener l) { support.addPropertyChangeListener(l); } /** * Removes a PropertyChangeListener. * * @param l a value of type 'PropertyChangeListener' */ public void removePropertyChangeListener(PropertyChangeListener l) { support.removePropertyChangeListener(l); } /** * Sets a new target object for customisation. * * @param targ a value of type 'Object' */ public synchronized void setTarget(Object targ) { // used to offset the components for the properties of targ // if there happens to be globalInfo available in targ int componentOffset = 0; // Close any child windows at this point removeAll(); GridBagLayout gbLayout = new GridBagLayout(); setLayout(gbLayout); setVisible(false); m_NumEditable = 0; m_Target = targ; try { BeanInfo bi = Introspector.getBeanInfo(m_Target.getClass()); m_Properties = bi.getPropertyDescriptors(); m_Methods = bi.getMethodDescriptors(); } catch (IntrospectionException ex) { System.err.println("PropertySheet: Couldn't introspect"); return; } JTextArea jt = new JTextArea(); m_HelpText = null; // Look for a globalInfo method that returns a string // describing the target for (int i = 0;i < m_Methods.length; i++) { String name = m_Methods[i].getDisplayName(); Method meth = m_Methods[i].getMethod(); if (name.equals("globalInfo")) { if (meth.getReturnType().equals(String.class)) { try { Object args[] = { }; String globalInfo = (String)(meth.invoke(m_Target, args)); String summary = globalInfo; int ci = globalInfo.indexOf('.'); if (ci != -1) { summary = globalInfo.substring(0, ci + 1); } final String className = targ.getClass().getName(); m_HelpText = new StringBuffer("NAME\n"); m_HelpText.append(className).append("\n\n"); m_HelpText.append("SYNOPSIS\n").append(globalInfo).append("\n\n"); m_HelpBut = new JButton("More"); m_HelpBut.setToolTipText("More information about " + className); m_HelpBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { openHelpFrame(); m_HelpBut.setEnabled(false); } }); if (m_Target instanceof CapabilitiesHandler) { m_CapabilitiesBut = new JButton("Capabilities"); m_CapabilitiesBut.setToolTipText("The capabilities of " + className); m_CapabilitiesBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { openCapabilitiesHelpFrame(); m_CapabilitiesBut.setEnabled(false); } }); } else { m_CapabilitiesBut = null; } jt.setColumns(30); jt.setFont(new Font("SansSerif", Font.PLAIN,12));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -