📄 layoutengine.java
字号:
/**************************************************************************
* Layout Form.
* For every Row, loop through the Format
* and calculate element size and position.
*/
private void layoutForm()
{
// log.info("layoutForm");
m_columnCount = 0;
if (m_data == null)
return;
// for every row
for (int row = 0; row < m_data.getRowCount(); row++)
{
log.info("Row=" + row);
m_data.setRowIndex(row);
boolean somethingPrinted = true; // prevent NL of nothing printed and supress null
// for every item
for (int i = 0; i < m_format.getItemCount(); i++)
{
MPrintFormatItem item = m_format.getItem(i);
// log.fine("layoutForm - Row=" + row + " - #" + i + " - " + item);
if (!item.isPrinted())
continue;
// log.fine("layoutForm - Row=" + row + " - #" + i + " - " + item);
m_columnCount++;
// Read Header/Footer just once
if (row > 0 && (item.isHeader() || item.isFooter()))
continue;
// Position
if (item.isHeader()) // Area
setArea(AREA_HEADER);
else if (item.isFooter())
setArea(AREA_FOOTER);
else
setArea(AREA_CONTENT);
//
if (item.isSetNLPosition() && item.isRelativePosition())
m_tempNLPositon = 0;
// New Page/Line
if (item.isNextPage()) // item.isPageBreak() // new page
newPage(false, false);
else if (item.isNextLine() && somethingPrinted) // new line
{
newLine ();
somethingPrinted = false;
}
else
addX(m_lastWidth[m_area]);
// Relative Position space
if (item.isRelativePosition())
{
addX(item.getXSpace());
addY(item.getYSpace());
}
else // Absolute relative position
setRelativePosition(item.getXPosition(), item.getYPosition());
// Temporary NL Position when absolute positioned
if (item.isSetNLPosition() && !item.isRelativePosition())
m_tempNLPositon = (int)getPosition().getX();
// line alignment
String alignment = item.getFieldAlignmentType();
int maxWidth = item.getMaxWidth();
boolean lineAligned = false;
if (item.isRelativePosition())
{
if (item.isLineAlignLeading())
{
alignment = MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft;
maxWidth = getAreaBounds().width;
lineAligned = true;
}
else if (item.isLineAlignCenter())
{
alignment = MPrintFormatItem.FIELDALIGNMENTTYPE_Center;
maxWidth = getAreaBounds().width;
lineAligned = true;
}
else if (item.isLineAlignTrailing())
{
alignment = MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight;
maxWidth = getAreaBounds().width;
lineAligned = true;
}
}
// Type
PrintElement element = null;
if (item.isTypePrintFormat()) //** included PrintFormat
{
element = includeFormat (item, m_data);
}
else if (item.isTypeImage()) //** Image
{
if (item.isImageField())
element = createImageElement (item);
else if (item.isImageIsAttached())
element = ImageElement.get (item.get_ID());
else
element = ImageElement.get (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 if (item.isTypeBox()) //** Line/Box
{
if (m_format.isForm())
element = createBoxElement(item);
}
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);
}
// Printed - set last width/height
if (element != null)
{
somethingPrinted = true;
if (!lineAligned)
m_lastWidth[m_area] = element.getWidth();
m_lastHeight[m_area] = element.getHeight();
}
else
{
somethingPrinted = false;
m_lastWidth[m_area] = 0f;
m_lastHeight[m_area] = 0f;
}
// Does it fit?
if (item.isRelativePosition() && !lineAligned)
{
if (!isXspaceFor(m_lastWidth[m_area]))
{
log.finest("Not enough X space for "
+ m_lastWidth[m_area] + " - remaining " + getXspace() + " - Area=" + m_area);
newLine ();
}
if (m_area == AREA_CONTENT && !isYspaceFor(m_lastHeight[m_area]))
{
log.finest("Not enough Y space "
+ m_lastHeight[m_area] + " - remaining " + getYspace() + " - Area=" + m_area);
newPage (true, true);
}
}
// We know Position and Size
// log.fine( "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 (m_lastHeight[m_area] > m_maxHeightSinceNewLine[m_area])
m_maxHeightSinceNewLine[m_area] = m_lastHeight[m_area];
} // for every item
} // for every row
} // layoutForm
/**
* Include Table Format
* @param item print format item
* @return Print Element
*/
private PrintElement includeFormat (MPrintFormatItem item, PrintData data)
{
newLine();
PrintElement element = null;
//
MPrintFormat format = MPrintFormat.get (getCtx(), item.getAD_PrintFormatChild_ID(), false);
format.setLanguage(m_format.getLanguage());
if (m_format.isTranslationView())
format.setTranslationLanguage(m_format.getLanguage());
int AD_Column_ID = item.getAD_Column_ID();
log.info(format + " - Item=" + item.getName() + " (" + AD_Column_ID + ")");
//
Object obj = data.getNode(new Integer(AD_Column_ID));
// Object obj = data.getNode(item.getColumnName()); // slower
if (obj == null)
{
data.dumpHeader();
data.dumpCurrentRow();
log.log(Level.SEVERE, "No Node - AD_Column_ID="
+ AD_Column_ID + " - " + item + " - " + data);
return null;
}
PrintDataElement dataElement = (PrintDataElement)obj;
String recordString = dataElement.getValueKey();
if (recordString == null || recordString.length() == 0)
{
data.dumpHeader();
data.dumpCurrentRow();
log.log(Level.SEVERE, "No Record Key - " + dataElement
+ " - AD_Column_ID=" + AD_Column_ID + " - " + item);
return null;
}
int Record_ID = 0;
try
{
Record_ID = Integer.parseInt(recordString);
}
catch (Exception e)
{
data.dumpCurrentRow();
log.log(Level.SEVERE, "Invalid Record Key - " + recordString
+ " (" + e.getMessage()
+ ") - AD_Column_ID=" + AD_Column_ID + " - " + item);
return null;
}
MQuery query = new MQuery (format.getAD_Table_ID());
query.addRestriction(item.getColumnName(), MQuery.EQUAL, new Integer(Record_ID));
format.setTranslationViewQuery(query);
log.fine(query.toString());
//
DataEngine de = new DataEngine(format.getLanguage());
PrintData includedData = de.getPrintData(data.getCtx(), format, query);
log.fine(includedData.toString());
if (includedData == null)
return null;
//
element = layoutTable (format, includedData, 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(true, false);
}
m_position[m_area] = loc;
((TableElement)element).setHeightToLastPage();
}
m_lastWidth[m_area] = element.getWidth();
m_lastHeight[m_area] = element.getHeight();
if (!isXspaceFor(m_lastWidth[m_area]))
{
log.finest("Not enough X space for "
+ m_lastWidth[m_area] + " - remaining " + getXspace() + " - Area=" + m_area);
newLine ();
}
if (m_area == AREA_CONTENT && !isYspaceFor(m_lastHeight[m_area]))
{
log.finest("Not enough Y space "
+ m_lastHeight[m_area] + " - remaining " + getYspace() + " - Area=" + m_area);
newPage (true, false);
}
//
return element;
} // includeFormat
/**
* 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)
{
if (content == null || content.length() == 0)
return null;
// Color / Font
Color color = getColor(); // default
if (AD_PrintColor_ID != 0 && m_printColor.get_ID() != AD_PrintColor_ID)
{
MPrintColor c = MPrintColor.get (getCtx(), AD_PrintColor_ID);
if (c.getColor() != null)
color = c.getColor();
}
Font font = m_printFont.getFont(); // default
if (AD_PrintFont_ID != 0 && m_printFont.get_ID() != 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.log(Level.SEVERE, "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;
// non-string
Object content = stringContent;
if (data.getValue() instanceof Boolean)
content = data.getValue();
// Convert AmtInWords Content to alpha
if (item.getColumnName().equals("AmtInWords"))
{
log.fine("AmtInWords: " + stringContent);
stringContent = Msg.getAmtInWords (m_format.getLanguage(), stringContent);
content = stringContent;
}
// 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.FIELDALIGNMENTTYPE_Default.equals(FieldAlignmentType))
{
if (data.isNumeric())
FieldAlignmentType = MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight;
else
FieldAlignmentType = MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft;
}
// Get Color/ Font
Color color = getColor(); // default
if (ID != null && !isForm)
; // link color/underline handeled in PrintElement classes
else if (item.getAD_PrintColor_ID() != 0 && m_printColor.get_ID() != item.getAD_PrintColor_ID())
{
MPrintColor c = MPrintColor.get (getCtx(), 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.get_ID() != item.getAD_PrintFont_ID())
{
MPrintFont f = MPrintFont.get (item.getAD_PrintFont_ID());
if (f.getFont() != null)
font = f.getFont();
}
// Create String, HTML or Location
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
{
if (HTMLElement.isHTML(stringContent))
e = new HTMLElement(stringContent);
else
e = new StringElement(content, font, color, isForm ? null : ID, label, labelSuffix);
e.layout (maxWidth, item.getMaxHeight(), item.isHeightOneLine(), FieldAlignmentType);
}
return e;
} // createFieldElement
/**
* Create Box/Line Element
* @param item item
* @return box element
*/
private PrintElement createBoxElement (MPrintFormatItem item)
{
Color color = getColor(); // default
if (item.getAD_PrintColor_ID() != 0
&& m_printColor.get_ID() != item.getAD_PrintColor_ID())
{
MPrintColor c = MPrintColor.get (getCtx(), item.getAD_PrintColor_ID());
if (c.getColor() != null)
color = c.getColor();
}
return new BoxElement(item, color);
} // createBoxElement
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -