1 14 package org.compiere.print.layout; 15 16 import java.awt.*; 17 import java.awt.geom.*; 18 import java.awt.font.*; 19 import java.text.*; 20 import java.util.*; 21 22 29 public class GridElement extends PrintElement 30 { 31 37 public GridElement(int rows, int cols) 38 { 39 m_rows = rows; 40 m_cols = cols; 41 m_data = new TextLayout[rows][cols]; 42 m_rowHeight = new int[rows]; 43 m_colWidth = new int[cols]; 44 for (int r = 0; r < m_rows; r++) 46 { 47 m_rowHeight[r] = 0; 48 for (int c = 0; c < m_cols; c++) 49 m_data[r][c] = null; 50 } 51 for (int c = 0; c < m_cols; c++) 52 m_colWidth[c] = 0; 53 } 55 56 private int m_rowGap = 3; 57 58 private int m_colGap = 5; 59 60 61 private int m_rows; 62 63 private int m_cols; 64 65 private TextLayout[][] m_data = null; 66 67 private int[] m_rowHeight = null; 68 69 private int[] m_colWidth = null; 70 71 private FontRenderContext m_frc = new FontRenderContext(null, true, true); 72 73 81 public void setData (int row, int col, String data, Font font, Paint foreground) 82 { 83 if (data == null || data.length() == 0) 84 return; 85 Map map = new HashMap(); 87 map.put(TextAttribute.FONT, font); 88 map.put(TextAttribute.FOREGROUND, foreground); 89 TextLayout layout = new TextLayout(data, map, m_frc); 90 setData (row, col, layout); 91 } 93 99 public void setData (int row, int col, TextLayout layout) 100 { 101 if (layout == null) 102 return; 103 if (p_sizeCalculated) 104 throw new IllegalStateException ("Size already calculated"); 105 if (row < 0 || row >= m_rows) 106 throw new ArrayIndexOutOfBoundsException ("Row Index=" + row + " Rows=" + m_rows); 107 if (col < 0 || col >= m_cols) 108 throw new ArrayIndexOutOfBoundsException ("Column Index=" + col + " Cols=" + m_cols); 109 m_data[row][col] = layout; 111 int height = (int)(layout.getAscent() + layout.getDescent() + layout.getLeading())+1; 113 int width = (int)layout.getAdvance()+1; 114 if (m_rowHeight[row] < height) 115 m_rowHeight[row] = height; 116 if (m_colWidth[col] < width) 117 m_colWidth[col] = width; 118 } 120 125 public void setGap (int rowGap, int colGap) 126 { 127 m_rowGap = rowGap; 128 m_colGap = colGap; 129 } 131 132 133 138 protected boolean calculateSize() 139 { 140 p_height = 0; 141 for (int r = 0; r < m_rows; r++) 142 { 143 p_height += m_rowHeight[r]; 144 if (m_rowHeight[r] > 0) 145 p_height += m_rowGap; 146 } 147 p_height -= m_rowGap; p_width = 0; 149 for (int c = 0; c < m_cols; c++) 150 { 151 p_width += m_colWidth[c]; 152 if (m_colWidth[c] > 0) 153 p_width += m_colGap; 154 } 155 p_width -= m_colGap; return true; 157 } 159 167 public void paint(Graphics2D g2D, int pageNo, Point2D pageStart, Properties ctx, boolean isView) 168 { 169 Point2D.Double location = getAbsoluteLocation(pageStart); 170 float y = (float)location.y; 171 for (int row = 0; row < m_rows; row++) 173 { 174 float x = (float)location.x; 175 for (int col = 0; col < m_cols; col++) 176 { 177 if (m_data[row][col] != null) 178 { 179 float yy = y + m_data[row][col].getAscent(); 180 m_data[row][col].draw(g2D, x, yy); 181 } 182 x += m_colWidth[col]; 183 if (m_colWidth[col] > 0) 184 x += m_colGap; 185 } 186 y += m_rowHeight[row]; 187 if (m_rowHeight[row] > 0) 188 y += m_rowGap; 189 } 190 } 192 } | Popular Tags |