1 32 33 package it.businesslogic.ireport.gui.style; 34 35 import it.businesslogic.ireport.Box; 36 import it.businesslogic.ireport.IReportFont; 37 import it.businesslogic.ireport.ReportElement; 38 import it.businesslogic.ireport.Style; 39 import it.businesslogic.ireport.gui.MainFrame; 40 import it.businesslogic.ireport.util.Java2DUtil; 41 import it.businesslogic.ireport.util.Misc; 42 import java.awt.Color ; 43 import java.awt.Font ; 44 import java.awt.Graphics ; 45 import java.awt.Graphics2D ; 46 import java.awt.Point ; 47 import java.awt.RenderingHints ; 48 import java.awt.Stroke ; 49 import java.awt.font.FontRenderContext ; 50 import java.awt.font.LineBreakMeasurer ; 51 import java.awt.font.TextAttribute ; 52 import java.awt.font.TextLayout ; 53 import java.awt.geom.AffineTransform ; 54 import java.text.AttributedCharacterIterator ; 55 import java.text.AttributedString ; 56 import java.util.ArrayList ; 57 import java.util.Iterator ; 58 import java.util.List ; 59 import java.util.StringTokenizer ; 60 61 62 66 public class JPanelStyleSample extends javax.swing.JPanel { 67 68 private Style style = null; 69 70 static public Rotation ROTATION_NONE; 71 static public Rotation ROTATION_LEFT; 72 static public Rotation ROTATION_RIGHT; 73 74 static 75 { 76 ROTATION_NONE = new Rotation("None", 0); 77 ROTATION_LEFT = new Rotation("Left", 1); 78 ROTATION_RIGHT = new Rotation("Right", 2); 79 } 80 81 82 83 public JPanelStyleSample() { 84 initComponents(); 85 } 86 87 public Style getStyle() { 88 return style; 89 } 90 91 public void setStyle(Style style) { 92 this.style = style; 93 this.repaint(); 94 } 95 96 101 private void initComponents() { 103 104 setLayout(null); 105 106 } 108 109 112 113 public void paint(Graphics g) 114 { 115 super.paint(g); 116 Graphics2D g2 = (Graphics2D )g; 117 118 if (getStyle() == null) return; 119 120 if (this.getSize().getWidth() <= 10) return; 121 if (this.getSize().getHeight() <= 10) return; 122 123 int x=5; 124 int y=5; 125 int width = (int)this.getSize().getWidth()-10; 126 int heigth = (int)this.getSize().getHeight()-10; 127 128 if (MainFrame.getMainInstance().getProperties().getProperty("Antialias","true").equals("false")) 129 { 130 131 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 132 RenderingHints.VALUE_ANTIALIAS_OFF); 133 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 134 RenderingHints.VALUE_STROKE_PURE); 135 g2.setRenderingHint(RenderingHints.KEY_DITHERING, 136 RenderingHints.VALUE_DITHER_DISABLE); 137 } 138 else 139 { 140 141 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 142 RenderingHints.VALUE_ANTIALIAS_ON); 143 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 144 RenderingHints.VALUE_STROKE_DEFAULT); 145 g2.setRenderingHint(RenderingHints.KEY_DITHERING, 146 RenderingHints.VALUE_DITHER_ENABLE); 147 } 148 149 int round = Integer.parseInt( getStyle().getAttributeString(Style.ATTRIBUTE_radius, "0")); 150 round *= 2; 151 152 Color c = getStyle().getAttributeColor(Style.ATTRIBUTE_backcolor, null); 154 String transparent = getStyle().getAttributeString(Style.ATTRIBUTE_mode, "Transparent"); 155 if (c != null && !transparent.equals("Transparent")) 156 { 157 g2.setColor(c); 158 g2.fillRoundRect(x,y,width,heigth,round,round); 159 } 160 161 c = getStyle().getAttributeColor(Style.ATTRIBUTE_forecolor, null); 163 Stroke stroke = ReportElement.getPenStroke(getStyle().getAttributeString(Style.ATTRIBUTE_pen, "Thin"), 1); 164 165 if (c != null && stroke != null) 166 { 167 g2.setStroke(stroke); 168 g2.setColor(c); 169 g2.drawRoundRect(x,y,width,heigth,round,round); 170 } 171 172 173 174 Box box = new Box(); 175 176 if (style.getAttributeString(style.ATTRIBUTE_border, null, true) != null) 177 box.setBorder( style.getAttributeString(style.ATTRIBUTE_border, null, true) ); 178 if (style.getAttributeColor(style.ATTRIBUTE_borderColor, null, true) != null) 179 box.setBorderColor( style.getAttributeColor(style.ATTRIBUTE_borderColor, null, true)); 180 if (style.getAttributeString(style.ATTRIBUTE_padding, null, true) != null) 181 box.setPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_padding, null, true) )); 182 183 if (style.getAttributeString(style.ATTRIBUTE_topBorder, null, true) != null) 184 box.setTopBorder( style.getAttributeString(style.ATTRIBUTE_topBorder, null, true) ); 185 if (style.getAttributeColor(style.ATTRIBUTE_topBorderColor, null, true) != null) 186 box.setTopBorderColor( style.getAttributeColor(style.ATTRIBUTE_topBorderColor, null, true)); 187 if (style.getAttributeString(style.ATTRIBUTE_topPadding, null, true) != null) 188 box.setTopPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_topPadding, null, true) )); 189 190 if (style.getAttributeString(style.ATTRIBUTE_leftBorder, null, true) != null) 191 box.setLeftBorder( style.getAttributeString(style.ATTRIBUTE_leftBorder, null, true) ); 192 if (style.getAttributeColor(style.ATTRIBUTE_leftBorderColor, null, true) != null) 193 box.setLeftBorderColor( style.getAttributeColor(style.ATTRIBUTE_leftBorderColor, null, true)); 194 if (style.getAttributeString(style.ATTRIBUTE_leftPadding, null, true) != null) 195 box.setLeftPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_leftPadding, null, true) )); 196 197 if (style.getAttributeString(style.ATTRIBUTE_rightBorder, null, true) != null) 198 box.setRightBorder( style.getAttributeString(style.ATTRIBUTE_rightBorder, null, true) ); 199 if (style.getAttributeColor(style.ATTRIBUTE_rightBorderColor, null, true) != null) 200 box.setRightBorderColor( style.getAttributeColor(style.ATTRIBUTE_rightBorderColor, null, true)); 201 if (style.getAttributeString(style.ATTRIBUTE_rightPadding, null, true) != null) 202 box.setRightPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_rightPadding, null, true) )); 203 204 if (style.getAttributeString(style.ATTRIBUTE_bottomBorder, null, true) != null) 205 box.setBottomBorder( style.getAttributeString(style.ATTRIBUTE_bottomBorder, null, true) ); 206 if (style.getAttributeColor(style.ATTRIBUTE_bottomBorderColor, null, true) != null) 207 box.setBottomBorderColor( style.getAttributeColor(style.ATTRIBUTE_bottomBorderColor, null, true)); 208 if (style.getAttributeString(style.ATTRIBUTE_bottomPadding, null, true) != null) 209 box.setBottomPadding( Integer.parseInt( style.getAttributeString(style.ATTRIBUTE_bottomPadding, null, true) )); 210 211 212 int ax = x; 213 int ay = y; 214 int bx = x+width; 215 int by = y+heigth; 216 217 Stroke newBoxStroke = null; 218 219 if (box.getLeftBorderColor() != null) 220 { 221 g2.setColor(box.getLeftBorderColor()); 222 } 223 else 224 { 225 g2.setColor( g2.getBackground()); 226 } 227 228 if ((newBoxStroke = ReportElement.getPenStroke(box.getLeftBorder(), 1)) != null) 229 { 230 g2.setStroke(newBoxStroke); 231 g2.drawLine(x, y, x+width, y+heigth); 232 } 233 234 if (box.getTopBorderColor() != null) 236 { 237 g2.setColor(box.getTopBorderColor()); 238 } 239 else 240 { 241 g2.setColor(g2.getBackground()); 242 } 243 244 if ((newBoxStroke = ReportElement.getPenStroke(box.getTopBorder(), 1)) != null) 245 { 246 g2.setStroke(newBoxStroke); 247 g2.drawLine(ax, ay, bx, ay); 248 } 249 250 if (box.getRightBorderColor() != null) 251 { 252 g2.setColor(box.getRightBorderColor()); 253 } 254 else 255 { 256 g2.setColor( g2.getBackground()); 257 } 258 259 if ((newBoxStroke = ReportElement.getPenStroke(box.getRightBorder(), 1)) != null) 260 { 261 g2.setStroke(newBoxStroke); 262 g2.drawLine(bx, ay, bx, by); 263 } 264 265 if (box.getBottomBorderColor() != null) 266 { 267 g2.setColor(box.getBottomBorderColor()); 268 } 269 else 270 { 271 g2.setColor(g2.getBackground()); 272 } 273 274 if ((newBoxStroke = ReportElement.getPenStroke(box.getBottomBorder(), 1)) != null) 275 { 276 g2.setStroke(newBoxStroke); 277 g2.drawLine(ax, by, bx, by); 278 } 279 280 renderText(g2, "This is a\ntest", getStyle(), new Box(), width, heigth); 281 282 } 283 284 public static IReportFont createIreportFont(Style myStyle) { 285 286 if (myStyle.getAttribute("style.font") != null) 287 { 288 return (IReportFont)myStyle.getAttribute("style.font"); 289 } 290 291 292 IReportFont ir = new IReportFont(); 293 294 if (!myStyle.getAttributes().containsKey("style.font") ) 295 { 296 ir.setBold( myStyle.getAttributeBoolean( myStyle.ATTRIBUTE_isBold, false)); 297 ir.setFontName( myStyle.getAttributeString( myStyle.ATTRIBUTE_fontName, ir.getFontName())); 298 ir.setFontSize( myStyle.getAttributeInteger( myStyle.ATTRIBUTE_fontSize,ir.getFontSize())); 299 ir.setItalic( myStyle.getAttributeBoolean( myStyle.ATTRIBUTE_isItalic, false)); 300 301 ir.setPdfEmbedded( myStyle.getAttributeBoolean( myStyle.ATTRIBUTE_isPdfEmbedded, false)); 302 ir.setPdfEncoding( myStyle.getAttributeString( myStyle.ATTRIBUTE_pdfEncoding, ir.getPdfEncoding())); 303 ir.setStrikeTrought( myStyle.getAttributeBoolean( myStyle.ATTRIBUTE_isStrikeThrough, false)); 304 305 ir.setPDFFontName( myStyle.getAttributeString( myStyle.ATTRIBUTE_pdfFontName, ir.getPDFFontName())); 307 ir.setUnderline( myStyle.getAttributeBoolean( myStyle.ATTRIBUTE_isUnderline, false)); 308 } 309 return ir; 310 } 311 312 313 private class TextReportElementLayout { 314 private TextLayout layout; 315 private float x; 316 private float y; 317 318 319 private TextReportElementLayout(TextLayout layout, float x, float y) { 320 this.layout = layout; 321 this.x = x; 322 this.y = y; 323 } 324 325 326 void drawWithOffset(Graphics2D g2, float yOffset) { 327 layout.draw(g2, x, y + yOffset); 328 } 329 } 330 331 334 335 public void renderText(Graphics2D g, String str, Style s, Box box, int width, int height) 336 { 337 338 IReportFont ir = createIreportFont(s); 339 Font font = ir.getJavaAWTFont(); 340 341 double zoom_factor = 1.0; 342 int x_shift_origin = 0; 343 int y_shift_origin = 0; 344 345 ArrayList textLayouts; 347 float x, y; 348 TextReportElementLayout textReportElementLayout; 349 AffineTransform transform; 350 351 Point position = new Point (5,5); 352 355 g.setColor( s.getAttributeColor( s.ATTRIBUTE_forecolor, Color.BLACK ) ); 356 357 360 361 363 364 int gfx_x = position.x + box.getLeftPadding()-x_shift_origin; 365 int gfx_y = position.y + box.getTopPadding()-y_shift_origin; 366 int gfx_width = width - box.getLeftPadding() - box.getRightPadding(); 367 int gfx_height = height - box.getTopPadding() - box.getBottomPadding(); 368 369 370 Java2DUtil.setClip(g, 371 gfx_x, 373 gfx_y, 374 gfx_width, 375 gfx_height); 376 377 double angle = 0; 378 double angle_restore = 0; 379 380 381 transform = null; 384 if(s.getAttributeString( s.ATTRIBUTE_rotation, "None" ).equals(ROTATION_LEFT.getName())) 385 { 386 transform = new AffineTransform (); 387 transform.rotate(-Math.PI / 2, gfx_x, gfx_y); 388 transform.translate(-gfx_height, -gfx_height); 389 gfx_y = position.y-y_shift_origin + height; 390 gfx_width = height; 391 gfx_height = width; 392 Java2DUtil.setTransform(g, transform); 393 } 394 else if(s.getAttributeString( s.ATTRIBUTE_rotation, "None" ).equals(ROTATION_RIGHT.getName())) 395 { 396 transform = new AffineTransform (); 397 transform.rotate(Math.PI / 2, gfx_x, gfx_y); 398 transform.translate(0, -gfx_width); 399 gfx_x = position.x-x_shift_origin + width; 400 gfx_width = height; 401 gfx_height = width; 402 403 Java2DUtil.setTransform(g, transform); 404 } 405 406 407 409 411 418 419 if (str != null && str.length() > 0) 420 { 421 int zoomedFieldHeight = gfx_height; 422 String allText = Misc.treatNewLineChars(str); 423 float formatWidth = (float)gfx_width; 424 float verticalOffset = 0f; 425 426 427 FontRenderContext fontRenderContext = g.getFontRenderContext(); 428 java.util.Map fontAttributes = font.getAttributes(); 429 fontAttributes.put(TextAttribute.SIZE, new Float (ir.getFontSize() ) ); 430 fontAttributes.put(TextAttribute.FAMILY, ir.getFontName() ); 431 if (ir.isBold()) 432 { 433 fontAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD); 434 } 435 if (ir.isItalic()) 436 { 437 fontAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); 438 } 439 if (ir.isUnderline()) 440 { 441 fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); 442 } 443 if (ir.isStrikeTrought()) 444 { 445 fontAttributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); 446 } 447 448 float lineSpacing = 1f; 449 String lineSpacingName = s.getAttributeString( s.ATTRIBUTE_lineSpacing, "Single" ); 450 if (lineSpacingName.equals("Single")) lineSpacing = 1f; 451 else if (lineSpacingName.equals("1_1_2")) lineSpacing = 1.5f; 452 else if (lineSpacingName.equals("Double")) lineSpacing = 2f; 453 454 AttributedString atext; 455 AttributedCharacterIterator paragraph; 456 int paragraphStart; 457 int paragraphEnd; 458 LineBreakMeasurer lineMeasurer; 459 TextLayout layout = null; 460 461 462 String paragr_text = ""; 463 boolean isMaxHeightReached = false; 464 465 StringTokenizer tkzer = new StringTokenizer (allText, "\n"); 466 467 float drawPosY = 0; 468 float drawPosX = 0; 469 470 paragr_text = ""; 471 isMaxHeightReached = false; 472 473 tkzer = new StringTokenizer (allText, "\n"); 474 475 476 textLayouts = new ArrayList (); 477 478 while(tkzer.hasMoreTokens() && !isMaxHeightReached) 481 { 482 paragr_text = tkzer.nextToken(); 483 484 atext = new AttributedString (paragr_text, fontAttributes); 485 paragraph = atext.getIterator(); 486 paragraphStart = paragraph.getBeginIndex(); 487 paragraphEnd = paragraph.getEndIndex(); 488 lineMeasurer = new LineBreakMeasurer (paragraph, fontRenderContext); 489 lineMeasurer.setPosition(paragraphStart); 490 491 layout = null; 492 while (lineMeasurer.getPosition() < paragraphEnd && !isMaxHeightReached) 493 { 494 layout = lineMeasurer.nextLayout(formatWidth); 495 496 drawPosY += layout.getLeading() + lineSpacing * layout.getAscent(); 497 498 if (drawPosY + layout.getDescent() <= zoomedFieldHeight+1) 499 { 500 if (s.getAttributeString( s.ATTRIBUTE_hAlign, "Left" ).equals("Justify")) 501 { 502 if (layout.isLeftToRight()) 503 { 504 drawPosX = 0; 505 } 506 else 507 { 508 drawPosX = formatWidth - layout.getAdvance(); 509 } 510 if (lineMeasurer.getPosition() < paragraphEnd) 511 { 512 layout = layout.getJustifiedLayout(formatWidth); 513 } 514 } 515 else if (s.getAttributeString( s.ATTRIBUTE_hAlign, "Left" ).equals("Right")) 516 { 517 if (layout.isLeftToRight()) 518 { 519 drawPosX = formatWidth - layout.getAdvance(); 520 } 521 else 522 { 523 drawPosX = formatWidth; 524 } 525 } 526 else if (s.getAttributeString( s.ATTRIBUTE_hAlign, "Left" ).equals("Center")) 527 { 528 drawPosX = (formatWidth - layout.getAdvance()) / 2; 529 } 530 else { 532 if (layout.isLeftToRight()) 533 { 534 drawPosX = 0; 535 } 536 else 537 { 538 drawPosX = formatWidth - layout.getAdvance(); 539 } 540 } 541 542 x = drawPosX + gfx_x; y = drawPosY + gfx_y; textReportElementLayout = new TextReportElementLayout(layout, x, y); 545 textLayouts.add(textReportElementLayout); 546 547 548 drawPosY += layout.getDescent(); 549 } 550 else 551 { 552 drawPosY -= layout.getLeading() + lineSpacing * layout.getAscent(); 553 isMaxHeightReached = true; 554 } 555 } 556 } 557 558 559 float textHeight = drawPosY; 561 if (s.getAttributeString( s.ATTRIBUTE_vAlign, "Top" ).equals("Top")) 562 { 563 verticalOffset = 0f; 564 } 565 else if (s.getAttributeString( s.ATTRIBUTE_vAlign, "Top" ).equals("Middle")) 566 { 567 verticalOffset = ((float)zoomedFieldHeight- textHeight) / 2f; 568 } 569 else if (s.getAttributeString( s.ATTRIBUTE_vAlign, "Top" ).equals("Bottom")) 570 { 571 verticalOffset = (float)zoomedFieldHeight - (float)textHeight; 572 } 573 574 575 576 579 580 for(Iterator i=textLayouts.iterator(); i.hasNext();) 582 { 583 textReportElementLayout = (TextReportElementLayout) i.next(); 584 textReportElementLayout.drawWithOffset(g, verticalOffset); 585 } 586 587 588 if(transform != null) 589 { 590 Java2DUtil.resetTransform(g); 592 } 593 594 595 611 614 } 615 616 617 618 621 Java2DUtil.resetClip(g); 622 623 } 624 625 static class Rotation { 626 static private ArrayList rotations; 627 private String name; 628 private int number; 629 630 631 Rotation(String name, int number) { 632 this.name = name; 633 this.number = number; 634 rotations = new ArrayList (); 635 rotations.add(this); 636 } 637 638 639 public int getNumber() { 640 return number; 641 } 642 643 644 public String getName() { 645 return name; 646 } 647 648 649 public String toString() { 650 return getName(); 651 } 652 } 653 654 static List getRotations() { 655 return Rotation.rotations; 656 } 657 } 658 | Popular Tags |