示例#1
0
  /**
   * Retrieve the header for this table. If border for this table is disabled, header will only be a
   * row that contains column's caption. If border is enabled, the header is three lines with CP347
   * pseudo-graphic characters to simulate a border.
   *
   * @return the definition of header.
   */
  public TextLine[] getHeader() {
    if (header == null) {
      List<TextLine> tmp = new ArrayList<>();
      StringBuilder line = new StringBuilder();

      // draw header upper border if necessary
      if (isDrawBorder()) {
        line.append(EscpUtil.CP347_LIGHT_DOWN_RIGHT);
        for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
          TableColumn column = columns.get(columnIndex);
          for (int i = 0; i < column.getWidth() - 1; i++) {
            line.append(EscpUtil.CP347_LIGHT_HORIZONTAL);
          }
          if (columnIndex == (columns.size() - 1)) {
            line.append(EscpUtil.CP347_LIGHT_DOWN_LEFT);
          } else {
            line.append(EscpUtil.CP347_LIGHT_DOWN_HORIZONTAL);
          }
        }
        tmp.add(new TextLine(line.toString()));
      }

      // draw column name
      line = new StringBuilder();
      if (isDrawBorder()) {
        line.append(EscpUtil.CP347_LIGHT_VERTICAL);
      }
      for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
        TableColumn column = columns.get(columnIndex);
        int width = column.getWidth() - (isDrawBorder() ? 1 : 0);
        StringUtil.ALIGNMENT alignment = (new BasicPlaceholder(column.getText())).getAlignment();
        if (alignment == null) {
          alignment = StringUtil.ALIGNMENT.LEFT;
        }
        line.append(StringUtil.align(column.getCaption(), width, alignment));
        if (isDrawBorder()) {
          line.append(EscpUtil.CP347_LIGHT_VERTICAL);
        }
      }
      tmp.add(new TextLine(line.toString()));

      // draw lower border if necessary
      line = new StringBuilder();
      if (isDrawBorder()) {
        line.append(EscpUtil.CP347_LIGHT_VERTICAL_RIGHT);
        for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
          TableColumn column = columns.get(columnIndex);
          for (int i = 0; i < column.getWidth() - 1; i++) {
            line.append(EscpUtil.CP347_LIGHT_HORIZONTAL);
          }
          if (columnIndex == (columns.size() - 1)) {
            line.append(EscpUtil.CP347_LIGHT_VERTICAL_LEFT);
          } else {
            line.append(EscpUtil.CP347_LIGHT_VERTICAL_HORIZONTAL);
          }
        }
        tmp.add(new TextLine(line.toString()));
      }
      header = tmp.toArray(new TextLine[0]);
    }
    return Arrays.copyOf(header, header.length);
  }