📄 propertysheet.java
字号:
/*
* 01/27/2005
*
* PropertySheet.java - A collection of editable properties.
* Copyright (C) 2005 Robert Futrell
* email@address.com
* www.website.com
*
* 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui.propertysheet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.*;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;
import javax.swing.event.EventListenerList;
import org.fife.ui.propertysheet.infos.DefaultPropertyInfo;
import org.fife.ui.propertysheet.infos.PropertyInfo;
/**
* A collection of editable properties. This component is similar to
* property sheets found in popular programs such as Microsoft Visual Studio.
*
* @author Robert Futrell
* @version 0.1
*/
public class PropertySheet extends JPanel {
/**
*
*/
private static final long serialVersionUID = -1918368960467878783L;
private JPanel interiorPanel;
private InfoPanel infoPanel;
private PropertyTableContainer propertyTableContainer;
private EventListenerList listenerList;
private BeanInfo beanInfo;
private boolean columnResizing;
private static final String ALPHABETICAL = "Alphabetical";
private static final String CATEGORIES = "Categories";
private static final String EVENTS = "Events";
private static final String PROPERTIES = "Properties";
public static final int PROPERTY_ROW_HEIGHT = 16;
/*****************************************************************************/
/**
* Constructor.
*/
public PropertySheet() {
this(false);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param sortedByCategory Whether the properties should be sorted by
* category.
*/
public PropertySheet(boolean sortedByCategory) {
this(null, sortedByCategory);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param bean A JavaBean.
*/
public PropertySheet(Object bean) {
this(bean, false);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param bean A JavaBean.
* @param sortedByCategory Whether the properties should be sorted by
* category.
*/
public PropertySheet(Object bean, boolean sortedByCategory) {
initialize(sortedByCategory);
if (bean!=null) {
beanInfo = addBeanProperties(bean);
}
}
/*****************************************************************************/
/**
* Adds all of the properties of the specified JavaBean to this property
* sheet.
*
* @param bean A JavaBean.
* @return The <code>BeanInfo</code> for a bean.
*/
protected BeanInfo addBeanProperties(Object bean) {
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(bean.getClass());
// Properties.
PropertyDescriptor[] pd = beanInfo.getPropertyDescriptors();
int count = pd==null ? 0 : pd.length;
PropertySheetManager psm = PropertySheetManager.getInstance();
for (int i=0; i<count; i++) {
String name = pd[i].getName();
String displayName = pd[i].getDisplayName();
Class type = pd[i].getPropertyType();
boolean modifiable = pd[i].getWriteMethod()!=null;
if (type!=null) { // May be null...
Class piClass = psm.getPropertyInfoClass(type);
if (piClass!=null) { // Should never happen.
try {
PropertyInfo pi = null;
if (piClass.equals(DefaultPropertyInfo.class)) {
pi = new DefaultPropertyInfo(name,
null, null);
}
else {
Class[] paramTypes = new Class[] {
String.class, type, type };
Constructor ctor = piClass.getDeclaredConstructor(paramTypes);
Object[] params = new Object[3];
params[0] = name;
Method getter = pd[i].getReadMethod();
// If we don't have a getter method, there's
// no need in having in in the property sheet.
// We also only handle no-parameter getters, as
// we have no way of knowing any parameter meanings.
if (getter!=null && getter.getParameterTypes().length==0) {
Object value = getter.invoke(bean, null);
params[1] = value;
params[2] = value;
pi = (PropertyInfo)ctor.newInstance(params);
pi.setModifiable(modifiable);
if (pd[i].isPreferred())
pi.setCategory("PREFERRED");
else if (pd[i].isExpert())
pi.setCategory("EXPERT");
else if (pd[i].isHidden())
pi.setCategory("HIDDEN");
else
pi.setCategory("UNKNOWN");
pi.setPropertyDescriptor(pd[i]);
}
pi.setDisplayName(displayName);
addProperty(pi);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//System.out.println(displayName + "\t\t- " + pd[i].getPropertyType());
}
// TODO: One day, support MethodDescriptors and EventDescriptors!
} catch (IntrospectionException ie) {}
return beanInfo;
}
/*****************************************************************************/
/**
* Adds a property to this property sheet.
*
* @param info The property to add.
* @throws BeanRepresentedException If this property sheet was initialized
* with a JavaBean.
*/
public void addProperty(PropertyInfo info)
throws BeanRepresentedException {
if (isInitializedByBean()) {
throw new BeanRepresentedException("A bean has already " +
"initialized the property sheet");
}
propertyTableContainer.addProperty(info);
}
/*****************************************************************************/
/**
* Adds a listener to this property sheet.
*
* @param l The listener to add.
* @see #removePropertySheetListener
*/
public void addPropertySheetListener(PropertySheetListener l) {
listenerList.add(PropertySheetListener.class, l);
}
/*****************************************************************************/
void beginResizingColumns() {
if (!columnResizing) {
columnResizing = true;
propertyTableContainer.beginResizingColumns();
}
}
/*****************************************************************************/
protected JToolBar createToolBar(boolean sorted) {
Listener listener = new Listener();
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
ClassLoader cl = this.getClass().getClassLoader();
URL url = cl.getResource("alpha.png");
ImageIcon icon = null;
try {
icon = new ImageIcon(ImageIO.read(url));
} catch (IOException ioe) {}
JToggleButton alphaButton = new JToggleButton(icon);
alphaButton.setSelected(!sorted);
alphaButton.setActionCommand(ALPHABETICAL);
alphaButton.addActionListener(listener);
toolBar.add(alphaButton);
url = cl.getResource("category.png");
try {
icon = new ImageIcon(ImageIO.read(url));
} catch (IOException ioe) {}
JToggleButton sortButton = new JToggleButton(icon);
sortButton.setSelected(sorted);
sortButton.setActionCommand(CATEGORIES);
sortButton.addActionListener(listener);
toolBar.add(sortButton);
ButtonGroup bg = new ButtonGroup();
bg.add(alphaButton);
bg.add(sortButton);
toolBar.addSeparator();
url = cl.getResource("properties.png");
try {
icon = new ImageIcon(ImageIO.read(url));
} catch (IOException ioe) {}
JToggleButton propButton = new JToggleButton(icon);
propButton.setSelected(true);
propButton.setActionCommand(PROPERTIES);
propButton.addActionListener(listener);
toolBar.add(propButton);
url = cl.getResource("events.png");
try {
icon = new ImageIcon(ImageIO.read(url));
} catch (IOException ioe) {}
JToggleButton eventsButton = new JToggleButton(icon);
eventsButton.setSelected(false);
eventsButton.setActionCommand(EVENTS);
eventsButton.addActionListener(listener);
eventsButton.setEnabled(false);
toolBar.add(eventsButton);
bg = new ButtonGroup();
bg.add(propButton);
bg.add(eventsButton);
return toolBar;
}
/*****************************************************************************/
/**
* Notified when a property changes in this property sheet.
* This method is used internally and should not be called by users.
*
* @param info Info on the changed property.
*/
public void displayedPropertyChanged(final PropertyInfo info) {
fireDisplayedPropertyChanged(info);
}
/*****************************************************************************/
/**
* Ends the resizing of the columns in this property container.
*
* @see #beginResizingColumns
* @see #resizeColumns
*/
void endResizingColumns() {
columnResizing = false;
propertyTableContainer.endResizingColumns();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -