Search in sources :

Example 1 with FieldBeanBindableInfo

use of org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo in project windowbuilder by eclipse.

the class ControllerSupport method moveBean.

private static void moveBean(DatabindingsProvider provider, IObserveInfo observe, AstEditor controllerEditor, TypeDeclaration controllerRootNode) throws Exception {
    if (observe instanceof FieldBeanBindableInfo) {
        // 
        FieldBeanBindableInfo fieldBindable = (FieldBeanBindableInfo) observe;
        VariableDeclarationFragment fragment = fieldBindable.getFragment();
        // 
        if (fragment != null) {
            FieldDeclaration fieldDeclaration = AstNodeUtils.getEnclosingFieldDeclaration(fragment);
            String modifier = Modifier.ModifierKeyword.fromFlagValue(fieldDeclaration.getModifiers()).toString();
            // 
            controllerEditor.addFieldDeclaration(modifier + " " + CoreUtils.getClassName(fieldBindable.getObjectType()) + " " + fragment.getName().getIdentifier() + ";", new BodyDeclarationTarget(controllerRootNode, true));
        }
    } else if (observe instanceof MethodBeanBindableInfo) {
        IObserveInfo parentObserve = observe.getParent();
        if (parentObserve == null) {
            MethodBeanBindableInfo methodBindable = (MethodBeanBindableInfo) observe;
            methodBindable.setReferenceProvider(new StringReferenceProvider(provider.getControllerViewerField() + "." + methodBindable.getReference()));
        } else {
            moveBean(provider, parentObserve, controllerEditor, controllerRootNode);
        }
    } else {
        GlobalFactoryHelper.moveBean(observe, controllerEditor, controllerRootNode);
    }
}
Also used : IObserveInfo(org.eclipse.wb.internal.core.databinding.model.IObserveInfo) BodyDeclarationTarget(org.eclipse.wb.internal.core.utils.ast.BodyDeclarationTarget) FieldBeanBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo) MethodBeanBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.MethodBeanBindableInfo) StringReferenceProvider(org.eclipse.wb.internal.core.databinding.model.reference.StringReferenceProvider) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 2 with FieldBeanBindableInfo

use of org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo in project windowbuilder by eclipse.

the class BeansObserveTypeContainer method synchronizeLocalComposites.

private void synchronizeLocalComposites(final BeanSupport beanSupport) throws Exception {
    int observableSize = m_observables.size();
    int localCompositeIndex = -1;
    // find first local Composite
    for (int i = 0; i < observableSize; i++) {
        BeanBindableInfo bindable = m_observables.get(i);
        if (bindable instanceof FieldBeanBindableInfo && ((FieldBeanBindableInfo) bindable).getFragment() == null) {
            localCompositeIndex = i;
            break;
        }
    }
    // or last field
    if (localCompositeIndex == -1) {
        for (int i = 0; i < observableSize; i++) {
            BeanBindableInfo bindable = m_observables.get(i);
            if (bindable instanceof FieldBeanBindableInfo && ((FieldBeanBindableInfo) bindable).getFragment() != null) {
                localCompositeIndex = i + 1;
                break;
            }
        }
    }
    // or sets as first item
    if (localCompositeIndex == -1) {
        localCompositeIndex = 0;
    }
    // update local Composites
    SynchronizeManager.synchronizeObjects(m_observables.subList(localCompositeIndex, observableSize), getLocalComposites(), new ISynchronizeProcessor<JavaInfo, BeanBindableInfo>() {

        @Override
        public boolean handleObject(BeanBindableInfo bindable) {
            return bindable instanceof FieldBeanBindableInfo && ((FieldBeanBindableInfo) bindable).getFragment() == null;
        }

        @Override
        public JavaInfo getKeyObject(BeanBindableInfo bindable) {
            FieldBeanBindableInfo fieldBindable = (FieldBeanBindableInfo) bindable;
            return fieldBindable.getHostJavaInfo();
        }

        @Override
        public boolean equals(JavaInfo key0, JavaInfo key1) {
            return key0 == key1;
        }

        @Override
        public BeanBindableInfo findObject(Map<JavaInfo, BeanBindableInfo> keyObjectToObject, JavaInfo key) throws Exception {
            return null;
        }

        @Override
        public BeanBindableInfo createObject(JavaInfo javaInfo) throws Exception {
            // prepare bean class
            Class<?> componentClass = javaInfo.getDescription().getComponentClass();
            // prepare reference provider
            IReferenceProvider javaReferenceProvider = new JavaInfoReferenceProvider(javaInfo, m_provider);
            FragmentReferenceProvider referenceProvider = new FragmentReferenceProvider(javaReferenceProvider);
            // 
            return new FieldBeanBindableInfo(beanSupport, null, componentClass, referenceProvider, javaInfo);
        }

        @Override
        public void update(BeanBindableInfo bindable) throws Exception {
            FieldBeanBindableInfo bean = (FieldBeanBindableInfo) bindable;
            bean.update(BeansObserveTypeContainer.this);
        }
    });
}
Also used : JavaInfoReferenceProvider(org.eclipse.wb.internal.rcp.databinding.model.widgets.bindables.JavaInfoReferenceProvider) FieldBeanBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo) IReferenceProvider(org.eclipse.wb.internal.core.databinding.model.reference.IReferenceProvider) JavaInfo(org.eclipse.wb.core.model.JavaInfo) FragmentReferenceProvider(org.eclipse.wb.internal.core.databinding.model.reference.FragmentReferenceProvider) FieldBeanBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo) BeanBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.BeanBindableInfo) MethodBeanBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.MethodBeanBindableInfo)

