/**
   * Utility method for binding an argument into the UI. Note that this method does not record any
   * commands, it simply adds a new row into the table and associates the passed {@link
   * PayloadFactoryArgument} to it.
   *
   * @return {@link TableItem} which was introduced.
   */
  private TableItem bindArgument(PayloadFactoryArgument argument) {
    TableItem item = new TableItem(tblArguments, SWT.NONE);
    ArgumentWrapper wrapper = new ArgumentWrapper(argument);
    wrapper.setArgumentExpression(
        EsbFactory.eINSTANCE.copyNamespacedProperty(argument.getArgumentExpression()));
    wrapper.setArgumentValue(argument.getArgumentValue());

    if (argument.getArgumentType() == PayloadFactoryArgumentType.EXPRESSION) {
      item.setText(
          new String[] {
            LITERAL_EXPRESSION,
            wrapper.getArgumentExpression().getPropertyValue(),
            argument.getEvaluator().toString()
          });
      wrapper.setEvaluator(argument.getEvaluator());
      wrapper.setExpression(true);
    } else {
      item.setText(new String[] {LITERAL_VALUE, wrapper.getArgumentValue(), ""});
      wrapper.setEvaluator(null);
      wrapper.setExpression(false);
    }

    item.setData(wrapper);
    return item;
  }
  /**
   * Removes the corresponding table item from the table and records a command for detaching the
   * corresponding argument from the model.
   *
   * @param itemIndex index of the row which is to be removed.
   */
  private void unbindArgument(int itemIndex) {
    TableItem item = tblArguments.getItem(itemIndex);
    ArgumentWrapper wrapper = (ArgumentWrapper) item.getData();
    PayloadFactoryArgument argument = wrapper.getArgument();

    if (null != argument.eContainer()) {
      RemoveCommand removeCmd =
          new RemoveCommand(
              editingDomain,
              mediator,
              EsbPackage.Literals.PAYLOAD_FACTORY_MEDIATOR__ARGS,
              argument);
      getResultCommand().append(removeCmd);
    }
    tblArguments.remove(tblArguments.indexOf(item));
  }
  @Override
  protected void okPressed() {
    for (TableItem item : tblArguments.getItems()) {
      ArgumentWrapper wrapper = (ArgumentWrapper) item.getData();
      PayloadFactoryArgument argument = wrapper.getArgument();

      // If the argument is a new one, add it to the model.
      if (null == argument.eContainer()) {
        AddCommand addCmd =
            new AddCommand(
                editingDomain,
                mediator,
                EsbPackage.Literals.PAYLOAD_FACTORY_MEDIATOR__ARGS,
                argument);
        getResultCommand().append(addCmd);
      }

      SetCommand setCmd = null;
      // set argument type if it has been changed i.e expression or value
      if ((argument.getArgumentType() == PayloadFactoryArgumentType.EXPRESSION)
          != wrapper.isExpression()) {
        setCmd =
            new SetCommand(
                editingDomain,
                argument,
                EsbPackage.Literals.PAYLOAD_FACTORY_ARGUMENT__ARGUMENT_TYPE,
                wrapper.isExpression()
                    ? PayloadFactoryArgumentType.EXPRESSION
                    : PayloadFactoryArgumentType.VALUE);
        getResultCommand().append(setCmd);
      }

      if (wrapper.isExpression()) {
        wrapper.setArgumentValue("default");
      } else {
        wrapper.getArgumentExpression().setPropertyValue("/default/expression");
      }

      if (wrapper.isExpression()) {
        setCmd =
            new SetCommand(
                editingDomain,
                argument,
                EsbPackage.Literals.PAYLOAD_FACTORY_ARGUMENT__EVALUATOR,
                wrapper.getEvaluator());
        getResultCommand().append(setCmd);
      }

      if (!argument.getArgumentExpression().equals(wrapper.getArgumentExpression())) {
        setCmd =
            new SetCommand(
                editingDomain,
                argument,
                EsbPackage.Literals.PAYLOAD_FACTORY_ARGUMENT__ARGUMENT_EXPRESSION,
                wrapper.getArgumentExpression());
        getResultCommand().append(setCmd);
      }

      if (!argument.getArgumentValue().equals(wrapper.getArgumentValue())) {
        setCmd =
            new SetCommand(
                editingDomain,
                argument,
                EsbPackage.Literals.PAYLOAD_FACTORY_ARGUMENT__ARGUMENT_VALUE,
                wrapper.getArgumentValue());
        getResultCommand().append(setCmd);
      }
    }
    // Apply changes.
    if (getResultCommand().canExecute()) {
      editingDomain.getCommandStack().execute(getResultCommand());
    }
    super.okPressed();
  }