Example usage for com.lowagie.text Table addCell

List of usage examples for com.lowagie.text Table addCell

Introduction

In this page you can find the example usage for com.lowagie.text Table addCell.

Prototype


public void addCell(String content) throws BadElementException 

Source Link

Document

Adds a Cell to the Table.

Usage

From source file:org.openswing.swing.export.java.ExportToRTF14.java

License:Open Source License

private void processComponent(Table table, int parentTableCols, Document document, ExportOptions opt,
        Object obj) throws Throwable {
    if (obj != null) {
        GridExportCallbacks callbacks = null;
        if (obj instanceof GridExportOptions) {
            callbacks = (GridExportCallbacks) ((GridExportOptions) obj).getCallbacks();
            if (callbacks != null)
                processComponent(table, parentTableCols, document, opt, callbacks.getHeaderComponent());
            prepareGrid(table, parentTableCols, document, opt, (GridExportOptions) obj);
            if (callbacks != null)
                processComponent(table, parentTableCols, document, opt, callbacks.getFooterComponent());
        } else if (obj instanceof ComponentExportOptions)
            prepareGenericComponent(table, parentTableCols, document, opt, (ComponentExportOptions) obj);
        else/*w  ww  .j  a v a  2  s.com*/
            return;

        if (table != null) {
            Cell cell = new Cell(new Paragraph("\n"));
            //        cell.setColspan(parentTableCols);
            table.addCell(cell);
        } else
            document.add(new Paragraph("\n"));
    }
}

From source file:org.openswing.swing.export.java.ExportToRTF14.java

License:Open Source License

private void prepareGenericComponent(Table parentTable, int parentTableCols, Document document,
        ExportOptions exportOptions, ComponentExportOptions opt) throws Throwable {
    if (opt.getCellsContent() == null || opt.getCellsContent().length == 0)
        return;/*from ww w .jav a  2 s  .  c  o  m*/

    int cols = opt.getCellsContent()[0].length;
    Object[] row = null;
    Object obj = null;
    SimpleDateFormat sdatf = new SimpleDateFormat(exportOptions.getDateTimeFormat());
    int[] headerwidths = new int[cols];
    for (int i = 0; i < headerwidths.length; i++)
        headerwidths[i] = (int) PageSize.A4.width() / cols;

    Table table = new Table(cols);
    table.setWidths(headerwidths);
    table.setBorderWidth(2);
    table.setBorderColor(Color.black);
    table.setGrayFill(exportOptions.getExportToRTFAdapter().getHeaderGrayFill());
    table.setPadding(3);

    for (int i = 0; i < opt.getCellsContent().length; i++) {
        row = opt.getCellsContent()[i];
        for (int j = 0; j < row.length; j++) {
            obj = row[j];

            if (obj != null) {
                if (obj instanceof Date || obj instanceof java.util.Date || obj instanceof java.sql.Timestamp) {
                    table.addCell(new Phrase(sdatf.format((java.util.Date) obj),
                            (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, obj)));
                } else {
                    table.addCell(new Phrase(obj.toString(),
                            (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, obj)));
                }
            } else {
                table.addCell(new Phrase("",
                        (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, null)));
            }

        }
    }

    if (parentTable != null) {
        Cell c = new Cell(table);
        //      c.setColspan(parentTableCols);
        parentTable.addCell(c);
    } else
        document.add(table);

}

From source file:org.openswing.swing.export.java.ExportToRTF14.java

License:Open Source License