Example 3 with FieldBeanBindableInfo

use of org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo in project windowbuilder by eclipse.

the class BeansObserveTypeContainer method createObservables.

@Override
public void createObservables(JavaInfo root, final IModelResolver resolver, AstEditor editor, TypeDeclaration rootNode) throws Exception {
    m_rootJavaInfo = root;
    m_observables = Lists.newArrayList();
    // handle fields
    ClassLoader classLoader = EditorState.get(m_rootJavaInfo.getEditor()).getEditorLoader();
    BeanSupport beanSupport = new BeanSupport(classLoader, resolver);
    beanSupport.setProvider(m_provider);
    // 
    for (VariableDeclarationFragment fragment : CoreUtils.getFieldFragments(rootNode)) {
        try {
            // prepare bean class
            Type type = CoreUtils.getType(fragment, true);
            Class<?> beanClass = loadClass(AstNodeUtils.getFullyQualifiedName(type, true));
            // prepare association widget
            JavaInfo widget = getJavaInfoRepresentedBy(fragment.getName().getIdentifier());
            // 
            m_observables.add(new FieldBeanBindableInfo(beanSupport, fragment, beanClass, widget));
        } catch (ClassNotFoundException e) {
            AbstractParser.addError(editor, "ClassNotFoundException: " + fragment, new Throwable());
        } catch (Throwable e) {
            throw ReflectionUtils.propagate(e);
        }
    }
    // handle all Composite declared as local variable
    for (JavaInfo javaInfo : getLocalComposites()) {
        // prepare bean class
        Class<?> componentClass = javaInfo.getDescription().getComponentClass();
        // prepare reference provider
        IReferenceProvider javaReferenceProvider = new JavaInfoReferenceProvider(javaInfo, m_provider);
        FragmentReferenceProvider referenceProvider = new FragmentReferenceProvider(javaReferenceProvider);
        // 
        m_observables.add(new FieldBeanBindableInfo(beanSupport, null, componentClass, referenceProvider, javaInfo));
    }
    // prepare super class
    Class<?> superClass = Object.class;
    if (rootNode.getSuperclassType() != null) {
        String className = AstNodeUtils.getFullyQualifiedName(rootNode.getSuperclassType(), true);
        try {
            superClass = loadClass(className);
        } catch (ClassNotFoundException e) {
            AbstractParser.addError(editor, "ClassNotFoundException: " + className, new Throwable());
            superClass = Object.class;
        }
    }
    // handle methods from super class
    Set<String> methodNames = Sets.newHashSet();
    // 
    BeanInfo beanInfo = Introspector.getBeanInfo(superClass);
    for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
        if (BeanSupport.isGetter(descriptor)) {
            String methodName = descriptor.getReadMethod().getName();
            if (!DataBindingsRootInfo.INIT_DATA_BINDINGS_METHOD_NAME.equals(methodName)) {
                methodNames.add(methodName);
                m_observables.add(new MethodBeanBindableInfo(beanSupport, null, descriptor.getPropertyType(), methodName + "()"));
            }
        }
    }
    // handle local methods
    for (MethodDeclaration method : rootNode.getMethods()) {
        String methodName = method.getName().getIdentifier();
        if (!methodNames.contains(methodName) && !DataBindingsRootInfo.INIT_DATA_BINDINGS_METHOD_NAME.equals(methodName) && methodName.startsWith("get")) {
            IMethodBinding binding = AstNodeUtils.getMethodBinding(method);
            ITypeBinding returnType = binding == null ? null : binding.getReturnType();
            // 
            if (CoreUtils.isIncludeTypeBinding(returnType)) {
                String className = AstNodeUtils.getFullyQualifiedName(returnType, true);
                try {
                    Class<?> propertyClass = loadClass(className);
                    // 
                    m_observables.add(new MethodBeanBindableInfo(beanSupport, null, propertyClass, methodName + "()"));
                } catch (ClassNotFoundException e) {
                    AbstractParser.addError(editor, "ClassNotFoundException: " + className, new Throwable());
                }
            }
        }
    }
    // handle initDataBindings() local variables
    for (VariableDeclarationFragment fragment : CoreUtils.getLocalFragments(rootNode, DataBindingsRootInfo.INIT_DATA_BINDINGS_METHOD_NAME)) {
        try {
            // prepare bean class
            ITypeBinding typeBinding = CoreUtils.getType(fragment, true);
            Class<?> beanClass = loadClass(AstNodeUtils.getFullyQualifiedName(typeBinding, true));
            m_observables.add(new LocalVariableBindableInfo(beanSupport, fragment, beanClass));
        } catch (ClassNotFoundException e) {
            AbstractParser.addError(editor, "ClassNotFoundException: " + fragment, new Throwable());
        } catch (Throwable e) {
            throw ReflectionUtils.propagate(e);
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) FieldBeanBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo) PropertyDescriptor(java.beans.PropertyDescriptor) MethodBeanBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.MethodBeanBindableInfo) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IReferenceProvider(org.eclipse.wb.internal.core.databinding.model.reference.IReferenceProvider) BeanInfo(java.beans.BeanInfo) FragmentReferenceProvider(org.eclipse.wb.internal.core.databinding.model.reference.FragmentReferenceProvider) JavaInfoReferenceProvider(org.eclipse.wb.internal.rcp.databinding.model.widgets.bindables.JavaInfoReferenceProvider) Type(org.eclipse.jdt.core.dom.Type) ObserveType(org.eclipse.wb.internal.core.databinding.ui.ObserveType) BeanSupport(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.BeanSupport) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) JavaInfo(org.eclipse.wb.core.model.JavaInfo) LocalVariableBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.LocalVariableBindableInfo)

