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

📄 tableelement.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
							else if (m_columnMaxHeight[col] == 0 || (height + lineHeight) <= m_columnMaxHeight[col])
								height += lineHeight;
						}
					}	//	for all lines
					dataSizes[row][col].addBelow(colWidth, height);
				}
				dataSizes[row][col].roundUp();
				if (dataItem instanceof NamePair)
					m_rowColDrillDown.put(new Point(row, col), (NamePair)dataItem);
			//	System.out.println("Col=" + col + ", row=" + row + " => " + dataSizes[row][col] + " - ColWidth=" + colWidth);
			}	//	for all data rows

			//	Column Width  for Header
			String string = "";
			if (m_columnHeader[dataCol] != null)
				string = m_columnHeader[dataCol].toString();

			//	Print below existing column
			if (col != dataCol)
				headerSizes[dataCol] = new Dimension2DImpl();
			else if (colWidth == 0 && m_columnMaxWidth[dataCol] < 0		//	suppress Null
					|| string.length() == 0)
				headerSizes[dataCol] = new Dimension2DImpl();
			else
			{
				Font font = getFont(HEADER_ROW, dataCol);
				if (!font.isBold())
					font = new Font(font.getName(), Font.BOLD, font.getSize());
				//	No Width Limitations
				if (m_columnMaxWidth[dataCol] == 0 || m_columnMaxWidth[dataCol] == -1 || !m_multiLineHeader)
				{
					TextLayout layout = new TextLayout (string, font, frc);
					float width = layout.getAdvance() + 3;	//	buffer
					float height = layout.getAscent() + layout.getDescent() + layout.getLeading();
					if (width > dynMxColumnWidth)
						m_columnMaxWidth[dataCol] = (int)Math.ceil(dynMxColumnWidth);
					else if (colWidth < width)
						colWidth = width;
					headerSizes[dataCol] = new Dimension2DImpl(width, height);
				}
				//	Width limitations
				if (m_columnMaxWidth[dataCol] != 0 && m_columnMaxWidth[dataCol] != -1)
				{
					float height = 0;
					//
					String[] lines = Pattern.compile("$", Pattern.MULTILINE).split(string);
					for (int lineNo = 0; lineNo < lines.length; lineNo++)
					{
						AttributedString aString = new AttributedString(lines[lineNo]);
						aString.addAttribute(TextAttribute.FONT, font);
						AttributedCharacterIterator iter = aString.getIterator();
						LineBreakMeasurer measurer = new LineBreakMeasurer(iter, frc);
						colWidth = Math.abs(m_columnMaxWidth[dataCol]);
						while (measurer.getPosition() < iter.getEndIndex())
						{
							TextLayout layout = measurer.nextLayout(colWidth);
							float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading();
							if (!m_multiLineHeader)			//	one line only
							{
								height = lineHeight;
								break;
							}
							else if (m_columnMaxHeight[dataCol] == 0
								|| (height + lineHeight) <= m_columnMaxHeight[dataCol])
								height += lineHeight;
						}
					}	//	for all lines
					headerSizes[dataCol] = new Dimension2DImpl(colWidth, height);
				}

			}	//	headerSize
			headerSizes[dataCol].roundUp();
			colWidth = (float)Math.ceil(colWidth);
		//	System.out.println("Col=" + dataCol + " => " + headerSizes[dataCol]);

			//	Round Column Width
			if (dataCol == 0)
				colWidth += V_LINE;
			if (colWidth != 0)
				colWidth += (2*H_GAP) + V_LINE;

			//	Print below existing column
			if (col != dataCol)
			{
				m_columnWidths.add(new Float(0.0));		//	for the data column
				Float origWidth = (Float)m_columnWidths.get(col);
				if (origWidth == null)
					Log.error("TableElement.calculateSize - Column " + dataCol + " below " + col + " - no value for orig width");
				else
				{
					if (origWidth.compareTo(new Float(colWidth)) >= 0)
						Log.trace(Log.l6_Database, "TableElement.calculateSize - Same Width",
							"Col=" + col + " - OrigWidth=" + origWidth + " - Width=" + colWidth + " - Total=" + p_width);
					else
					{
						m_columnWidths.set(col, new Float(colWidth));
						p_width += (colWidth - origWidth.floatValue());
						Log.trace(Log.l6_Database, "TableElement.calculateSize - New Width",
							"Col=" + col +  " - OrigWidth=" + origWidth + " - Width=" + colWidth + " - Total=" + p_width);
					}
				}
			}
			//	Add new Column
			else
			{
				m_columnWidths.add(new Float(colWidth));
				p_width += colWidth;
				Log.trace(Log.l6_Database, "TableElement.calculateSize - Width",
					"Col=" + dataCol + " - Width=" + colWidth + " - Total=" + p_width);
			}
		}	//	for all columns

		//	Height	**********
		p_height = 0;
		for (int row = 0; row < rows; row++)
		{
			float rowHeight = 0f;
			for (int col = 0; col < cols; col++)
			{
				if (dataSizes[row][col].height > rowHeight)
					rowHeight = (float)dataSizes[row][col].height;
			}	//	for all columns
			rowHeight += H_LINE + (2*V_GAP);
			m_rowHeights.add(new Float(rowHeight));
			p_height += rowHeight;
		}	//	for all rows
		//	HeaderRow
		m_headerHeight = 0;
		for (int col = 0; col < cols; col++)
		{
			if (headerSizes[col].height > m_headerHeight)
				m_headerHeight = (int)headerSizes[col].height;
		}	//	for all columns
		m_headerHeight += (4*H_LINE) + (2*V_GAP);	//	Thick lines
		p_height += m_headerHeight;


		//	Page Layout	*******************************************************

		Log.trace(Log.l5_DData, "TableElement.calculateSize",
			"FirstPage=" + m_firstPage + ", NextPages=" + m_nextPages);
		//	One Page on Y | Axis
		if (m_firstPage.height >= p_height && m_pageBreak.size() == 0)
		{
			Log.trace(Log.l6_Database,
				"Page Y=1 - PageHeight=" + m_firstPage.height + " - TableHeight=" + p_height);
			m_firstRowOnPage.add(new Integer(0));	//	Y
			m_pageHeight.add(new Float(p_height));	//	Y index only
		}
		//	multiple pages on Y | Axis
		else
		{
			float availableHeight = 0f;
			float usedHeight = 0f;
			boolean firstPage = true;
			//	for all rows
			for (int row = 0; row < m_rowHeights.size(); row++)
			{
				float rowHeight = ((Float)m_rowHeights.get(row)).floatValue();
				//	Y page break
				boolean forcePageBreak = isPageBreak(row);
				if (availableHeight < rowHeight || forcePageBreak)
				{
					availableHeight = firstPage ? m_firstPage.height : m_nextPages.height;
					m_firstRowOnPage.add(new Integer(row));		//	Y
					Log.trace(Log.l6_Database, "Page Y=" + m_firstRowOnPage.size(), "Row=" + row + " - force=" + forcePageBreak);
					if (!firstPage)
					{
						m_pageHeight.add(new Float(usedHeight));//	Y index only
						Log.trace(Log.l6_Database, "Page Y=" + m_pageHeight.size(), "PageHeight=" + usedHeight);
					}
					firstPage = false;
					//
					availableHeight -= m_headerHeight;
					usedHeight += m_headerHeight;
				}
				availableHeight -= rowHeight;
				usedHeight += rowHeight;
			}	//	for all rows
			m_pageHeight.add(new Float(usedHeight));			//	Y index only
			Log.trace(Log.l6_Database, "Page Y=" + m_pageHeight.size(), "PageHeight=" + usedHeight);
		}	//	multiple Y | pages

		//	One page on - X Axis
		if (m_firstPage.width >= p_width)
		{
			Log.trace(Log.l6_Database,
				"Page X=1 - PageWidth=" + m_firstPage.width + " - TableWidth=" + p_width);
			m_firstColumnOnPage.add(new Integer(0));	//	X
			//
			distributeColumns (m_firstPage.width-(int)p_width, 0, m_columnWidths.size());
		}
		//	multiple pages on - X Axis
		else
		{
			int availableWidth = 0;
			int lastStart = 0;
			for (int col = 0; col < m_columnWidths.size(); col++)
			{
				int columnWidth = ((Float)m_columnWidths.get(col)).intValue();
				//	X page preak
				if (availableWidth < columnWidth)
				{
					if (col != 0)
						distributeColumns (availableWidth, lastStart, col);
					//
					m_firstColumnOnPage.add(new Integer(col));	//	X
					Log.trace(Log.l6_Database,
						"Page X=" + m_firstColumnOnPage.size(), "Col=" + col);
					lastStart = col;
					availableWidth = m_firstPage.width; 		//	Width is the same on all pages
					//
					for (int repCol = 0; repCol < m_repeatedColumns && col > repCol; repCol++)
					{
						float repColumnWidth = ((Float)m_columnWidths.get(repCol)).floatValue();
						//	leave 50% of space available for non repeated columns
						if (availableWidth < m_firstPage.width * 0.5)
							break;
						availableWidth -= repColumnWidth;
					}
				}	//	pageBreak
				availableWidth -= columnWidth;
			}	//	for acc columns
		}	//	multiple - X pages

		//	Last row Lines
		p_height += H_LINE;			//	last fat line
		if (m_lastLineFunction)
			p_height += H_LINE;		//	fat line

		Log.trace(Log.l4_Data, "TableElement.calculateSize",
			"Pages=" + getPageCount() + " X=" + m_firstColumnOnPage.size() + "/Y=" + m_firstRowOnPage.size()
			+ " - Width=" + p_width + ", Height=" + p_height);
		return true;
	}	//	calculateSize


	/**
	 * 	Distribute Columns to fill page
	 * 	@param availableWidth width to distribute
	 * 	@param fromCol start column
	 * 	@param toCol end column (not included)
	 */
	private void distributeColumns (int availableWidth, int fromCol, int toCol)
	{
		Log.trace(8, "TableElement.distributeColumns", "Available=" + availableWidth + ", Columns " + fromCol + "->" + toCol);
		int start = fromCol;
		if (fromCol == 0 && m_repeatedColumns > 0)
			start = m_repeatedColumns;
		//	calculate total Width
		int totalWidth = availableWidth;
		for (int col = start; col < toCol; col++)
			totalWidth += ((Float)m_columnWidths.get(col)).floatValue();
		int remainingWidth = availableWidth;
		//	distribute proportionally (does not increase zero width columns)
		for (int x = 0; remainingWidth > 0 && x < 5; x++)	//	max 4 iterations
		{
			Log.trace(9, "TotalWidth=" + totalWidth + ", Remaining=" + remainingWidth);
			for (int col = start; col < toCol && remainingWidth != 0; col++)
			{
				int columnWidth = ((Float)m_columnWidths.get(col)).intValue();
				if (columnWidth != 0)
				{
					int additionalPart = columnWidth * availableWidth / totalWidth;
					if (remainingWidth < additionalPart)
					{
						m_columnWidths.set(col, new Float(columnWidth+remainingWidth));
						remainingWidth = 0;
					}
					else
					{
						m_columnWidths.set(col, new Float(columnWidth+additionalPart));
						remainingWidth -= additionalPart;
					}
					Log.trace(9, "Column " + col, "From " + columnWidth + " to " + m_columnWidths.get(col));
				}
			}
		}
		//	add remainder to last non 0 width column
		for (int c = toCol-1; remainingWidth != 0 && c >=0; c--)
		{
			int columnWidth = ((Float)m_columnWidths.get(c)).intValue();
			if (columnWidth > 0)
			{
				m_columnWidths.set(c, new Float(columnWidth+remainingWidth));
				Log.trace(9, "Final Column " + c, "From " + columnWidth + " to " + m_columnWidths.get(c));
				remainingWidth = 0;
			}
		}
	}	//	distribute Columns

	/**
	 * 	Check for for PageBreak
	 * 	@param row current row
	 * 	@return true if row should be on new page
	 */
	private boolean isPageBreak (int row)
	{
		for (int i = 0; i < m_pageBreak.size(); i++)
		{
			Integer rr = (Integer)m_pageBreak.get(i);
			if (rr.intValue()+1 == row)
				return true;
			else if (rr.intValue() > row)
				return false;
		}
		return false;
	}	//	isPageBreak

	/**
	 * 	For Multi-Page Tables, set Height to Height of last Page
	 */
	public void setHeightToLastPage()
	{
		p_height = getHeight(getPageCount());
		Log.trace(Log.l5_DData, "TableElement.setHeightToLastPage (" + getPageCount() + ")", String.valueOf(p_height));
	}	//	setHeightToLastPage

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

	/**
	 * 	Get Font.
	 *  Based on Point (row,col).
	 *  <pre>
	 *  Examples:
	 * 	  From general to specific:
	 *      (-1,-1) => for entire table
	 *  	(-1, c) => for entire column c
	 *  	(r, -1) => for entire row r (overwrites column)
	 *      (r, c)  => for specific cell (highest priority)
	 *    Header is row -2
	 * 		(-2,-1) => for all header columns
	 * 		(-2, c) => for header column c
	 *  </pre>
	 * 	@param row row
	 * 	@param col column
	 * 	@return Font for row/col
	 */
	private Font getFont (int row, int col)
	{
		//	First specific position
		Font font = (Font)m_rowColFont.get(new Point(row, col));

⌨️ 快捷键说明

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