private void prepareGrid(Table parentTable, int parentTableCols, Document document, ExportOptions exportOptions,
        GridExportOptions opt) throws Throwable {
    // prepare vo getters methods...
    String methodName = null;/*ww w .  j a  v a2 s .  c  o m*/
    String attributeName = null;
    Hashtable gettersMethods = new Hashtable();
    Method[] voMethods = opt.getValueObjectType().getMethods();
    for (int i = 0; i < voMethods.length; i++) {
        methodName = voMethods[i].getName();
        if (methodName.startsWith("get")) {
            attributeName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
            if (opt.getExportAttrColumns().contains(attributeName))
                gettersMethods.put(attributeName, voMethods[i]);
        }
    }

    Response response = null;
    int start = 0;
    int rownum = 0;
    Object value = null;
    Object vo = null;
    int type;

    SimpleDateFormat sdf = new SimpleDateFormat(exportOptions.getDateFormat());
    SimpleDateFormat sdatf = new SimpleDateFormat(exportOptions.getDateTimeFormat());
    SimpleDateFormat stf = new SimpleDateFormat(exportOptions.getTimeFormat());

    int headerwidths[] = new int[opt.getExportColumns().size()];
    int total = 0;
    for (int i = 0; i < opt.getExportColumns().size(); i++) {
        headerwidths[i] = Math.max(opt.getExportColumns().get(i).toString().length() * 10,
                ((Integer) opt.getColumnsWidth().get(opt.getExportAttrColumns().get(i))).intValue());
        total += headerwidths[i];
    }

    //    Document document = new Document();
    //    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //    RtfWriter2 w = RtfWriter2.getInstance(document,baos);
    //    document.open();

    Paragraph line = null;
    if (opt.getTitle() != null && !opt.getTitle().equals("")) {
        line = new Paragraph(opt.getTitle(), (Font) exportOptions.getExportToRTFAdapter().getFontTitle());
        line.setAlignment(Element.ALIGN_CENTER);
        document.add(line);
        document.add(new Paragraph(""));
    }
    String[] filters = opt.getFilteringConditions();
    if (filters != null) {
        for (int i = 0; i < filters.length; i++) {
            line = new Paragraph(filters[i]);
            document.add(line);
        }
        document.add(new Paragraph(""));
    }

    Table table = new Table(opt.getExportColumns().size());
    table.setWidths(headerwidths);
    table.setBorderWidth(2);
    table.setBorderColor(Color.black);
    table.setGrayFill(exportOptions.getExportToRTFAdapter().getHeaderGrayFill());
    table.setPadding(3);

    Phrase cell = null;
    for (int i = 0; i < opt.getExportColumns().size(); i++) {
        cell = new Phrase(opt.getExportColumns().get(i).toString(), (Font) exportOptions.getExportToRTFAdapter()
                .getHeaderFont(opt.getExportAttrColumns().get(i).toString()));
        table.addCell(cell);
    }
    //    table.setHeaderRows(1);
    //    table.getDefaultCell().setBorderWidth(1);
    //    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
    //    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP);

    for (int j = 0; j < opt.getTopRows().size(); j++) {
        // create a row for each top rows...
        vo = opt.getTopRows().get(j);
        appendRow(document, exportOptions, table, vo, opt, gettersMethods, sdf, sdatf, stf, j, 0);
    }

    do {
        response = opt.getGridDataLocator().loadData(GridParams.NEXT_BLOCK_ACTION, start,
                opt.getFilteredColumns(), opt.getCurrentSortedColumns(), opt.getCurrentSortedVersusColumns(),
                opt.getValueObjectType(), opt.getOtherGridParams());
        if (response.isError())
            throw new Exception(response.getErrorMessage());

        boolean even = false;

        for (int j = 0; j < ((VOListResponse) response).getRows().size(); j++) {
            if (even) {
                even = false;
            } else {
                even = true;
            }

            vo = ((VOListResponse) response).getRows().get(j);

            appendRow(document, exportOptions, table, vo, opt, gettersMethods, sdf, sdatf, stf, rownum, 1);

            rownum++;
        }

        start = start + ((VOListResponse) response).getRows().size();

        if (!((VOListResponse) response).isMoreRows())
            break;
    } while (rownum < opt.getMaxRows());

    for (int j = 0; j < opt.getBottomRows().size(); j++) {
        // create a row for each bottom rows...
        table.setGrayFill(exportOptions.getExportToRTFAdapter().getBottomRowsGrayFill(j));
        vo = opt.getBottomRows().get(j);
        appendRow(document, exportOptions, table, vo, opt, gettersMethods, sdf, sdatf, stf, j, 2);
    }

    //    rtfTable.importTable(table,total);
    if (parentTable != null) {
        Cell c = new Cell(table);
        //      c.setColspan(parentTableCols);
        parentTable.addCell(c);
        //      table.setCellsFitPage(true);
        //      parentTable.insertTable(table,new Point(parentTable.getNextRow()+1,0));

    } else
        document.add(table);

    //    document.add(table);
}

