⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table.java

📁 iText可以制作中文PDF文件的JAVA源程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        // check one column at a time, find maximum needed nr of cols        for (j=0; j < columns; j++) {            lNewMaxColumns = 1; // value to hold in how many columns the current one will be split            for (i=0; i < rows.size(); i++) {                if ( Table.class.isInstance(((Row) rows.get(i)).getCell(j)) ) {                    lDummyTable = ((Table) ((Row) rows.get(i)).getCell(j));                    if ( lDummyTable.getDimension().width > lNewMaxColumns ) {                        lNewMaxColumns = lDummyTable.getDimension().width;                        lDummyColumnWidths[j] = lDummyTable.widths; // bugfix Tony Copping                    }                }            }            lTotalColumns += lNewMaxColumns;            lDummyWidths [j] = lNewMaxColumns;        }        // next we'll add new rows when needed        for (i=0; i < rows.size(); i++) {            lNewMaxRows = 1;    // holds value in how many rows the current one will be split            for (j=0; j < columns; j++) {                if ( Table.class.isInstance(((Row) rows.get(i)).getCell(j)) ) {                    lDummyTable = (Table) ((Row) rows.get(i)).getCell(j);                    if ( lDummyTable.getDimension().height > lNewMaxRows ) {                        lNewMaxRows = lDummyTable.getDimension().height;                    }                }            }            lTotalRows += lNewMaxRows;            lDummyHeights [i] = lNewMaxRows;        }        if ( (lTotalColumns != columns) || (lTotalRows != rows.size()) )    // NO ADJUSTMENT        {            // ** WIDTH            // set correct width for new columns            // divide width over new nr of columns            lNewWidths = new float [lTotalColumns];            int lDummy = 0;            for (int tel=0; tel < widths.length;tel++) {                if ( lDummyWidths[tel] != 1) {                    // divide                    for (int tel2 = 0; tel2 < lDummyWidths[tel]; tel2++) {                        // lNewWidths[lDummy] = widths[tel] / lDummyWidths[tel];                        lNewWidths[lDummy] = widths[tel] * lDummyColumnWidths[tel][tel2] / 100f; // bugfix Tony Copping                        lDummy++;                    }                }                else {                    lNewWidths[lDummy] = widths[tel];                    lDummy++;                }            }            // ** FILL OUR NEW TABLE            // generate new table            // set new widths            // copy old values            newRows = new ArrayList(lTotalRows);            for (i = 0; i < lTotalRows; i++) {                newRows.add(new Row(lTotalColumns));            }            int lDummyRow = 0, lDummyColumn = 0;        // to remember where we are in the new, larger table            Object lDummyElement = null;            for (i=0; i < rows.size(); i++) {                lDummyColumn = 0;                lNewMaxRows = 1;                for (j=0; j < columns; j++) {                    if ( Table.class.isInstance(((Row) rows.get(i)).getCell(j)) )       // copy values from embedded table                    {                        lDummyTable = (Table) ((Row) rows.get(i)).getCell(j);                        for (int k=0; k < lDummyTable.getDimension().height; k++) {                            for (int l=0; l < lDummyTable.getDimension().width; l++) {                                lDummyElement = lDummyTable.getElement(k,l);                                if (lDummyElement != null) {                                    ((Row) newRows.get(k + lDummyRow)).addElement(lDummyElement,l + lDummyColumn);  // use addElement to set reserved status ok in row                                }                            }                        }                    }                    else        // copy others values                    {                        Object aElement = getElement(i,j);                        if ( Cell.class.isInstance(aElement) ) {                            // adjust spans for cell                            ((Cell) aElement).setRowspan(((Cell) ((Row) rows.get(i)).getCell(j)).rowspan() + lDummyHeights[i] - 1);                            ((Cell) aElement).setColspan(((Cell) ((Row) rows.get(i)).getCell(j)).colspan() + lDummyWidths[j] - 1);                            // most likely this cell covers a larger area because of the row/cols splits : define not-to-be-filled cells                            placeCell(newRows,((Cell) aElement), new Point(lDummyRow,lDummyColumn));                        }                    }                    lDummyColumn += lDummyWidths[j];                }                lDummyRow += lDummyHeights[i];            }            // Set our new matrix            columns     = lTotalColumns;            rows = newRows;            this.widths = lNewWidths;        }    }/** * adds new<CODE>Cell</CODE>'s to empty/null spaces. */    private void fillEmptyMatrixCells() {        try {            for (int i=0; i < rows.size(); i++) {                for (int j=0; j < columns; j++) {                    if ( ((Row) rows.get(i)).isReserved(j) == false) {                        addCell(defaultLayout, new Point(i, j));                    }                }            }        }        catch(BadElementException bee) {            throw new ExceptionConverter(bee);        }    }/** * check if <CODE>Cell</CODE> 'fits' the table. * <P> * <UL><LI>rowspan/colspan not beyond borders *     <LI>spanned cell don't overlap existing cells</UL> * * @param   aCell       the cell that has to be checked * @param   aLocation   the location where the cell has to be placed */    private boolean isValidLocation(Cell aCell, Point aLocation)    {        // rowspan not beyond last column        if ( aLocation.x < rows.size() )        // if false : new location is already at new, not-yet-created area so no check        {            if ((aLocation.y + aCell.colspan()) > columns) {                return false;            }            int difx = ((rows.size() - aLocation.x) >  aCell.rowspan()) ? aCell.rowspan() : rows.size() - aLocation.x;            int dify = ((columns - aLocation.y) >  aCell.colspan()) ? aCell.colspan() : columns - aLocation.y;            // no other content at cells targetted by rowspan/colspan            for (int i=aLocation.x; i < (aLocation.x + difx); i++) {                for (int j=aLocation.y; j < (aLocation.y + dify); j++) {                    if ( ((Row) rows.get(i)).isReserved(j) == true ) {                        return false;                    }                }            }        }        else {            if ((aLocation.y + aCell.colspan()) > columns) {                return false;            }        }        return true;    }/** * Inserts a Cell in a cell-array and reserves cells defined by row-/colspan. * * @param   someRows    some rows * @param   aCell       the cell that has to be inserted * @param   aPosition   the position where the cell has to be placed */    private void placeCell(ArrayList someRows, Cell aCell, Point aPosition) {        int i;        Row row = null;        int lColumns = ((Row) someRows.get(0)).columns();        int rowCount = aPosition.x + aCell.rowspan() - someRows.size();        assumeTableDefaults(aCell);        if ( (aPosition.x + aCell.rowspan()) > someRows.size() )        //create new rows ?        {            for (i = 0; i < rowCount; i++) {                row = new Row(lColumns);                someRows.add(row);            }        }        // reserve cell in rows below        for (i = aPosition.x + 1; i < (aPosition.x  + aCell.rowspan()); i++) {            if ( !((Row) someRows.get(i)).reserve(aPosition.y, aCell.colspan())) {                // should be impossible to come here :-)                throw new RuntimeException("addCell - error in reserve");            }        }        row = (Row) someRows.get(aPosition.x);        row.addElement(aCell, aPosition.y);    }/** * Gives you the posibility to add columns. * * @param   aColumns    the number of columns to add */    public void addColumns(int aColumns) {        ArrayList newRows = new ArrayList(rows.size());        int newColumns = columns + aColumns;        Row row;        for (int i = 0; i < rows.size(); i++) {            row = new Row(newColumns);            for (int j = 0; j < columns; j++) {                row.setElement(((Row) rows.get(i)).getCell(j) ,j);            }            for (int j = columns; j < newColumns && i < curPosition.x; j++) {                row.setElement(defaultLayout, j);            }            newRows.add(row);        }        // applied 1 column-fix; last column needs to have a width of 0        float [] newWidths = new float[newColumns];        for (int j = 0; j < columns; j++) {            newWidths[j] = widths[j];        }        for (int j = columns; j < newColumns ; j++) {            newWidths[j] = 0;        }        columns = newColumns;        widths = newWidths;        rows = newRows;    }/** * Gets an array with the positions of the borders between every column. * <P> * This method translates the widths expressed in percentages into the * x-coordinate of the borders of the columns on a real document. * * @param       left            this is the position of the first border at the left (cellpadding not included) * @param       totalWidth      this is the space between the first border at the left *                                              and the last border at the right (cellpadding not included) * @return      an array with borderpositions */    public float[] getWidths(float left, float totalWidth) {        // for x columns, there are x+1 borders        float[] w = new float[columns + 1];        // the border at the left is calculated        switch(alignment) {            case Element.ALIGN_LEFT:                w[0] = left;                break;            case Element.ALIGN_RIGHT:                w[0] = left + (totalWidth * (100 - widthPercentage)) / 100;                break;            case Element.ALIGN_CENTER:                default:                    w[0] = left + (totalWidth * (100 - widthPercentage)) / 200;        }        // the total available width is changed        totalWidth = (totalWidth * widthPercentage) / 100;        // the inner borders are calculated        for (int i = 1; i < columns; i++) {            w[i] = w[i - 1] + (widths[i - 1] * totalWidth / 100);        }        // the border at the right is calculated        w[columns] = w[0] + totalWidth;        return w;    }/** *  Sets current col/row to valid(empty) pos after addCell/Table */    private void setCurrentLocationToNextValidPosition(Point aLocation)    {        // set latest location to next valid position        int i, j;        i = aLocation.x;        j = aLocation.y;        do {            if ( (j + 1)  == columns ) {    // goto next row                i++;                j = 0;            }            else {                j++;            }        }        while (        (i < rows.size()) && (j < columns) && (((Row) rows.get(i)).isReserved(j) == true)        );        curPosition = new Point(i, j);    }/** * Checks if a given tag corresponds with this object. * * @param   tag     the given tag * @return  true if the tag corresponds */    public static boolean isTag(String tag) {        return ElementTags.TABLE.equals(tag);    }/** * Allows clients to set up alternating attributes for each Row in the Table. * <P> * This code was contributed by Matt Benson. * * @param   name    the name of the attribute * @param   value0  the value of the attribute for even rows * @param   value1  the value of the attribute for odd rows */    public void setAlternatingRowAttribute(String name, String value0, String value1) {        if (value0 == null || value1 == null) {            throw new NullPointerException("MarkupTable#setAlternatingRowAttribute(): null values are not permitted.");        }        alternatingRowAttributes = (alternatingRowAttributes == null) ?  new Hashtable() : alternatingRowAttributes;        // we could always use new Arrays but this is big enough        String[] value = (String[])(alternatingRowAttributes.get(name));        value = (value == null) ? new String[2] : value;        value[0] = value0;        value[1] = value1;        alternatingRowAttributes.put(name, value);    }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public float top() {       throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public float bottom() {       throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public float left() {       throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public float right() {       throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public float top(int margin) {       throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public float bottom(int margin) {       throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public float left(int margin) {       throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public float right(int margin) {       throw new UnsupportedOperationException("Dimensions of a Table can't be calculated. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public void setTop(int value) {       throw new UnsupportedOperationException("Dimensions of a Table are attributed automagically. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public void setBottom(int value) {       throw new UnsupportedOperationException("Dimensions of a Table are attributed automagically. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public void setLeft(int value) {       throw new UnsupportedOperationException("Dimensions of a Table are attributed automagically. See the FAQ.");   }/** * This method throws an <CODE>UnsupportedOperationException</CODE>. */   public void setRight(int value) {       throw new UnsupportedOperationException("Dimensions of a Table are attributed automagically. See the FAQ.");   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -