View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.properties;
17  
18  import java.lang.reflect.Method;
19  
20  import org.xulux.api.gui.IProperty;
21  import org.xulux.api.gui.IWidget;
22  import org.xulux.api.gui.PropertyHandlerException;
23  
24  /**
25   * Set values based on introspection.
26   * For now pretty basic and limits to strings.
27   * TODO : Use BeanDataProvider for this!
28   *
29   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
30   * @version $Id: $
31   */
32  public class IntrospectionProperty extends SwingProperty {
33  
34      /**
35       * 
36       */
37      public IntrospectionProperty() {
38          super();
39  
40      }
41  
42      /**
43       * @see org.xulux.api.gui.IPropertyHandler#handleProperty(org.xulux.api.gui.IWidget, org.xulux.api.gui.IProperty)
44       */
45      public boolean handleProperty(IWidget widget, IProperty property) {
46          Object nativeWidget = widget.getNativeWidget();
47          try {
48              String methodName = "set"+property.getName().substring(0,1).toUpperCase()+property.getName().substring(1).toLowerCase();
49              System.out.println("methodname : " + methodName);
50              Method method = nativeWidget.getClass().getMethod(methodName, new Class[] { String.class });
51              method.invoke(nativeWidget, new Object[] { property.getValue() });
52              return true;
53          } catch (Throwable e) {
54              throw new PropertyHandlerException("Dynamically setting property failed", e);
55          }
56      }
57  }