📄 stringelement.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.font.*;
import java.awt.geom.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
import org.compiere.print.*;
import org.compiere.util.*;
import org.compiere.model.*;
/**
* String Print ELement.
* The input can be multiple lines. The first tab is expabded.
*
* @author Jorg Janke
* @version $Id: StringElement.java,v 1.20 2003/03/21 02:38:31 jjanke Exp $
*/
public class StringElement extends PrintElement
{
/**
* Standard Field Constructor.
* Created in LayoutEngine
* @param inText text
* @param font font
* @param paint paint
* @param ID optional ID (null if document)
* @param translateText if true, check for optional text translation
*/
public StringElement (String inText, Font font, Paint paint, NamePair ID, boolean translateText)
{
Log.trace(Log.l6_Database, "StringElement", "Text=" + inText + ", ID=" + ID + ", Translate=" + translateText);
if (translateText)
{
int count = Util.getCount(inText, '@');
if (count > 0 && count % 2 == 0)
{
m_originalString = inText;
m_originalFont = font;
m_originalPaint = paint;
// Translate it to get rough space (not correct context) = may be too small
inText = Msg.parseTranslation(Env.getCtx(), m_originalString);
}
}
m_ID = ID;
String[] lines = Pattern.compile("$", Pattern.MULTILINE).split(inText);
m_string_paper = new AttributedString[lines.length];
m_string_view = new AttributedString[lines.length];
for (int i = 0; i < lines.length; i++)
{
String line = Util.removeCRLF (lines[i]);
m_string_paper[i] = new AttributedString(line);
if (line.length() == 0)
continue;
Log.trace(9, "Line " + i, line);
m_string_paper[i].addAttribute(TextAttribute.FONT, font);
m_string_paper[i].addAttribute(TextAttribute.FOREGROUND, paint);
if (m_ID != null && i == 0) // first line only - create special Attributed String
{
m_string_view[i] = new AttributedString(line);
m_string_view[i].addAttribute(TextAttribute.FONT, font);
int endIndex = line.length();
m_string_view[i].addAttribute(TextAttribute.FOREGROUND, LINK_COLOR);
m_string_view[i].addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL, 0, endIndex);
}
else
m_string_view[i] = m_string_paper[i];
}
// Load Image
waitForLoad(LayoutEngine.IMAGE_TRUE);
waitForLoad(LayoutEngine.IMAGE_FALSE);
} // StringElement
/**
* Attributed String Constructor
* @param string attributed string
*/
public StringElement(AttributedString string)
{
m_string_paper = new AttributedString[] {string};
m_string_view = m_string_paper;
} // StringElement
/**
* Field Constructor.
* Created in LayoutEngine
* @param content text or boolean
* @param font font
* @param paint paint
* @param ID optional ID (null if document)
* @param label optional label
* @param labelSuffix optional label suffix
*/
public StringElement (Object content, Font font, Paint paint, NamePair ID, String label, String labelSuffix)
{
Log.trace(Log.l6_Database, "StringElement", "Label=" + label + "|" + labelSuffix
+ ", Content=" + content + ", ID=" + ID);
int startIndex = 0;
int endOffset = 0;
StringBuffer text = new StringBuffer();
if (label != null && label.length() > 0)
{
text.append(label).append(" ");
startIndex = label.length() + 1;
}
if (content instanceof Boolean)
m_check = (Boolean)content;
else
text.append(content);
if (labelSuffix != null && labelSuffix.length() > 0)
{
text.append(labelSuffix);
endOffset = labelSuffix.length();
}
m_ID = ID;
String[] lines = Pattern.compile("$", Pattern.MULTILINE).split(text);
m_string_paper = new AttributedString[lines.length];
m_string_view = new AttributedString[lines.length];
for (int i = 0; i < lines.length; i++)
{
String line = Util.removeCRLF (lines[i]);
m_string_paper[i] = new AttributedString(line);
if (line.length() == 0)
continue;
Log.trace(9, "Line " + i, line);
m_string_paper[i].addAttribute(TextAttribute.FONT, font);
m_string_paper[i].addAttribute(TextAttribute.FOREGROUND, paint);
if (m_ID != null && i == 0) // first line only - create special Attributed String
{
m_string_view[i] = new AttributedString(line);
m_string_view[i].addAttribute(TextAttribute.FONT, font);
int endIndex = line.length() - endOffset;
if (endIndex > startIndex)
{
m_string_view[i].addAttribute (TextAttribute.FOREGROUND, LINK_COLOR, startIndex, endIndex);
m_string_view[i].addAttribute (TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL, startIndex, endIndex);
}
}
else
m_string_view[i] = m_string_paper[i];
}
} // StringElement
/** Actual Elements */
private AttributedString[] m_string_view = null;
private AttributedString[] m_string_paper = null;
/** To be translated String */
private String m_originalString = null;
private Font m_originalFont = null;
private Paint m_originalPaint = null;
/** Optional ID of String */
private NamePair m_ID = null;
/** Optional CheckBox */
private Boolean m_check = null;
/**
* Get optional ID
* @return ID or null
*/
public NamePair getID()
{
return m_ID;
} // getID
/**
* Translate Context if required
* If content is translated, the element needs to stay in the bounds
* of the originally calculated size and need to align the field.
* @param ctx context
*/
public void translate(Properties ctx)
{
if (m_originalString == null)
return;
String inText = Msg.parseTranslation(ctx, m_originalString);
// Log.trace(Log.l6_Database, "StringElement.translate", inText);
String[] lines = Pattern.compile("$", Pattern.MULTILINE).split(inText);
m_string_paper = new AttributedString[lines.length];
for (int i = 0; i < lines.length; i++)
{
String line = Util.removeCRLF (lines[i]);
m_string_paper[i] = new AttributedString(line);
if (line.length() > 0)
{
m_string_paper[i].addAttribute(TextAttribute.FONT, m_originalFont);
m_string_paper[i].addAttribute(TextAttribute.FOREGROUND, m_originalPaint);
}
}
m_string_view = m_string_paper;
} // translate
/*************************************************************************/
/**
* Layout and Calculate Size.
* Set p_width & p_height
* @return Size
*/
protected boolean calculateSize()
{
if (p_sizeCalculated)
return true;
//
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout layout = null;
p_height = 0f;
p_width = 0f;
// No Limit
if (p_maxWidth == 0f && p_maxHeight == 0f)
{
for (int i = 0; i < m_string_paper.length; i++)
{
AttributedCharacterIterator iter = m_string_paper[i].getIterator();
if (iter.getBeginIndex() == iter.getEndIndex())
continue;
// Check for Tab (just first)
int tabPos = -1;
for (char c = iter.first(); c != iter.DONE && tabPos == -1; c = iter.next())
{
if (c == '\t')
tabPos = iter.getIndex();
}
if (tabPos == -1)
{
layout = new TextLayout (iter, frc);
p_height += layout.getAscent() + layout.getDescent() + layout.getLeading();
if (p_width < layout.getAdvance())
p_width = layout.getAdvance();
}
else // with tab
{
LineBreakMeasurer measurer = new LineBreakMeasurer(iter, frc);
layout = measurer.nextLayout(9999, tabPos, false);
p_height += layout.getAscent() + layout.getDescent() + layout.getLeading();
float width = getTabPos (0, layout.getAdvance());
layout = measurer.nextLayout(9999, iter.getEndIndex(), true);
width += layout.getAdvance();
if (p_width < width)
p_width = width;
}
} // for all strings
// Add CheckBox Size
if (m_check != null)
{
p_width += LayoutEngine.IMAGE_SIZE.width;
if (p_height < LayoutEngine.IMAGE_SIZE.height)
p_height = LayoutEngine.IMAGE_SIZE.height;
}
}
// Size Limits
else
{
p_width = p_maxWidth;
for (int i = 0; i < m_string_paper.length; i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -