/** {@inheritDoc} */
  @Override
  protected boolean handleCreateConnection() {
    CreateRelatedTableCommand command = (CreateRelatedTableCommand) this.getCommand();

    if (command != null) {
      ERTable target = (ERTable) command.getTargetModel();

      if (!target.isReferable()) {
        Activator.showErrorDialog("error.no.referenceable.column");

        this.eraseSourceFeedback();

        return false;
      }
    }

    return super.handleCreateConnection();
  }
  /** {@inheritDoc} */
  @Override
  public boolean validate(ERTable table, NormalColumn column) {
    if (column.getType() == null
        || column.getType().getAlias(table.getDiagram().getDatabase()) == null) {
      ValidateResult validateResult = new ValidateResult();
      validateResult.setMessage(
          ResourceString.getResourceString("error.validate.no.column.type1")
              + table.getPhysicalName()
              + ResourceString.getResourceString("error.validate.no.column.type2")
              + column.getPhysicalName());
      validateResult.setLocation(table.getLogicalName());
      validateResult.setSeverity(IMarker.SEVERITY_WARNING);
      validateResult.setObject(table);

      this.addError(validateResult);
    }

    return true;
  }
  @Override
  public void initComposite() {
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 1;
    this.setLayout(gridLayout);

    this.descriptionText =
        CompositeFactory.createTextArea(null, this, "label.table.description", -1, 400, 1, true);

    this.descriptionText.setText(Format.null2blank(copyData.getDescription()));
  }
  /** {@inheritDoc} */
  @Override
  public void generate(
      IProgressMonitor monitor,
      HSSFWorkbook workbook,
      int sheetNo,
      boolean useLogicalNameAsSheetName,
      Map<String, Integer> sheetNameMap,
      Map<String, ObjectModel> sheetObjectMap,
      ERDiagram diagram,
      Map<String, LoopDefinition> loopDefinitionMap) {
    this.clear();

    List<ERTable> nodeSet = null;

    if (diagram.getCurrentCategory() != null) {
      nodeSet = diagram.getCurrentCategory().getTableContents();
    } else {
      nodeSet = diagram.getDiagramContents().getContents().getTableSet().getList();
    }

    for (ERTable table : nodeSet) {
      String name = null;
      if (useLogicalNameAsSheetName) {
        name = table.getLogicalName();
      } else {
        name = table.getPhysicalName();
      }

      HSSFSheet newSheet = createNewSheet(workbook, sheetNo, name, sheetNameMap);

      sheetObjectMap.put(workbook.getSheetName(workbook.getSheetIndex(newSheet)), table);

      this.setTableData(workbook, newSheet, table);

      monitor.worked(1);
    }
  }
  /** {@inheritDoc} */
  @Override
  protected boolean handleCreateConnection() {
    CreateRelationCommand command = (CreateRelationCommand) this.getCommand();

    if (command == null) {
      return false;
    }

    TableView source = (TableView) command.getSourceModel();
    TableView target = (TableView) command.getTargetModel();

    if (ERTable.isRecursive(source, target)) {
      Activator.showErrorDialog("error.recursive.relation");

      this.eraseSourceFeedback();

      return false;
    }

    return super.handleCreateConnection();
  }
  /** {@inheritDoc} */
  @Override
  public boolean validate(ERTable table) {
    for (Column column : table.getColumns()) {
      if (column instanceof NormalColumn) {
        NormalColumn normalColumn = (NormalColumn) column;

        if (!this.validate(table, normalColumn)) {
          return false;
        }

      } else {
        ColumnGroup columnGroup = (ColumnGroup) column;

        for (NormalColumn normalColumn : columnGroup.getColumns()) {
          if (!this.validate(table, normalColumn)) {
            return false;
          }
        }
      }
    }

    return true;
  }
  private void setComplexUniqueKeyMatrix(
      HSSFWorkbook workbook,
      HSSFSheet sheet,
      ERTable table,
      CellLocation cellLocation,
      MatrixCellStyle matrixCellStyle,
      boolean isLogical) {

    int rowNum = cellLocation.r;
    int columnNum = cellLocation.c;

    HSSFRow headerTemplateRow = sheet.getRow(rowNum);
    HSSFCell headerTemplateCell = headerTemplateRow.getCell(columnNum);

    int num = table.getComplexUniqueKeyList().size();

    if (num == 0) {
      headerTemplateRow.removeCell(headerTemplateCell);

      HSSFRow row = sheet.getRow(rowNum - 1);
      if (row != null) {
        HSSFCell cell = row.getCell(columnNum);
        if (cell != null) {
          cell.getCellStyle().setBorderBottom(headerTemplateCell.getCellStyle().getBorderBottom());
        }
      }
      return;
    }

    HSSFRow headerRow = sheet.createRow(rowNum++);

    for (int i = 0; i < num + 1; i++) {
      HSSFCell cell = headerRow.createCell(columnNum + i);

      if (i == 0) {
        cell.setCellStyle(matrixCellStyle.style11);

      } else {
        ComplexUniqueKey complexUniqueKey = table.getComplexUniqueKeyList().get(i - 1);
        HSSFRichTextString text =
            new HSSFRichTextString(Format.null2blank(complexUniqueKey.getUniqueKeyName()));
        cell.setCellValue(text);

        if (i != num) {
          cell.setCellStyle(matrixCellStyle.style12);
        } else {
          cell.setCellStyle(matrixCellStyle.style13);
        }
      }
    }

    int columnSize = table.getExpandedColumns().size();
    for (int j = 0; j < columnSize; j++) {
      NormalColumn normalColumn = table.getExpandedColumns().get(j);

      HSSFRow row = POIUtils.insertRow(sheet, rowNum++);

      for (int i = 0; i < num + 1; i++) {
        HSSFCell cell = row.createCell(columnNum + i);

        if (i == 0) {
          String columnName = null;
          if (isLogical) {
            columnName = normalColumn.getLogicalName();
          } else {
            columnName = normalColumn.getPhysicalName();
          }

          HSSFRichTextString text = new HSSFRichTextString(columnName);
          cell.setCellValue(text);
          cell.setCellStyle(headerTemplateCell.getCellStyle());

          if (j != columnSize - 1) {
            cell.setCellStyle(matrixCellStyle.style21);
          } else {
            cell.setCellStyle(matrixCellStyle.style31);
          }

        } else {
          ComplexUniqueKey complexUniqueKey = table.getComplexUniqueKeyList().get(i - 1);
          List<NormalColumn> targetColumnList = complexUniqueKey.getColumnList();

          int indexNo = targetColumnList.indexOf(normalColumn);
          if (indexNo != -1) {
            cell.setCellValue(indexNo + 1);
          }

          if (i != num) {
            if (j != columnSize - 1) {
              cell.setCellStyle(matrixCellStyle.style22);
            } else {
              cell.setCellStyle(matrixCellStyle.style32);
            }

          } else {
            if (j != columnSize - 1) {
              cell.setCellStyle(matrixCellStyle.style23);
            } else {
              cell.setCellStyle(matrixCellStyle.style33);
            }
          }
        }
      }
    }
  }
  public void setTableData(HSSFWorkbook workbook, HSSFSheet sheet, ERTable table) {
    POIUtils.replace(
        sheet,
        KEYWORD_LOGICAL_TABLE_NAME,
        this.getValue(this.keywordsValueMap, KEYWORD_LOGICAL_TABLE_NAME, table.getLogicalName()));

    POIUtils.replace(
        sheet,
        KEYWORD_PHYSICAL_TABLE_NAME,
        this.getValue(this.keywordsValueMap, KEYWORD_PHYSICAL_TABLE_NAME, table.getPhysicalName()));

    POIUtils.replace(
        sheet,
        KEYWORD_TABLE_DESCRIPTION,
        this.getValue(this.keywordsValueMap, KEYWORD_TABLE_DESCRIPTION, table.getDescription()));

    POIUtils.replace(
        sheet,
        KEYWORD_TABLE_CONSTRAINT,
        this.getValue(this.keywordsValueMap, KEYWORD_TABLE_CONSTRAINT, table.getConstraint()));

    CellLocation cellLocation = POIUtils.findCell(sheet, FIND_KEYWORDS_OF_COLUMN);

    if (cellLocation != null) {
      int rowNum = cellLocation.r;
      HSSFRow templateRow = sheet.getRow(rowNum);

      if (this.columnTemplate == null) {
        this.columnTemplate = this.loadColumnTemplate(workbook, sheet, cellLocation);
      }

      int order = 1;

      for (NormalColumn normalColumn : table.getExpandedColumns()) {
        HSSFRow row = POIUtils.insertRow(sheet, rowNum++);
        this.setColumnData(this.keywordsValueMap, columnTemplate, row, normalColumn, table, order);
        order++;
      }

      this.setCellStyle(
          columnTemplate,
          sheet,
          cellLocation.r,
          rowNum - cellLocation.r,
          templateRow.getFirstCellNum());
    }

    CellLocation fkCellLocation = POIUtils.findCell(sheet, FIND_KEYWORDS_OF_FK_COLUMN);

    if (fkCellLocation != null) {
      int rowNum = fkCellLocation.r;
      HSSFRow templateRow = sheet.getRow(rowNum);

      if (this.fkColumnTemplate == null) {
        this.fkColumnTemplate = this.loadColumnTemplate(workbook, sheet, fkCellLocation);
      }

      int order = 1;

      for (NormalColumn normalColumn : table.getExpandedColumns()) {
        if (normalColumn.isForeignKey()) {
          HSSFRow row = POIUtils.insertRow(sheet, rowNum++);
          this.setColumnData(
              this.keywordsValueMap, this.fkColumnTemplate, row, normalColumn, table, order);
          order++;
        }
      }

      this.setCellStyle(
          this.fkColumnTemplate,
          sheet,
          fkCellLocation.r,
          rowNum - fkCellLocation.r,
          templateRow.getFirstCellNum());
    }

    this.setIndexMatrix(workbook, sheet, table);
    this.setComplexUniqueKeyMatrix(workbook, sheet, table);
  }