package slipapp;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.swing.JOptionPane;
/**
* @author Doys
*/
public class Printing {
private XmlHandle xmlHandle;
public String primaryColumn = "";
private int columnCount = 0;
public int rowPerPage = 0;
public int rowCount = 0;
public int totalPage = 0;
public PrintingHead head = null;
public PrintingDetail detail = null;
public PrintingFoot foot = null;
public PrintingFont font;
public boolean autoPrint = false;
public boolean autoCloseDlg = false;
public String printerName = "";
public int maxPrintingHead = 20;
public int maxPrintingDetail = 50;
public int maxPrintingFoot = 20;
public XmlType xmlType = XmlType.DATAWINDOW;
public BufferedImage[] buffImage;
public double paperWidth = 0;
public double paperHeight = 0;
private String[][] detailText;
private String[] headText;
private String[] footText;
private boolean isPrint = false;
private int indexPage = 0;
private int printCurrentPage = 0;
public int orientation = PageFormat.PORTRAIT;
public double frameWidth = 0;
public double frameHeight = 0;
public Printing(XmlHandle xml) {
this.xmlHandle = xml;
this.font = new PrintingFont();
}
public void preAccept() {
//ส่วน position
this.head = new PrintingHead(this.maxPrintingHead);
this.head.initPosition();
this.foot = new PrintingFoot(this.maxPrintingFoot);
this.foot.initPosition();
this.detail = new PrintingDetail(this.maxPrintingDetail);
this.detail.initPosition();
//ส่วน xml และ row
this.xmlHandle.xmlType = this.xmlType;
this.xmlHandle.setRowCount(this.primaryColumn);
this.rowCount = xmlHandle.getRowCount();
//หาจำนวนหน้า
double dbRowCount = (double) rowCount;
double dbRowPerPage = (double) rowPerPage;
this.totalPage = (int) Math.ceil(dbRowCount / dbRowPerPage);
}
public int indexOf(String name, Position p) {
if (p == Position.HEAD) {
for (int i = 0; i < this.head.count; i++) {
if (name.equals(this.head.column[i].name)) {
return i;
}
}
} else if (p == Position.FOOT) {
for (int i = 0; i < this.foot.count; i++) {
if (name.equals(this.foot.column[i].name)) {
return i;
}
}
} else if (p == Position.DETAIL) {
for (int i = 0; i < this.detail.count; i++) {
if (name.equals(this.detail.column[i].name)) {
return i;
}
}
}
return -1;
}
public void addColumn(PrintColumn printObject) throws Exception {
try {
if (printObject.position == Position.HEAD) {
if (this.indexOf(printObject.name, Position.HEAD) >= 0) {
throw new Exception("พบ object name: " + printObject.name + " ซ้ำ(head)");
}
this.head.column[this.head.count] = printObject;
this.head.count++;
} else if (printObject.position == Position.FOOT) {
if (this.indexOf(printObject.name, Position.FOOT) >= 0) {
throw new Exception("พบ object name: " + printObject.name + " ซ้ำ(foot)");
}
this.foot.column[this.foot.count] = printObject;
this.foot.count++;
} else {
if (this.indexOf(printObject.name, Position.DETAIL) >= 0) {
throw new Exception("พบ object name: " + printObject.name + " ซ้ำ(detail)");
}
this.detail.column[this.detail.count] = printObject;
this.detail.count++;
}
this.columnCount++;
} catch (Exception exx) {
JOptionPane.showMessageDialog(null, exx.getMessage());
System.exit(0);
throw exx;
}
}
private String inputFormat(String text, Format format, PrintColumn pc) {
return inputFormat(text, format, pc.decDigit);
}
private String inputFormat(String text, Format format, int decDigit) {
try {
if (this.xmlType == XmlType.DATATABLE) {
if (format == Format.TDATE || format == Format.MTHAI_DATE || format == Format.TIME) {
//2012-06-07T00:00:00+07:00
String text2 = text.substring(0, 10);
text2 += " " + text.substring(11, 11 + 8);
text = text2;
}
}
if (format == Format.DEFAULT) {
return text;
} else if (format == Format.DEC) {
text = cnvNumberFormat(text, decDigit);
} else if (format == Format.DEC_POINT) {
text = cnvNumberWithPointFormat(text, decDigit);
} else if (format == Format.TDATE) {
text = cnvDateTh(text);
} else if (format == Format.THAIBATH) {
text = cnvNumberWord(text);
} else if (format == Format.MTHAI_DATE) { //-----dot-----ฟอร์มวันที่เดือนไทย
text = cnvDatemonthTh(text);
} else if (format == Format.AGE) { //-----dot-----คำนวนอายุจากวันเกิดที่ส่งมา
text = cnvBirthToAge(text);
} else if (format == Format.LEVEL) { //-----dot-----ถ้า level = 0 ไม่พิมพ์
text = printLevel(text);
} else if (format == Format.THAIWORD) { //-----dot-----อ่านไทยไม่มีคำว่าบาทถ้วน
text = thaiword(text);
} else if (format == Format.DEPT_ACC_MHD) { //-------dot-----รหัสบัญชี
text = depAccount(text);
} else if (format == Format.PERIOD_COUNT) { //-------dot---- จำนวนงวด
text = periodnum(text);
} else if (format == Format.STAR_DEC_FIRST) { //-----dot-----ใส่ดาวหน้าจำนวนเงิน
text = cnvMoneyAddStar(text);
} else if (format == Format.TIME) { //-----dot-----รับเฉพาะค่าเวลา
text = cnvtime(text);
} else if (format == Format.LOANCONTRACT) {
text = loancontract(text);
} else if (format == Format.STAR_NEW_DEC_FIRST) { //-----dot-----ใส่ดาวหน้าจำนวนเงิน
text = cnvMoneyAddStarNew(text);
} else if (format == Format.PLUS_DEC_FIRST) { //-----dot-----ใส่บวกหน้าจำนวนเงิน
text = cnvMoneyAddPlus(text);
} else if (format == Format.MINUS_DEC_FIRST) { //-----dot-----ใส่ลบหน้าจำนวนเงิน
text = cnvMoneyAddMinus(text);
}
return text;
} catch (Exception ex) {
return text;
}
}
public String cnvNumberFormat(String text, int decDigit) {
text = text.trim();
if (text.equals("") || text == null || text.equals("+")) {
return "";
}
int numFixed = decDigit;
double num = Double.parseDouble(text);
DecimalFormat dc = new DecimalFormat();
String digitFormat = decDigit > 0 ? "." : "";
for (int i = 0; i < decDigit; i++) {
digitFormat += "0";
}
dc.applyPattern("#,##0" + digitFormat);
String result = dc.format(num);
return result;
}
public String cnvNumberWithPointFormat(String text, int decDigit) {
text = text.trim();
if (text.equals("") || text == null || text.equals("+")) {
return "";
}
int numFixed = decDigit;
double num = Double.parseDouble(text);
DecimalFormat dc = new DecimalFormat();
String digitFormat = decDigit > 0 ? "." : "";
for (int i = 0; i < decDigit; i++) {
digitFormat += "0";
}
dc.applyPattern("#,##0.00" + digitFormat);
String result = dc.format(num);
return result;
}
public String cnvDateTh(String text) {
try {
if (text.length() < 18 && text.length() > 25) {
return "00/00/0000";
}
Locale lc = new Locale("en", "US");
SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", lc);
Date date2 = d1.parse(text);
SimpleDateFormat d2 = new SimpleDateFormat("dd/MM/yyyy", new Locale("th", "TH"));
return d2.format(date2);
} catch (Exception ex) {
return "00/00/0000";
}
}
public String cnvDatemonthTh(String text) {
try {
if (text.length() < 18 && text.length() > 25) {
return "00/00/0000";
}
Locale lc = new Locale("en", "US");
SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", lc);
Date date2 = d1.parse(text);
SimpleDateFormat d2 = new SimpleDateFormat("dd MMMM yyyy", new Locale("th", "TH"));
return d2.format(date2);
} catch (Exception ex) {
return "";
}
}
public String cnvtime(String text) {
try {
if (text.length() < 18 && text.length() > 25) {
return "00/00/0000";
}
Locale lc = new Locale("en", "US");
SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", lc);
Date date2 = d1.parse(text);
SimpleDateFormat d2 = new SimpleDateFormat("HH:mm:ss", new Locale("th", "TH"));
return d2.format(date2);
} catch (Exception ex) {
return "";
}
}
private String cnvNumberWord(String money) {
if (money == null || money.equals("")) {
return "";
}
try {
String decimalpoint = "";
if (money.indexOf(".") != -1) {
decimalpoint = money.substring(money.indexOf(".") + 1);
if (decimalpoint.length() == 1) {
decimalpoint += "0";
} else if (decimalpoint.length() > 2) {
double decimalnum = Double.parseDouble(decimalpoint.substring(0, 3));
if (decimalnum % 10 > 4) {
decimalnum = Math.floor((decimalnum + 10) / 10);
} else {
decimalnum = Math.floor(decimalnum / 10);
}
decimalpoint = decimalnum + "";
}
} else {
money = money + ".00";
cnvNumberWord(money); // -------Dot--------
}
money = money.substring(0, money.indexOf("."));
money = money.replace("/,/g", "");
String decimalword = "";
String moneyword = "บาท";
int i = decimalpoint.length() - 1;
int digitpos = 0;
if ("00".equals(decimalpoint) || "0".equals(decimalpoint) || "".equals(decimalpoint)) {
decimalword += "ถ้วน";
} else {
decimalword += "สตางค์";
while (i >= 0) {
decimalword = numToWord(decimalpoint.charAt(i) + "", digitpos, decimalpoint.length()) + getNumNaming(digitpos, decimalpoint.charAt(i) + "") + decimalword;
i--;
digitpos++;
}
}
i = money.length() - 1;
digitpos = 0;
while (i >= 0) {
moneyword = numToWord(money.charAt(i) + "", digitpos, money.length()) + getNumNaming(digitpos, money.charAt(i) + "") + moneyword;
i--;
digitpos++;
}
if ("บาท".equals(moneyword)) {
moneyword = "ศูนย์" + moneyword;
}
moneyword += decimalword;
return moneyword;
} catch (Exception ex) {
return "ศุนย์บาทถ้วน";
}
}
private String getNumNaming(int digitpos, String num) {
String numNaming = "";
if (Integer.parseInt(num) == 0) {
} else {
switch (digitpos % 6) {
case 0:
if (digitpos != 0) {
numNaming = "ล้าน";
} else {
numNaming = "";
}
break;
case 1:
numNaming = "สิบ";
;
break;
case 2:
numNaming = "ร้อย";
break;
case 3:
numNaming = "พัน";
break;
case 4:
numNaming = "หมื่น";
break;
case 5:
numNaming = "แสน";
break;
}
}
return numNaming;
}
private String numToWord(String num, int digitpos, int numlength) {
String wordnum = "";
switch (Integer.parseInt(num)) {
case 1:
if (digitpos % 6 == 1) {
wordnum = "";
} else {
if ((numlength == digitpos + 1) || digitpos != 0) {
wordnum = "หนึ่ง";
} else {
wordnum = "เอ็ด";
}
}
break;
case 2:
if (digitpos % 6 == 1) {
wordnum = "ยี่";
} else {
wordnum = "สอง";
}
break;
case 3:
wordnum = "สาม";
break;
case 4:
wordnum = "สี่";
break;
case 5:
wordnum = "ห้า";
break;
case 6:
wordnum = "หก";
break;
case 7:
wordnum = "เจ็ด";
break;
case 8:
wordnum = "แปด";
break;
case 9:
wordnum = "เก้า";
break;
case 0:
wordnum = "";
break;
}
return wordnum;
}
public String cnvBirthToAge(String birth) {
Date day = new Date();
int nowyear = day.getYear() + 1900;
if (birth == null || birth.equals("")) {
return "";
}
try {
String[] splitTime = birth.split(" ");
String[] sss = splitTime[0].split("-");
String yyyy = sss[0];
return (nowyear - Integer.parseInt(yyyy)) + "";
} catch (Exception err) {
return "agefail";
}
}
public String thaiword(String num) {
if (num == null || num.equals("")) {
return "";
}
int i = num.length() - 1;
int digitpos = 0;
String wordthai = "";
while (i >= 0) {
wordthai = numToWord(num.charAt(i) + "", digitpos, num.length()) + getNumNaming(digitpos, num.charAt(i) + "") + wordthai;
i--;
digitpos++;
}
return wordthai;
}
private String printLevel(String level) {
if (level == null || level.equals("")) {
return "";
} else if (level.trim().equals("0")) {
return "";
}
return level;
}
private String loancontract(String contr) {
try {
if (contr == null || contr.equals("")) {
return "";
}
String str1 = contr.substring(0, 8);
String str2 = contr.substring(8);
return str1 + "-" + str2;
} catch (Exception ex) {
return "";
}
}
private String depAccount(String numacc) {
if (numacc == null || numacc.equals("")) {
return "";
}
String[] numb = numacc.split("");
numacc = "";
int i = 0;
while (i <= 10) {
if (i == 3 || i == 4 || i == 10) {
numacc += "-" + numb[i];
} else {
numacc += numb[i];
}
i++;
}
return numacc;
}
private String periodnum(String peri) {
try {
if (peri == null || peri.equals("")) {
return "";
}
Date[] periday = new Date[2];
String[] splitDate = null;
if (peri.indexOf("diffdate") != -1) {
splitDate = peri.split("diffdate");
}
int i = 0;
while (i < 2) {
String text2 = splitDate[i].substring(0, 10);
text2 += " " + splitDate[0].substring(11, 11 + 8);
splitDate[i] = text2;
String[] splitTime = splitDate[i].split(" ");
String[] sss = splitTime[0].split("-");
String yyyy = sss[0];
String mm = sss[1];
String dd = sss[2];
periday[i] = new SimpleDateFormat("yyy-MM-dd", new Locale("en", "US")).parse(splitTime[0]);
//periday[i] = new Date(parseInt(yyyy) , mm - 1, dd);
i++;
}
long numday = Long.parseLong((periday[0].getTime() - periday[1].getTime()) / (24 * 3600 * 1000) + "");
return numday + "";
} catch (Exception ex) {
return "";
}
}
private String cnvMoneyAddStar(String money) {
if (money == null || money.trim().equals("")) {
return "";
}
money = money.trim();
money = money.replace(",", "");
money = money.replace("*", "");
String moneys = money;
String moneya[];
String star = "";
star = "***********************";
String result = star + cnvNumberFormat(money, 2);
result = result.substring(result.length() - 15);
return result;
}
private String cnvMoneyAddStarNew(String money) {
if (money == null || money.trim().equals("")) {
return "";
}
money = money.trim();
money = money.replace(",", "");
money = money.replace("*", "");
String moneys = money;
String star = "";
star = "**************************";
String result = star + cnvNumberFormat(money, 2);
result = result.substring(result.length() - 15);
return result;
}
private String cnvMoneyAddPlus(String money) {
if (money == null || money.trim().equals("")) {
return "";
}
money = money.trim();
money = money.replace(",", "");
money = money.replace("+", "");
String moneys = money;
String moneya[];
String star = "";
star = "+++++++++++++++++++++++++";
String result = "";
double num = Double.parseDouble(money);
if(num >= 100000000){
result = star + cnvNumberFormat(money, 0);
result = result.substring(result.length() - 18);
}
else{
result = star + cnvNumberFormat(money, 2);
result = result.substring(result.length() - 18);
}
return result;
}
private String cnvMoneyAddMinus(String money) {
if (money == null || money.trim().equals("")) {
return "";
}
money = money.trim();
money = money.replace(",", "");
money = money.replace("-", "");
String moneys = money;
String moneya[];
String star = "";
star = "--------------------------";
String result = star + cnvNumberFormat(money, 2);
result = result.substring(result.length() - 16);
return result;
}
public void print(Graphics2D g2, int indexPage) throws Exception {
this.isPrint = true;
this.indexPage = indexPage;
detailText = new String[this.rowCount][this.detail.count];
headText = new String[this.head.count];
footText = new String[this.foot.count];
for (int i = 0; i < this.totalPage; i++) {
//g2.setColor(Color.WHITE);
//g2.fillRect(0, 0, buffImage[i].getWidth(), buffImage[i].getHeight());
this.printCurrentPage = i;
//ส่วน Head
this.preWriteHead(g2, i + 1);
//ส่วน Detail
this.preWriteDetail(g2, i + 1);
//ส่วน Foot
this.preWriteFoot(g2, i + 1);
}
}
public BufferedImage[] write() throws Exception {
detailText = new String[this.rowCount][this.detail.count];
headText = new String[this.head.count];
footText = new String[this.foot.count];
buffImage = new BufferedImage[totalPage];
for (int i = 0; i < this.totalPage; i++) {
buffImage[i] = new BufferedImage((int) this.frameWidth, (int) this.frameHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = buffImage[i].createGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, buffImage[i].getWidth(), buffImage[i].getHeight());
//ส่วน Head
this.preWriteHead(g2, i + 1);
//ส่วน Detail
this.preWriteDetail(g2, i + 1);
//ส่วน Foot
this.preWriteFoot(g2, i + 1);
}
return buffImage;
}
private void preWriteHead(Graphics2D g2, int currentPage) throws Exception {
for (int j = 0; j < this.head.count; j++) {
//ประกาศตัวแปร PrintColumn
PrintColumn pc = this.head.column[j];
//ประกาศตัวแปร columnType
ColumnType columnType = pc.columnType;
//ประกาศ topNew ดึงค่ามาตรงๆ
float topNew = pc.top;
String v = "";
if (columnType == ColumnType.DATA || columnType == ColumnType.TEXT || columnType == ColumnType.COMPUTE) {
//ดึงข้อมูลจาก xml ถ้าดึงค่าไม่ได้ให้ break
try {
if (columnType == ColumnType.DATA || columnType == ColumnType.COMPUTE) {
if (columnType == ColumnType.COMPUTE) {
v = this.deFunction(pc.column, 0, columnType, pc.position);
} else {
v = xmlHandle.getItemString(0, pc.column);// "";//defunction(pc.column, 0, columnType, xmlHandle, this.pd, pc.position, this.xml_type);
}
} else if (columnType == ColumnType.TEXT) {
v = pc.text;
}
} catch (Exception ex) {
v = "";
}
pc.text = v;
//this.pd.addHeadData(pc.name, v);
}
headText[j] = pc.text;
//ใช้คำสั่ง postWrite
this.postWrite(g2, pc, topNew, v);
}
}
private void preWriteDetail(Graphics2D g2, int currentPage) {
for (int i = 0; i < this.rowCount; i++) {
for (int j = 0; j < this.detail.count; j++) {
//ประกาศตัวแปร PrintColumn ดึงจาก loop
PrintColumn pc = this.detail.column[j];
//ประกาศตัวแปร columnType
ColumnType columnType = pc.columnType;
//หาค่า valid เพื่อตัดสินใจจะโชว์แถวในแต่ละหน้า
boolean validRow = false;
double rowpp = (double) this.rowPerPage;
double ii = (double) i;
double loopCurrentPage = Math.ceil((ii + 1) / rowpp);
boolean validPage = loopCurrentPage == currentPage;
//หาค่า topNew เพื่อใส่แกน Y
int modPage = i % this.rowPerPage;
float topNew = (modPage * this.detail.height) + this.head.height;
topNew = topNew + pc.top;
//หาค่า v เพื่อนำไปแสดงหน้าใบเสร็จ ตามประเภท columnType
String v = "";
try {
if (columnType == ColumnType.DATA || columnType == ColumnType.COMPUTE) {
//หาค่าจาก xml
if (columnType == ColumnType.COMPUTE) {
v = this.deFunction(pc.column, i, columnType, pc.position);
} else {
v = xmlHandle.getItemString(i, pc.column);// defunction(pc.column, i, columnType, xmlHandle, this.pd, pc.position, this.xml_type);
}
} else if (columnType == ColumnType.TEXT) {
//ดึงจาก text
v = pc.text;
} else if (columnType == ColumnType.RUNNING) {
//หาค่า running
v = (i + 1) + "";
}
} catch (Exception ex) {
v = "";
}
//เซ็ตค่าใส่ text
pc.text = v;
detailText[i][j] = pc.text;
//this.pd.addData(i, pc.name, v);
if (validPage) {
//ใช้คำสั่ง postWrite
this.postWrite(g2, pc, topNew, v);
}
}
}
}
private void preWriteFoot(Graphics2D g2, int currentPage) {
for (int j = 0; j < this.foot.count; j++) {
//ประกาศตัวแปร Print Column
PrintColumn pc = this.foot.column[j];
//ดึงค่า columnType
ColumnType columnType = pc.columnType;
String v = "";
try {
if (columnType == ColumnType.DATA || columnType == ColumnType.COMPUTE) {
if (columnType == ColumnType.COMPUTE) {
v = this.deFunction(pc.column, this.rowCount - 1, columnType, Position.FOOT);
} else {
v = this.xmlHandle.getItemString(this.rowCount - 1, pc.column);// //defunction(pc.column, this.rowCount - 1, columnType, xmlHandle, this.pd, pc.position, this.xml_type);
}
} else if (columnType == ColumnType.TEXT) {
v = pc.text;
} else if (columnType == ColumnType.SUM) {
double vs = 0;
int sumCount = this.rowCount;
if (this.totalPage > currentPage) {
sumCount = currentPage * this.rowPerPage;
}
for (int i = 0; i < sumCount; i++) {
double vv = 0;
try {
// if (this.xml_type == "datatable") {
// vv = this.xmlHandle.xmlDoc.getElementsByTagName(pc.column.toUpperCase())[i].childNodes[0].nodeValue + "";
// } else {
// vv = this.xmlHandle.xmlDoc.getElementsByTagName(pc.column.toLowerCase())[i].childNodes[0].nodeValue + "";
// }
vv = this.xmlHandle.getItemDouble(i, pc.column);
} catch (Exception ex) {
}
vs += vv;
}
v = vs + "";
} else if (columnType == ColumnType.SUMC) {
double vs = 0;
int sumCount = this.rowCount;
if (this.totalPage > currentPage) {
sumCount = currentPage * this.rowPerPage;
}
for (int i = 0; i < sumCount; i++) {
double vv = 0;
try {
vv = Double.parseDouble(this.getColumnText(pc.column, i, Position.DETAIL));// parseFloat(this.pd.getData(i, pc.column) + "");
} catch (Exception ex) {
}
vs += vv;
}
v = vs + "";
} else if (columnType == ColumnType.COUNT) {
int sumCount = this.rowCount;
if (this.totalPage > currentPage) {
sumCount = currentPage * this.rowPerPage;
}
v = sumCount + "";
//alert(pc.name);
//alert(v);
}
} catch (Exception exx) {
v = "";
}
pc.text = v;
this.footText[j] = pc.text;
float topNew = pc.top;
//ใช้คำสั่ง postWrite
this.postWrite(g2, pc, topNew, v);
}
}
private void writeString(Graphics2D g2, String text, PrintColumn pc, float top) {
Font nowFont = g2.getFont();
FontMetrics fm = g2.getFontMetrics(nowFont);
float fHeight = pc.height > 0 ? pc.height : (float) fm.getStringBounds(text, g2).getHeight();
float newX = 0;
float newY = 0;
if (pc.align == Align.LEFT) {
newX = pc.left;
newY = top + fHeight;
} else {
float fWidth = fm.stringWidth(text);
float start = pc.align == Align.RIGHT ? pc.width - fWidth : pc.width / 2 - fWidth / 2;
newX = start + pc.left;
newY = top + fHeight;
}
//ค่อยกลับมาทำทีหลัง
if (pc.height > fHeight && pc.valign == Valign.MIDDLE) {
} else if (pc.height > fHeight && pc.valign == Valign.BOTTOM) {
}
g2.drawString(text, newX, newY);
}
private void postWrite(Graphics2D g2, PrintColumn pc, float topNew, String v) {
if (!pc.visible) {
return;
}
if (isPrint) {
if (indexPage != printCurrentPage) {
return;
}
}
// if (pc.font.u) v = "" + v + "";
// if (pc.font.s) v = "" + v + "";
if (pc.columnType == ColumnType.LINE_LAND) {
g2.setColor(pc.lineColor);
g2.drawLine((int) pc.left, (int) topNew, (int) (pc.width + pc.left), (int) topNew);
} else if (pc.columnType == ColumnType.LINE_PORT) {
g2.setColor(pc.lineColor);
g2.drawLine((int) pc.left, (int) topNew, (int) pc.left, (int) (topNew + pc.height));
} else {
if (pc.format == Format.HIDE_IF_EMPTY && v.equals("")) {
return;
}
v = inputFormat(v, pc.format, pc);
String fontName = !pc.font.name.equals("") ? pc.font.name : this.font.name;
int fontSize = pc.font.size > 0 ? pc.font.size : this.font.size;
if (pc.bgColor != null) {
float newHeight = pc.height > 0 ? pc.height : 12;
g2.setColor(pc.bgColor);
g2.fillRect((int) pc.left, (int) topNew, (int) pc.width, (int) newHeight);
}
int iii = Font.TRUETYPE_FONT;
if (pc.font.b && pc.font.i) {
iii = Font.BOLD | Font.ITALIC;
} else if (pc.font.b) {
iii = Font.BOLD;
} else if (pc.font.i) {
iii = Font.ITALIC;
}
Font font1 = new Font(fontName, iii, fontSize);
g2.setColor(pc.font.color);
g2.setFont(font1);
writeString(g2, v, pc, topNew);
}
}
private String getColumnText(String column, int row, Position position) {
try {
String result = "";
if (position == Position.DETAIL) {
result = detailText[row][this.indexOf(column, position)];
} else if (position == Position.HEAD) {
result = headText[this.indexOf(column, position)];
} else if (position == Position.HEAD) {
result = footText[this.indexOf(column, position)];
}
return result != null ? result : "";
} catch (Exception ex) {
return "";
}
}
private String deFunction(String str, int row, ColumnType columnType, Position position) {
//String regex1 = "/#x\\{|#c\\{/i";
while (str.indexOf("#x{") >= 0 || str.indexOf("#c{") >= 0) {
int ix = str.indexOf("#x{");
int ic = str.indexOf("#c{");
int index1 = -1;
if (ix >= 0 && ic >= 0) {
index1 = ix > ic ? ic : ix;
} else {
index1 = ix >= 0 ? ix : ic;
}
String func = str.substring(index1, 1 + str.indexOf("}")).trim();// , str.search(regex1)));
String ss1 = str.substring(0, index1);
String ss2 = str.substring(str.indexOf("}") + 1);
String sss = getFunction(func, row, columnType, position);
str = ss1 + sss + ss2;
//str = str.replace(func, defunction2(func, index, columnType, xmlHandle, pd, position, xml_type));
}
return str;
}
private String getFunction(String func, int row, ColumnType columnType, Position position) {
String xoc = func.substring(1, 2).toLowerCase();
String pure = func.substring(3, func.indexOf("}"));
pure = pure.replace(" ", "");
String sFormat = "";
if (pure.indexOf(":") >= 0) {
String[] sp = pure.split(":");
pure = sp[0];
sFormat = sp[1];
}
Format format = getCompleteFormat(sFormat);
int digit = 0;
if (format == Format.DEC) {
digit = Integer.parseInt(sFormat.substring(3));
}
String result = doysRegex(pure, xoc, format, digit, row, columnType, position);
return result;
}
private Format getCompleteFormat(String format) {
try {
format = format.toUpperCase();
if ("".equals(format)) {
return Format.DEFAULT;
} else if (format.indexOf("DEC") == 0) {
return Format.DEC;
}
return Format.valueOf(format);
} catch (Exception ex) {
return Format.DEFAULT;
}
}
public String doysRegex(String text, String xoc, Format format, int decDigit, int row, ColumnType columnType, Position position) {
text = text.replace(" ", "");
//System.out.println("Text is : " + text);
Pattern patIsAloan = Pattern.compile("[/+/-/*///(/)]");
Matcher matIsAloan = patIsAloan.matcher(text);
if (!matIsAloan.find()) {
//System.out.println("Not match");
String result = "";
if ("c".equals(xoc)) {
result = this.getColumnText(text, row, position);
} else {
result = this.xmlHandle.getItemStringTryCatch(row, text);
}
result = inputFormat(result, format, decDigit);
return result;
}
Pattern p = Pattern.compile("\\w*");
//System.out.println("========================");
Matcher matcher = p.matcher(text);
int[] inStart = new int[50];
int[] inEnd = new int[50];
String[] inName = new String[50];
int ii = 0;
while (matcher.find()) {
String grup = matcher.group().trim();
if (!grup.equals("")) {
String fText = grup.charAt(0) + "";
Matcher mat = Pattern.compile("[a-zA-Z]").matcher(fText);
if (mat.matches()) {
inStart[ii] = matcher.start();
inEnd[ii] = matcher.end();
inName[ii] = grup;
ii++;
//System.out.println(ii + ". Starting & ending index of " + matcher.group() + " **** " + "start=" + matcher.start() + " end = " + matcher.end());
}
}
}
//System.out.println("========================");
for (int i = ii; i > 0; i--) {
//String nv = i + ".n1";
//System.out.println(i + ". backword " + inName[i - 1]);
String ss1 = text.substring(0, inStart[i - 1]);
String ss2 = text.substring(inEnd[i - 1]);
String sss = xoc.equals("c") ? this.getColumnText(inName[i - 1], row, position) : this.xmlHandle.getItemStringTryCatch(row, inName[i - 1]);
text = ss1 + sss + ss2;
}
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
try {
text = engine.eval(text).toString();
} catch (Exception ex) {
}
text = inputFormat(text, format, decDigit);
//System.out.println("Result is : " + text);
return text;
}
}