Package org.eclipse.e4.xwt.javabean.metadata.properties

Source Code of org.eclipse.e4.xwt.javabean.metadata.properties.BeanProperty

/*******************************************************************************
* Copyright (c) 2006, 2008 Soyatec (http://www.soyatec.com) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Soyatec - initial API and implementation
*******************************************************************************/
package org.eclipse.e4.xwt.javabean.metadata.properties;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.e4.xwt.XWTException;
import org.eclipse.e4.xwt.annotation.Containment;
import org.eclipse.e4.xwt.core.IBinding;
import org.eclipse.e4.xwt.internal.utils.ObjectUtil;
import org.eclipse.e4.xwt.internal.utils.UserData;

public class BeanProperty extends AbstractProperty {
  protected PropertyDescriptor descriptor;
  private boolean containment = false;

  public BeanProperty(PropertyDescriptor descriptor) {
    super(descriptor.getName(), descriptor.getPropertyType());
    this.descriptor = descriptor;
   
    Method readMethod = descriptor.getReadMethod();
    if (readMethod != null) {
      Annotation annotation = readMethod.getAnnotation(Containment.class);
      if (annotation != null) {
        containment = true;
      }
    }
    Method writeMethod = descriptor.getWriteMethod();
    if (writeMethod != null) {
      Annotation annotation = writeMethod.getAnnotation(Containment.class);
      if (annotation != null) {
        containment = true;
      }
    }
  }

  public void setValue(Object target, Object value)
      throws IllegalArgumentException, IllegalAccessException,
      InvocationTargetException, SecurityException, NoSuchFieldException {
    if (descriptor != null && descriptor.getWriteMethod() != null) {
      Method writeMethod = descriptor.getWriteMethod();
      // Bug of invoke boolean value.
      Class<?>[] parameterTypes = writeMethod.getParameterTypes();
      if (parameterTypes.length == 1) {
        Class<?> type = parameterTypes[0];
        Class<?> propertyType = getType();
        if (propertyType != Object.class) {
          type = propertyType;
        }
        if (!ObjectUtil.isAssignableFrom(IBinding.class, propertyType)) {
          if (value != null && type != value.getClass()) {
            value = ObjectUtil.resolveValue(value, type, value);
          }
        }

        Object oldValue = null;
        Method readMethod = descriptor.getReadMethod();
        if (readMethod != null) {
          oldValue = readMethod.invoke(target);
        }

        if (value == null && type != null
            && UserData.getWidget(target) != null) {
          if (type == String.class) {
            value = "";
          } else if (type == Boolean.class) {
            value = false;
          }
        }

        if (oldValue != value) {
          writeMethod.setAccessible(true);
          try {
            writeMethod.invoke(target, value);
            fireSetPostAction(target, this, value);
          } catch (IllegalArgumentException e) {
            if (value == null) {
              throw new XWTException("Property type " + getName()
                  + " of " + target.getClass().getName()
                  + " cannot be null.", e);
            }
            throw new XWTException("Property type " + getName()
                + " of " + target.getClass().getName()
                + " is mismatch with "
                + value.getClass().getName()
                + " expected type is "
                + parameterTypes[0].getName(), e);
          }
          catch (InvocationTargetException e) {
            throw new XWTException("Problem of invoke " + getName()
                + " of " + target.getClass().getName()
                + " with a value of type "
                + value.getClass().getName(), e);
          }
        }
      }
    }
  }

  public Object getValue(Object target) throws IllegalArgumentException,
      IllegalAccessException, InvocationTargetException,
      SecurityException, NoSuchFieldException {
    if (descriptor != null && descriptor.getReadMethod() != null) {
      Method readMethod = descriptor.getReadMethod();
      readMethod.setAccessible(true);
      return readMethod.invoke(target);
    }
    return null;
  }

  @Override
  public boolean isDefault() {
    return true;
  }
 
  public boolean isContainement() {
    return containment;
  }

  public boolean isReadOnly() {
    return descriptor.getWriteMethod() == null;
  }
}
TOP

Related Classes of org.eclipse.e4.xwt.javabean.metadata.properties.BeanProperty

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.