Beispiel #1
0
 public String getElementName() {
   IObservable model = context.getModel();
   if (elementName == null && model != null && model.getType() == IObservable.OBSERVE_SWT_JFACE) {
     Object source = model.getSource();
     elementName = getWidgetName(source);
   }
   return elementName;
 }
  /**
   * Invoke the method and return a Publisher for the return value.
   *
   * @param exchange the current exchange
   * @param bindingContext the binding context to use
   * @param providedArgs optional list of argument values to check by type (via {@code instanceof})
   *     for resolving method arguments.
   * @return Publisher that produces a single HandlerResult or an error signal; never throws an
   *     exception
   */
  public Mono<HandlerResult> invokeForRequest(
      ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) {

    return resolveArguments(exchange, bindingContext, providedArgs)
        .then(
            args -> {
              try {
                Object value = doInvoke(args);
                ModelMap model = bindingContext.getModel();
                HandlerResult handlerResult =
                    new HandlerResult(this, value, getReturnType(), model);
                return Mono.just(handlerResult);
              } catch (InvocationTargetException ex) {
                return Mono.error(ex.getTargetException());
              } catch (Throwable ex) {
                String s = getInvocationErrorMessage(args);
                return Mono.error(new IllegalStateException(s));
              }
            });
  }
Beispiel #3
0
 public IObservable getModel() {
   return context.getModel();
 }
Beispiel #4
0
  public Command bindWithCommand() {
    if (!canBinding()) {
      return null;
    }
    CompoundCommand commandList = new CompoundCommand();

    Property targetProperty = context.getTargetProperty();
    Property modelProperty = context.getModelProperty();
    IObservable target = context.getTarget();
    IObservable model = context.getModel();
    Object targetSource = target.getSource();

    Object modelSource = model.getSource();

    XamlNode node = null;
    if (targetSource instanceof Widget) {
      node = XWTProxy.getModel((Widget) targetSource);
    } else if (targetSource instanceof XamlElement) {
      node = (XamlElement) targetSource;
    }

    if (node == null) {
      return UnexecutableCommand.INSTANCE;
    }

    // 1. Get attr of targetProperty, such as < text="{}"/>
    XamlAttribute attribute = node.getAttribute(targetProperty.getName(), IConstants.XWT_NAMESPACE);
    if (attribute != null) {
      commandList.add(new DeleteCommand(attribute));
      attribute = null;
    }
    if (attribute == null) {
      attribute =
          XamlFactory.eINSTANCE.createAttribute(targetProperty.getName(), IConstants.XWT_NAMESPACE);
    }
    // 2. Binding Node.
    if (bindingNode == null) {
      bindingNode = attribute.getChild(BINDING, IConstants.XWT_NAMESPACE);
    }
    if (bindingNode == null) {
      bindingNode = XamlFactory.eINSTANCE.createElement(BINDING, IConstants.XWT_NAMESPACE);
    }
    // 3. ElementName
    int type = model.getType();
    if (type == IObservable.OBSERVE_SWT_JFACE) {
      addAttr(
          bindingNode,
          ELEMENT_NAME,
          IConstants.XWT_NAMESPACE,
          getCreateName(modelSource, commandList));
    }
    // 4. DataContext
    else if (IObservable.OBSERVE_JAVA_BAEN == type) {
      XamlDocument ownerDocument = node.getOwnerDocument();
      XamlElement rootElement = ownerDocument.getRootElement();
      DataContext dataContext = (DataContext) modelSource;
      String key = dataContext.getKey();
      if (key != null) {
        boolean canUseDC = getCodeStyles().useDataContext;
        if (canUseDC) {
          XamlAttribute contextNode =
              getAttribute(rootElement, DATA_CONTEXT, IConstants.XWT_NAMESPACE);
          contextNode.setUseFlatValue(true);

          XamlElement resourceNode =
              getElement(contextNode, STATIC_RESOURCE, IConstants.XWT_NAMESPACE);
          if (!key.equals(resourceNode.getValue())) {
            if (resourceNode.getValue() != null) {
              canUseDC = false;
            } else {
              resourceNode.setValue(key);
              commandList.add(new AddNewChildCommand(contextNode, resourceNode));
              commandList.add(new AddNewChildCommand(rootElement, contextNode));
            }
          }
        }
        if (!canUseDC || !getCodeStyles().useDataContext) {
          XamlAttribute sourceAttr = getAttribute(bindingNode, SOURCE, IConstants.XWT_NAMESPACE);
          sourceAttr.setUseFlatValue(true);
          XamlElement resourceNode =
              getElement(sourceAttr, STATIC_RESOURCE, IConstants.XWT_NAMESPACE);

          resourceNode.setValue(key);
          commandList.add(new AddNewChildCommand(sourceAttr, resourceNode));
          commandList.add(new AddNewChildCommand(bindingNode, sourceAttr));
        }
      }
    }

    // 5. Path
    addAttr(bindingNode, PATH, IConstants.XWT_NAMESPACE, modelProperty.toString());

    // 6. BindingMode
    if (bindingMode != null && BindingMode.TwoWay.equals(bindingMode)) {
      addAttr(bindingNode, MODE, IConstants.XWT_NAMESPACE, bindingMode.name());
    }

    // 7. Converter.
    if (converter != null) {
      addAttr(bindingNode, CONVERTER, IConstants.XWT_NAMESPACE, converter);
    }

    // 8. UpdateSourceTrigger
    if (triggerMode != null) {
      addAttr(bindingNode, UPDATE_SOURCE_TRIGGER, IConstants.XWT_NAMESPACE, triggerMode.name());
    }

    // 9. Code Style.
    attribute.setUseFlatValue(getCodeStyles().useFlatVlaue);

    if (!attribute.getChildNodes().contains(bindingNode)) {
      attribute.getChildNodes().add(bindingNode);
    }
    if (!node.getAttributes().contains(attribute)) {
      commandList.add(new AddNewChildCommand(node, attribute));
    }
    return commandList.unwrap();
  }