1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
|
/* DefaultSwatchChooserPanel.java --
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing.colorchooser;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* This class is the DefaultSwatchChooserPanel. This chooser panel displays a
* set of colors that can be picked. Recently picked items will go into a
* side panel so the user can see the history of the chosen colors.
*/
class DefaultSwatchChooserPanel extends AbstractColorChooserPanel
{
/** The main panel that holds the set of choosable colors. */
MainSwatchPanel mainPalette;
/** A panel that holds the recent colors. */
RecentSwatchPanel recentPalette;
/** The mouse handlers for the panels. */
MouseListener mouseHandler;
/**
* This the base class for all swatch panels. Swatch panels are panels that
* hold a set of blocks where colors are displayed.
*/
abstract static class SwatchPanel extends JPanel
{
/** The width of each block. */
protected int cellWidth = 10;
/** The height of each block. */
protected int cellHeight = 10;
/** The gap between blocks. */
protected int gap = 1;
/** The number of rows in the swatch panel. */
protected int numRows;
/** The number of columns in the swatch panel. */
protected int numCols;
/**
* Creates a new SwatchPanel object.
*/
SwatchPanel()
{
super();
setBackground(Color.WHITE);
}
/**
* This method returns the preferred size of the swatch panel based on the
* number of rows and columns and the size of each cell.
*
* @return The preferred size of the swatch panel.
*/
public Dimension getPreferredSize()
{
int height = numRows * cellHeight + (numRows - 1) * gap;
int width = numCols * cellWidth + (numCols - 1) * gap;
Insets insets = getInsets();
return new Dimension(width + insets.left + insets.right,
height + insets.top + insets.bottom);
}
/**
* This method returns the color for the given position.
*
* @param x The x coordinate of the position.
* @param y The y coordinate of the position.
*
* @return The color at the given position.
*/
public abstract Color getColorForPosition(int x, int y);
/**
* This method initializes the colors for the swatch panel.
*/
protected abstract void initializeColors();
}
/**
* This is the main swatch panel. This panel sits in the middle and allows a
* set of colors to be picked which will move to the recent swatch panel.
*/
static class MainSwatchPanel extends SwatchPanel
{
/** The color describing (204, 255, 255) */
public static final Color C204255255 = new Color(204, 204, 255);
/** The color describing (255, 204, 204) */
public static final Color C255204204 = new Color(255, 204, 204);
/** The color describing (204, 255, 204) */
public static final Color C204255204 = new Color(204, 255, 204);
/** The color describing (204, 204, 204) */
public static final Color C204204204 = new Color(204, 204, 204);
/** The color (153, 153, 255). */
public static final Color C153153255 = new Color(153, 153, 255);
/** The color (51, 51, 255). */
public static final Color C051051255 = new Color(51, 51, 255);
/** The color (153, 0, 153). */
public static final Color C153000153 = new Color(153, 0, 153);
/** The color (0, 51, 51). */
public static final Color C000051051 = new Color(0, 51, 51);
/** The color (51, 0, 51). */
public static final Color C051000051 = new Color(51, 0, 51);
/** The color (51, 51, 0). */
public static final Color C051051000 = new Color(51, 51, 0);
/** The color (102, 102, 0). */
public static final Color C102102000 = new Color(102, 102, 0);
/** The color (153, 255, 153). */
public static final Color C153255153 = new Color(153, 255, 153);
/** The color (102, 255, 102). */
public static final Color C102255102 = new Color(102, 255, 102);
/** The color (0, 102, 102). */
public static final Color C000102102 = new Color(0, 102, 102);
/** The color (102, 0, 102). */
public static final Color C102000102 = new Color(102, 0, 102);
/** The color (0, 153, 153). */
public static final Color C000153153 = new Color(0, 153, 153);
/** The color (153, 153, 0). */
public static final Color C153153000 = new Color(153, 153, 0);
/** The color (204, 204, 0). */
public static final Color C204204000 = new Color(204, 204, 0);
/** The color (204, 0, 204). */
public static final Color C204000204 = new Color(204, 0, 204);
/** The color (0, 204, 204). */
public static final Color C000204204 = new Color(0, 204, 204);
/** The color (51, 255, 51). */
public static final Color C051255051 = new Color(51, 255, 51);
/** The color (255, 51, 51). */
public static final Color C255051051 = new Color(255, 51, 51);
/** The color (255, 102, 102). */
public static final Color C255102102 = new Color(255, 102, 102);
/** The color (102, 102, 255). */
public static final Color C102102255 = new Color(102, 102, 255);
/** The color (255, 153, 153). */
public static final Color C255153153 = new Color(255, 153, 153);
static Color[] colors =
{
// Row 1
Color.WHITE, new Color(204, 255, 255), C204255255, C204255255, C204255255,
C204255255, C204255255, C204255255, C204255255,
C204255255, C204255255, new Color(255, 204, 255),
C255204204, C255204204, C255204204, C255204204,
C255204204, C255204204, C255204204, C255204204,
C255204204, new Color(255, 255, 204), C204255204,
C204255204, C204255204, C204255204, C204255204,
C204255204, C204255204, C204255204, C204255204,
// Row 2
C204204204, new Color(153, 255, 255), new Color(153, 204, 255), C153153255,
C153153255, C153153255, C153153255, C153153255,
C153153255, C153153255, new Color(204, 153, 255),
new Color(255, 153, 255),
new Color(255, 153, 204), C255153153, C255153153,
C255153153, C255153153, C255153153, C255153153,
C255153153, new Color(255, 204, 153),
new Color(255, 255, 153),
new Color(204, 255, 153), C153255153, C153255153,
C153255153, C153255153, C153255153, C153255153,
C153255153, new Color(153, 255, 204),
// Row 3
C204204204, new Color(102, 255, 255), new Color(102, 204, 255),
new Color(102, 153, 255), C102102255, C102102255,
C102102255, C102102255, C102102255,
new Color(153, 102, 255),
new Color(204, 102, 255),
new Color(255, 102, 255),
new Color(255, 102, 204),
new Color(255, 102, 153), C255102102, C255102102,
C255102102, C255102102, C255102102,
new Color(255, 153, 102),
new Color(255, 204, 102),
new Color(255, 255, 102),
new Color(204, 255, 102),
new Color(153, 255, 102), C102255102, C102255102,
C102255102, C102255102, C102255102,
new Color(102, 255, 153),
new Color(102, 255, 204),
// Row 4
new Color(153, 153, 153), new Color(51, 255, 255), new Color(51, 204, 255),
new Color(51, 153, 255), new Color(51, 102, 255),
C051051255, C051051255, C051051255,
new Color(102, 51, 255), new Color(153, 51, 255),
new Color(204, 51, 255), new Color(255, 51, 255),
new Color(255, 51, 204), new Color(255, 51, 153),
new Color(255, 51, 102), C255051051, C255051051,
C255051051, new Color(255, 102, 51),
new Color(255, 153, 51), new Color(255, 204, 51),
new Color(255, 255, 51), new Color(204, 255, 51),
new Color(153, 255, 51), new Color(102, 255, 51),
C051255051, C051255051, C051255051,
new Color(51, 255, 102), new Color(51, 255, 153),
new Color(51, 255, 204),
// Row 5
new Color(153, 153, 153), new Color(0, 255, 255), new Color(0, 204, 255),
new Color(0, 153, 255), new Color(0, 102, 255),
new Color(0, 51, 255), new Color(0, 0, 255),
new Color(51, 0, 255), new Color(102, 0, 255),
new Color(153, 0, 255), new Color(204, 0, 255),
new Color(255, 0, 255), new Color(255, 0, 204),
new Color(255, 0, 153), new Color(255, 0, 102),
new Color(255, 0, 51), new Color(255, 0, 0),
new Color(255, 51, 0), new Color(255, 102, 0),
new Color(255, 153, 0), new Color(255, 204, 0),
new Color(255, 255, 0), new Color(204, 255, 0),
new Color(153, 255, 0), new Color(102, 255, 0),
new Color(51, 255, 0), new Color(0, 255, 0),
new Color(0, 255, 51), new Color(0, 255, 102),
new Color(0, 255, 153), new Color(0, 255, 204),
// Row 6
new Color(102, 102, 102), C000204204, C000204204, new Color(0, 153, 204),
new Color(0, 102, 204), new Color(0, 51, 204),
new Color(0, 0, 204), new Color(51, 0, 204),
new Color(102, 0, 204), new Color(153, 0, 204),
C204000204, C204000204, C204000204,
new Color(204, 0, 153), new Color(204, 0, 102),
new Color(204, 0, 51), new Color(204, 0, 0),
new Color(204, 51, 0), new Color(204, 102, 0),
new Color(204, 153, 0), C204204000, C204204000,
C204204000, new Color(153, 204, 0),
new Color(102, 204, 0), new Color(51, 204, 0),
new Color(0, 204, 0), new Color(0, 204, 51),
new Color(0, 204, 102), new Color(0, 204, 153),
new Color(0, 204, 204),
// Row 7
new Color(102, 102, 102), C000153153, C000153153, C000153153,
new Color(0, 102, 153), new Color(0, 51, 153),
new Color(0, 0, 153), new Color(51, 0, 153),
new Color(102, 0, 153), C153000153, C153000153,
C153000153, C153000153, C153000153,
new Color(153, 0, 102), new Color(153, 0, 51),
new Color(153, 0, 0), new Color(153, 51, 0),
new Color(153, 102, 0), C153153000, C153153000,
C153153000, C153153000, C153153000,
new Color(102, 153, 0), new Color(51, 153, 0),
new Color(0, 153, 0), new Color(0, 153, 51),
new Color(0, 153, 102), C000153153, C000153153,
// Row 8
new Color(51, 51, 51), C000102102, C000102102, C000102102, C000102102,
new Color(0, 51, 102), new Color(0, 0, 102),
new Color(51, 0, 102), C102000102, C102000102,
C102000102, C102000102, C102000102, C102000102,
C102000102, new Color(102, 0, 51),
new Color(102, 0, 0), new Color(102, 51, 0),
C102102000, C102102000, C102102000, C102102000,
C102102000, C102102000, C102102000,
new Color(51, 102, 0), new Color(0, 102, 0),
new Color(0, 102, 51), C000102102, C000102102,
C000102102,
// Row 9.
Color.BLACK, C000051051, C000051051, C000051051, C000051051, C000051051,
new Color(0, 0, 51), C051000051, C051000051,
C051000051, C051000051, C051000051, C051000051,
C051000051, C051000051, C051000051,
new Color(51, 0, 0), C051051000, C051051000,
C051051000, C051051000, C051051000, C051051000,
C051051000, C051051000, new Color(0, 51, 0),
C000051051, C000051051, C000051051, C000051051,
new Color(51, 51, 51)
};
/**
* Creates a new MainSwatchPanel object.
*/
MainSwatchPanel()
{
super();
numCols = 31;
numRows = 9;
initializeColors();
revalidate();
}
/**
* This method returns the color for the given position.
*
* @param x The x location for the position.
* @param y The y location for the position.
*
* @return The color for the given position.
*/
public Color getColorForPosition(int x, int y)
{
if (x % (cellWidth + gap) > cellWidth
|| y % (cellHeight + gap) > cellHeight)
// position is located in gap.
return null;
int row = y / (cellHeight + gap);
int col = x / (cellWidth + gap);
return colors[row * numCols + col];
}
/**
* This method initializes the colors for the main swatch panel.
*/
protected void initializeColors()
{
// Unnecessary
}
/**
* This method paints the main graphics panel with the given Graphics
* object.
*
* @param graphics The Graphics object to paint with.
*/
public void paint(Graphics graphics)
{
int index = 0;
Insets insets = getInsets();
int currX = insets.left;
int currY = insets.top;
Color saved = graphics.getColor();
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
graphics.setColor(colors[index++]);
graphics.fill3DRect(currX, currY, cellWidth, cellHeight, true);
currX += gap + cellWidth;
}
currX = insets.left;
currY += gap + cellHeight;
}
graphics.setColor(saved);
}
/**
* This method returns the tooltip text for the given MouseEvent.
*
* @param e The MouseEvent to find tooltip text for.
*
* @return The tooltip text.
*/
public String getToolTipText(MouseEvent e)
{
Color c = getColorForPosition(e.getX(), e.getY());
if (c == null)
return null;
return (c.getRed() + "," + c.getGreen() + "," + c.getBlue());
}
}
/**
* This class is the recent swatch panel. It holds recently selected colors.
*/
static class RecentSwatchPanel extends SwatchPanel
{
/** The array for storing recently stored colors. */
Color[] colors;
/** The default color. */
public static final Color defaultColor = Color.GRAY;
/** The index of the array that is the start. */
int start = 0;
/**
* Creates a new RecentSwatchPanel object.
*/
RecentSwatchPanel()
{
super();
numCols = 5;
numRows = 7;
initializeColors();
revalidate();
}
/**
* This method returns the color for the given position.
*
* @param x The x coordinate of the position.
* @param y The y coordinate of the position.
*
* @return The color for the given position.
*/
public Color getColorForPosition(int x, int y)
{
if (x % (cellWidth + gap) > cellWidth
|| y % (cellHeight + gap) > cellHeight)
// position is located in gap.
return null;
int row = y / (cellHeight + gap);
int col = x / (cellWidth + gap);
return colors[getIndexForCell(row, col)];
}
/**
* This method initializes the colors for the recent swatch panel.
*/
protected void initializeColors()
{
colors = new Color[numRows * numCols];
for (int i = 0; i < colors.length; i++)
colors[i] = defaultColor;
}
/**
* This method returns the array index for the given row and column.
*
* @param row The row.
* @param col The column.
*
* @return The array index for the given row and column.
*/
private int getIndexForCell(int row, int col)
{
return ((row * numCols) + col + start) % (numRows * numCols);
}
/**
* This method adds the given color to the beginning of the swatch panel.
*
* @param c The color to add.
*/
private void addColorToQueue(Color c)
{
if (--start == -1)
start = numRows * numCols - 1;
colors[start] = c;
}
/**
* This method paints the panel with the given Graphics object.
*
* @param g The Graphics object to paint with.
*/
public void paint(Graphics g)
{
Color saved = g.getColor();
Insets insets = getInsets();
int currX = insets.left;
int currY = insets.top;
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
g.setColor(colors[getIndexForCell(i, j)]);
g.fill3DRect(currX, currY, cellWidth, cellHeight, true);
currX += cellWidth + gap;
}
currX = insets.left;
currY += cellWidth + gap;
}
}
/**
* This method returns the tooltip text for the given MouseEvent.
*
* @param e The MouseEvent.
*
* @return The tooltip text.
*/
public String getToolTipText(MouseEvent e)
{
Color c = getColorForPosition(e.getX(), e.getY());
if (c == null)
return null;
return c.getRed() + "," + c.getGreen() + "," + c.getBlue();
}
}
/**
* This class handles mouse events for the two swatch panels.
*/
class MouseHandler extends MouseAdapter
{
/**
* This method is called whenever the mouse is pressed.
*
* @param e The MouseEvent.
*/
public void mousePressed(MouseEvent e)
{
SwatchPanel panel = (SwatchPanel) e.getSource();
Color c = panel.getColorForPosition(e.getX(), e.getY());
recentPalette.addColorToQueue(c);
DefaultSwatchChooserPanel.this.getColorSelectionModel().setSelectedColor(c);
DefaultSwatchChooserPanel.this.repaint();
}
}
/**
* This is the layout manager for the main panel.
*/
static class MainPanelLayout implements LayoutManager
{
/**
* This method is called when a new component is added to the container.
*
* @param name The name of the component.
* @param comp The added component.
*/
public void addLayoutComponent(String name, Component comp)
{
}
/**
* This method is called to set the size and position of the child
* components for the given container.
*
* @param parent The container to lay out.
*/
public void layoutContainer(Container parent)
{
Component[] comps = parent.getComponents();
Insets insets = parent.getInsets();
Dimension[] pref = new Dimension[comps.length];
int xpos = 0;
int ypos = 0;
int maxHeight = 0;
int totalWidth = 0;
for (int i = 0; i < comps.length; i++)
{
pref[i] = comps[i].getPreferredSize();
if (pref[i] == null)
return;
maxHeight = Math.max(maxHeight, pref[i].height);
totalWidth += pref[i].width;
}
ypos = (parent.getSize().height - maxHeight) / 2 + insets.top;
xpos = insets.left + (parent.getSize().width - totalWidth) / 2;
for (int i = 0; i < comps.length; i++)
{
if (pref[i] == null)
continue;
comps[i].setBounds(xpos, ypos, pref[i].width, pref[i].height);
xpos += pref[i].width;
}
}
/**
* This method is called when a component is removed from the container.
*
* @param comp The component that was removed.
*/
public void removeLayoutComponent(Component comp)
{
}
/**
* This methods calculates the minimum layout size for the container.
*
* @param parent The container.
*
* @return The minimum layout size.
*/
public Dimension minimumLayoutSize(Container parent)
{
return preferredLayoutSize(parent);
}
/**
* This method returns the preferred layout size for the given container.
*
* @param parent The container.
*
* @return The preferred layout size.
*/
public Dimension preferredLayoutSize(Container parent)
{
int xmax = 0;
int ymax = 0;
Component[] comps = parent.getComponents();
Dimension pref;
for (int i = 0; i < comps.length; i++)
{
pref = comps[i].getPreferredSize();
if (pref == null)
continue;
xmax += pref.width;
ymax = Math.max(ymax, pref.height);
}
Insets insets = parent.getInsets();
return new Dimension(insets.left + insets.right + xmax,
insets.top + insets.bottom + ymax);
}
}
/**
* This is the layout manager for the recent swatch panel.
*/
static class RecentPanelLayout implements LayoutManager
{
/**
* This method is called when a component is added to the container.
*
* @param name The name of the component.
* @param comp The added component.
*/
public void addLayoutComponent(String name, Component comp)
{
// Nothing needs to be done.
}
/**
* This method sets the size and position of the child components of the
* given container.
*
* @param parent The container to lay out.
*/
public void layoutContainer(Container parent)
{
Component[] comps = parent.getComponents();
Dimension parentSize = parent.getSize();
Insets insets = parent.getInsets();
int currY = insets.top;
Dimension pref;
for (int i = 0; i < comps.length; i++)
{
pref = comps[i].getPreferredSize();
if (pref == null)
continue;
comps[i].setBounds(insets.left, currY, pref.width, pref.height);
currY += pref.height;
}
}
/**
* This method calculates the minimum layout size for the given container.
*
* @param parent The container.
*
* @return The minimum layout size.
*/
public Dimension minimumLayoutSize(Container parent)
{
return preferredLayoutSize(parent);
}
/**
* This method calculates the preferred layout size for the given
* container.
*
* @param parent The container.
*
* @return The preferred layout size.
*/
public Dimension preferredLayoutSize(Container parent)
{
int width = 0;
int height = 0;
Insets insets = parent.getInsets();
Component[] comps = parent.getComponents();
Dimension pref;
for (int i = 0; i < comps.length; i++)
{
pref = comps[i].getPreferredSize();
if (pref != null)
{
width = Math.max(width, pref.width);
height += pref.height;
}
}
return new Dimension(width + insets.left + insets.right,
height + insets.top + insets.bottom);
}
/**
* This method is called whenever a component is removed from the
* container.
*
* @param comp The removed component.
*/
public void removeLayoutComponent(Component comp)
{
// Nothing needs to be done.
}
}
/**
* Creates a new DefaultSwatchChooserPanel object.
*/
DefaultSwatchChooserPanel()
{
super();
}
/**
* This method updates the chooser panel with the new value from the
* JColorChooser.
*/
public void updateChooser()
{
}
/**
* This method builds the chooser panel.
*/
protected void buildChooser()
{
// The structure of the swatch panel is:
// One large panel (minus the insets).
// Inside that panel, there are two panels, one holds the palette.
// The other holds the label and the recent colors palette.
// The two palettes are two custom swatch panels.
setLayout(new MainPanelLayout());
JPanel mainPaletteHolder = new JPanel();
JPanel recentPaletteHolder = new JPanel();
mainPalette = new MainSwatchPanel();
recentPalette = new RecentSwatchPanel();
JLabel label = new JLabel("Recent:");
mouseHandler = new MouseHandler();
mainPalette.addMouseListener(mouseHandler);
recentPalette.addMouseListener(mouseHandler);
mainPaletteHolder.setLayout(new BorderLayout());
mainPaletteHolder.add(mainPalette, BorderLayout.CENTER);
recentPaletteHolder.setLayout(new RecentPanelLayout());
recentPaletteHolder.add(label);
recentPaletteHolder.add(recentPalette);
JPanel main = new JPanel();
main.add(mainPaletteHolder);
main.add(recentPaletteHolder);
this.add(main);
}
/**
* This method removes the chooser panel from the JColorChooser.
*
* @param chooser The JColorChooser this panel is being removed from.
*/
public void uninstallChooserPanel(JColorChooser chooser)
{
recentPalette = null;
mainPalette = null;
removeAll();
super.uninstallChooserPanel(chooser);
}
/**
* This method returns the JTabbedPane displayed name.
*
* @return The name displayed in the JTabbedPane.
*/
public String getDisplayName()
{
return "Swatches";
}
/**
* This method returns the small display icon.
*
* @return The small display icon.
*/
public Icon getSmallDisplayIcon()
{
return null;
}
/**
* This method returns the large display icon.
*
* @return The large display icon.
*/
public Icon getLargeDisplayIcon()
{
return null;
}
/**
* This method paints the chooser panel with the given Graphics object.
*
* @param g The Graphics object to paint with.
*/
public void paint(Graphics g)
{
super.paint(g);
}
/**
* This method returns the tooltip text for the given MouseEvent.
*
* @param e The MouseEvent.
*
* @return The tooltip text.
*/
public String getToolTipText(MouseEvent e)
{
return null;
}
}
|