1 14 package org.compiere.print.layout; 15 16 import java.awt.*; 17 import java.awt.print.*; 18 import java.awt.geom.*; 19 import java.util.*; 20 import java.sql.*; 21 import java.net.*; 22 23 import javax.print.*; 24 import javax.print.attribute.*; 25 import javax.print.attribute.standard.*; 26 27 import org.apache.log4j.Logger; 28 29 import org.compiere.model.*; 30 import org.compiere.print.*; 31 import org.compiere.util.DisplayType; 32 import org.compiere.util.NamePair; 33 import org.compiere.util.KeyNamePair; 34 import org.compiere.util.ValueNamePair; 35 import org.compiere.util.Env; 36 import org.compiere.util.Msg; 37 import org.compiere.util.Log; 38 39 47 public class LayoutEngine 48 { 49 55 public LayoutEngine (MPrintFormat format, PrintData data, MQuery query) 56 { 57 log.info(format + " - " + data + " - " + query); 58 setPrintFormat(format, false); 59 setPrintData(data, query, false); 60 layout(); 61 } 63 64 65 66 67 private Logger log = Logger.getLogger (getClass()); 68 69 private boolean m_hasLayout = false; 70 71 private MPrintFormat m_format; 72 73 private Properties m_printCtx; 74 75 private PrintData m_data; 76 77 private MQuery m_query; 78 79 private MPrintColor m_printColor; 80 81 private MPrintFont m_printFont; 82 83 private int m_columnCount = -1; 84 85 86 87 private CPaper m_paper; 88 89 private int m_headerHeight = 18; 91 private int m_footerHeight = 18; 92 93 94 95 private int m_pageNo = 0; 96 97 private Page m_currPage; 98 99 private ArrayList m_pages = new ArrayList(); 100 101 private HeaderFooter m_headerFooter; 102 103 104 105 private Rectangle m_header = new Rectangle (); 106 107 private Rectangle m_content = new Rectangle(); 108 109 private Rectangle m_footer = new Rectangle(); 110 111 private int m_tempNLPositon = 0; 112 113 114 public static final int AREA_HEADER = 0; 115 116 public static final int AREA_CONTENT = 1; 117 118 public static final int AREA_FOOTER = 2; 119 120 private int m_area = AREA_CONTENT; 121 122 123 private Point2D.Double[] m_position = new Point2D.Double[] {new Point2D.Double(0,0), new Point2D.Double(0,0), new Point2D.Double(0,0)}; 124 125 private float m_maxHeightSinceNewLine[] = new float[] {0f, 0f, 0f}; 126 127 private Font m_font; 128 129 private Paint m_paint; 130 131 private Stroke m_stroke; 132 133 134 private TableElement m_tableElement = null; 135 136 137 138 public static Image IMAGE_TRUE = null; 139 public static Image IMAGE_FALSE = null; 140 public static Dimension IMAGE_SIZE = new Dimension(10,10); 141 142 static { 143 Toolkit tk = Toolkit.getDefaultToolkit(); 144 URL url = LayoutEngine.class.getResource("true10.gif"); 145 if (url != null) 146 IMAGE_TRUE = tk.getImage(url); 147 url = LayoutEngine.class.getResource("false10.gif"); 148 149 if (url != null) 150 IMAGE_FALSE = tk.getImage(url); 151 } 153 154 155 161 public void setPrintFormat (MPrintFormat format, boolean doLayout) 162 { 163 m_format = format; 164 m_printCtx = new Properties(format.getCtx()); 166 167 boolean tempHasLayout = m_hasLayout; 169 m_hasLayout = false; MPrintPaper mPaper = MPrintPaper.get(format.getAD_PrintPaper_ID()); 171 if (m_format.isStandardHeaderFooter()) 172 setPaper(mPaper.getCPaper()); 173 else 174 setPaper(mPaper.getCPaper(), 175 m_format.getHeaderMargin(), m_format.getFooterMargin()); 176 m_hasLayout = tempHasLayout; 177 m_printColor = MPrintColor.get(format.getAD_PrintColor_ID()); 179 setPaint(m_printColor.getColor()); 180 m_printFont = MPrintFont.get (format.getAD_PrintFont_ID()); 181 setFont(m_printFont.getFont()); 182 m_stroke = new BasicStroke (1.0f); 183 184 Env.setContext(m_printCtx, Page.CONTEXT_REPORTNAME, m_format.getName()); 186 Env.setContext(m_printCtx, Page.CONTEXT_HEADER, Env.getHeader(m_printCtx, 0)); 187 Env.setContext(m_printCtx, Env.LANG, m_format.getLanguage().getAD_Language()); 188 189 if (m_hasLayout && doLayout) 190 layout(); } 193 200 public void setPrintData (PrintData data, MQuery query, boolean doLayout) 201 { 202 m_data = data; 203 m_query = query; 204 if (m_hasLayout && doLayout) 205 layout(); } 208 209 210 214 public void setPaper (CPaper paper) 215 { 216 setPaper(paper, m_headerHeight, m_footerHeight); 217 } 219 226 public void setPaper (CPaper paper, int headerHeight, int footerHeight) 227 { 228 if (paper == null) 229 return; 230 boolean paperChange = headerHeight != m_headerHeight || footerHeight != m_footerHeight; 232 if (!paperChange) 233 paperChange = !paper.equals(m_paper); 234 log.debug("setPaper " + paper + " - Header=" + headerHeight + ", Footer=" + footerHeight); 236 m_paper = paper; 237 m_headerHeight = headerHeight; 238 m_footerHeight = footerHeight; 239 calculatePageSize(); 240 if (m_hasLayout && paperChange) 242 layout(); } 245 250 public void pageSetupDialog (PrinterJob job) 251 { 252 log.info("pageSetupDialog"); 253 if (m_paper.pageSetupDialog(job)) 254 { 255 setPaper(m_paper); 256 layout(); 257 } 258 } 260 265 protected void setPageFormat (PageFormat pf) 266 { 267 if (pf != null) 268 setPaper(new CPaper(pf)); 269 else 270 setPaper(null); 271 } 273 277 public PageFormat getPageFormat () 278 { 279 return m_paper.getPageFormat(); 280 } 282 302 private void calculatePageSize() 303 { 304 int x = (int)m_paper.getImageableX (true); 305 int w = (int)m_paper.getImageableWidth (true); 306 int y = (int)m_paper.getImageableY (true); 308 int h = (int)m_paper.getImageableHeight (true); 309 310 int height = m_headerHeight; 311 m_header.setBounds (x, y, w, height); 312 y += height; 314 height = h-m_headerHeight-m_footerHeight; 315 m_content.setBounds (x, y, w, height); 316 y += height; 318 height = m_footerHeight; 319 m_footer.setBounds (x, y, w, height); 320 321 log.debug ("calulatePaperSize - Paper=" + m_paper + ",HeaderHeight=" + m_headerHeight + ",FooterHeight=" + m_footerHeight 322 + " => Header=" + m_header + ",Contents=" + m_content + ",Footer=" + m_footer); 323 } 325 329 public CPaper getPaper() 330 { 331 return m_paper; 332 } 334 335 336 339 private void layout() 340 { 341 log.debug("layout"); 342 343 m_headerFooter = new HeaderFooter(m_printCtx); 345 if (!m_format.isForm() && m_format.isStandardHeaderFooter()) 346 createStandardHeaderFooter(); 347 m_pageNo = 0; 349 m_pages.clear(); 350 m_tableElement = null; 351 newPage(); 352 if (m_format.isForm()) 354 layoutForm(); 355 else 356 { 357 PrintElement element = layoutParameter(); 359 if (element != null) 360 { 361 m_currPage.addElement (element); 362 element.setLocation(m_position[AREA_CONTENT]); 363 m_position[AREA_CONTENT].y += element.getHeight() + 5; } 365 element = layoutTable(m_format, m_data, 0); 367 element.setLocation(m_content.getLocation()); 368 for (int p = 1; p <= element.getPageCount(); p++) 369 { 370 if (p != 1) 371 newPage(); 372 m_currPage.addElement (element); 373 } 374 } 375 String pageInfo = String.valueOf(m_pages.size()) + getPageInfo(m_pages.size()); 377 Env.setContext(m_printCtx, Page.CONTEXT_PAGECOUNT, pageInfo); 378 Timestamp now = new Timestamp(System.currentTimeMillis()); 379 Env.setContext(m_printCtx, Page.CONTEXT_DATE, 380 DisplayType.getDateFormat(DisplayType.Date).format(now)); 381 Env.setContext(m_printCtx, Page.CONTEXT_TIME, 382 DisplayType.getDateFormat(DisplayType.DateTime).format(now)); 383 int pages = m_pages.size(); 385 for (int i = 0; i < pages; i++) 386 { 387 Page page = (Page)m_pages.get(i); 388 int pageNo = page.getPageNo(); 389 pageInfo = String.valueOf(pageNo) + getPageInfo(pageNo); 390 page.setPageInfo(pageInfo); 391 page.setPageCount(pages); 392 } 393 394 395 m_hasLayout = true; 396 } 398 399 400 404 public Properties getCtx() 405 { 406 return m_printCtx; 407 } 409 413 public int getColumnCount() 414 { 415 return m_columnCount; 416 } 418 422 protected void setArea (int area) 423 { 424 if (m_area == area) 425 return; 426 if (area < 0 || area > 2) 427 throw new ArrayIndexOutOfBoundsException (area); 428 m_area = area; 429 } 431 435 public int getArea () 436 { 437 return m_area; 438 } 440 444 public Rectangle getAreaBounds() 445 { 446 Rectangle part = m_content; 447 if (m_area == AREA_HEADER) 448 part = m_header; 449 else if (m_area == AREA_FOOTER) 450 part = m_footer; 451 return part; 453 } 455 456 457 461 protected int newPage() 462 { 463 m_pageNo++; 464 m_currPage = new Page (m_printCtx, m_pageNo); 465 m_pages.add(m_currPage); 466 m_position[AREA_HEADER].setLocation(m_header.x, m_header.y); 468 m_position[AREA_CONTENT].setLocation(m_content.x, m_content.y); 469 m_position[AREA_FOOTER].setLocation(m_footer.x, m_footer.y); 470 m_maxHeightSinceNewLine = new float[] {0f, 0f, 0f};; 471 log.debug("newPage - Page=" + m_pageNo); 472 return m_pageNo; 473 } 475 478 protected void newLine () 479 { 480 Rectangle part = m_content; 481 if (m_area == AREA_HEADER) 482 part = m_header; 483 else if (m_area == AREA_FOOTER) 484 part = m_footer; 485 486 int xPos = part.x; 488 if (m_tempNLPositon != 0) 489 xPos = m_tempNLPositon; 490 491 if (isYspaceFor(m_maxHeightSinceNewLine[m_area])) 492 { 493 m_position[m_area].setLocation(xPos, m_position[m_area].y + m_maxHeightSinceNewLine[m_area]); 494 if (Log.isTraceLevel(10)) 495 log.debug("newLine - Page=" + m_pageNo + " [" + m_area + "] " + m_position[m_area].x + "/" + m_position[m_area].y); 496 } 497 else if (m_area == AREA_CONTENT) 498 newPage(); 499 else { 501 m_position[m_area].setLocation(part.x, m_position[m_area].y + m_maxHeightSinceNewLine[m_area]); 502 log.error("newLine - Outside of Area(" + m_area + "): " + m_position[m_area]); 503 } 504 m_maxHeightSinceNewLine[m_area] = 0f; 505 } 507 508 512 public int getPageNo() 513 { 514 return m_pageNo; 515 } 517 522 public Page getPage (int pageNo) 523 { 524 if (pageNo <= 0 || pageNo > m_pages.size()) 525 { 526 log.error("getPage - No page #" + pageNo); 527 return null; 528 } 529 Page retValue = (Page)m_pages.get(pageNo-1); 530 return retValue; 531 } 533 537 public int getPageCount () 538 { 539 return m_pages.size(); 540 } 542 543 547 public ArrayList getPages() 548 { 549 return m_pages; 550 } 552 556 public HeaderFooter getHeaderFooter() 557 { 558 return m_headerFooter; 559 } 561 565 protected void setPage (int pageNo) 566 { 567 if (pageNo <= 0 || pageNo > m_pages.size()) 568 { 569 log.error("setPage - No page #" + pageNo); 570 return; 571 } 572 Page retValue = (Page)m_pages.get(pageNo-1); 573 m_currPage = retValue; 574 } 576 581 public String getPageInfo(int pageNo) 582 { 583 if (m_tableElement == null || m_tableElement.getPageXCount() == 1) 584 return ""; 585 int pi = m_tableElement.getPageIndex(pageNo); 586 StringBuffer sb = new StringBuffer ("("); 587 sb.append(m_tableElement.getPageYIndex(pi)+1).append(",") 588 .append(m_tableElement.getPageXIndex(pi)+1).append(")"); 589 return sb.toString(); 590 } 592 596 public String getPageInfoMax() 597 { 598 if (m_tableElement == null || m_tableElement.getPageXCount() == 1) 599 return ""; 600 StringBuffer sb = new StringBuffer ("("); 601 sb.append(m_tableElement.getPageYCount()).append(",") 602 .append(m_tableElement.getPageXCount()).append(")"); 603 return sb.toString(); 604 } 606 607 608 612 protected void setRelativePosition (Point2D p) 613 { 614 if (p == null) 615 return; 616 Rectangle part = m_content; 617 if (m_area == AREA_HEADER) 618 part = m_header; 619 else if (m_area == AREA_FOOTER) 620 part = m_footer; 621 m_position[m_area].setLocation(part.x + p.getX(), part.y + p.getY()); 622 if (Log.isTraceLevel(10)) 623 log.debug("setPosition - Page=" + m_pageNo + " [" + m_area + "] " + m_position[m_area].x + "/" + m_position[m_area].y); 624 } 626 631 protected void setRelativePosition (float x, float y) 632 { 633 setRelativePosition(new Point2D.Float(x, y)); 634 } 636 640 public Point2D getPosition () 641 { 642 return m_position[m_area]; 643 } 645 649 protected void setX (float x) 650 { 651 m_position[m_area].x = x; 652 if (Log.isTraceLevel(10)) 653 log.debug("setX - Page=" + m_pageNo + " [" + m_area + "] " + m_position[m_area].x + "/" + m_position[m_area].y); 654 } 656 660 protected void addX (float xOffset) 661 { 662 if (xOffset == 0f) 663 return; 664 m_position[m_area].x += xOffset; 665 if (Log.isTraceLevel(10)) 666 log.debug("addX - Page=" + m_pageNo + " [" + m_area + "] " + m_position[m_area].x + "/" + m_position[m_area].y); 667 } 669 673 public float getX () 674 { 675 return (float)m_position[m_area].x; 676 } 678 682 protected void setY (int y) 683 { 684 m_position[m_area].y = y; 685 if (Log.isTraceLevel(10)) 686 log.debug("setY - Page=" + m_pageNo + " [" + m_area + "] " + m_position[m_area].x + "/" + m_position[m_area].y); 687 } 689 693 protected void addY (int yOffset) 694 { 695 if (yOffset == 0f) 696 return; 697 if (isYspaceFor(yOffset)) 698 { 699 m_position[m_area].y += yOffset; 700 if (Log.isTraceLevel(10)) 701 log.debug("addY - Page=" + m_pageNo + " [" + m_area + "] " + m_position[m_area].x + "/" + m_position[m_area].y); 702 } 703 else if (m_area == AREA_CONTENT) 704 newPage(); 705 else 706 { 707 m_position[m_area].y += yOffset; 708 log.error("addY - Outside of Area: " + m_position); 709 } 710 } 712 716 public float getY () 717 { 718 return (float)m_position[m_area].y; 719 } 721 722 723 727 public float getXspace() 728 { 729 Rectangle part = m_content; 730 if (m_area == AREA_HEADER) 731 part = m_header; 732 else if (m_area == AREA_FOOTER) 733 part = m_footer; 734 return (float)(part.x + part.width - m_position[m_area].x); 736 } 738 743 public boolean isXspaceFor (float width) 744 { 745 return (getXspace()-width) > 0f; 746 } 748 752 public float getYspace() 753 { 754 Rectangle part = m_content; 755 if (m_area == AREA_HEADER) 756 part = m_header; 757 else if (m_area == AREA_FOOTER) 758 part = m_footer; 759 return (float)(part.y + part.height - m_position[m_area].y); 761 } 763 768 public boolean isYspaceFor (float height) 769 { 770 return (getYspace()-height) > 0f; 771 } 773 774 775 779 protected void setFont (Font font) 780 { 781 if (font != null) 782 m_font = font; 783 } 785 789 public Font getFont() 790 { 791 return m_font; 792 } 794 798 protected void setFontName (String name) 799 { 800 m_font = new Font(name, m_font.getStyle(), m_font.getSize()); 801 } 803 807 protected void setFontStyle (int style) 808 { 809 m_font = new Font(m_font.getName(), style, m_font.getSize()); 810 } 812 816 protected void setFontSize (int size) 817 { 818 m_font = new Font(m_font.getName(), m_font.getStyle(), size); 819 } 821 822 823 827 protected void setPaint (Paint paint) 828 { 829 if (paint != null) 830 m_paint = paint; 831 } 833 837 public Paint getPaint () 838 { 839 return m_paint; 840 } 842 848 protected void setPaintAlpha (int alpha) 849 { 850 if (m_paint instanceof Color) 851 { 852 Color c = (Color)m_paint; 853 m_paint = new Color (c.getRed(), c.getGreen(), c.getBlue(), alpha); 854 } 855 } 857 858 859 863 protected void setStroke (Stroke stroke) 864 { 865 if (stroke != null) 866 m_stroke = stroke; 867 } 869 873 public Stroke getStroke() 874 { 875 return m_stroke; 876 } 878 879 880 887 private void createStandardHeaderFooter() 888 { 889 PrintElement element = new ImageElement(org.compiere.Compiere.getImageLogoSmall()); element.layout(m_header.width, 0, false, MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft); 892 element.setLocation(m_header.getLocation()); 893 m_headerFooter.addElement(element); 894 MPrintTableFormat tf = m_format.getTableFormat(); 896 Font font = tf.getPageHeader_Font(); 897 Color color = tf.getPageHeaderFG_Color(); 898 element = new StringElement("@*ReportName@", font, color, null, true); 900 element.layout (m_header.width, 0, true, MPrintFormatItem.FIELDALIGNMENTTYPE_Center); 901 element.setLocation(m_header.getLocation()); 902 m_headerFooter.addElement(element); 903 element = new StringElement("@Page@ @*Page@ @of@ @*PageCount@", font, color, null, true); 906 element.layout (m_header.width, 0, true, MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight); 907 element.setLocation(m_header.getLocation()); 908 m_headerFooter.addElement(element); 909 910 font = tf.getPageFooter_Font(); 912 color = tf.getPageFooterFG_Color(); 913 element = new StringElement("@CopyRight@", font, color, null, true); 915 element.layout (m_footer.width, 0, true, MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft); 916 Point ft = m_footer.getLocation(); 917 ft.y += m_footer.height - element.getHeight() - 2; element.setLocation(ft); 919 m_headerFooter.addElement(element); 920 element = new StringElement("@*Header@", font, color, null, true); 922 element.layout (m_footer.width, 0, true, MPrintFormatItem.FIELDALIGNMENTTYPE_Center); 923 element.setLocation(ft); 924 m_headerFooter.addElement(element); 925 element = new StringElement("@*CurrentDateTime@", font, color, null, true); 927 element.layout (m_footer.width, 0, true, MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight); 928 element.setLocation(ft); 929 m_headerFooter.addElement(element); 930 } 932 933 934 939 private void layoutForm() 940 { 941 log.info("layoutForm"); 942 m_columnCount = 0; 943 float lastHeight[] = new float[] {0f, 0f, 0f}; 944 float lastWidth[] = new float[] {0f, 0f, 0f}; 945 for (int row = 0; row < m_data.getRowCount(); row++) 947 { 948 log.debug("layoutForm - Row=" + row); 949 m_data.setRowIndex(row); 950 boolean somethingPrinted = true; for (int i = 0; i < m_format.getItemCount(); i++) 953 { 954 MPrintFormatItem item = m_format.getItem(i); 955 if (!item.isPrinted()) 956 continue; 957 m_columnCount++; 958 if (row > 0 && (item.isHeader() || item.isFooter())) 960 continue; 961 if (item.isHeader()) setArea(AREA_HEADER); 964 else if (item.isFooter()) 965 setArea(AREA_FOOTER); 966 else 967 setArea(AREA_CONTENT); 968 if (item.isSetNLPosition() && item.isRelativePosition()) 970 m_tempNLPositon = 0; 971 if (item.isNextPage()) newPage(); 973 else if (item.isNextLine() && somethingPrinted) { 975 newLine (); 976 somethingPrinted = false; 977 } 978 else 979 addX(lastWidth[m_area]); 980 if (item.isRelativePosition()) 981 { 982 addX(item.getXspace()); 983 addY(item.getYspace()); 984 } 985 else setRelativePosition(item.getXposition(), item.getYposition()); 987 if (item.isSetNLPosition() && !item.isRelativePosition()) 989 m_tempNLPositon = (int)getPosition().getX(); 990 991 String alignment = item.getFieldAlignmentType(); 993 int maxWidth = item.getMaxWidth(); 994 boolean lineAligned = false; 995 if (item.isRelativePosition()) 996 { 997 if (item.isLineAlignLeading()) 998 { 999 alignment = MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft; 1000 maxWidth = getAreaBounds().width; 1001 lineAligned = true; 1002 } 1003 else if (item.isLineAlignCenter()) 1004 { 1005 alignment = MPrintFormatItem.FIELDALIGNMENTTYPE_Center; 1006 maxWidth = getAreaBounds().width; 1007 lineAligned = true; 1008 } 1009 else if (item.isLineAlignTrailing()) 1010 { 1011 alignment = MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight; 1012 maxWidth = getAreaBounds().width; 1013 lineAligned = true; 1014 } 1015 } 1016 1017 PrintElement element = null; 1019 if (item.isTypePrintFormat()) { 1021 newLine(); 1022 try 1023 { 1024 MPrintFormat format = MPrintFormat.get (item.getAD_PrintFormatChild_ID(), false); 1025 format.setLanguage(m_format.getLanguage()); 1026 if (m_format.isTranslationView()) 1027 format.setTranslationLanguage(m_format.getLanguage()); 1028 log.info("layoutForm - include Format " + format + " - Item=" + item.getName()); 1029 int AD_Column_ID = item.getAD_Column_ID(); 1031 Object obj = m_data.getNode(new Integer (AD_Column_ID)); 1032 PrintDataElement data = (PrintDataElement)obj; 1034 int Record_ID = Integer.parseInt(data.getValueKey()); 1035 MQuery query = new MQuery (format.getAD_Table_ID()); 1036 query.addRestriction(item.getColumnName(), MQuery.EQUAL, new Integer (Record_ID)); 1037 format.setTranslationViewQuery(query); 1038 log.debug("layoutForm - include " + query); 1039 DataEngine de = new DataEngine(format.getLanguage()); 1041 PrintData printData = de.getPrintData(m_data.getCtx(), format, query); 1042 log.debug("layoutForm - include " + printData); 1043 element = layoutTable (format, printData, item.getXspace()); 1045 if (element.getPageCount() > 1) 1047 { 1048 Point2D.Double loc = m_position[m_area]; 1049 element.setLocation(loc); 1050 for (int p = 1; p < element.getPageCount(); p++) { 1052 m_currPage.addElement (element); 1053 newPage(); 1054 } 1055 m_position[m_area] = loc; 1056 ((TableElement)element).setHeightToLastPage(); 1057 } 1058 } 1059 catch (Exception e) 1060 { 1061 log.error("layoutForm", e); 1062 } 1063 if (element == null) 1064 log.error("layoutForm - No included Format"); 1065 } 1066 else if (item.isTypeImage()) { 1068 if (item.isImageIsAttached()) 1069 element = new ImageElement(item.getID()); 1070 else 1071 element = new ImageElement(item.getImageURL()); 1072 element.layout(maxWidth, item.getMaxHeight(), false, alignment); 1073 } 1074 else if (item.isTypeField()) { 1076 if (maxWidth == 0 && item.isFieldAlignBlock()) 1077 maxWidth = getAreaBounds().width; 1078 element = createFieldElement (item, maxWidth, alignment, m_format.isForm()); 1079 } 1080 else { 1082 if (maxWidth == 0 && item.isFieldAlignBlock()) 1083 maxWidth = getAreaBounds().width; 1084 element = createStringElement (item.getPrintName (m_format.getLanguage ()), 1085 item.getAD_PrintColor_ID (), item.getAD_PrintFont_ID (), 1086 maxWidth, item.getMaxHeight (), item.isHeightOneLine (), alignment, true); 1087 } 1088 if (element != null) 1090 { 1091 somethingPrinted = true; 1092 if (!lineAligned) 1093 lastWidth[m_area] = element.getWidth(); 1094 lastHeight[m_area] = element.getHeight(); 1095 } 1096 else 1097 { 1098 somethingPrinted = false; 1099 lastWidth[m_area] = 0f; 1100 lastHeight[m_area] = 0f; 1101 } 1102 1103 if (item.isRelativePosition() && !lineAligned) 1105 { 1106 if (!isXspaceFor(lastWidth[m_area])) 1107 { 1108 if (Log.isTraceLevel(9)) 1109 log.debug("layoutForm - new Line - not enough X space for " 1110 + lastWidth[m_area] + " - remaining " + getXspace() + " - Area=" + m_area); 1111 newLine (); 1112 } 1113 if (m_area == AREA_CONTENT && !isYspaceFor(lastHeight[m_area])) 1114 { 1115 if (Log.isTraceLevel(9)) 1116 log.debug("layoutForm - new Page - not enough Y space " 1117 + lastHeight[m_area] + " - remaining " + getXspace() + " - Area=" + m_area); 1118 newPage (); 1119 } 1120 } 1121 if (element != null) 1126 element.setLocation(m_position[m_area]); 1127 if (m_area == AREA_CONTENT) 1129 m_currPage.addElement (element); 1130 else 1131 m_headerFooter.addElement (element); 1132 if (lastHeight[m_area] > m_maxHeightSinceNewLine[m_area]) 1134 m_maxHeightSinceNewLine[m_area] = lastHeight[m_area]; 1135 1136 } } } 1140 1153 private PrintElement createStringElement (String content, int AD_PrintColor_ID, int AD_PrintFont_ID, 1154 int maxWidth, int maxHeight, boolean isHeightOneLine, String FieldAlignmentType, boolean isTranslated) 1155 { 1156 Color color = m_printColor.getColor(); if (AD_PrintColor_ID != 0 && m_printColor.getID() != AD_PrintColor_ID) 1159 { 1160 MPrintColor c = MPrintColor.get (AD_PrintColor_ID); 1161 if (c.getColor() != null) 1162 color = c.getColor(); 1163 } 1164 Font font = m_printFont.getFont(); if (AD_PrintFont_ID != 0 && m_printFont.getID() != AD_PrintFont_ID) 1166 { 1167 MPrintFont f = MPrintFont.get (AD_PrintFont_ID); 1168 if (f.getFont() != null) 1169 font = f.getFont(); 1170 } 1171 PrintElement e = new StringElement(content, font, color, null, isTranslated); 1172 e.layout (maxWidth, maxHeight, isHeightOneLine, FieldAlignmentType); 1173 return e; 1174 } 1176 1184 private PrintElement createFieldElement (MPrintFormatItem item, int maxWidth, 1185 String FieldAlignmentType, boolean isForm) 1186 { 1187 Object obj = m_data.getNode(new Integer (item.getAD_Column_ID())); 1189 if (obj == null) 1190 return null; 1191 else if (obj instanceof PrintDataElement) 1192 ; 1193 else 1194 { 1195 log.error("createFieldElement - Element not PrintDataElement " + obj.getClass()); 1196 return null; 1197 } 1198 1199 PrintDataElement data = (PrintDataElement)obj; 1201 if (data.isNull() && item.isSuppressNull()) 1202 return null; 1203 String stringContent = data.getValueDisplay (m_format.getLanguage()); 1204 if ((stringContent == null || stringContent.length() == 0) && item.isSuppressNull()) 1205 return null; 1206 Object content = stringContent; 1207 if (data.getValue() instanceof Boolean ) 1208 content = data.getValue(); 1209 if (item.getColumnName().equals("AmtInWords")) 1211 { 1212 String amtInWords = Msg.getAmtInWords (m_format.getLanguage(), stringContent); 1213 log.debug("createFieldElement - AmtInWords: " + content); 1214 content = amtInWords; 1215 } 1216 String label = item.getPrintName(m_format.getLanguage()); 1218 String labelSuffix = item.getPrintNameSuffix(m_format.getLanguage()); 1219 1220 NamePair ID = null; 1222 if (data.isID()) 1223 { Object value = data.getValue(); 1225 if (value instanceof KeyNamePair) 1226 ID = new KeyNamePair(((KeyNamePair)value).getKey(), item.getColumnName()); 1227 else if (value instanceof ValueNamePair) 1228 ID = new ValueNamePair(((ValueNamePair)value).getValue(), item.getColumnName()); 1229 } 1230 else if (MPrintFormatItem.FIELDALIGNMENTTYPE_Default.equals(FieldAlignmentType)) 1231 { 1232 if (data.isNumeric()) 1233 FieldAlignmentType = MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight; 1234 else 1235 FieldAlignmentType = MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft; 1236 } 1237 1238 Color color = m_printColor.getColor(); if (ID != null && !isForm) 1241 ; else if (item.getAD_PrintColor_ID() != 0 && m_printColor.getID() != item.getAD_PrintColor_ID()) 1243 { 1244 MPrintColor c = MPrintColor.get (item.getAD_PrintColor_ID()); 1245 if (c.getColor() != null) 1246 color = c.getColor(); 1247 } 1248 1249 Font font = m_printFont.getFont(); if (item.getAD_PrintFont_ID() != 0 && m_printFont.getID() != item.getAD_PrintFont_ID()) 1251 { 1252 MPrintFont f = MPrintFont.get (item.getAD_PrintFont_ID()); 1253 if (f.getFont() != null) 1254 font = f.getFont(); 1255 } 1256 1257 1258 PrintElement e = null; 1259 if (data.getDisplayType() == DisplayType.Location) 1260 { 1261 e = new LocationElement(m_printCtx, ((KeyNamePair)ID).getKey(), font, color); 1262 e.layout (maxWidth, item.getMaxHeight(), item.isHeightOneLine(), FieldAlignmentType); 1263 } 1264 else 1265 { 1266 e = new StringElement(content, font, color, isForm ? null : ID, label, labelSuffix); 1267 e.layout (maxWidth, item.getMaxHeight(), item.isHeightOneLine(), FieldAlignmentType); 1268 } 1269 return e; 1270 } 1272 1273 1274 1275 1283 private PrintElement layoutTable (MPrintFormat format, PrintData printData, 1284 int xOffset) 1285 { 1286 log.debug("layoutTable " + format.getName() + " - " + printData.getName()); 1287 MPrintTableFormat tf = format.getTableFormat(); 1288 HashMap rowColFont = new HashMap(); 1290 MPrintFont printFont = MPrintFont.get (format.getAD_PrintFont_ID()); 1291 rowColFont.put(new Point(TableElement.ALL,TableElement.ALL), printFont.getFont()); 1292 tf.setStandard_Font(printFont.getFont()); 1293 rowColFont.put(new Point(TableElement.HEADER_ROW,TableElement.ALL), tf.getHeader_Font()); 1294 HashMap rowColColor = new HashMap(); 1296 MPrintColor printColor = MPrintColor.get (format.getAD_PrintColor_ID()); 1297 rowColColor.put(new Point(TableElement.ALL,TableElement.ALL), printColor.getColor()); 1298 rowColColor.put(new Point(TableElement.HEADER_ROW,TableElement.ALL), tf.getHeaderFG_Color()); 1299 HashMap rowColBackground = new HashMap(); 1301 rowColBackground.put(new Point(TableElement.HEADER_ROW,TableElement.ALL), tf.getHeaderBG_Color()); 1302 boolean multiLineHeader = false; 1304 int pageNoStart = m_pageNo; 1305 int repeatedColumns = 1; 1306 Rectangle firstPage = new Rectangle(m_content); 1307 firstPage.x += xOffset; 1308 firstPage.width -= xOffset; 1309 int yOffset = (int)m_position[AREA_CONTENT].y - m_content.y; 1310 firstPage.y += yOffset; 1311 firstPage.height -= yOffset; 1312 Rectangle nextPages = new Rectangle(m_content); 1313 nextPages.x += xOffset; 1314 nextPages.width -= xOffset; 1315 m_columnCount = 0; 1317 for (int c = 0; c < format.getItemCount(); c++) 1318 { 1319 if (format.getItem(c).isPrinted()) 1320 m_columnCount++; 1321 } 1322 1324 ValueNamePair[] columnHeader = new ValueNamePair[m_columnCount]; 1326 int[] columnMaxWidth = new int[m_columnCount]; 1327 int[] columnMaxHeight = new int[m_columnCount]; 1328 boolean[] fixedWidth = new boolean [m_columnCount]; 1329 String [] columnJustification = new String [m_columnCount]; 1330 HashMap additionalLines = new HashMap(); 1331 1332 int col = 0; 1333 for (int c = 0; c < format.getItemCount(); c++) 1334 { 1335 MPrintFormatItem item = format.getItem(c); 1336 if (item.isPrinted()) 1337 { 1338 if (item.isNextLine() && item.getBelowColumn() != 0) 1339 { 1340 additionalLines.put(new Integer (col), new Integer (item.getBelowColumn()-1)); 1341 item.setIsSuppressNull(true); } 1343 columnHeader[col] = new ValueNamePair(item.getColumnName(), 1344 item.getPrintName(format.getLanguage())); 1345 columnMaxWidth[col] = item.getMaxWidth(); 1346 fixedWidth[col] = (columnMaxWidth[col] != 0 && item.isFixedWidth()); 1347 if (item.isSuppressNull()) 1348 { 1349 if (columnMaxWidth[col] == 0) 1350 columnMaxWidth[col] = -1; else 1352 columnMaxWidth[col] *= -1; 1353 } 1354 columnMaxHeight[col] = item.getMaxHeight(); 1355 columnJustification[col] = item.getFieldAlignmentType(); 1356 if (columnJustification[col] == null || columnJustification[col].equals(MPrintFormatItem.FIELDALIGNMENTTYPE_Default)) 1357 columnJustification[col] = MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft; if (item.getAD_PrintFont_ID() != 0 && item.getAD_PrintFont_ID() != format.getAD_PrintFont_ID()) 1360 { 1361 MPrintFont font = MPrintFont.get(item.getAD_PrintFont_ID()); 1362 rowColFont.put(new Point(TableElement.ALL, col), font.getFont()); 1363 } 1364 if (item.getAD_PrintColor_ID() != 0 && item.getAD_PrintColor_ID() != format.getAD_PrintColor_ID()) 1365 { 1366 MPrintColor color = MPrintColor.get (item.getAD_PrintColor_ID()); 1367 rowColColor.put(new Point(TableElement.ALL, col), color.getColor()); 1368 } 1369 col++; 1371 } 1372 } 1373 1374 int rows = printData.getRowCount(); 1376 Object [][] data = new Object [rows][m_columnCount]; 1378 KeyNamePair[] pk = new KeyNamePair[rows]; 1379 String pkColumnName = null; 1380 boolean lastLineFunction = false; 1381 ArrayList pageBreak = new ArrayList(); 1382 1383 for (int row = 0; row < rows; row++) 1385 { 1386 printData.setRowIndex(row); 1388 if (printData.isFunctionRow()) 1389 { 1390 rowColFont.put(new Point(row, TableElement.ALL), tf.getFunct_Font()); 1391 rowColColor.put(new Point(row, TableElement.ALL), tf.getFunctFG_Color()); 1392 rowColBackground.put(new Point(row, TableElement.ALL), tf.getFunctBG_Color()); 1393 lastLineFunction = true; 1394 if (printData.isPageBreak()) 1395 { 1396 pageBreak.add(new Integer (row)); 1397 if (Log.isTraceLevel(8)) 1398 log.debug("layoutTable - PageBreak row=" + row); 1399 } 1400 } 1401 else 1403 { 1404 int levelNo = printData.getLineLevelNo(); 1405 if (levelNo != 0) 1406 { 1407 Font base = printFont.getFont(); 1408 rowColFont.put(new Point(row, TableElement.ALL), new Font (base.getName(), 1409 Font.PLAIN, base.getSize()-levelNo)); 1410 } 1411 } 1412 col = 0; 1414 for (int c = 0; c < format.getItemCount(); c++) 1415 { 1416 MPrintFormatItem item = format.getItem(c); 1417 Object dataElement = null; 1418 if (item.isPrinted()) 1419 { 1420 Object obj = printData.getNode(new Integer (item.getAD_Column_ID())); 1421 if (obj == null) 1422 ; 1423 else if (obj instanceof PrintDataElement) 1424 { 1425 PrintDataElement pde = (PrintDataElement)obj; 1426 if (pde.isID() || pde.isYesNo()) 1427 dataElement = pde.getValue(); 1428 else 1429 dataElement = pde.getValueDisplay(format.getLanguage()); 1430 } 1431 else 1432 log.error("layoutTable - Element not PrintDataElement " + obj.getClass()); 1433 1434 data[row][col] = dataElement; 1436 col++; 1437 } } 1440 PrintDataElement pde = printData.getPKey(); 1441 if (pde != null) { 1443 pk[row] = (KeyNamePair)pde.getValue(); 1444 if (pkColumnName == null) 1445 pkColumnName = pde.getColumnName(); 1446 } 1447 } 1451 TableElement table = new TableElement(columnHeader, 1453 columnMaxWidth, columnMaxHeight, columnJustification, 1454 fixedWidth, lastLineFunction, multiLineHeader, 1455 data, pk, pkColumnName, 1456 pageNoStart, firstPage, nextPages, repeatedColumns, additionalLines, 1457 rowColFont, rowColColor, rowColBackground, 1458 tf, pageBreak); 1459 table.layout(0,0,false, MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft); 1460 if (m_tableElement == null) 1461 m_tableElement = table; 1462 return table; 1463 } 1465 1469 private PrintElement layoutParameter () 1470 { 1471 if (m_query == null || !m_query.isActive()) 1472 return null; 1473 ParameterElement pe = new ParameterElement(m_query, m_printCtx, m_format.getTableFormat()); 1475 pe.layout(0, 0, false, null); 1476 return pe; 1477 } 1479 1480 1481} | Popular Tags |