01: /**
02: * L2FProd.com Common Components 7.3 License.
03: *
04: * Copyright 2005-2007 L2FProd.com
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package com.l2fprod.common.demo;
18:
19: import com.l2fprod.common.model.DefaultBeanInfoResolver;
20: import com.l2fprod.common.propertysheet.Property;
21: import com.l2fprod.common.propertysheet.PropertySheetPanel;
22:
23: import java.beans.BeanInfo;
24: import java.beans.PropertyChangeEvent;
25: import java.beans.PropertyChangeListener;
26: import java.beans.PropertyVetoException;
27:
28: import javax.swing.UIManager;
29:
30: /**
31: * Binds a bean object to a PropertySheet.
32: *
33: * <b>Note: this class is not part of the library</b>
34: */
35: public class BeanBinder {
36:
37: private final Object bean;
38: private final PropertySheetPanel sheet;
39: private final PropertyChangeListener listener;
40:
41: public BeanBinder(Object bean, PropertySheetPanel sheet) {
42: this (bean, sheet, new DefaultBeanInfoResolver()
43: .getBeanInfo(bean));
44: }
45:
46: public BeanBinder(Object bean, PropertySheetPanel sheet,
47: BeanInfo beanInfo) {
48: this .bean = bean;
49: this .sheet = sheet;
50:
51: sheet.setProperties(beanInfo.getPropertyDescriptors());
52: sheet.readFromObject(bean);
53:
54: // everytime a property change, update the button with it
55: listener = new PropertyChangeListener() {
56: public void propertyChange(PropertyChangeEvent evt) {
57: Property prop = (Property) evt.getSource();
58: try {
59: prop.writeToObject(BeanBinder.this .bean);
60: } catch (RuntimeException e) {
61: // handle PropertyVetoException and restore previous value
62: if (e.getCause() instanceof PropertyVetoException) {
63: UIManager.getLookAndFeel()
64: .provideErrorFeedback(
65: BeanBinder.this .sheet);
66: prop.setValue(evt.getOldValue());
67: }
68: }
69: }
70: };
71: sheet.addPropertySheetChangeListener(listener);
72: }
73:
74: public void unbind() {
75: sheet.removePropertyChangeListener(listener);
76: sheet.setProperties(new Property[0]);
77: }
78:
79: }
|