@Test
  public void testCreateLabel() throws NoLabelFoundException {
    /* setup */
    final VControl control1 = mock(VControl.class);
    final VFeaturePathDomainModelReference dmr1 = mock(VFeaturePathDomainModelReference.class);
    when(control1.getDomainModelReference()).thenReturn(dmr1);

    final VControl control2 = mock(VControl.class);
    final VFeaturePathDomainModelReference dmr2 = mock(VFeaturePathDomainModelReference.class);
    when(control2.getDomainModelReference()).thenReturn(dmr2);

    when(compoundControl.getControls()).thenReturn(ECollections.asEList(control1, control2));

    final EObject domainModel = mock(EObject.class);
    when(viewModelContext.getDomainModel()).thenReturn(domainModel);

    when(emfFormsLabelProvider.getDisplayName(dmr1, domainModel))
        .thenReturn(Observables.constantObservableValue(LABEL1, String.class));

    when(emfFormsLabelProvider.getDisplayName(dmr2, domainModel))
        .thenReturn(Observables.constantObservableValue(LABEL2, String.class));

    final CompoundControlSWTRenderer renderer = createRenderer();

    /* act */
    final Control label = renderer.createLabel(shell);

    /* assert */
    assertTrue(Composite.class.isInstance(label));
    final Composite labelComposite = Composite.class.cast(label);
    assertEquals(3, labelComposite.getChildren().length);
    for (final Control control : labelComposite.getChildren()) {
      assertTrue(Label.class.isInstance(control));
    }
    assertEquals(LABEL1, Label.class.cast(labelComposite.getChildren()[0]).getText());
    assertEquals("/", Label.class.cast(labelComposite.getChildren()[1]).getText()); // $NON-NLS-1$
    assertEquals(LABEL2, Label.class.cast(labelComposite.getChildren()[2]).getText());
  }
  @Test
  public void testCreateControls() throws EMFFormsNoRendererException {
    /* setup */
    final VControl control1 = mock(VControl.class);
    final VFeaturePathDomainModelReference dmr1 = mock(VFeaturePathDomainModelReference.class);
    when(control1.getDomainModelReference()).thenReturn(dmr1);

    final VControl control2 = mock(VControl.class);
    final VFeaturePathDomainModelReference dmr2 = mock(VFeaturePathDomainModelReference.class);
    when(control2.getDomainModelReference()).thenReturn(dmr2);

    when(compoundControl.getControls()).thenReturn(ECollections.asEList(control1, control2));

    final EObject domainModel = mock(EObject.class);
    when(viewModelContext.getDomainModel()).thenReturn(domainModel);

    final DummyRenderer dummyRenderer1 = createDummyRenderer(control1);
    final DummyRenderer dummyRenderer2 = createDummyRenderer(control2);

    when(emfFormsRendererFactory.getRendererInstance(control1, viewModelContext))
        .thenReturn(dummyRenderer1);

    when(emfFormsRendererFactory.getRendererInstance(control2, viewModelContext))
        .thenReturn(dummyRenderer2);

    final CompoundControlSWTRenderer renderer = spy(createRenderer());

    doReturn(GridLayoutFactory.fillDefaults().create())
        .when(renderer)
        .getColumnLayout(any(Integer.class), any(Boolean.class));

    doReturn(GridDataFactory.fillDefaults().create())
        .when(renderer)
        .getLayoutData(
            any(SWTGridCell.class),
            any(SWTGridDescription.class),
            any(SWTGridDescription.class),
            any(SWTGridDescription.class),
            any(VElement.class),
            any(EObject.class),
            any(Control.class));

    doReturn(GridDataFactory.fillDefaults().create())
        .when(renderer)
        .getSpanningLayoutData(
            any(VContainedElement.class), any(Integer.class), any(Integer.class));

    /* act */
    final Control controls = renderer.createControls(shell);

    /* assert */
    assertTrue(Composite.class.isInstance(controls));
    final Composite controlsComposite = Composite.class.cast(controls);
    assertEquals(2, controlsComposite.getChildren().length);
    for (final Control control : controlsComposite.getChildren()) {
      assertTrue(Composite.class.isInstance(control));
    }

    final Composite control1Composite = Composite.class.cast(controlsComposite.getChildren()[0]);
    assertEquals(2, control1Composite.getChildren().length);
    for (final Control control : control1Composite.getChildren()) {
      assertTrue(Label.class.isInstance(control));
    }
    assertEquals(C_VALIDATION, Label.class.cast(control1Composite.getChildren()[0]).getText());
    assertEquals(C_CONTROL, Label.class.cast(control1Composite.getChildren()[1]).getText());

    final Composite control2Composite = Composite.class.cast(controlsComposite.getChildren()[1]);
    assertEquals(2, control2Composite.getChildren().length);
    for (final Control control : control2Composite.getChildren()) {
      assertTrue(Label.class.isInstance(control));
    }
    assertEquals(C_VALIDATION, Label.class.cast(control2Composite.getChildren()[0]).getText());
    assertEquals(C_CONTROL, Label.class.cast(control2Composite.getChildren()[1]).getText());

    verify(control1, times(1)).setLabelAlignment(LabelAlignment.NONE);
    verify(control2, times(1)).setLabelAlignment(LabelAlignment.NONE);
  }
Esempio n. 3
0
  /**
   * Copies the values from one {@link VControl} to another.
   *
   * @param from the {@link VControl} holding the values
   * @param fromDomainModel The domain model belonging to the 'from control'.
   * @param to the {@link VControl} which values should be updated
   * @param toDomainModel The domain model belonging to the 'to control'.
   * @since 1.6
   */
  @SuppressWarnings("unchecked")
  public static void copyValues(
      VControl from, EObject fromDomainModel, VControl to, EObject toDomainModel) {
    final IObservableValue fromObservableValue;
    final IObservableValue toObservableValue;
    try {
      fromObservableValue =
          Activator.getDefault()
              .getEMFFormsDatabinding()
              .getObservableValue(from.getDomainModelReference(), fromDomainModel);
      toObservableValue =
          Activator.getDefault()
              .getEMFFormsDatabinding()
              .getObservableValue(to.getDomainModelReference(), toDomainModel);
    } catch (final DatabindingFailedException ex) {
      Activator.getDefault().getReportService().report(new DatabindingFailedReport(ex));
      return;
    }
    final EObject fromEObject = (EObject) ((IObserving) fromObservableValue).getObserved();
    final EStructuralFeature fromStructuralFeature =
        (EStructuralFeature) fromObservableValue.getValueType();
    final EObject toEObject = (EObject) ((IObserving) toObservableValue).getObserved();
    final EStructuralFeature toStructuralFeature =
        (EStructuralFeature) toObservableValue.getValueType();

    fromObservableValue.dispose();
    toObservableValue.dispose();

    if (!toStructuralFeature.isChangeable()) {
      return;
    }

    final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(toEObject);

    if (toStructuralFeature.isMany()) {
      editingDomain
          .getCommandStack()
          .execute(
              RemoveCommand.create(
                  editingDomain,
                  toEObject,
                  toStructuralFeature,
                  (Collection<?>) toEObject.eGet(toStructuralFeature, true)));
    }
    if (EAttribute.class.isInstance(toStructuralFeature)) {

      if (toStructuralFeature.isMany()) {
        editingDomain
            .getCommandStack()
            .execute(
                AddCommand.create(
                    editingDomain,
                    toEObject,
                    toStructuralFeature,
                    (Collection<?>) fromEObject.eGet(fromStructuralFeature, true)));
      } else {
        editingDomain
            .getCommandStack()
            .execute(
                SetCommand.create(
                    editingDomain,
                    toEObject,
                    toStructuralFeature,
                    fromEObject.eGet(fromStructuralFeature, true)));
      }
    }
    if (EReference.class.isInstance(toStructuralFeature)) {
      if (toStructuralFeature.isMany()) {
        for (final EObject eObject :
            (Collection<EObject>) fromEObject.eGet(fromStructuralFeature, true)) {
          editingDomain
              .getCommandStack()
              .execute(
                  AddCommand.create(
                      editingDomain, toEObject, toStructuralFeature, EcoreUtil.copy(eObject)));
        }
        return;
      }
      editingDomain
          .getCommandStack()
          .execute(
              SetCommand.create(
                  editingDomain,
                  toEObject,
                  toStructuralFeature,
                  EcoreUtil.copy((EObject) fromEObject.eGet(fromStructuralFeature, true))));
    }
  }