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

📄 gridelement.java

📁 Java写的ERP系统
💻 JAVA
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.print.layout;

import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.text.*;
import java.util.*;

/**
 *	Grid Element.
 *  Simple Table with Rows/Columns, but no Headers
 *
 * 	@author 	Jorg Janke
 * 	@version 	$Id: GridElement.java,v 1.4 2002/08/26 05:24:20 jjanke Exp $
 */
public class GridElement extends PrintElement
{
	/**
	 *	Grid Element Constructor
	 *  Call setData to initialize content
	 *  @param rows max rows
	 *  @param cols max cols
	 */
	public GridElement(int rows, int cols)
	{
		m_rows = rows;
		m_cols = cols;
		m_data = new TextLayout[rows][cols];
		m_rowHeight = new int[rows];
		m_colWidth = new int[cols];
		//	explicit init
		for (int r = 0; r < m_rows; r++)
		{
			m_rowHeight[r] = 0;
			for (int c = 0; c < m_cols; c++)
				m_data[r][c] = null;
		}
		for (int c = 0; c < m_cols; c++)
			m_colWidth[c] = 0;
	}	//	GridElement

	/**	Gap between Rows		*/
	private int			m_rowGap = 3;
	/**	Gap between Columns		*/
	private int			m_colGap = 5;

	/** Rows				*/
	private int			m_rows;
	/**	Columns				*/
	private int			m_cols;
	/**	The Data			*/
	private TextLayout[][] 	m_data = null;
	/**	Row Height			*/
	private int[]		m_rowHeight = null;
	/**	Column Width		*/
	private int[]		m_colWidth = null;
	/** Context				*/
	private FontRenderContext	m_frc = new FontRenderContext(null, true, true);

	/**
	 * 	Create TextLayout from Data and calculate size
	 *  @param row row
	 *  @param col column
	 *  @param data info element
	 *  @param font font
	 *  @param foreground color for foreground
	 */
	public void setData (int row, int col, String data, Font font, Paint foreground)
	{
		if (data == null || data.length() == 0)
			return;
		//
		Map map = new HashMap();
		map.put(TextAttribute.FONT, font);
		map.put(TextAttribute.FOREGROUND, foreground);
		TextLayout layout = new TextLayout(data, map, m_frc);
		setData (row, col, layout);
	}	//	setData

	/**
	 * 	Create TextLayout from Data and calculate size
	 *  @param row row
	 *  @param col column
	 *  @param layout single line layout
	 */
	public void setData (int row, int col, TextLayout layout)
	{
		if (layout == null)
			return;
		if (p_sizeCalculated)
			throw new IllegalStateException("Size already calculated");
		if (row < 0 || row >= m_rows)
			throw new ArrayIndexOutOfBoundsException("Row Index=" + row + " Rows=" + m_rows);
		if (col < 0 || col >= m_cols)
			throw new ArrayIndexOutOfBoundsException("Column Index=" + col + " Cols=" + m_cols);
		//
		m_data[row][col] = layout;
		//	Set Size
		int height = (int)(layout.getAscent() + layout.getDescent() + layout.getLeading())+1;
		int width = (int)layout.getAdvance()+1;
		if (m_rowHeight[row] < height)
			m_rowHeight[row] = height;
		if (m_colWidth[col] < width)
			m_colWidth[col] = width;
	}	//	setData

	/**
	 * 	Set Rpw/Column gap
	 * 	@param rowGap row gap
	 * 	@param colGap column gap
	 */
	public void setGap (int rowGap, int colGap)
	{
		m_rowGap = rowGap;
		m_colGap = colGap;
	}	//	setGap

	/*************************************************************************/

	/**
	 * 	Layout & Calculate Image Size.
	 * 	Set p_width & p_height
	 * 	@return true if calculated
	 */
	protected boolean calculateSize()
	{
		p_height = 0;
		for (int r = 0; r < m_rows; r++)
		{
			p_height += m_rowHeight[r];
			if (m_rowHeight[r] > 0)
				p_height += m_rowGap;
		}
		p_height -= m_rowGap;	//	remove last
		p_width = 0;
		for (int c = 0; c < m_cols; c++)
		{
			p_width += m_colWidth[c];
			if (m_colWidth[c] > 0)
				p_width += m_colGap;
		}
		p_width -= m_colGap;		//	remove last
		return true;
	}	//	calculateSize

	/**
	 * 	Paint it
	 * 	@param g2D Graphics
	 *  @param pageStart top left Location of page
	 *  @param pageNo page number for multi page support (0 = header/footer) - ignored
	 *  @param ctx print context
	 *  @param isView true if online view (IDs are links)
	 */
	public void paint(Graphics2D g2D, int pageNo, Point2D pageStart, Properties ctx, boolean isView)
	{
		Point2D.Double location = getAbsoluteLocation(pageStart);
		float y = (float)location.y;
		//
		for (int row = 0; row < m_rows; row++)
		{
			float x = (float)location.x;
			for (int col = 0; col < m_cols; col++)
			{
				if (m_data[row][col] != null)
				{
					float yy = y + m_data[row][col].getAscent();
					m_data[row][col].draw(g2D, x, yy);
				}
				x += m_colWidth[col];
				if (m_colWidth[col] > 0)
					x += m_colGap;
			}
			y += m_rowHeight[row];
			if (m_rowHeight[row] > 0)
				y += m_rowGap;
		}
	}	//	paint

}	//	GridElement

⌨️ 快捷键说明

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