@Override
 public synchronized void dispose() {
   for (final IObservable obs : this.dependencies) {
     obs.removeChangeListener(this);
   }
   super.dispose();
 }
 public ComputedOnChangeValue(final Object valueType, final IObservable... dependencies) {
   super(dependencies[0].getRealm());
   this.valueType = valueType;
   this.dependencies = dependencies;
   for (final IObservable obs : dependencies) {
     obs.addChangeListener(this);
   }
 }
 public AggregatedObservable aggregate(IObservable observable) {
   if (observable instanceof IObservableValue) {
     observableValues.add((IObservableValue) observable);
     observable.addChangeListener(this);
     return this;
   }
   return null;
 }
  public void prepare(Object target) {
    if (property != null) {
      final Object source = getElementByName(target, sourceName);
      if (source == null) {
        throw new XWTException("No element is found with the name = " + sourceName);
      }
      IObservable observableValue =
          ScopeManager.observeValue(source, source, property, UpdateSourceTrigger.PropertyChanged);
      observableValue.addChangeListener(
          new AbstractChangeListener(target) {

            public void handleChange(ChangeEvent event) {
              Class<?> valueType = JFaceXWTDataBinding.getValueType(source.getClass(), property);
              if (valueType == null) {
                LoggerManager.log(
                    "Type of the property "
                        + property
                        + " is not found in "
                        + source.getClass().getName());
                return;
              }
              Widget widget = UserData.getWidget(source);
              if (widget == null) {
                return;
              }

              //
              // test value ==
              //
              Object realValue = value;
              if (value != null) {
                IConverter converter = XWT.findConvertor(value.getClass(), valueType);
                if (converter != null) {
                  realValue = converter.convert(value);
                }
              }
              Object newValue = event.getSource();
              if (newValue instanceof IObservableValue) {
                IObservableValue observableValue = (IObservableValue) newValue;
                newValue = observableValue.getValue();
              }
              if (newValue != null) {
                IConverter newConverter = XWT.findConvertor(newValue.getClass(), valueType);
                if (newConverter != null) {
                  newValue = newConverter.convert(newValue);
                }
              }

              if (!Operator.compare(newValue, operator, realValue)) {
                restoreValues();
                if (oldvalues != null) {
                  oldvalues.clear();
                }
                return;
              }

              if (oldvalues != null && !oldvalues.isEmpty()) {
                return;
              }

              for (SetterBase setter : getSetters()) {
                try {
                  Object oldValue = setter.applyTo(element, true);
                  if (oldvalues == null) {
                    oldvalues = new HashMap<SetterBase, Object>();
                  }
                  oldvalues.put(setter, oldValue);
                } catch (RuntimeException e) {
                  continue;
                }
              }
            }
          });
    }
  }
 /**
  * Listen to changes in observable <code>source</code> and fire a {@link PropertyChangeEvent} with
  * supplied property name(s) on supplied <code>target</code> whenever the observed instance
  * changes.
  *
  * @param source
  * @param target
  * @param propertyNames
  */
 public static void replicate(IObservable source, Observable target, String... propertyNames) {
   source.addChangeListener(new ChangeReplicator(target, propertyNames));
 }
Exemple #6
0
  protected void bindNestedPojoProperty(
      FormToolkit toolkit,
      final IMessageManager mmng,
      final IPropertyDescriptor complexProperty,
      Composite parent,
      Object value,
      PropertyDescriptor property,
      Class<?> complexType) {
    String labelText;
    String propertyName = property.getName();
    Class<?> propertyType = property.getPropertyType();
    labelText = capitalize(splitCamelCase(propertyName));
    if (complexProperty instanceof ComplexUnionPropertyDescriptor) {
      // lets fix up the background colour!
      Label label = new Label(parent, SWT.NONE);
      label.setText(labelText);
    } else {
      Label label = toolkit.createLabel(parent, labelText);
    }

    Control widget;
    Class refType = isBeanRef(value, propertyName);
    IObservable observable;
    if (boolean.class.isAssignableFrom(propertyType)
        || Boolean.class.isAssignableFrom(propertyType)) {
      Button checkbox = new Button(parent, SWT.CHECK | SWT.BORDER);
      widget = checkbox;
      ISWTObservableValue textValue = Forms.observe(checkbox);
      observable = textValue;
      Forms.bindPojoProperty(
          bindingContext,
          mmng,
          value,
          propertyName,
          isMandatory(value, propertyName),
          labelText,
          textValue,
          checkbox);
    } else if (refType != null) {
      Combo combo = new Combo(parent, SWT.NONE | SWT.BORDER);
      String[] beanRefs = getBeanRefs(refType);
      combo.setItems(beanRefs);
      toolkit.adapt(combo, true, true);
      widget = combo;

      ISWTObservableValue comboValue = WidgetProperties.selection().observe(combo);
      observable = comboValue;
      Forms.bindPojoProperty(
          bindingContext,
          mmng,
          value,
          propertyName,
          isMandatory(value, propertyName),
          labelText,
          comboValue,
          combo);
    } else if (isEndpointUri(value, propertyName)) {
      Combo combo = new Combo(parent, SWT.NONE | SWT.BORDER);
      combo.setItems(getEndpointUris());
      toolkit.adapt(combo, true, true);
      widget = combo;

      ISWTObservableValue comboValue = WidgetProperties.selection().observe(combo);
      observable = comboValue;
      Forms.bindPojoProperty(
          bindingContext,
          mmng,
          value,
          propertyName,
          isMandatory(value, propertyName),
          labelText,
          comboValue,
          combo);
    } else if (Enum.class.isAssignableFrom(propertyType)) {
      ComboViewer combo = new ComboViewer(parent, SWT.READ_ONLY | SWT.BORDER);
      combo.setContentProvider(ArrayContentProvider.getInstance());
      combo.setInput(getEnumValues((Class<? extends Enum>) propertyType));

      IViewerObservableValue comboValue = ViewersObservables.observeSingleSelection(combo);
      observable = comboValue;
      Control control = combo.getControl();
      Forms.bindPojoProperty(
          bindingContext,
          mmng,
          value,
          propertyName,
          isMandatory(value, propertyName),
          labelText,
          comboValue,
          control);

      toolkit.adapt(control, true, true);
      widget = control;

    } else {
      Text text = toolkit.createText(parent, "");
      widget = text;
      // text.setToolTipText(tooltip);
      ISWTObservableValue textValue = Forms.observe(text);
      observable = textValue;
      Forms.bindPojoProperty(
          bindingContext,
          mmng,
          value,
          propertyName,
          isMandatory(value, propertyName),
          labelText,
          textValue,
          text);
    }
    widget.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    if (observable != null && node != null) {
      observable.addChangeListener(
          new IChangeListener() {

            @Override
            public void handleChange(ChangeEvent event) {
              // lets notify the node that its changed
              String id = complexProperty.getId().toString();
              fireNodePropertyChangedEvent(id);
            }
          });
    }
  }