From source file:org.openswing.swing.export.java.ExportToRTF14.java

License:Open Source License

/**
 * Append current row to result.//from   w ww  .j a  va2s. c o  m
 * @return current row to append
 */
private void appendRow(Document document, ExportOptions exportOptions, Table table, Object vo,
        GridExportOptions opt, Hashtable gettersMethods, SimpleDateFormat sdf, SimpleDateFormat sdatf,
        SimpleDateFormat stf, int row, int tableType // 0 = header, 1 = body, 2 = footer
) throws Throwable {
    int type;
    String aName = null;
    Method getter = null;
    Class clazz = null;
    Object obj = null;

    for (int i = 0; i < opt.getExportColumns().size(); i++) {
        //      value = ((Method)gettersMethods.get(opt.getExportAttrColumns().get(i))).invoke(vo,new Object[0]);

        clazz = vo.getClass();
        obj = vo;
        aName = opt.getExportAttrColumns().get(i).toString();

        // check if the specified attribute is a composed attribute and there exist inner v.o. to instantiate...
        while (aName.indexOf(".") != -1) {
            try {
                getter = clazz.getMethod(
                        "get" + aName.substring(0, 1).toUpperCase() + aName.substring(1, aName.indexOf(".")),
                        new Class[0]);
            } catch (NoSuchMethodException ex2) {
                getter = clazz.getMethod(
                        "is" + aName.substring(0, 1).toUpperCase() + aName.substring(1, aName.indexOf(".")),
                        new Class[0]);
            }
            aName = aName.substring(aName.indexOf(".") + 1);
            clazz = getter.getReturnType();
            obj = getter.invoke(obj, new Object[0]);
            if (obj == null)
                break;
        }

        try {
            getter = clazz.getMethod("get" + aName.substring(0, 1).toUpperCase() + aName.substring(1),
                    new Class[0]);
        } catch (NoSuchMethodException ex2) {
            getter = clazz.getMethod("is" + aName.substring(0, 1).toUpperCase() + aName.substring(1),
                    new Class[0]);
        }

        if (obj != null)
            obj = getter.invoke(obj, new Object[0]);

        if (obj != null) {
            if (obj instanceof Date || obj instanceof java.util.Date || obj instanceof java.sql.Timestamp) {
                type = ((Integer) opt.getColumnsType().get(opt.getExportAttrColumns().get(i))).intValue();
                if (type == opt.TYPE_DATE) {
                    //                table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(new Phrase(sdf.format((java.util.Date) obj),
                            (Font) exportOptions.getExportToRTFAdapter().getRowFont(aName)));
                } else if (type == opt.TYPE_DATE_TIME) {
                    //                table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(new Phrase(sdatf.format((java.util.Date) obj),
                            (Font) exportOptions.getExportToRTFAdapter().getRowFont(aName)));
                } else if (type == opt.TYPE_TIME) {
                    //                table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(new Phrase(stf.format((java.util.Date) obj),
                            (Font) exportOptions.getExportToRTFAdapter().getRowFont(aName)));
                }
            } else {
                //              table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                table.addCell(new Phrase(obj.toString(),
                        (Font) exportOptions.getExportToRTFAdapter().getRowFont(aName)));
            }
        } else {
            table.addCell(new Phrase("", (Font) exportOptions.getExportToRTFAdapter().getRowFont(aName)));
        }
    }

    if (opt.getCallbacks() != null) {
        if (tableType == 0)
            processComponent(table, opt.getExportColumns().size(), document, exportOptions,
                    opt.getCallbacks().getComponentPerRowInHeader((ValueObject) vo, row));
        else if (tableType == 1)
            processComponent(table, opt.getExportColumns().size(), document, exportOptions,
                    opt.getCallbacks().getComponentPerRow((ValueObject) vo, row));
        else if (tableType == 2)
            processComponent(table, opt.getExportColumns().size(), document, exportOptions,
                    opt.getCallbacks().getComponentPerRowInFooter((ValueObject) vo, row));
    }
}

