Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ====================================================================
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at
  
         http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
 
 package org.apache.poi.hslf.model;
 
 
 import java.util.*;
 import java.util.List;
 import java.awt.*;

Represents a table in a PowerPoint presentation

Author(s):
Yegor Kozlov
 
 public final class Table extends ShapeGroup {
 
     protected static final int BORDER_TOP = 1;
     protected static final int BORDER_RIGHT = 2;
     protected static final int BORDER_BOTTOM = 3;
     protected static final int BORDER_LEFT = 4;
 
     protected static final int BORDERS_ALL = 5;
     protected static final int BORDERS_OUTSIDE = 6;
     protected static final int BORDERS_INSIDE = 7;
     protected static final int BORDERS_NONE = 8;
 
 
     protected TableCell[][] cells;

    
Create a new Table of the given number of rows and columns

Parameters:
numrows the number of rows
numcols the number of columns
 
     public Table(int numrowsint numcols) {
         super();
 
         if(numrows < 1) throw new IllegalArgumentException("The number of rows must be greater than 1");
         if(numcols < 1) throw new IllegalArgumentException("The number of columns must be greater than 1");
 
         int x=0, y=0, tblWidth=0, tblHeight=0;
          = new TableCell[numrows][numcols];
         for (int i = 0; i < .i++) {
             x = 0;
             for (int j = 0; j < [i].lengthj++) {
                 [i][j] = new TableCell(this);
                 Rectangle anchor = new Rectangle(xy..);
                 [i][j].setAnchor(anchor);
                 x += .;
             }
             y += .;
         }
         tblWidth = x;
         tblHeight = y;
         setAnchor(new Rectangle(0, 0, tblWidthtblHeight));
 
         EscherOptRecord opt = new EscherOptRecord();
         opt.setRecordId((short)0xF122);
         opt.addEscherProperty(new EscherSimpleProperty((short)0x39F, 1));
         EscherArrayProperty p = new EscherArrayProperty((short)0x43A0, falsenull);
         p.setSizeOfElements(0x0004);
         p.setNumberOfElementsInArray(numrows);
         p.setNumberOfElementsInMemory(numrows);
         opt.addEscherProperty(p);
         List<EscherRecordlst = spCont.getChildRecords();
         lst.add(lst.size()-1, opt);
         spCont.setChildRecords(lst);
     }

    
Create a Table object and initilize it from the supplied Record container.

Parameters:
escherRecord EscherSpContainer container which holds information about this shape
parent the parent of the shape
 
     public Table(EscherContainerRecord escherRecordShape parent) {
         super(escherRecordparent);
     }

    
Gets a cell

Parameters:
row the row index (0-based)
col the column index (0-based)
Returns:
the cell
    public TableCell getCell(int rowint col) {
        return [row][col];
    }
    public int getNumberOfColumns() {
        return [0].length;
    }
    public int getNumberOfRows() {
        return .;
    }
    protected void afterInsert(Sheet sh){
        super.afterInsert(sh);
        List<EscherRecordlst = spCont.getChildRecords();
        EscherOptRecord opt = (EscherOptRecord)lst.get(lst.size()-2);
        for (int i = 0; i < .i++) {
            TableCell cell = [i][0];
            int rowHeight = cell.getAnchor().*/;
            byte[] val = new byte[4];
            LittleEndian.putInt(valrowHeight);
            p.setElement(ival);
            for (int j = 0; j < [i].lengthj++) {
                TableCell c = [i][j];
                addShape(c);
                Line bt = c.getBorderTop();
                if(bt != nulladdShape(bt);
                Line br = c.getBorderRight();
                if(br != nulladdShape(br);
                Line bb = c.getBorderBottom();
                if(bb != nulladdShape(bb);
                Line bl = c.getBorderLeft();
                if(bl != nulladdShape(bl);
            }
        }
    }
    protected void initTable(){
        Shape[] sh = getShapes();
        Arrays.sort(shnew Comparator(){
            public int compareObject o1Object o2 ) {
                Rectangle anchor1 = ((Shape)o1).getAnchor();
                Rectangle anchor2 = ((Shape)o2).getAnchor();
                int delta = anchor1.y - anchor2.y;
                if(delta == 0) delta = anchor1.x - anchor2.x;
                return delta;
            }
        });
        int y0 = -1;
        int maxrowlen = 0;
        ArrayList lst = new ArrayList();
        ArrayList row = null;
        for (int i = 0; i < sh.lengthi++) {
            if(sh[iinstanceof TextShape){
                Rectangle anchor = sh[i].getAnchor();
                if(anchor.y != y0){
                    y0 = anchor.y;
                    row = new ArrayList();
                    lst.add(row);
                }
                row.add(sh[i]);
                maxrowlen = Math.max(maxrowlenrow.size());
            }
        }
         = new TableCell[lst.size()][maxrowlen];
        for (int i = 0; i < lst.size(); i++) {
            row = (ArrayList)lst.get(i);
            for (int j = 0; j < row.size(); j++) {
                TextShape tx = (TextShape)row.get(j);
                [i][j] = new TableCell(tx.getSpContainer(), getParent());
                [i][j].setSheet(tx.getSheet());
            }
        }
    }

    
Assign the SlideShow this shape belongs to

Parameters:
sheet owner of this shape
    public void setSheet(Sheet sheet){
        super.setSheet(sheet);
        if( == nullinitTable();
    }

    
Sets the row height.

Parameters:
row the row index (0-based)
height the height to set (in pixels)
    public void setRowHeight(int rowint height){
        int currentHeight = [row][0].getAnchor().;
        int dy = height - currentHeight;
        for (int i = rowi < .i++) {
            for (int j = 0; j < [i].lengthj++) {
                Rectangle anchor = [i][j].getAnchor();
                if(i == rowanchor.height = height;
                else anchor.y += dy;
                [i][j].setAnchor(anchor);
            }
        }
        Rectangle tblanchor = getAnchor();
        tblanchor.height += dy;
        setAnchor(tblanchor);
    }

    
Sets the column width.

Parameters:
col the column index (0-based)
width the width to set (in pixels)
    public void setColumnWidth(int colint width){
        int currentWidth = [0][col].getAnchor().;
        int dx = width - currentWidth;
        for (int i = 0; i < .i++) {
            Rectangle anchor = [i][col].getAnchor();
            anchor.width = width;
            [i][col].setAnchor(anchor);
            if(col < [i].length - 1) for (int j = col+1; j < [i].lengthj++) {
                anchor = [i][j].getAnchor();
                anchor.x += dx;
                [i][j].setAnchor(anchor);
            }
        }
        Rectangle tblanchor = getAnchor();
        tblanchor.width += dx;
        setAnchor(tblanchor);
    }

    
Format the table and apply the specified Line to all cell boundaries, both outside and inside

Parameters:
line the border line
    public void setAllBorders(Line line){
        for (int i = 0; i < .i++) {
            for (int j = 0; j < [i].lengthj++) {
                TableCell cell = [i][j];
                cell.setBorderTop(cloneBorder(line));
                cell.setBorderLeft(cloneBorder(line));
                if(j == [i].length - 1) cell.setBorderRight(cloneBorder(line));
                if(i == . - 1) cell.setBorderBottom(cloneBorder(line));
            }
        }
    }

    
Format the outside border using the specified Line object

Parameters:
line the border line
    public void setOutsideBorders(Line line){
        for (int i = 0; i < .i++) {
            for (int j = 0; j < [i].lengthj++) {
                TableCell cell = [i][j];
                if(j == 0) cell.setBorderLeft(cloneBorder(line));
                if(j == [i].length - 1) cell.setBorderRight(cloneBorder(line));
                else {
                    cell.setBorderLeft(null);
                    cell.setBorderLeft(null);
                }
                if(i == 0) cell.setBorderTop(cloneBorder(line));
                else if(i == . - 1) cell.setBorderBottom(cloneBorder(line));
                else {
                    cell.setBorderTop(null);
                    cell.setBorderBottom(null);
                }
            }
        }
    }

    
Format the inside border using the specified Line object

Parameters:
line the border line
    public void setInsideBorders(Line line){
        for (int i = 0; i < .i++) {
            for (int j = 0; j < [i].lengthj++) {
                TableCell cell = [i][j];
                if(j != [i].length - 1)
                    cell.setBorderRight(cloneBorder(line));
                else {
                    cell.setBorderLeft(null);
                    cell.setBorderLeft(null);
                }
                if(i != . - 1) cell.setBorderBottom(cloneBorder(line));
                else {
                    cell.setBorderTop(null);
                    cell.setBorderBottom(null);
                }
            }
        }
    }
    private Line cloneBorder(Line line){
        Line border = createBorder();
        border.setLineWidth(line.getLineWidth());
        border.setLineStyle(line.getLineStyle());
        border.setLineDashing(line.getLineDashing());
        border.setLineColor(line.getLineColor());
        return border;
    }

    
Create a border to format this table

Returns:
the created border
    public Line createBorder(){
        Line line = new Line(this);
        setEscherProperty(opt., 0x80000);
        return line;
    }
New to GrepCode? Check out our FAQ X