1 22 23 24 package net.sourceforge.chart2d; 25 26 27 import java.awt.*; 28 29 30 40 class TitledArea extends FontArea { 41 42 43 private boolean titleAutoLocate; 44 private boolean titleExistence; 45 private TextArea title; 46 private Rectangle maxEntitledSpaceBounds; 47 private Rectangle minEntitledSpaceBounds; 48 private Color titleBackgroundColor; 49 private boolean betweenTitleAndSpaceGapExistence; 50 private int betweenTitleAndSpaceGapThicknessModel; 51 private int betweenTitleAndSpaceGapThickness; 52 private Dimension minUsedSpaceSize; 53 private boolean needsUpdate; 54 55 56 70 TitledArea() { 71 72 title = new TextArea(); 73 maxEntitledSpaceBounds = new Rectangle(); 74 minEntitledSpaceBounds = new Rectangle(); 75 76 setTitleExistence (true); 77 setFontPointModel (14); 78 setBetweenTitleAndSpaceGapExistence (true); 79 setBetweenTitleAndSpaceGapThicknessModel (5); 80 setTitleJustifications (CENTER, TOP); 81 setTitleAutoLocate (true); 82 83 title.setAutoJustifys (false, false); 84 title.setBorderExistences (false, false, false, false); 85 title.setGapExistences (false, false, false, false); 86 title.setBackgroundExistence (false); 87 resetTitledAreaModel (true); 88 needsUpdate = true; 89 } 90 91 92 98 final void setTitleExistence (boolean existence) { 99 100 needsUpdate = true; 101 titleExistence = existence; 102 } 103 104 105 109 final void setTitle (String title) { 110 111 needsUpdate = true; 112 this.title.setText (title); 113 } 114 115 116 122 final void setTitleAutoLocate (boolean auto) { 123 124 needsUpdate = true; 125 titleAutoLocate = auto; 126 } 127 128 129 134 final void setTitleLocation (Point location) { 135 136 needsUpdate = true; 137 title.setSpaceSizeLocation (MIN, location); 138 } 139 140 141 149 final void setTitleRotateLeft (boolean rotate) { 150 151 needsUpdate = true; 152 title.setRotateLeft (rotate); 153 } 154 155 156 167 final void setTitleJustifications (int horizontal, int vertical) { 168 169 needsUpdate = true; 170 title.setJustifications (horizontal, vertical); 171 } 172 173 174 183 final void setBetweenTitleAndSpaceGapExistence (boolean existence) { 184 185 needsUpdate = true; 186 betweenTitleAndSpaceGapExistence = existence; 187 } 188 189 190 202 final void setBetweenTitleAndSpaceGapThicknessModel (int gap) { 203 204 needsUpdate = true; 205 betweenTitleAndSpaceGapThicknessModel = gap; 206 } 207 208 209 213 final int getBetweenTitleAndSpaceGapThicknessModel() { 214 215 return betweenTitleAndSpaceGapThicknessModel; 216 } 217 218 219 226 final Rectangle getMaxEntitledSpaceBounds (Graphics2D g2D) { 227 228 updateTitledArea (g2D); 229 return maxEntitledSpaceBounds; 230 } 231 232 233 240 final Rectangle getMinEntitledSpaceBounds (Graphics2D g2D) { 241 updateTitledArea (g2D); 242 return minEntitledSpaceBounds; 243 } 244 245 246 250 final boolean getTitleExistence () { 251 252 return titleExistence; 253 } 254 255 256 260 final String getTitleText() { 261 262 return title.getText(); 263 } 264 265 266 270 final TextArea getTitle() { 271 return title; 272 } 273 274 275 283 final Dimension getTitleSize (int which, Graphics2D g2D) { 284 285 updateTitledArea (g2D); 286 return title.getSize (which); 287 } 288 289 290 299 final int getTitleJustifications (int which) { 300 301 return title.getJustifications (which); 302 } 303 304 305 311 final boolean getTitleAutoLocate () { 312 313 return titleAutoLocate; 314 } 315 316 317 324 final int getBetweenTitleAndSpaceGapThickness (Graphics2D g2D) { 325 326 updateTitledArea (g2D); 327 return betweenTitleAndSpaceGapThickness; 328 } 329 330 331 335 final boolean getTitledAreaNeedsUpdate() { 336 337 return (needsUpdate || getFontAreaNeedsUpdate()); 338 } 339 340 341 346 final void updateTitledArea (Graphics2D g2D) { 347 348 if (getTitledAreaNeedsUpdate()) { 349 updateFontArea(); 350 update (g2D); 351 } 352 needsUpdate = false; 353 } 354 355 356 367 final void resetTitledAreaModel (boolean reset) { 368 369 needsUpdate = true; 370 resetFontAreaModel (reset); 371 title.resetTextAreaModel (reset); 372 } 373 374 375 379 void paintComponent (Graphics2D g2D) { 380 381 updateTitledArea (g2D); 382 super.paintComponent (g2D); 383 title.paintComponent (g2D); 384 } 385 386 387 private void update (Graphics2D g2D) { 388 updateMaxTitle(); 389 title.updateTextArea (g2D); 390 updateGap(); 391 updateMaxBounds (g2D); 392 updateMinBounds (g2D); 393 updateMinTitle(); 394 } 395 396 397 private void updateMaxTitle() { 398 399 if (titleExistence) { 400 title.setCustomRatio (WIDTH, true, getRatio (WIDTH)); 401 title.setCustomRatio (HEIGHT, true, getRatio (HEIGHT)); 402 title.setAutoSizes (getAutoSize (MAXMODEL), false); 403 title.setSizeLocation (MAX, new Point ( 404 getSpaceSizeLocation (MAX).x, getSpaceSizeLocation (MAX).y)); 405 Dimension titleSize = new Dimension ( 406 getSpaceSize (MAX).width, getSpaceSize (MAX).height); 407 title.setSize (MAX, titleSize); 408 title.setFontPointModel (getFontPointModel()); 409 title.setFontColor (getFontColor()); 410 title.setFontName (getFont().getName()); 411 title.setFontStyle (getFont().getStyle()); 412 } 413 else title.setSize (MAX, new Dimension()); 414 } 415 416 417 private void updateGap() { 418 419 if (betweenTitleAndSpaceGapExistence) { 420 421 float ratio; 422 int available; 423 if (!title.getRotateLeft()) { 424 ratio = getRatio (HEIGHT); 425 available = getSpaceSize (MAX).height - title.getSize (MIN).height; 426 } 427 else { 428 ratio = getRatio (WIDTH); 429 available = getSpaceSize (MAX).width - title.getSize (MIN).width; 430 } 431 432 if (titleExistence && 433 title.getSize (MIN).width > 0 && title.getSize (MIN).height > 0) { 434 betweenTitleAndSpaceGapThickness = 435 applyRatio (betweenTitleAndSpaceGapThicknessModel, ratio); 436 betweenTitleAndSpaceGapThickness = 437 betweenTitleAndSpaceGapThickness < available ? 438 betweenTitleAndSpaceGapThickness : available; 439 } 440 else betweenTitleAndSpaceGapThickness = 0; 441 } 442 443 else betweenTitleAndSpaceGapThickness = 0; 444 } 445 446 447 private void updateMaxBounds (Graphics2D g2D) { 448 449 int spaceX; 450 int spaceY; 451 int spaceWidth; 452 int spaceHeight; 453 if (!title.getRotateLeft()) { 454 spaceX = getSpaceSizeLocation (MAX).x; 455 spaceWidth = getSpaceSize (MAX).width; 456 spaceHeight = getSpaceSize (MAX).height - 457 (title.getSize (MIN).height + betweenTitleAndSpaceGapThickness); 458 if (title.getJustifications (VERTICAL) == TOP) { 459 spaceY = getSpaceSizeLocation (MAX).y + 460 title.getSize (MIN).height + betweenTitleAndSpaceGapThickness; 461 } 462 else spaceY = getSpaceSizeLocation (MAX).y; 463 } 464 else { 465 spaceY = getSpaceSizeLocation (MAX).y; 466 spaceWidth = getSpaceSize (MAX).width - 467 (title.getSize (MIN).width + betweenTitleAndSpaceGapThickness); 468 spaceHeight = getSpaceSize (MAX).height; 469 if (title.getJustifications (HORIZONTAL) == LEFT) { 470 spaceX = getSpaceSizeLocation (MAX).x + 471 title.getSize (MIN).width + betweenTitleAndSpaceGapThickness; 472 } 473 else spaceX = getSpaceSizeLocation (MAX).x; 474 } 475 maxEntitledSpaceBounds.setBounds (spaceX, spaceY, spaceWidth, spaceHeight); 476 } 477 478 479 private void updateMinBounds (Graphics2D g2D) { 480 481 int spaceX; 482 int spaceY; 483 int spaceWidth; 484 int spaceHeight; 485 if (!title.getRotateLeft()) { 486 spaceX = getSpaceSizeLocation (MIN).x; 487 spaceWidth = getSpaceSize (MIN).width; 488 spaceHeight = getSpaceSize (MIN).height - 489 (title.getSize (MIN).height + betweenTitleAndSpaceGapThickness); 490 if (title.getJustifications (VERTICAL) == TOP) { 491 spaceY = getSpaceSizeLocation (MIN).y + 492 title.getSize (MIN).height + betweenTitleAndSpaceGapThickness; 493 } 494 else spaceY = getSpaceSizeLocation (MIN).y; 495 } 496 else { 497 spaceY = getSpaceSizeLocation (MIN).y; 498 spaceWidth = getSpaceSize (MIN).width - 499 (title.getSize (MIN).width + betweenTitleAndSpaceGapThickness); 500 spaceHeight = getSpaceSize (MIN).height; 501 if (title.getJustifications (HORIZONTAL) == LEFT) { 502 spaceX = getSpaceSizeLocation (MIN).x + 503 title.getSize (MIN).width + betweenTitleAndSpaceGapThickness; 504 } 505 else spaceX = getSpaceSizeLocation (MIN).x; 506 } 507 minEntitledSpaceBounds.setBounds (spaceX, spaceY, spaceWidth, spaceHeight); 508 } 509 510 511 private void updateMinTitle() { 512 513 if (titleAutoLocate) { 514 515 int betweenWidth = title.getSizeLocation (MIN).x - 516 title.getSpaceSizeLocation (MIN).x; 517 int spaceX; 518 if (title.getJustifications (HORIZONTAL) == LEFT) { 519 spaceX = getSpaceSizeLocation (MIN).x + betweenWidth; 520 } 521 else if (title.getJustifications (HORIZONTAL) == RIGHT) { 522 spaceX = getSpaceSizeLocation (MIN).x + getSpaceSize (MIN).width - 523 title.getSpaceSize (MIN).width - betweenWidth; 524 } 525 else { 526 spaceX = getSpaceSizeLocation(MIN).x + 527 (getSpaceSize (MIN).width - title.getSpaceSize (MIN).width) / 2; 528 } 529 530 int betweenHeight = 531 title.getSpaceSizeLocation (MIN).y - title.getSizeLocation (MIN).y; 532 int spaceY; 533 if (title.getJustifications (VERTICAL) == TOP) { 534 spaceY = getSpaceSizeLocation (MIN).y + betweenHeight; 535 } 536 else if (title.getJustifications (VERTICAL) == BOTTOM) { 537 spaceY = getSpaceSizeLocation (MIN).y + getSpaceSize (MIN).height - 538 title.getSpaceSize (MIN).height - betweenHeight; 539 } 540 else { 541 spaceY = getSpaceSizeLocation(MIN).y + 542 (getSpaceSize (MIN).height - title.getSpaceSize (MIN).height) / 2; 543 } 544 title.setSpaceSizeLocation (MIN, new Point (spaceX, spaceY)); 545 } 546 } 547 } | Popular Tags |