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

📄 layoutengine.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

				//	line alignment
				String alignment = item.getFieldAlignmentType();
				int maxWidth = item.getMaxWidth();
				boolean lineAligned = false;
				if (item.isRelativePosition())
				{
					if (item.isLineAlignLeading())
					{
						alignment = MPrintFormatItem.FIELD_ALIGN_LEADING;
						maxWidth = getAreaBounds().width;
						lineAligned = true;
					}
					else if (item.isLineAlignCenter())
					{
						alignment = MPrintFormatItem.FIELD_ALIGN_CENTER;
						maxWidth = getAreaBounds().width;
						lineAligned = true;
					}
					else if (item.isLineAlignTrailing())
					{
						alignment = MPrintFormatItem.FIELD_ALIGN_TRAILING;
						maxWidth = getAreaBounds().width;
						lineAligned = true;
					}
				}

				//	Type
				PrintElement element = null;
				if (item.isTypePrintFormat())		//** PrintFormat
				{
					newLine();
					try
					{
						MPrintFormat format = MPrintFormat.get (item.getAD_PrintFormatChild_ID(), false);
						format.setLanguage(m_format.getLanguage());
						if (m_format.isTranslationView())
							format.setTranslationLanguage(m_format.getLanguage());
						log.info("layoutForm - include Format " + format + " - Item=" + item.getName());
						//
						int AD_Column_ID = item.getAD_Column_ID();
						Object obj = m_data.getNode(new Integer(AD_Column_ID));
					//	Object obj = m_data.getNode(item.getColumnName());	//	slower
						PrintDataElement data = (PrintDataElement)obj;
						int Record_ID = Integer.parseInt(data.getValueKey());
						MQuery query = new MQuery (format.getAD_Table_ID());
						query.addRestriction(item.getColumnName(), MQuery.EQUAL, new Integer(Record_ID));
						format.setTranslationViewQuery(query);
						log.debug("layoutForm - include " + query);
						//
						DataEngine de = new DataEngine(format.getLanguage());
						PrintData printData = de.getPrintData(m_data.getCtx(), format, query);
						log.debug("layoutForm - include " + printData);
						//
						element = layoutTable (format, printData, item.getXspace());
						//	handle multi page tables
						if (element.getPageCount() > 1)
						{
							Point2D.Double loc = m_position[m_area];
							element.setLocation(loc);
							for (int p = 1; p < element.getPageCount(); p++)	//	don't add last one
							{
								m_currPage.addElement (element);
								newPage();
							}
							m_position[m_area] = loc;
							((TableElement)element).setHeightToLastPage();
						}
					}
					catch (Exception e)
					{
						log.error("layoutForm", e);
					}
					if (element == null)
						log.error("layoutForm - No included Format");
				}
				else if (item.isTypeImage())		//**	Image
				{
					if (item.isImageAttached())
						element = new ImageElement(item.getID());
					else
						element = new ImageElement(item.getImageURL());
					element.layout(maxWidth, item.getMaxHeight(), false, alignment);
				}
				else if (item.isTypeField())		//**	Field
				{
					if (maxWidth == 0 && item.isFieldAlignBlock())
						maxWidth = getAreaBounds().width;
					element = createFieldElement (item, maxWidth, alignment, m_format.isForm());
				}
				else	//	(item.isTypeText())		//**	Text
				{
					if (maxWidth == 0 && item.isFieldAlignBlock())
						maxWidth = getAreaBounds().width;
					element = createStringElement (item.getPrintName (m_format.getLanguage ()),
						item.getAD_PrintColor_ID (), item.getAD_PrintFont_ID (),
						maxWidth, item.getMaxHeight (), item.isHeightOneLine (), alignment, true);
				}
				//
				if (element != null)
				{
					somethingPrinted = true;
					if (!lineAligned)
						lastWidth[m_area] = element.getWidth();
					lastHeight[m_area] = element.getHeight();
				}
				else
				{
					somethingPrinted = false;
					lastWidth[m_area] = 0f;
					lastHeight[m_area] = 0f;
				}

				//	Does it fit?
				if (item.isRelativePosition() && !lineAligned)
				{
					if (!isXspaceFor(lastWidth[m_area]))
					{
						if (Log.isTraceLevel(9))
							log.debug("layoutForm - new Line - not enough X space for "
								+ lastWidth[m_area] + " - remaining " + getXspace() + " - Area=" + m_area);
						newLine ();
					}
					if (m_area == AREA_CONTENT && !isYspaceFor(lastHeight[m_area]))
					{
						if (Log.isTraceLevel(9))
							log.debug("layoutForm - new Page - not enough Y space "
								+ lastHeight[m_area] + " - remaining " + getXspace() + " - Area=" + m_area);
						newPage ();
					}
				}
				//	We know Position and Size
			//	Log.trace(Log.l6_Database, "LayoutEngine.layoutForm",
			//		"Page=" + m_pageNo + " [" + m_area + "] " + m_position[m_area].x + "/" + m_position[m_area].y
			//		+ " w=" + lastWidth[m_area] + ",h=" + lastHeight[m_area] + " " + item);
				if (element != null)
					element.setLocation(m_position[m_area]);
				//	Add to Area
				if (m_area == AREA_CONTENT)
					m_currPage.addElement (element);
				else
					m_headerFooter.addElement (element);
				//
				if (lastHeight[m_area] > m_maxHeightSinceNewLine[m_area])
					m_maxHeightSinceNewLine[m_area] = lastHeight[m_area];

			}	//	for every item
		}	//	for every row
	}	//	layoutForm

	/**
	 *	Create String Element
	 *
	 * 	@param content string to be printed
	 * 	@param AD_PrintColor_ID color
	 * 	@param AD_PrintFont_ID font
	 * 	@param maxWidth max width
	 * 	@param maxHeight max height
	 * 	@param isHeightOneLine onle line only
	 * 	@param FieldAlignmentType alignment type (MPrintFormatItem.FIELD_ALIGN_*)
	 *  @param isTranslated if true and content contaiins @variable@, it is dynamically translated during print
	 * 	@return Print Element
	 */
	private PrintElement createStringElement (String content, int AD_PrintColor_ID, int AD_PrintFont_ID,
		int maxWidth, int maxHeight, boolean isHeightOneLine, String FieldAlignmentType, boolean isTranslated)
	{
		//	Color / Font
		Color color = m_printColor.getColor();	//	default
		if (AD_PrintColor_ID != 0 && m_printColor.getID() != AD_PrintColor_ID)
		{
			MPrintColor c = MPrintColor.get (AD_PrintColor_ID);
			if (c.getColor() != null)
				color = c.getColor();
		}
		Font font = m_printFont.getFont();		//	default
		if (AD_PrintFont_ID != 0 && m_printFont.getID() != AD_PrintFont_ID)
		{
			MPrintFont f = MPrintFont.get (AD_PrintFont_ID);
			if (f.getFont() != null)
				font = f.getFont();
		}
		PrintElement e = new StringElement(content, font, color, null, isTranslated);
		e.layout (maxWidth, maxHeight, isHeightOneLine, FieldAlignmentType);
		return e;
	}	//	createStringElement

	/**
	 * 	Create Field Element
	 * 	@param item Format Item
	 * 	@param maxWidth max width
	 * 	@param FieldAlignmentType alignment type (MPrintFormatItem.FIELD_ALIGN_*)
	 * 	@param isForm true if document
	 * 	@return Print Element or null if nothing to print
	 */
	private PrintElement createFieldElement (MPrintFormatItem item, int maxWidth,
		String FieldAlignmentType, boolean isForm)
	{
		//	Get Data
		Object obj = m_data.getNode(new Integer(item.getAD_Column_ID()));
		if (obj == null)
			return null;
		else if (obj instanceof PrintDataElement)
			;
		else
		{
			log.error("createFieldElement - Element not PrintDataElement " + obj.getClass());
			return null;
		}

		//	Convert DataElement to String
		PrintDataElement data = (PrintDataElement)obj;
		if (data.isNull() && item.isSuppressNull())
			return null;
		String stringContent = data.getValueDisplay (m_format.getLanguage());
		if ((stringContent == null || stringContent.length() == 0) && item.isSuppressNull())
			return null;
		Object content = stringContent;
		if (data.getValue() instanceof Boolean)
			content = data.getValue();
		//	Convert AmtInWords Content to alpha
		if (item.getColumnName().equals("AmtInWords"))
		{
			String amtInWords = Msg.getAmtInWords (m_format.getLanguage(), stringContent);
			log.debug("createFieldElement - AmtInWords: " + content);
			content = amtInWords;
		}
		//	Label
		String label = item.getPrintName(m_format.getLanguage());
		String labelSuffix = item.getPrintNameSuffix(m_format.getLanguage());

		//	ID Type
		NamePair ID = null;
		if (data.isID())
		{	//	Record_ID/ColumnName
			Object value = data.getValue();
			if (value instanceof KeyNamePair)
				ID = new KeyNamePair(((KeyNamePair)value).getKey(), item.getColumnName());
			else if (value instanceof ValueNamePair)
				ID = new ValueNamePair(((ValueNamePair)value).getValue(), item.getColumnName());
		}
		else if (MPrintFormatItem.FIELD_ALIGN_DEFAULT.equals(FieldAlignmentType))
		{
			if (data.isNumeric())
				FieldAlignmentType = MPrintFormatItem.FIELD_ALIGN_TRAILING;
			else
				FieldAlignmentType = MPrintFormatItem.FIELD_ALIGN_LEADING;
		}

		//	Get Color/ Font
		Color color = m_printColor.getColor();	//	default
		if (ID != null && !isForm)
			;									//	link color/underline handeled in PrintElement classes
		else if (item.getAD_PrintColor_ID() != 0 && m_printColor.getID() != item.getAD_PrintColor_ID())
		{
			MPrintColor c = MPrintColor.get (item.getAD_PrintColor_ID());
			if (c.getColor() != null)
				color = c.getColor();
		}

		Font font = m_printFont.getFont();		//	default
		if (item.getAD_PrintFont_ID() != 0 && m_printFont.getID() != item.getAD_PrintFont_ID())
		{
			MPrintFont f = MPrintFont.get (item.getAD_PrintFont_ID());
			if (f.getFont() != null)
				font = f.getFont();
		}


		PrintElement e = null;
		if (data.getDisplayType() == DisplayType.Location)
		{
			e = new LocationElement(m_printCtx, ((KeyNamePair)ID).getKey(), font, color);
			e.layout (maxWidth, item.getMaxHeight(), item.isHeightOneLine(), FieldAlignmentType);
		}
		else
		{
			e = new StringElement(content, font, color, isForm ? null : ID, label, labelSuffix);
			e.layout (maxWidth, item.getMaxHeight(), item.isHeightOneLine(), FieldAlignmentType);
		}
		return e;
	}	//	createFieldElement


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

	/**
	 * 	Layout Table.
	 *	Convert PrintData into TableElement
	 *  @param format format to use
	 *  @param printData data to use
	 *  @param xOffset X Axis - offset (start of table) i.e. indentation
	 *  @return TableElement
	 */
	private PrintElement layoutTable (MPrintFormat format, PrintData printData,
		int xOffset)
	{
		log.debug("layoutTable " + format.getName() + " - " + printData.getName());
		MPrintTableFormat tf = format.getTableFormat();
		//	Initial Values
		HashMap rowColFont = new HashMap();
		MPrintFont printFont = MPrintFont.get (format.getAD_PrintFont_ID());
		rowColFont.put(new Point(TableElement.ALL,TableElement.ALL), printFont.getFont());
		tf.setStandard_Font(printFont.getFont());
		rowColFont.put(new Point(TableElement.HEADER_ROW,TableElement.ALL), tf.getHeader_Font());
		//
		HashMap rowColColor = new HashMap();
		MPrintColor printColor = MPrintColor.get (format.getAD_PrintColor_ID());
		rowColColor.put(new Point(TableElement.ALL,TableElement.ALL), printColor.getColor());
		rowColColor.put(new Point(TableElement.HEADER_ROW,TableElement.ALL), tf.getHeaderFG_Color());
		//
		HashMap rowColBackground = new HashMap();
		rowColBackground.put(new Point(TableElement.HEADER_ROW,TableElement.ALL), tf.getHeaderBG_Color());
		//	Sizes
		boolean multiLineHeader = false;
		int pageNoStart = m_pageNo;
		int repeatedColumns = 1;
		Rectangle firstPage = new Rectangle(m_content);
		firstPage.x += xOffset;
		firstPage.width -= xOffset;
		int yOffset = (int)m_position[AREA_CONTENT].y - m_content.y;
		firstPage.y += yOffset;
		firstPage.height -= yOffset;
		Rectangle nextPages = new Rectangle(m_content);
		nextPages.x += xOffset;
		nextPages.width -= xOffset;
		//	Column count
		m_columnCount = 0;
		for (int c = 0; c < format.getItemCount(); c++)
		{
			if (format.getItem(c).isPrinted())
				m_columnCount++;
		}
		//	System.out.println("Cols=" + cols);

		//	Header & Column Setup
		ValueNamePair[] columnHeader = new ValueNamePair[m_columnCount];
		int[] columnMaxWidth = new int[m_columnCount];
		int[] columnMaxHeight = new int[m_columnCount];
		boolean[] fixedWidth = new boolean [m_columnCount];
		String[] columnJustification = new String[m_columnCount];
		HashMap additionalLines = new HashMap();

		int col = 0;
		for (int c = 0; c < format.getItemCount(); c++)
		{
			MPrintFormatItem item = format.getItem(c);
			if (item.isPrinted())
			{
				if (item.isNextLine() && item.getBelowColumn() != 0)
				{
					additionalLines.put(new Integer(col), new Integer(item.getBelowColumn()-1));
					item.setSuppressNull(true);		//	display size will be set to 0 in TableElement
				}
				columnHeader[col] = new ValueNamePair(item.getColumnName(),
					item.getPrintName(format.getLanguage()));
				columnMaxWidth[col] = item.getMaxWidth();
				fixedWidth[col] = (columnMaxWidth[col] != 0 && item.isFixedWidth());
				if (item.isSuppressNull())
				{
					if (columnMaxWidth[col] == 0)
						columnMaxWidth[col] = -1;		//	indication suppress if Null
					else
						columnMaxWidth[col] *= -1;
				}
				columnMaxHeight[col] = item.getMaxHeight();
				columnJustification[col] = item.getFieldAlignmentType();
				if (columnJustification[col] == null || columnJustification[col].equals(MPrintFormatItem.FIELD_ALIGN_DEFAULT))
					columnJustification[col] = MPrintFormatItem.FIELD_ALIGN_LEADING;	//	when generated sets correct alignment
				//	Column Fonts
				if (item.getAD_PrintFont_ID() != 0 && item.getAD_PrintFont_ID() != format.getAD_PrintFont_ID())
				{
					MPrintFont font = MPrintFont.get(item.getAD_PrintFont_ID());
					rowColFont.put(new Point(TableElement.ALL, col), font.getFont());
				}
				if (item.getAD_PrintColor_ID() != 0 && item.getAD_PrintColor_ID() != format.getAD_PrintColor_ID())
				{
					MPrintColor color = MPrintColor.get (item.getAD_PrintColor_ID());
					rowColColor.put(new Point(TableElement.ALL, col), color.getColor());
				}
				//
				col++;
			}
		}

		//	The Data
		int rows = printData.getRowCount();
	//	System.out.println("Rows=" + rows);
		Object[][] data = new Object [rows][m_columnCount];
		KeyNamePair[] pk = new KeyNamePair[rows];
		String pkColumnName = null;
		boolean lastLineFunction = false;
		ArrayList pageBreak = new ArrayList();

		//	for all rows
		for (int row = 0; row < rows; row++)
		{
		//	System.out.println("row=" + row);
			printData.setRowIndex(row);
			if (printData.isFunctionRow())
			{
				rowColFont.put(new Point(row, TableElement.ALL), tf.getFunct_Font());
				rowColColor.put(new Point(row, TableElement.ALL), tf.getFunctFG_Color());
				rowColBackground.put(new Point(row, TableElement.ALL), tf.getFunctBG_Color());
				lastLineFunction = true;
				if (printData.isPageBreak())
				{
					pageBreak.add(new Integer(row));
					if (Log.isTraceLevel(8))
						log.debug("layoutTable - PageBreak row=" + row);
				}
			}
			//	Summary/Line Levels for Finanial Reports
			else
			{
				int levelNo = printData.getLineLevelNo();
				if (levelNo != 0)
				{
					Font base = printFont.getFont();
					rowColFont.put(new Point(row, TableElement.ALL), new Font (base.getName(),
						Font.PLAIN, base.getSize()-levelNo));
				}
			}
			//	for all columns
			col = 0;
			for (int c = 0; c < format.getItemCount(); c++)
			{
				MPrintFormatItem item = format.getItem(c);
				Object dataElement = null;
				if (item.isPrinted())
				{
					Object obj = printData.getNode(new Integer(item.getAD_Column_ID()));
					if (obj == null)
						;
					else if (obj instanceof PrintDataElement)
					{
						PrintDataElement pde = (PrintDataElement)obj;
						if (pde.isID() || pde.isYesNo())
							dataElement = pde.getValue();
						else
							dataElement = pde.getValueDisplay(format.getLanguage());
					}
					else
						log.error("layoutTable - Element not PrintDataElement " + obj.getClass());

				//	System.out.println("  col=" + col + " " + item.getAD_Column_ID() + " => " + dataElement);
					data[row][col] = dataElement;
					col++;
				}	//	printed
			}	//	for all columns

			PrintDataElement pde = printData.getPKey();
			if (pde != null)	//	for FunctionRows
			{
				pk[row] = (KeyNamePair)pde.getValue();
				if (pkColumnName == null)
					pkColumnName = pde.getColumnName();
			}
		//	else
		//		System.out.println("No PK " + printData);
		}	//	for all rows

		//
		TableElement table = new TableElement(columnHeader,
			columnMaxWidth, columnMaxHeight, columnJustification,
			fixedWidth, lastLineFunction, multiLineHeader,
			data, pk, pkColumnName,
			pageNoStart, firstPage, nextPages, repeatedColumns, additionalLines,
			rowColFont, rowColColor, rowColBackground,
			tf, pageBreak);
		table.layout(0,0,false, MPrintFormatItem.FIELD_ALIGN_LEADING);
		if (m_tableElement == null)
			m_tableElement = table;
		return table;
	}	//	layoutTable

	/**
	 * 	Layout Parameter based on MQuery
	 * 	@return PrintElement
	 */
	private PrintElement layoutParameter ()
	{
		if (m_query == null || !m_query.isActive())
			return null;
		//
		ParameterElement pe = new ParameterElement(m_query, m_printCtx, m_format.getTableFormat());
		pe.layout(0, 0, false, null);
		return pe;
	}	//	layoutParameter

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

}	//	LayoutEngine

⌨️ 快捷键说明

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