Example 4 with FieldBeanBindableInfo

use of org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo in project windowbuilder by eclipse.

the class BeanBindableTest method test_children_properties.

public void test_children_properties() throws Exception {
    CompositeInfo shell = parseComposite("import java.util.ArrayList;", "public class Test {", "  protected Shell m_shell;", "  private String m_name;", "  private ArrayList m_list;", "  public static void main(String[] args) {", "    Test test = new Test();", "    test.open();", "  }", "  public void open() {", "    Display display = new Display();", "    createContents();", "    m_shell.open();", "    m_shell.layout();", "    while (!m_shell.isDisposed()) {", "      if (!display.readAndDispatch()) {", "        display.sleep();", "      }", "    }", "  }", "  protected void createContents() {", "    m_shell = new Shell();", "  }", "}");
    assertNotNull(shell);
    // 
    DatabindingsProvider provider = getDatabindingsProvider();
    assertInstanceOf(BeansObserveTypeContainer.class, provider.getContainers().get(0));
    // 
    List<IObserveInfo> observes = provider.getObserves(ObserveType.BEANS);
    assertNotNull(observes);
    assertEquals(4, observes.size());
    // 
    assertBindable(FieldBeanBindableInfo.class, "m_name - String|m_name|java.lang.String", observes.get(1));
    // 
    List<IObserveInfo> nameChildren = observes.get(1).getChildren(ChildrenContext.ChildrenForMasterTable);
    assertEquals(1, nameChildren.size());
    assertBindable(MethodBeanBindableInfo.class, observes.get(1), true, "m_name.getClass()|m_name.getClass()|java.lang.Class", nameChildren.get(0));
    // 
    List<IObserveInfo> nameProperties = observes.get(1).getChildren(ChildrenContext.ChildrenForPropertiesTable);
    assertEquals(3, nameProperties.size());
    // 
    assertBindable(BeanPropertyDescriptorBindableInfo.class, null, true, "bytes|\"bytes\"|[B", nameProperties.get(0));
    // 
    List<IObserveInfo> bytesProperties = nameProperties.get(0).getChildren(ChildrenContext.ChildrenForPropertiesTable);
    assertEquals(1, bytesProperties.size());
    // 
    assertBindable(BeanPropertyDescriptorBindableInfo.class, nameProperties.get(0), true, "class|\"bytes.class\"|java.lang.Class", bytesProperties.get(0));
    // 
    FieldBeanBindableInfo nameField = (FieldBeanBindableInfo) observes.get(1);
    assertNull(nameField.resolvePropertyReference("bytez"));
    assertSame(nameProperties.get(0), nameField.resolvePropertyReference("\"bytes\""));
    assertSame(bytesProperties.get(0), nameField.resolvePropertyReference("\"bytes.class\""));
    // 
    assertBindable(BeanPropertyDescriptorBindableInfo.class, null, true, "class|\"class\"|java.lang.Class", nameProperties.get(1));
    // 
    assertBindable(BeanPropertyDescriptorBindableInfo.class, null, false, "empty|\"empty\"|boolean", nameProperties.get(2));
    // 
    assertBindable(FieldBeanBindableInfo.class, "m_list - ArrayList|m_list|java.util.ArrayList", observes.get(2));
    List<IObserveInfo> listChildren = observes.get(2).getChildren(ChildrenContext.ChildrenForMasterTable);
    assertEquals(1, listChildren.size());
    assertBindable(MethodBeanBindableInfo.class, observes.get(2), true, "m_list.getClass()|m_list.getClass()|java.lang.Class", listChildren.get(0));
    // 
    List<IObserveInfo> listProperties = observes.get(2).getChildren(ChildrenContext.ChildrenForPropertiesTable);
    assertEquals(3, listProperties.size());
    // 
    assertBindable(CollectionPropertyBindableInfo.class, null, false, "Collection as WritableList/Properties.selfList()|m_list|java.util.ArrayList", listProperties.get(0));
    // 
    assertBindable(BeanPropertyDescriptorBindableInfo.class, null, true, "class|\"class\"|java.lang.Class", listProperties.get(1));
    // 
    assertBindable(BeanPropertyDescriptorBindableInfo.class, null, false, "empty|\"empty\"|boolean", listProperties.get(2));
}
Also used : IObserveInfo(org.eclipse.wb.internal.core.databinding.model.IObserveInfo) FieldBeanBindableInfo(org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo) DatabindingsProvider(org.eclipse.wb.internal.rcp.databinding.DatabindingsProvider) CompositeInfo(org.eclipse.wb.internal.swt.model.widgets.CompositeInfo)

Aggregations

FieldBeanBindableInfo (org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.FieldBeanBindableInfo)4 MethodBeanBindableInfo (org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.MethodBeanBindableInfo)3 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 JavaInfo (org.eclipse.wb.core.model.JavaInfo)2 IObserveInfo (org.eclipse.wb.internal.core.databinding.model.IObserveInfo)2 FragmentReferenceProvider (org.eclipse.wb.internal.core.databinding.model.reference.FragmentReferenceProvider)2 IReferenceProvider (org.eclipse.wb.internal.core.databinding.model.reference.IReferenceProvider)2 JavaInfoReferenceProvider (org.eclipse.wb.internal.rcp.databinding.model.widgets.bindables.JavaInfoReferenceProvider)2 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)1 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 Type (org.eclipse.jdt.core.dom.Type)1 StringReferenceProvider (org.eclipse.wb.internal.core.databinding.model.reference.StringReferenceProvider)1 ObserveType (org.eclipse.wb.internal.core.databinding.ui.ObserveType)1 BodyDeclarationTarget (org.eclipse.wb.internal.core.utils.ast.BodyDeclarationTarget)1 DatabindingsProvider (org.eclipse.wb.internal.rcp.databinding.DatabindingsProvider)1 BeanBindableInfo (org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.BeanBindableInfo)1