/* (non-Javadoc)
   * @see org.eclipse.vtp.desktop.ui.app.editor.model.DatabaseQuerySettingsListener#sourceDatabaseChanged()
   */
  public void sourceDatabaseChanged() {
    settings.dataMapping.clear();

    if (currentObject != null && currentObject.getType().hasBaseType()) {
      if (!currentObject.getType().isObject()) {
        settings.dataMapping.add(settings.new DataMapping("Value", -1, null));
      } else {
        List<IBusinessObject> businessObjects =
            getElement()
                .getDesign()
                .getDocument()
                .getProject()
                .getBusinessObjectSet()
                .getBusinessObjects();

        for (int i = 0; i < businessObjects.size(); i++) {
          IBusinessObject ibo = businessObjects.get(i);

          if (ibo.getName().equals(settings.targetVariableType)) {
            List<IBusinessObjectField> fields = ibo.getFields();

            for (int f = 0; f < fields.size(); f++) {
              IBusinessObjectField ibof = fields.get(f);
              settings.dataMapping.add(settings.new DataMapping(ibof.getName(), -1, null));
            }
          }
        }
      }
    } else if (currentObject != null) {
      if (currentObject.getType().isObject()) {
        List<ObjectField> fields = currentObject.getFields();

        for (int i = 0; i < fields.size(); i++) {
          ObjectField of = fields.get(i);
          settings.dataMapping.add(settings.new DataMapping(of.getName(), -1, null));
        }
      } else {
        settings.dataMapping.add(settings.new DataMapping(currentObject.getPath(), -1, null));
      }
    }

    mappingTableViewer.refresh();
  }
  public void createControls(Composite parent) {
    Table mappingTable = new Table(parent, SWT.FULL_SELECTION | SWT.BORDER | SWT.SINGLE);
    mappingTable.setHeaderVisible(true);

    TableColumn businessObjectFieldColumn = new TableColumn(mappingTable, SWT.NONE);
    businessObjectFieldColumn.setText("Business Object Field");
    businessObjectFieldColumn.setWidth(150);

    TableColumn valueColumn = new TableColumn(mappingTable, SWT.NONE);
    valueColumn.setText("Value");
    valueColumn.setWidth(200);
    valueCellEditor = new ValueCellEditor(mappingTable);
    mappingTableViewer = new TableViewer(mappingTable);
    mappingTableViewer.setColumnProperties(new String[] {"Field", "Value"});
    mappingTableViewer.setCellModifier(new MappingCellModifier());
    mappingTableViewer.setCellEditors(new CellEditor[] {null, valueCellEditor});
    mappingTableViewer.setContentProvider(new MappingContentProvider());
    mappingTableViewer.setLabelProvider(new MappingLabelProvider());
    mappingTableViewer.setInput(this);

    if ((settings.targetVariableName != null)
        && !settings.targetVariableName.equals("")
        && (settings.targetVariableType != null)
        && !settings.targetVariableType.equals("")) {
      if (settings.targetVariableExists) {
        String tname = settings.targetVariableName;
        String[] parts = tname.split("\\.");
        currentObject = findObjectDefinition(incomingVariables, parts[0]);

        for (int i = 1; i < parts.length; i++) {
          currentObject = findObjectDefinition(currentObject.getFields(), parts[i]);

          if (currentObject == null) {
            break;
          }
        }

        if (currentObject != null) {
          if (currentObject.getType().hasBaseType()) {
            if (!currentObject.getType().isObject()) {
              settings.dataMapping.add(settings.new DataMapping("Value", -1, null));
            } else {
              List<IBusinessObject> businessObjects =
                  getElement()
                      .getDesign()
                      .getDocument()
                      .getProject()
                      .getBusinessObjectSet()
                      .getBusinessObjects();

              for (int i = 0; i < businessObjects.size(); i++) {
                IBusinessObject ibo = businessObjects.get(i);

                if (ibo.getName().equals(settings.targetVariableType)) {
                  List<IBusinessObjectField> fields = ibo.getFields();

                  for (int f = 0; f < fields.size(); f++) {
                    IBusinessObjectField ibof = fields.get(f);
                    settings.dataMapping.add(settings.new DataMapping(ibof.getName(), -1, null));
                  }
                }
              }
            }
          } else {
            if (currentObject.getType().isObject()) {
              List<ObjectField> fields = currentObject.getFields();

              for (int i = 0; i < fields.size(); i++) {
                ObjectField of = fields.get(i);
                settings.dataMapping.add(settings.new DataMapping(of.getName(), -1, null));
              }
            } else {
              settings.dataMapping.add(settings.new DataMapping(currentObject.getPath(), -1, null));
            }
          }
        }
      } else {
        currentObject =
            VariableHelper.constructVariable(
                settings.targetVariableName,
                getElement().getDesign().getDocument().getProject().getBusinessObjectSet(),
                settings.targetVariableType);
      }
    }

    mappingTableViewer.refresh();
  }
  /* (non-Javadoc)
   * @see org.eclipse.vtp.desktop.ui.app.editor.model.DatabaseQuerySettingsListener#targetVariableChanged()
   */
  public void targetVariableChanged() {
    if ((settings.targetVariableName == null)
        || (settings.targetVariableType == null)
        || settings.targetVariableName.equals("")) {
      currentObject = null;
      mappingTableViewer.refresh();

      return;
    }

    if ((currentObject == null)
        || (!settings.targetVariableName.equals(currentObject.getPath())
            || !settings.targetVariableType.equals(currentObject.getType()))) {
      if (settings.targetVariableExists) {
        for (int i = 0; i < incomingVariables.size(); i++) {
          Variable v = incomingVariables.get(i);

          if (v.getName().equals(settings.targetVariableName)) {
            currentObject = v;
          } else if ((settings.targetVariableName != null)
              && settings.targetVariableName.startsWith(v.getName())) {
            List<ObjectField> objectFields = v.getFields();

            for (int f = 0; f < objectFields.size(); f++) {
              ObjectField of = objectFields.get(f);

              if (of.getPath().equals(settings.targetVariableName)) {
                currentObject = of;
              }
            }
          }
        }
      } else {
        currentObject =
            VariableHelper.constructVariable(
                settings.targetVariableName,
                getElement().getDesign().getDocument().getProject().getBusinessObjectSet(),
                settings.targetVariableType);
      }

      settings.dataMapping.clear();

      if (currentObject.getType().hasBaseType()) {
        if (!currentObject.getType().isObject()) {
          settings.dataMapping.add(settings.new DataMapping("Value", -1, null));
        } else {
          List<IBusinessObject> businessObjects =
              getElement()
                  .getDesign()
                  .getDocument()
                  .getProject()
                  .getBusinessObjectSet()
                  .getBusinessObjects();

          for (int i = 0; i < businessObjects.size(); i++) {
            IBusinessObject ibo = businessObjects.get(i);

            if (ibo.getName().equals(settings.targetVariableType)) {
              List<IBusinessObjectField> fields = ibo.getFields();

              for (int f = 0; f < fields.size(); f++) {
                IBusinessObjectField ibof = fields.get(f);
                settings.dataMapping.add(settings.new DataMapping(ibof.getName(), -1, null));
              }
            }
          }
        }
      } else {
        if (currentObject.getType().isObject()) {
          List<ObjectField> fields = currentObject.getFields();

          for (int i = 0; i < fields.size(); i++) {
            ObjectField of = fields.get(i);
            settings.dataMapping.add(settings.new DataMapping(of.getName(), -1, null));
          }
        } else {
          settings.dataMapping.add(settings.new DataMapping(currentObject.getPath(), -1, null));
        }
      }

      mappingTableViewer.refresh();
    }
  }