From source file:org.openswing.swing.export.java.ExportToRTF15.java

License:Open Source License

private void prepareGenericComponent(Table parentTable, int parentTableCols, Document document,
        ExportOptions exportOptions, ComponentExportOptions opt) throws Throwable {
    if (opt.getCellsContent() == null || opt.getCellsContent().length == 0)
        return;/*w  ww .  j  a v  a  2 s .  c om*/

    int cols = opt.getCellsContent()[0].length;
    Object[] row = null;
    Object obj = null;
    SimpleDateFormat sdatf = new SimpleDateFormat(exportOptions.getDateTimeFormat());
    int[] headerwidths = new int[cols];
    for (int i = 0; i < headerwidths.length; i++)
        headerwidths[i] = (int) PageSize.A4.getWidth() / cols;

    Table table = new Table(cols);
    table.setWidths(headerwidths);
    table.setBorderWidth(2);
    table.setBorderColor(Color.black);
    table.setGrayFill(exportOptions.getExportToRTFAdapter().getHeaderGrayFill());
    table.setPadding(3);

    for (int i = 0; i < opt.getCellsContent().length; i++) {
        row = opt.getCellsContent()[i];
        for (int j = 0; j < row.length; j++) {
            obj = row[j];

            if (obj != null) {
                if (obj instanceof Date || obj instanceof java.util.Date || obj instanceof java.sql.Timestamp) {
                    table.addCell(new Phrase(sdatf.format((java.util.Date) obj), (Font) (Font) exportOptions
                            .getExportToRTFAdapter().getGenericComponentFont(i, j, obj)));
                } else {
                    table.addCell(new Phrase(obj.toString(),
                            (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, obj)));
                }
            } else {
                table.addCell(new Phrase("",
                        (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, null)));
            }

        }
    }

    if (parentTable != null) {
        Cell c = new Cell(table);
        //      c.setColspan(parentTableCols);
        parentTable.addCell(c);
    } else
        document.add(table);

}

From source file:org.sigmah.server.report.renderer.itext.ItextPivotTableRenderer.java

License:Open Source License

public void render(DocWriter writer, Document document, PivotTableElement element) {
    try {/*from   w  w w  .j  a  v a 2s  . c  o m*/

        document.add(ThemeHelper.elementTitle(element.getTitle()));
        ItextRendererHelper.addFilterDescription(document, element.getContent().getFilterDescriptions());

        PivotTableData data = element.getContent().getData();

        if (data.isEmpty()) {
            document.add(new Paragraph("Aucune Donnes")); // TODO: i18n

        } else {

            int colDepth = data.getRootColumn().getDepth();
            List<PivotTableData.Axis> colLeaves = data.getRootColumn().getLeaves();
            int colBreadth = colLeaves.size();

            Table table = new Table(colBreadth + 1, 1);
            table.setUseVariableBorders(true);
            table.setWidth(100.0f);
            table.setWidths(calcColumnWidths(document, data, colLeaves));
            table.setBorderWidth(0);

            // first write the column headers

            for (int depth = 1; depth <= colDepth; ++depth) {

                if (depth == 1) {
                    Cell cell = ThemeHelper.cornerCell();
                    cell.setRowspan(colDepth);
                    table.addCell(cell);
                }

                List<PivotTableData.Axis> columns = data.getRootColumn().getDescendantsAtDepth(depth);
                for (PivotTableData.Axis column : columns) {
                    Cell cell = ThemeHelper.columnHeaderCell(column.getLabel(), column.isLeaf());
                    cell.setColspan(Math.max(1, column.getChildCount()));
                    table.addCell(cell);
                }

            }
            table.endHeaders();

            for (PivotTableData.Axis row : data.getRootRow().getChildren()) {
                writeRow(table, row, colLeaves, 0);
            }

            document.add(table);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.sigmah.server.report.renderer.itext.ItextPivotTableRenderer.java

License:Open Source License

protected void writeRow(Table table, PivotTableData.Axis row, List<PivotTableData.Axis> leafColumns, int depth)
        throws BadElementException {

    table.addCell(ThemeHelper.bodyCell(row.getLabel(), true, depth, row.isLeaf()));

    NumberFormat format = NumberFormat.getIntegerInstance();
    format.setGroupingUsed(true);/* w  w w.j a v  a 2  s  . c  o  m*/

    for (PivotTableData.Axis column : leafColumns) {

        PivotTableData.Cell pivotCell = row.getCell(column);
        String label;
        if (pivotCell == null) {
            label = null;
        } else {
            label = format.format(pivotCell.getValue());
        }
        table.addCell(ThemeHelper.bodyCell(label, false, depth, row.isLeaf()));
    }

    for (PivotTableData.Axis childRow : row.getChildren()) {
        writeRow(table, childRow, leafColumns, depth + 1);
    }
}

From source file:org.sigmah.server.report.renderer.itext.ItextTableRenderer.java

License:Open Source License

private void renderTable(Document document, TableData data) throws DocumentException {
    int colDepth = data.getRootColumn().getDepth();
    List<TableColumn> colLeaves = data.getRootColumn().getLeaves();
    int colBreadth = colLeaves.size();

    Table table = new Table(colBreadth, 1);
    table.setUseVariableBorders(true);/*w ww  .j  a  v a 2s  .c  o  m*/
    table.setWidth(100.0f);
    table.setBorderWidth(0);

    // first write the column headers

    for (int depth = 1; depth <= colDepth; ++depth) {
        List<TableColumn> columns = data.getRootColumn().getDescendantsAtDepth(depth);
        for (TableColumn column : columns) {
            Cell cell = ThemeHelper.columnHeaderCell(column.getLabel(), column.isLeaf(), computeHAlign(column));
            cell.setColspan(Math.max(1, column.getChildren().size()));
            cell.setRowspan(colDepth - depth - column.getDepth() + 1);
            table.addCell(cell);
        }
    }
    table.endHeaders();

    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
    NumberFormat numberFormat = NumberFormat.getIntegerInstance();
    numberFormat.setGroupingUsed(true);

    for (TableData.Row row : data.getRows()) {
        for (TableColumn column : colLeaves) {

            Object value = row.values[data.getColumnIndex(column)];

            String label = "";
            if (value instanceof Date) {
                label = dateFormat.format(value);
            } else if (value instanceof Number) {
                label = numberFormat.format(value);
            } else if (value != null) {
                label = value.toString();
            }

            table.addCell(ThemeHelper.bodyCell(label, false, 0, true, computeHAlign(column)));
        }
    }
    document.add(table);
}

From source file:sg.edu.nus.util.ReportWriter.java

private static void createTable(Section subCatPart) throws BadElementException {
    Table t = new Table(3, 2);

    t.setBorderColor(Color.GRAY);
    t.setPadding(4);//  ww w .  j a va  2 s  . c  o  m
    t.setSpacing(4);
    t.setBorderWidth(1);

    Cell c1 = new Cell("Table Header 1");
    c1.setHeader(true);
    t.addCell(c1);
    c1 = new Cell("Table Header 2");
    t.addCell(c1);
    c1 = new Cell("Table Header 3");
    t.addCell(c1);
    t.endHeaders();

    t.addCell("1.0");
    t.addCell("1.1");
    t.addCell("1.2");
    t.addCell("2.1");
    t.addCell("2.2");
    t.addCell("2.3");

    subCatPart.add(t);

}