1 32 33 package it.businesslogic.ireport.gui; 34 35 import java.awt.event.MouseEvent ; 36 import java.awt.geom.AffineTransform ; 37 import javax.swing.*; 38 import java.awt.*; 39 import it.businesslogic.ireport.util.*; 40 import java.awt.event.MouseListener ; 41 42 46 public class JRulePanel extends JPanel { 47 48 public static int TYPE_HORIZONTAL = 0; 49 public static int TYPE_VERTICAL = 1; 50 51 private boolean fontSet = false; 52 private JReportFrame jReportFrame = null; 53 54 private int type = TYPE_HORIZONTAL; 55 56 private int cursorPosition; 57 58 private boolean dragging = false; 59 private java.awt.image.BufferedImage savedImage = null; 60 private ImageIcon horizontalRuleStopIcon = null; 61 62 private java.util.List guideLines = new java.util.ArrayList (); 63 private int lastTempGuidePosition = -1; 64 private Stroke dottedStroke = null; 65 66 67 68 public JRulePanel() { 69 setCursorPosition(-1); 70 Font f = this.getFont(); 71 this.setFont( new Font( f.getName(), 0, 10)); 73 setDottedStroke((Stroke) new BasicStroke((float) (1f), 74 BasicStroke.CAP_BUTT, 75 BasicStroke.JOIN_BEVEL, 0f, 76 new float[] { 1f, 1f }, 0f)); 77 78 horizontalRuleStopIcon = new javax.swing.ImageIcon (getClass().getResource("/it/businesslogic/ireport/icons/toolbars/rulestop.png")); 79 80 addMouseMotionListener(new java.awt.event.MouseMotionAdapter () { 81 public void mouseDragged(java.awt.event.MouseEvent evt) { 82 panelMouseDragged(evt); 83 } 84 public void mouseMoved(java.awt.event.MouseEvent evt) { 85 panelMouseMoved(evt); 86 } 87 }); 88 89 addMouseListener(new java.awt.event.MouseAdapter () { 90 public void mousePressed(java.awt.event.MouseEvent evt) { 91 panelMousePressed(evt); 92 } 93 public void mouseReleased(java.awt.event.MouseEvent evt) { 94 panelMouseReleased(evt); 95 } 96 }); 97 } 98 99 public void setRightFont(Graphics g) 100 { 101 if (fontSet) return; 102 103 if (g == null) return; 104 109 } 112 113 137 public void paint(Graphics g) { 138 139 if (getJReportFrame() == null) return; 140 setRightFont(g); 141 g.setFont( this.getFont()); 142 setCursorPosition(-1); 143 144 g.setColor(this.getBackground()); 146 g.fillRect(0,0, this.getWidth(), this.getHeight()); 147 148 String unitName = MainFrame.getMainInstance().getProperties().getProperty("DefaultUnit","cm"); 149 double unit = Unit.CENTIMETERS; 150 if (unitName.equals("cm")) unit = Unit.CENTIMETERS; 151 else if (unitName.equals("pixels")) unit = Unit.PIXEL; 152 else if (unitName.equals("mm")) unit = Unit.MILLIMETERS; 153 else if (unitName.equals("inches")) unit = Unit.INCHES; 154 155 g.setColor(new Color( 255,255,255)); 156 157 158 double k=0; 159 160 double zoomfactor = getJReportFrame().getZoomFactor(); 161 162 int line=0; 163 int i=0; 164 int oldi=-100; 166 double module = 2; 167 168 boolean isMillimeters = (unit == Unit.MILLIMETERS); 169 if (isMillimeters) 170 { 171 unit = Unit.CENTIMETERS; 172 } 173 174 boolean isPixel = false; 175 int tick_space = 50; 177 if (unit == Unit.PIXEL) 178 { 179 isPixel = true; 180 unit = 50; 181 tick_space = 100; 182 } 183 184 if (((int)(convertUnitToPixel(1,unit)*zoomfactor)) >= tick_space) 185 { 186 module = 10; 187 } 188 189 if (getType() == TYPE_HORIZONTAL) 190 { 191 int x1=10 + getJReportFrame().getZoomedDim( getJReportFrame().getReport().getLeftMargin()) - getJReportFrame().getHScroll(); 192 int x2=10 + getJReportFrame().getZoomedDim( getJReportFrame().getReport().getWidth() - getJReportFrame().getReport().getRightMargin() ) - getJReportFrame().getHScroll(); 193 int x3=10 + getJReportFrame().getZoomedDim( getJReportFrame().getReport().getWidth()) - getJReportFrame().getHScroll()-1; 194 195 g.fillRect(Math.max(x1,0) ,0, Math.min(x2, this.getWidth()) - Math.max(x1,0), this.getHeight()); 196 197 java.awt.Color c = this.getBackground().darker(); 198 if (x1 > 0) 199 { 200 g.setColor(c); 201 g.fillRect(Math.max(10 - getJReportFrame().getHScroll(),0), 0, x1-Math.max(10 - getJReportFrame().getHScroll(),0), this.getHeight()); 202 g.setColor(c.darker()); 203 g.drawRect(Math.max(10 - getJReportFrame().getHScroll(),0), 0, x1-Math.max(10 - getJReportFrame().getHScroll(),0), this.getHeight()); 204 } 205 206 if (x2 < this.getWidth()) 207 { 208 g.setColor(c); 209 g.fillRect(x2, 0, Math.min(this.getWidth(), x3) - x2, this.getHeight()); 210 g.setColor(c.darker()); 211 g.drawRect(x2, 0, Math.min(this.getWidth(), x3) - x2, this.getHeight()); 212 } 213 214 g.setColor(new Color(0,0,0)); 215 g.drawLine(0,this.getHeight()-1, this.getWidth(), this.getHeight()-1); 216 217 i = x1; 218 219 220 while (i< this.getWidth()) 221 { 222 if (i>=0) 223 { 224 if ((line%module)==0) 225 { 226 if (i-oldi > 20) 227 { 228 String s = ""+(int)k; 229 if (isMillimeters) s += "0"; 230 if (isPixel) s = ""+((int)k*50); 231 232 int w = g.getFontMetrics().stringWidth(s); 233 g.drawString(s ,i - (w/2), (g.getFontMetrics().getHeight()/2)+3); 234 oldi= i; 236 g.drawLine(i,16,i,12); 237 } 238 else 239 { 240 g.drawLine(i,5,i,10); 241 } 242 243 } 244 else 245 { 246 if (module == 10 && (line%5) !=0) 247 { 248 g.drawLine(i,7,i,8); 249 } 250 else 251 { 252 g.drawLine(i,6,i,9); 253 } 254 } 255 } 256 line++; 257 k = line*(1.0/module); 258 i = x1 + (int)(convertUnitToPixel(k,unit) * zoomfactor); 259 260 261 } 262 263 264 line=1; 265 k = 1.0/module; 266 oldi=x1; 267 i = x1 - (int)(convertUnitToPixel(k,unit) * zoomfactor); 268 while (x1 > 0 && i > -10) 269 { 270 if (i>=0) 271 { 272 if ((line%module)==0) 273 { 274 if (oldi-i > 20) 275 { 276 String s = ""+(int)k; 277 if (isMillimeters) s += "0"; 278 if (isPixel) s = ""+((int)k*50); 279 280 int w = g.getFontMetrics().stringWidth(s); 281 g.drawString(s ,i - (w/2), (g.getFontMetrics().getHeight()/2)+3); 282 oldi= i; 283 g.drawLine(i,16,i,12); 284 } 285 else 286 { 287 g.drawLine(i,5,i,10); 288 } 289 290 } 291 else 292 { 293 if (module == 10 && (line%5) !=0) 294 { 295 g.drawLine(i,7,i,8); 296 } 297 else 298 { 299 g.drawLine(i,6,i,9); 300 } 301 } 302 } 303 line++; 304 k = line*(1.0/module); 305 i = x1 - (int)(convertUnitToPixel(k,unit) * zoomfactor); 306 } 307 308 for (i=0; i<getGuideLines().size(); ++i) 309 { 310 Integer pos = (Integer )getGuideLines().get(i); 311 int posI = pos.intValue(); 312 posI = 10 + (int)(posI*zoomfactor) - getJReportFrame().getHScroll(); 314 g.drawImage(horizontalRuleStopIcon.getImage(),posI-4, 7, this); 315 } 316 317 } 318 else { 320 int y1=10 + getJReportFrame().getZoomedDim( getJReportFrame().getReport().getTopMargin()) - getJReportFrame().getVScroll(); 321 int y2=10 + getJReportFrame().getZoomedDim( getJReportFrame().getReport().getDesignHeight() - getJReportFrame().getReport().getBottomMargin() ) - getJReportFrame().getVScroll(); 322 int y3=10 + getJReportFrame().getZoomedDim( getJReportFrame().getReport().getDesignHeight()) - getJReportFrame().getVScroll()-1; 323 324 g.fillRect(0, Math.max(y1,0), this.getWidth(), Math.min(y2, this.getHeight()) - Math.max(y1,0)); 325 326 java.awt.Color c = this.getBackground().darker(); 327 if (y1 > 0) 328 { 329 g.setColor(c); 330 g.fillRect(0, Math.max(10 - getJReportFrame().getVScroll(),0), this.getWidth(), y1-Math.max(10 - getJReportFrame().getVScroll(),0)); 331 g.setColor(c.darker()); 332 g.drawRect(0, Math.max(10 - getJReportFrame().getVScroll(),0), this.getWidth(), y1-Math.max(10 - getJReportFrame().getVScroll(),0)); 333 } 334 335 if (y2 < this.getHeight()) 336 { 337 g.setColor(c); 338 g.fillRect(0,y2, this.getWidth(), Math.min(this.getHeight(), y3) - y2); 339 g.setColor(c.darker()); 340 g.drawRect(0,y2, this.getWidth(), Math.min(this.getHeight(), y3) - y2); 341 } 342 343 g.setColor(new Color(0,0,0)); 344 g.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()); 345 346 347 i=y1; 348 349 while (i< this.getHeight()) 350 { 351 if (i>=0) 352 { 353 if ((line%module)==0) 354 { 355 if (i-oldi > 20) 356 { 357 String s = ""+(int)k; 358 if (isMillimeters) s += "0"; 359 if (isPixel) s = ""+((int)k*50); 360 writeRotateString((Graphics2D)g, 4, i, s); 361 oldi= i; 363 g.drawLine(16,i,12,i); 364 } 365 else 366 { 367 g.drawLine(5,i,10,i); 368 } 369 370 } 371 else 372 { 373 if (module == 10 && (line%5) !=0) 374 { 375 g.drawLine(7,i,8,i); 376 } 377 else 378 { 379 g.drawLine(6,i,9,i); 380 } 381 } 382 } 383 line++; 384 k = line*(1.0/module); 385 i = y1 + (int)(convertUnitToPixel(k,unit) * zoomfactor); 386 } 387 388 389 390 line=1; 391 k = 1.0/module; 392 oldi=y1; 393 i = y1 - (int)(convertUnitToPixel(k,unit) * zoomfactor); 394 while (y1 > 0 && i > -10) 395 { 396 if (i>=0) 397 { 398 if ((line%module)==0) 399 { 400 if (i-oldi > 20) 401 { 402 String s = ""+(int)k; 403 if (isMillimeters) s += "0"; 404 if (isPixel) s = ""+((int)k*50); 405 406 int w = g.getFontMetrics().stringWidth(s); 407 writeRotateString((Graphics2D)g, 4, i, s); 408 oldi= i; 410 g.drawLine(16,i,12,i); 411 } 412 else 413 { 414 g.drawLine(5,i,10,i); 415 } 416 417 } 418 else 419 { 420 if (module == 10 && (line%5) !=0) 421 { 422 g.drawLine(7,i,8,i); 423 } 424 else 425 { 426 g.drawLine(6,i,9,i); 427 } 428 } 429 } 430 line++; 431 k = line*(1.0/module); 432 i = y1 - (int)(convertUnitToPixel(k,unit) * zoomfactor); 433 } 434 435 for (i=0; i<getGuideLines().size(); ++i) 436 { 437 Integer pos = (Integer )getGuideLines().get(i); 438 int posI = pos.intValue(); 439 posI = 10 + (int)(posI*zoomfactor) - getJReportFrame().getHScroll(); 441 g.drawImage(horizontalRuleStopIcon.getImage(),7,posI-4, this); 442 } 443 444 } 445 } 446 447 448 449 public void writeRotateString(Graphics2D g2, int x, int yCenter, String s) 450 { 451 452 453 java.awt.geom.Rectangle2D sb = g2.getFontMetrics().getStringBounds(s, g2) ; 454 int sw = (int)sb.getWidth(); 455 int sh = (int)(sb.getHeight()/2); 456 457 459 460 AffineTransform oldAr = g2.getTransform(); 461 462 int rotX = x; 463 int rotY = yCenter; 464 465 AffineTransform at = g2.getTransform(); 466 at.rotate(-Math.PI/2.0, x, yCenter); 467 at.translate(-(sw/2),sh); 468 g2.setTransform(at); 471 472 g2.drawString(s,x,yCenter); g2.setTransform(oldAr); 474 475 } 476 477 481 public int getCursorPosition() { 482 return cursorPosition; 483 } 484 485 489 public void setCursorPosition(int cursorPosition) { 490 Graphics g = this.getGraphics(); 491 if (g==null) return; 492 g.setXORMode(Color.WHITE); 493 494 if (getType() == TYPE_HORIZONTAL) 495 { 496 g.drawLine(this.cursorPosition, 0,this.cursorPosition, this.getHeight()); 497 this.cursorPosition = cursorPosition; 498 g.drawLine(this.cursorPosition, 0,this.cursorPosition, this.getHeight()); 499 } 500 else 501 { 502 g.drawLine( 0, this.cursorPosition, this.getWidth(), this.cursorPosition); 503 this.cursorPosition = cursorPosition; 504 g.drawLine(0, this.cursorPosition, this.getWidth(), this.cursorPosition); 505 } 506 507 g.setPaintMode(); 508 } 509 510 public JReportFrame getJReportFrame() { 511 return jReportFrame; 512 } 513 514 public void setJReportFrame(JReportFrame jReportFrame) { 515 this.jReportFrame = jReportFrame; 516 } 517 518 public int convertUnitToPixel( double value, double unit) 519 { 520 if (unit == Unit.PIXEL) return (int)value; 521 return (int)Unit.convertToPixels(value,unit); 522 } 523 524 public int getType() { 525 return type; 526 } 527 528 public void setType(int type) { 529 if (type != this.type) 530 { 531 horizontalRuleStopIcon = new javax.swing.ImageIcon (getClass().getResource("/it/businesslogic/ireport/icons/toolbars/rulestop" + ((type == TYPE_VERTICAL) ? "v" : "") + ".png")); 532 this.type = type; 533 } 534 } 535 536 public void panelMousePressed(java.awt.event.MouseEvent evt) { 537 538 int currentLine = -1; 540 for (int i=0; i<getGuideLines().size(); ++i) 541 { 542 Integer pos = (Integer )getGuideLines().get(i); 543 int posI = pos.intValue(); 544 int scroll = (getType() == TYPE_HORIZONTAL) ? getJReportFrame().getHScroll() : getJReportFrame().getVScroll(); 546 posI = 10 + (int)(posI*getJReportFrame().getZoomFactor()) - scroll; 547 548 int mousePos = (getType() == TYPE_HORIZONTAL) ? evt.getX() : evt.getY(); 549 550 if (mousePos > posI-3 && mousePos < posI+3) 551 { 552 currentLine = posI; 553 getGuideLines().remove(i); 554 break; 555 } 556 } 557 558 savedImage = ((java.awt.image.BufferedImage )this.createImage(this.getWidth(), this.getHeight())); 559 Graphics2D savedGraphics = savedImage.createGraphics(); 560 this.paint( savedGraphics ); 561 562 if (getType() == TYPE_HORIZONTAL) this.getGraphics().drawImage(horizontalRuleStopIcon.getImage(),evt.getX()-4, 7, this); 563 else this.getGraphics().drawImage(horizontalRuleStopIcon.getImage(),7, evt.getY()-4, this); 564 565 lastTempGuidePosition = (getType() == TYPE_HORIZONTAL) ? evt.getX() : evt.getY(); 566 Graphics2D jrfGraphics = (Graphics2D)this.getJReportFrame().getReportPanel().getGraphics(); 567 Stroke oldStroke = jrfGraphics.getStroke(); 568 jrfGraphics.setXORMode(java.awt.Color.YELLOW); 569 jrfGraphics.setStroke(getDottedStroke()); 570 if (lastTempGuidePosition >= 0) 571 { 572 if (getType() == TYPE_HORIZONTAL) jrfGraphics.drawLine(currentLine, 0,currentLine,this.getJReportFrame().getHeight()); 573 else jrfGraphics.drawLine(0,currentLine, this.getJReportFrame().getWidth(), currentLine); 574 } 575 if (getType() == TYPE_HORIZONTAL) jrfGraphics.drawLine(lastTempGuidePosition, 0,lastTempGuidePosition,this.getJReportFrame().getHeight()); 576 else jrfGraphics.drawLine(0,lastTempGuidePosition, this.getJReportFrame().getWidth(), lastTempGuidePosition); 577 jrfGraphics.setPaintMode(); 578 jrfGraphics.setStroke(oldStroke); 579 } 580 581 public void panelMouseReleased(java.awt.event.MouseEvent evt) { 582 583 this.getGraphics().drawImage(savedImage,0, 0, this); 584 585 if (getType() == TYPE_HORIZONTAL) 586 { 587 if (evt.getX() > 0 && evt.getX() < this.getWidth()) 588 { 589 int newPosition = getJReportFrame().getLogicalDim((int)evt.getX()+getJReportFrame().getHScroll()-10); 590 this.getGuideLines().add( new Integer (newPosition) ); 591 this.repaint(); 592 this.getJReportFrame().repaint(); 593 } 594 } 595 else 596 { 597 if (evt.getY() > 0 && evt.getY() < this.getHeight()) 598 { 599 int newPosition = getJReportFrame().getLogicalDim((int)evt.getY()+getJReportFrame().getVScroll()-10); 600 this.getGuideLines().add( new Integer (newPosition) ); 601 this.repaint(); 602 this.getJReportFrame().repaint(); 603 } 604 } 605 } 606 607 public void panelMouseDragged(MouseEvent e) { 608 609 this.getGraphics().drawImage(savedImage,0, 0, this); 610 611 if (getType() == TYPE_HORIZONTAL) 612 { 613 this.getGraphics().drawImage(horizontalRuleStopIcon.getImage(),e.getX()-4, 7, this); 614 615 } 616 else 617 { 618 this.getGraphics().drawImage(horizontalRuleStopIcon.getImage(),7, e.getY()-4, this); 619 } 620 621 Graphics2D jrfGraphics = (Graphics2D)this.getJReportFrame().getReportPanel().getGraphics(); 622 Stroke oldStroke = jrfGraphics.getStroke(); 623 jrfGraphics.setXORMode(java.awt.Color.YELLOW); 624 jrfGraphics.setStroke(getDottedStroke()); 625 if (lastTempGuidePosition >= 0) 626 { 627 if (getType() == TYPE_HORIZONTAL) jrfGraphics.drawLine(lastTempGuidePosition, 0,lastTempGuidePosition,this.getJReportFrame().getHeight()); 628 else jrfGraphics.drawLine(0,lastTempGuidePosition, this.getJReportFrame().getWidth(), lastTempGuidePosition); 629 } 630 lastTempGuidePosition = (getType() == TYPE_HORIZONTAL) ? e.getX() : e.getY(); 631 if (getType() == TYPE_HORIZONTAL) jrfGraphics.drawLine(lastTempGuidePosition, 0,lastTempGuidePosition,this.getJReportFrame().getHeight()); 632 else jrfGraphics.drawLine(0,lastTempGuidePosition, this.getJReportFrame().getWidth(), lastTempGuidePosition); 633 jrfGraphics.setPaintMode(); 634 jrfGraphics.setStroke(oldStroke); 635 } 636 637 public void panelMouseMoved(MouseEvent e) { 638 setCursorPosition( getType() == TYPE_HORIZONTAL ? e.getX() : e.getY()); 639 } 640 641 public boolean isDragging() { 642 return dragging; 643 } 644 645 public void setDragging(boolean dragging) { 646 this.dragging = dragging; 647 } 648 649 public java.util.List getGuideLines() { 650 return guideLines; 651 } 652 653 public void setGuideLines(java.util.List guideLines) { 654 this.guideLines = guideLines; 655 } 656 657 public Stroke getDottedStroke() { 658 return dottedStroke; 659 } 660 661 public void setDottedStroke(Stroke dottedStroke) { 662 this.dottedStroke = dottedStroke; 663 } 664 } 665 | Popular Tags |