📄 propertysheet.java
字号:
/*****************************************************************************/
/**
* Notifies all registered listeners that a displayed property has
* changed.
*
* @param info Information on the changed property.
*/
protected void fireDisplayedPropertyChanged(final PropertyInfo info) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
DisplayedPropertyChangeEvent e =
new DisplayedPropertyChangeEvent(this, info);
// Process the listeners last to first, notifying
// those that are interested in this event.
for (int i=listeners.length-2; i>=0; i-=2) {
if (listeners[i]==PropertySheetListener.class) {
((PropertySheetListener)listeners[i+1]).
displayedPropertyChanged(e);
}
}
}
/*****************************************************************************/
/**
* Gets the properties of the specified JavaBean and sets the values
* displayed by this property sheet.
*
* @param bean A JavaBean. This must be of the same class as the class
* passed into the constructor.
* @see #setPropertiesOfBean
* @throws BeanNotRepresentedException If this property sheet isn't
* explicitly being used to represent a JavaBean.
*/
public void getPropertiesOfBean(Object bean)
throws BeanNotRepresentedException {
// Ensure we're modeling a JavaBean.
if (!isInitializedByBean()) {
throw new BeanNotRepresentedException("A bean is not being " +
"represented by this property sheet");
}
// Ensure the passed-in bean is of the right type.
Class clazz = bean.getClass();
Class beanInfoClass = beanInfo.getBeanDescriptor().getBeanClass();
if (!clazz.equals(beanInfoClass)) {
throw new IllegalArgumentException("JavaBean is wrong class: " +
"expected " + clazz + ", received " + beanInfoClass);
}
//int propertyCount = container.getPropertyCount();
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
int propertyCount = descriptors==null ? 0 : descriptors.length;
for (int i=0; i<propertyCount; i++) {
Method getter = descriptors[i].getReadMethod();
if (getter!=null) {
try {
// NOTE: We're ASSUMING a no-parameter getter!!!
// We should really check param count, but how would
// we get parameter values??
Object result = getter.invoke(bean, null);
PropertyInfo pi = propertyTableContainer.getPropertyInfo(
descriptors[i].getName());
if (pi!=null) {
// Set default value too as it's what we have as
// the "default".
pi.setValue(result);
pi.setDefaultValue(result);
}
} catch (Exception e) {
System.out.println(descriptors[i].getName());
e.printStackTrace();
}
}
}
}
/*****************************************************************************/
/**
* Initializes this property sheet. This is called by all constructors.
*
* @param sortedByCategory Whether the properties should be sorted by
* category.
*/
protected void initialize(boolean sortedByCategory) {
listenerList = new EventListenerList();
setLayout(new BorderLayout());
add(createToolBar(sortedByCategory), BorderLayout.NORTH);
setBackground(PropertySheetUtilities.getPropertyColor());
setBackground(Color.MAGENTA);
// Create the panel actually containing our properties.
interiorPanel = new JPanel();
interiorPanel.setBackground(Color.RED);
interiorPanel.setLayout(new BoxLayout(interiorPanel, BoxLayout.Y_AXIS));
propertyTableContainer = new PropertyTableContainer(sortedByCategory);
interiorPanel.add(propertyTableContainer);
MainPanel mp = new MainPanel(new BorderLayout());
mp.add(interiorPanel, BorderLayout.NORTH);
// JScrollPane scrollPane = new JScrollPane(mp);
javax.swing.JViewport viewport = new javax.swing.JViewport() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(PropertySheetUtilities.getPropertyColor());
Rectangle bounds = getBounds();
g.fillRect(0,0, bounds.width, bounds.height);
}
};
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewport(viewport);
scrollPane.setViewportView(mp);
// Create the panel below containing information on the selected
// property.
infoPanel = new InfoPanel();
// Add them to our split pane and we're done!
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
scrollPane, infoPanel);
splitPane.setResizeWeight(0.6);
splitPane.setContinuousLayout(true);
splitPane.setOneTouchExpandable(true);
add(splitPane);
}
/*****************************************************************************/
/**
* Returns whether a JavaBean class has been used to set the properties in
* this property sheet; if it has, then no more properties can be added.
*
* @return Whether this property sheet represents a JavaBean.
*/
private boolean isInitializedByBean() {
return beanInfo!=null;
}
/*****************************************************************************/
/**
* Returns whether the columns in this property sheet are being resized.
*
* @return Whether the columns in this property sheet are being resized.
*/
boolean isResizingColumns() {
return columnResizing;
}
/*****************************************************************************/
/**
* Returns whether or not the properties are sorted by category.
*
* @return Whether the properties are sorted by category.
* @see #setSortByCategory
*/
public boolean getSortByCategory() {
return propertyTableContainer.getSortByCategory();
}
/*****************************************************************************/
/**
* Removes the specified property sheet listener.
*
* @param l The listener to remove.
* @see #addPropertySheetListener
*/
public void removePropertySheetListener(PropertySheetListener l) {
listenerList.remove(PropertySheetListener.class, l);
}
/*****************************************************************************/
/**
* Resizes the columns such that the first column is the specified width,
* and the second column is the remaining space in the table.
*
* @param width The new width for the first column.
* @see #beginResizingColumns
* @see #endResizingColumns
*/
void resizeColumns(int width) {
if (isResizingColumns()) {
propertyTableContainer.resizeColumns(width);
}
}
/*****************************************************************************/
/**
* Callback for whenever a property is selected.
*
* @param e The selected property event.
*/
public void selectedPropertyChanged(SelectedPropertyEvent e) {
infoPanel.setPropertyInfo(e);
}
/*****************************************************************************/
/**
* Sets the properties of the specified JavaBean to the values represented
* in this property sheet.
*
* @param bean A JavaBean. This must be of the same class as the class
* passed into the constructor.
* @see #getPropertiesOfBean
* @throws BeanNotRepresentedException If this property sheet isn't
* explicitly being used to represent a JavaBean.
*/
public void setPropertiesOfBean(Object bean)
throws BeanNotRepresentedException {
// Ensure we're editing a JavaBean.
if (!isInitializedByBean()) {
throw new BeanNotRepresentedException(
"JavaBean not represented");
}
// Ensure the passed-in bean is of the right type.
Class clazz = bean.getClass();
Class beanInfoClass = beanInfo.getBeanDescriptor().getBeanClass();
if (!clazz.equals(beanInfoClass)) {
throw new IllegalArgumentException("JavaBean is wrong class: " +
"expected " + clazz + ", received " + beanInfoClass);
}
propertyTableContainer.setBeanProperties(bean);
}
/*****************************************************************************/
public void setSortByCategory(boolean sortByCategory) {
propertyTableContainer.setSortByCategory(sortByCategory);
}
/*****************************************************************************/
/*********************** INNER CLASSES ***************************************/
/*****************************************************************************/
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals(ALPHABETICAL)) {
setSortByCategory(false);
}
else if (command.equals(CATEGORIES)) {
setSortByCategory(true);
}
else if (command.equals(EVENTS)) {
}
else if (command.equals(PROPERTIES)) {
}
}
}
/*****************************************************************************/
private static class MainPanel extends JPanel implements Scrollable {
/**
*
*/
private static final long serialVersionUID = -5398399362948762890L;
public static final int ROW_COUNT = 12;
public MainPanel(LayoutManager layout) {
super(layout);
}
public Dimension getPreferredScrollableViewportSize() {
Dimension size = getPreferredSize();
size.height = PROPERTY_ROW_HEIGHT * ROW_COUNT;
return size;
}
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation, int direction) {
switch (orientation) {
case SwingConstants.VERTICAL:
return getVisibleRect().height;
case SwingConstants.HORIZONTAL:
return getVisibleRect().width;
}
return 1; // Should never happen.
}
public boolean getScrollableTracksViewportHeight() {
return false;
}
public boolean getScrollableTracksViewportWidth() {
return true;
}
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation, int direction) {
switch (orientation) {
case SwingConstants.VERTICAL:
return PROPERTY_ROW_HEIGHT;
case SwingConstants.HORIZONTAL:
return 10; // Arbitrary.
}
return 1; // Should never happen.
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(PropertySheetUtilities.getPropertyColor());
Rectangle bounds = getBounds();
g.fillRect(20,0, bounds.width-20,bounds.height);
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -