KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > beans > JavaBeansScalarObservableValueFactory


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.internal.databinding.provisional.beans;
12
13 import java.beans.BeanInfo JavaDoc;
14 import java.beans.IntrospectionException JavaDoc;
15 import java.beans.Introspector JavaDoc;
16 import java.beans.PropertyDescriptor JavaDoc;
17
18 import org.eclipse.jface.internal.databinding.internal.beans.JavaBeanObservableValue;
19 import org.eclipse.jface.internal.databinding.provisional.description.Property;
20 import org.eclipse.jface.internal.databinding.provisional.factories.IObservableFactory;
21 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable;
22 import org.eclipse.jface.internal.databinding.provisional.observable.value.IObservableValue;
23
24 /**
25  * This is an optional IObservableFactory that forces all JavaBeans observables
26  * that it creates to be an IObservableValue, even if the actual type of
27  * the property is a collection.
28  *
29  * @since 1.0
30  */

31 public class JavaBeansScalarObservableValueFactory extends Object JavaDoc implements
32         IObservableFactory {
33
34     /* (non-Javadoc)
35      * @see org.eclipse.jface.internal.databinding.provisional.IObservableFactory#createObservable(org.eclipse.jface.internal.databinding.provisional.DataBindingContext, java.lang.Object)
36      */

37     public IObservable createObservable(Object JavaDoc description) {
38         if (! (description instanceof Property)) {
39            return null;
40         }
41         Property property = (Property) description;
42         Object JavaDoc collectionContainer = property.getObject();
43         String JavaDoc propertyName = (String JavaDoc) property.getPropertyID();
44         
45         BeanInfo JavaDoc beanInfo = null;
46         try {
47            beanInfo = Introspector.getBeanInfo(collectionContainer.getClass());
48         } catch (IntrospectionException JavaDoc e) {
49            return null;
50         }
51
52         boolean found = false;
53         int position = 0;
54         PropertyDescriptor JavaDoc[] pds = beanInfo.getPropertyDescriptors();
55         while (!found && position < pds.length) {
56            if (pds[position].getName().equals(propertyName)) {
57               found = true;
58               break;
59            }
60            ++position;
61         }
62         if (!found) {
63            return null;
64         }
65         IObservableValue updatable = new JavaBeanObservableValue(collectionContainer, pds[position]);
66         return updatable;
67      }
68
69 }
70
Popular Tags