1 14 package org.compiere.print.layout; 15 16 import java.awt.*; 17 import java.awt.font.*; 18 import java.awt.geom.*; 19 import java.text.*; 20 import java.util.*; 21 import java.util.regex.*; 22 23 import org.compiere.print.*; 24 import org.compiere.util.*; 25 import org.compiere.model.*; 26 27 34 public class StringElement extends PrintElement 35 { 36 45 public StringElement (String inText, Font font, Paint paint, NamePair ID, boolean translateText) 46 { 47 Log.trace(Log.l6_Database, "StringElement", "Text=" + inText + ", ID=" + ID + ", Translate=" + translateText); 48 if (translateText) 49 { 50 int count = Util.getCount(inText, '@'); 51 if (count > 0 && count % 2 == 0) 52 { 53 m_originalString = inText; 54 m_originalFont = font; 55 m_originalPaint = paint; 56 inText = Msg.parseTranslation(Env.getCtx(), m_originalString); 58 } 59 } 60 m_ID = ID; 61 String [] lines = Pattern.compile("$", Pattern.MULTILINE).split(inText); 62 m_string_paper = new AttributedString[lines.length]; 63 m_string_view = new AttributedString[lines.length]; 64 for (int i = 0; i < lines.length; i++) 65 { 66 String line = Util.removeCRLF (lines[i]); 67 m_string_paper[i] = new AttributedString(line); 68 if (line.length() == 0) 69 continue; 70 Log.trace(9, "Line " + i, line); 71 m_string_paper[i].addAttribute(TextAttribute.FONT, font); 72 m_string_paper[i].addAttribute(TextAttribute.FOREGROUND, paint); 73 if (m_ID != null && i == 0) { 75 m_string_view[i] = new AttributedString(line); 76 m_string_view[i].addAttribute(TextAttribute.FONT, font); 77 int endIndex = line.length(); 78 m_string_view[i].addAttribute(TextAttribute.FOREGROUND, LINK_COLOR); 79 m_string_view[i].addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL, 0, endIndex); 80 } 81 else 82 m_string_view[i] = m_string_paper[i]; 83 } 84 waitForLoad(LayoutEngine.IMAGE_TRUE); 86 waitForLoad(LayoutEngine.IMAGE_FALSE); 87 } 89 93 public StringElement(AttributedString string) 94 { 95 m_string_paper = new AttributedString[] {string}; 96 m_string_view = m_string_paper; 97 } 99 109 public StringElement (Object content, Font font, Paint paint, NamePair ID, String label, String labelSuffix) 110 { 111 Log.trace(Log.l6_Database, "StringElement", "Label=" + label + "|" + labelSuffix 112 + ", Content=" + content + ", ID=" + ID); 113 int startIndex = 0; 114 int endOffset = 0; 115 116 StringBuffer text = new StringBuffer (); 117 if (label != null && label.length() > 0) 118 { 119 text.append(label).append(" "); 120 startIndex = label.length() + 1; 121 } 122 if (content instanceof Boolean ) 123 m_check = (Boolean )content; 124 else 125 text.append(content); 126 if (labelSuffix != null && labelSuffix.length() > 0) 127 { 128 text.append(labelSuffix); 129 endOffset = labelSuffix.length(); 130 } 131 m_ID = ID; 132 String [] lines = Pattern.compile("$", Pattern.MULTILINE).split(text); 133 m_string_paper = new AttributedString[lines.length]; 134 m_string_view = new AttributedString[lines.length]; 135 for (int i = 0; i < lines.length; i++) 136 { 137 String line = Util.removeCRLF (lines[i]); 138 m_string_paper[i] = new AttributedString(line); 139 if (line.length() == 0) 140 continue; 141 Log.trace(9, "Line " + i, line); 142 m_string_paper[i].addAttribute(TextAttribute.FONT, font); 143 m_string_paper[i].addAttribute(TextAttribute.FOREGROUND, paint); 144 if (m_ID != null && i == 0) { 146 m_string_view[i] = new AttributedString(line); 147 m_string_view[i].addAttribute(TextAttribute.FONT, font); 148 int endIndex = line.length() - endOffset; 149 if (endIndex > startIndex) 150 { 151 m_string_view[i].addAttribute (TextAttribute.FOREGROUND, LINK_COLOR, startIndex, endIndex); 152 m_string_view[i].addAttribute (TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL, startIndex, endIndex); 153 } 154 } 155 else 156 m_string_view[i] = m_string_paper[i]; 157 } 158 } 160 161 162 private AttributedString[] m_string_view = null; 163 private AttributedString[] m_string_paper = null; 164 165 private String m_originalString = null; 166 private Font m_originalFont = null; 167 private Paint m_originalPaint = null; 168 169 private NamePair m_ID = null; 170 171 private Boolean m_check = null; 172 173 177 public NamePair getID() 178 { 179 return m_ID; 180 } 182 188 public void translate(Properties ctx) 189 { 190 if (m_originalString == null) 191 return; 192 String inText = Msg.parseTranslation(ctx, m_originalString); 193 String [] lines = Pattern.compile("$", Pattern.MULTILINE).split(inText); 195 m_string_paper = new AttributedString[lines.length]; 196 for (int i = 0; i < lines.length; i++) 197 { 198 String line = Util.removeCRLF (lines[i]); 199 m_string_paper[i] = new AttributedString(line); 200 if (line.length() > 0) 201 { 202 m_string_paper[i].addAttribute(TextAttribute.FONT, m_originalFont); 203 m_string_paper[i].addAttribute(TextAttribute.FOREGROUND, m_originalPaint); 204 } 205 } 206 m_string_view = m_string_paper; 207 } 209 210 211 216 protected boolean calculateSize() 217 { 218 if (p_sizeCalculated) 219 return true; 220 FontRenderContext frc = new FontRenderContext(null, true, true); 222 TextLayout layout = null; 223 p_height = 0f; 224 p_width = 0f; 225 226 if (p_maxWidth == 0f && p_maxHeight == 0f) 228 { 229 for (int i = 0; i < m_string_paper.length; i++) 230 { 231 AttributedCharacterIterator iter = m_string_paper[i].getIterator(); 232 if (iter.getBeginIndex() == iter.getEndIndex()) 233 continue; 234 235 int tabPos = -1; 237 for (char c = iter.first(); c != CharacterIterator.DONE && tabPos == -1; c = iter.next()) 238 { 239 if (c == '\t') 240 tabPos = iter.getIndex(); 241 } 242 243 if (tabPos == -1) 244 { 245 layout = new TextLayout (iter, frc); 246 p_height += layout.getAscent() + layout.getDescent() + layout.getLeading(); 247 if (p_width < layout.getAdvance()) 248 p_width = layout.getAdvance(); 249 } 250 else { 252 LineBreakMeasurer measurer = new LineBreakMeasurer(iter, frc); 253 layout = measurer.nextLayout(9999, tabPos, false); 254 p_height += layout.getAscent() + layout.getDescent() + layout.getLeading(); 255 float width = getTabPos (0, layout.getAdvance()); 256 layout = measurer.nextLayout(9999, iter.getEndIndex(), true); 257 width += layout.getAdvance(); 258 if (p_width < width) 259 p_width = width; 260 } 261 } 263 if (m_check != null) 265 { 266 p_width += LayoutEngine.IMAGE_SIZE.width; 267 if (p_height < LayoutEngine.IMAGE_SIZE.height) 268 p_height = LayoutEngine.IMAGE_SIZE.height; 269 } 270 } 271 else 273 { 274 p_width = p_maxWidth; 275 for (int i = 0; i < m_string_paper.length; i++) 276 { 277 AttributedCharacterIterator iter = m_string_paper[i].getIterator(); 278 if (iter.getBeginIndex() == iter.getEndIndex()) 279 continue; 280 281 LineBreakMeasurer measurer = new LineBreakMeasurer(iter, frc); 282 while (measurer.getPosition() < iter.getEndIndex()) 284 { 285 layout = measurer.nextLayout(p_maxWidth); 287 float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading(); 288 if (p_maxHeight == -1f && i == 0) p_maxHeight = lineHeight; 291 if (p_maxHeight == 0f || (p_height + lineHeight) <= p_maxHeight) 292 p_height += lineHeight; 293 } 294 } 296 if (m_check != null) 298 { 299 if (p_height < LayoutEngine.IMAGE_SIZE.height) 301 p_height = LayoutEngine.IMAGE_SIZE.height; 302 } 303 } 305 308 if (p_FieldAlignmentType != null) 310 { 311 boolean changed = false; 312 if (p_height < p_maxHeight) 313 { 314 p_height = p_maxHeight; 315 changed = true; 316 } 317 if (p_width < p_maxWidth) 318 { 319 p_width = p_maxWidth; 320 changed = true; 321 } 322 } 326 return true; 327 } 329 330 331 337 public MQuery getDrillDown (Point relativePoint, int pageNo) 338 { 339 if (m_ID != null && getBounds().contains(relativePoint)) 340 { 341 Log.trace(Log.l5_DData, "StringElement.getDrillDown", toString()); 342 String columnName = m_ID.getName(); 343 String tableName = columnName; 344 int index = tableName.lastIndexOf("_ID"); 345 if (index != -1) 346 tableName = tableName.substring(0, index); 347 Object code = m_ID.getID(); 348 if (m_ID instanceof KeyNamePair) 349 code = new Integer (((KeyNamePair)m_ID).getKey()); 350 MQuery query = new MQuery(tableName); 352 query.addRestriction(columnName, MQuery.EQUAL, code); 353 return query; 354 } 355 return null; 356 } 358 364 public MQuery getDrillAcross (Point relativePoint, int pageNo) 365 { 366 return null; 369 } 371 372 373 383 public void paint (Graphics2D g2D, int pageNo, Point2D pageStart, Properties ctx, boolean isView) 384 { 385 Point2D.Double location = getAbsoluteLocation(pageStart); 391 if (m_originalString != null) 393 translate(ctx); 394 395 AttributedCharacterIterator iter = null; 396 float xPos = (float)location.x; 397 float yPos = (float)location.y; 398 float yPen = 0f; 399 float height = 0f; 400 float width = 0f; 401 for (int i = 0; i < m_string_paper.length; i++) 403 { 404 if (isView) 406 iter = m_string_view[i].getIterator(); 407 else 408 iter = m_string_paper[i].getIterator(); 409 if (iter.getBeginIndex() == iter.getEndIndex()) 410 continue; 411 412 int tabPos = -1; 414 for (char c = iter.first(); c != CharacterIterator.DONE && tabPos == -1; c = iter.next()) 415 { 416 if (c == '\t') 417 tabPos = iter.getIndex(); 418 } 419 420 TextLayout layout = null; 421 float xPen = xPos; 422 423 if (p_maxWidth == 0f) 425 { 426 if (tabPos == -1) 427 { 428 layout = new TextLayout (iter, g2D.getFontRenderContext()); 429 yPen = yPos + layout.getAscent(); 430 layout.draw(g2D, xPen, yPen); 431 yPos += layout.getAscent() + layout.getDescent() + layout.getLeading(); 432 if (width < layout.getAdvance()) 433 width = layout.getAdvance(); 434 } 435 else 436 { 437 LineBreakMeasurer measurer = new LineBreakMeasurer(iter, g2D.getFontRenderContext()); 438 layout = measurer.nextLayout(9999, tabPos, false); 439 float lineHeight1 = layout.getAscent() + layout.getDescent() + layout.getLeading(); 440 yPen = yPos + layout.getAscent(); 441 layout.draw(g2D, xPen, yPen); xPen = getTabPos (xPos, layout.getAdvance()); 443 float lineWidth = xPen - xPos; 444 layout = measurer.nextLayout(9999, iter.getEndIndex(), true); 445 float lineHeight2 = layout.getAscent() + layout.getDescent() + layout.getLeading(); 446 layout.draw(g2D, xPen, yPen); 447 yPos += Math.max(lineHeight1, lineHeight2); 448 lineWidth += layout.getAdvance(); 449 if (width < lineWidth) 450 width = lineWidth; 451 } 452 } 455 else 457 { 458 LineBreakMeasurer measurer = new LineBreakMeasurer(iter, g2D.getFontRenderContext()); 459 while (measurer.getPosition() < iter.getEndIndex()) 460 { 461 if (tabPos == -1) 462 layout = measurer.nextLayout(p_maxWidth); 463 else 464 layout = measurer.nextLayout(p_maxWidth, tabPos, false); 465 float lineHeight = layout.getAscent() + layout.getDescent() + layout.getLeading(); 467 if (p_maxHeight == -1f && i == 0) p_maxHeight = lineHeight; 469 if (p_maxHeight == 0f || (height + lineHeight) <= p_maxHeight) 471 { 472 yPen = (float)location.y + height + layout.getAscent(); 473 if (tabPos != -1) 475 { 476 layout.draw(g2D, xPen, yPen); xPen = getTabPos (xPos, layout.getAdvance()); 478 layout = measurer.nextLayout(p_width, iter.getEndIndex(), true); 479 tabPos = -1; } 481 else if ((MPrintFormatItem.FIELDALIGNMENTTYPE_TrailingRight.equals(p_FieldAlignmentType) && layout.isLeftToRight()) 482 || (MPrintFormatItem.FIELDALIGNMENTTYPE_LeadingLeft.equals(p_FieldAlignmentType) && !layout.isLeftToRight())) 483 xPen += p_maxWidth - layout.getAdvance(); 484 else if (MPrintFormatItem.FIELDALIGNMENTTYPE_Center.equals(p_FieldAlignmentType)) 485 xPen += (p_maxWidth - layout.getAdvance()) / 2; 486 else if (MPrintFormatItem.FIELDALIGNMENTTYPE_Block.equals(p_FieldAlignmentType) && measurer.getPosition() < iter.getEndIndex()) 487 layout = layout.getJustifiedLayout(p_maxWidth); 488 layout.draw(g2D, xPen, yPen); 489 height += lineHeight; 490 } 493 } 494 width = p_maxWidth; 495 } } if (m_check != null) 498 { 499 int x = (int)(location.x + width + 1); 500 int y = (int)(location.y); 501 g2D.drawImage(m_check.booleanValue() ? LayoutEngine.IMAGE_TRUE : LayoutEngine.IMAGE_FALSE, x, y, this); 502 } 503 } 505 515 private float getTabPos (float xPos, float length) 516 { 517 float retValue = xPos + length; 518 int iLength = (int)Math.ceil(length); 519 int tabSpace = iLength % 30; 520 retValue += (30 - tabSpace); 521 return retValue; 522 } 524 528 public String toString() 529 { 530 StringBuffer sb = new StringBuffer ("StringElement["); 531 sb.append("Bounds=").append(getBounds()) 532 .append(",Height=").append(p_height).append("(").append(p_maxHeight) 533 .append("),Width=").append(p_width).append("(").append(p_maxHeight) 534 .append("),PageLocation=").append(p_pageLocation).append(" - "); 535 for (int i = 0; i < m_string_paper.length; i++) 536 { 537 if (m_string_paper.length > 1) 538 sb.append(Env.NL).append(i).append(":"); 539 AttributedCharacterIterator iter = m_string_paper[i].getIterator(); 540 for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) 541 sb.append(c); 542 } 543 if (m_ID != null) 544 sb.append(",ID=(").append(m_ID.toStringX()).append(")"); 545 sb.append("]"); 546 return sb.toString(); 547 } 549 } | Popular Tags |