/** * */ package visualizationElements; import java.awt.Font; import java.awt.Graphics; /** * Represents a table to visualize. * @author MSchaefer * */ public class Table extends VisualizationElement { protected static final int START_Y_POS = 20; protected static final int START_X_POS = 20; protected static final int CELL_HEIGHT = 20; protected static final int CELL_WIDTH = 80; protected int numberOfRows; protected int numberOfColumns; private int height; private int width; protected Object[][] valueTable; private String[] columnCaptions; /** * Creates a new Table. * @param values The values of the table. * @param columnCaptions The captions of the table columns. * @param width Width of the table. * @param height Height of the table. * */ public Table(Object[][] values, String[] columnCaptions, int width, int height) { super(); if(columnCaptions.length != values[0].length){ throw new IllegalArgumentException("Not all columns have got a caption!"); } this.setValues(values); this.setColumnCaptions(columnCaptions); } /** * Creates a new Table. * @param values The values of the table. * @param width Width of the table. * @param height Height of the table. * */ public Table(Object[][] values, int width, int height) { super(); this.setValues(values); this.setColumnCaptions(null); } /* (non-Javadoc) * @see visualizationElements.VisualizationElement#draw() */ @Override public void draw(Graphics g) { numberOfRows = valueTable.length; numberOfColumns = valueTable[0].length; int xpos = START_X_POS; int ypos = START_Y_POS; for(int j = 0; j < numberOfColumns; j++){ xpos = xpos + CELL_WIDTH; ypos = START_Y_POS; if(columnCaptions != null){ g.setFont(new Font("Arial", Font.BOLD, 13)); g.drawRect(xpos, ypos, CELL_WIDTH, CELL_HEIGHT); g.drawRect(xpos+1, ypos + 1, CELL_WIDTH-2, CELL_HEIGHT-2); g.drawString(columnCaptions[j].toString(), xpos + 5, ypos + 15); g.setFont(new Font("Arial", Font.PLAIN, 13)); } for(int i = 0; i < numberOfRows; i++){ ypos = ypos + CELL_HEIGHT; g.drawRect(xpos, ypos, CELL_WIDTH, CELL_HEIGHT); if(valueTable[i][j] != null){ g.drawString(valueTable[i][j].toString(), xpos + CELL_HEIGHT, ypos + 15); } } } } /** * @param width the width to set */ public void setWidth(int width) { this.width = width; } /** * @return the width */ public int getWidth() { return width; } /** * @param height the height to set */ public void setHeight(int height) { this.height = height; } /** * @return the height */ public int getHeight() { return height; } public void setColumnCaptions(String[] columnCaptions) { this.columnCaptions = columnCaptions; } public String[] getColumnCaptions(){ return this.columnCaptions; } /** * @param values the values to set */ public void setValues(Object[][] values) { this.valueTable = values; } /** * @return the values */ public Object[][] getValues() { return valueTable; } }