KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > layout > GridElement


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

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 /**
23  * Grid Element.
24  * Simple Table with Rows/Columns, but no Headers
25  *
26  * @author Jorg Janke
27  * @version $Id: GridElement.java,v 1.4 2002/08/26 05:24:20 jjanke Exp $
28  */

29 public class GridElement extends PrintElement
30 {
31     /**
32      * Grid Element Constructor
33      * Call setData to initialize content
34      * @param rows max rows
35      * @param cols max cols
36      */

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         // explicit init
45
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     } // GridElement
54

55     /** Gap between Rows */
56     private int m_rowGap = 3;
57     /** Gap between Columns */
58     private int m_colGap = 5;
59
60     /** Rows */
61     private int m_rows;
62     /** Columns */
63     private int m_cols;
64     /** The Data */
65     private TextLayout[][] m_data = null;
66     /** Row Height */
67     private int[] m_rowHeight = null;
68     /** Column Width */
69     private int[] m_colWidth = null;
70     /** Context */
71     private FontRenderContext m_frc = new FontRenderContext(null, true, true);
72
73     /**
74      * Create TextLayout from Data and calculate size
75      * @param row row
76      * @param col column
77      * @param data info element
78      * @param font font
79      * @param foreground color for foreground
80      */

81     public void setData (int row, int col, String JavaDoc data, Font font, Paint foreground)
82     {
83         if (data == null || data.length() == 0)
84             return;
85         //
86
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     } // setData
92

93     /**
94      * Create TextLayout from Data and calculate size
95      * @param row row
96      * @param col column
97      * @param layout single line layout
98      */

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 JavaDoc("Size already calculated");
105         if (row < 0 || row >= m_rows)
106             throw new ArrayIndexOutOfBoundsException JavaDoc("Row Index=" + row + " Rows=" + m_rows);
107         if (col < 0 || col >= m_cols)
108             throw new ArrayIndexOutOfBoundsException JavaDoc("Column Index=" + col + " Cols=" + m_cols);
109         //
110
m_data[row][col] = layout;
111         // Set Size
112
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     } // setData
119

120     /**
121      * Set Rpw/Column gap
122      * @param rowGap row gap
123      * @param colGap column gap
124      */

125     public void setGap (int rowGap, int colGap)
126     {
127         m_rowGap = rowGap;
128         m_colGap = colGap;
129     } // setGap
130

131     /*************************************************************************/
132
133     /**
134      * Layout & Calculate Image Size.
135      * Set p_width & p_height
136      * @return true if calculated
137      */

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; // remove last
148
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; // remove last
156
return true;
157     } // calculateSize
158

159     /**
160      * Paint it
161      * @param g2D Graphics
162      * @param pageStart top left Location of page
163      * @param pageNo page number for multi page support (0 = header/footer) - ignored
164      * @param ctx print context
165      * @param isView true if online view (IDs are links)
166      */

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         //
172
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     } // paint
191

192 } // GridElement
193
Popular Tags