1 7 8 package javax.swing.colorchooser; 9 10 import javax.swing.*; 11 import javax.swing.border.*; 12 import javax.swing.event.*; 13 import java.awt.*; 14 import java.awt.image.*; 15 import java.awt.event.*; 16 import java.beans.PropertyChangeEvent ; 17 import java.beans.PropertyChangeListener ; 18 import java.io.Serializable ; 19 20 21 36 class DefaultSwatchChooserPanel extends AbstractColorChooserPanel { 37 38 SwatchPanel swatchPanel; 39 RecentSwatchPanel recentSwatchPanel; 40 MouseListener mainSwatchListener; 41 MouseListener recentSwatchListener; 42 43 private static String recentStr = UIManager.getString("ColorChooser.swatchesRecentText"); 44 45 public DefaultSwatchChooserPanel() { 46 super(); 47 } 48 49 public String getDisplayName() { 50 return UIManager.getString("ColorChooser.swatchesNameText"); 51 } 52 53 72 public int getMnemonic() { 73 return getInt("ColorChooser.swatchesMnemonic", -1); 74 } 75 76 100 public int getDisplayedMnemonicIndex() { 101 return getInt("ColorChooser.swatchesDisplayedMnemonicIndex", -1); 102 } 103 104 public Icon getSmallDisplayIcon() { 105 return null; 106 } 107 108 public Icon getLargeDisplayIcon() { 109 return null; 110 } 111 112 116 public void installChooserPanel(JColorChooser enclosingChooser) { 117 super.installChooserPanel(enclosingChooser); 118 } 119 120 protected void buildChooser() { 121 122 GridBagLayout gb = new GridBagLayout(); 123 GridBagConstraints gbc = new GridBagConstraints(); 124 JPanel superHolder = new JPanel(gb); 125 126 swatchPanel = new MainSwatchPanel(); 127 swatchPanel.getAccessibleContext().setAccessibleName(getDisplayName()); 128 129 recentSwatchPanel = new RecentSwatchPanel(); 130 recentSwatchPanel.getAccessibleContext().setAccessibleName(recentStr); 131 132 mainSwatchListener = new MainSwatchListener(); 133 swatchPanel.addMouseListener(mainSwatchListener); 134 recentSwatchListener = new RecentSwatchListener(); 135 recentSwatchPanel.addMouseListener(recentSwatchListener); 136 137 138 Border border = new CompoundBorder( new LineBorder(Color.black), 139 new LineBorder(Color.white) ); 140 swatchPanel.setBorder(border); 141 gbc.weightx = 1.0; 142 gbc.gridwidth = 2; 143 gbc.gridheight = 2; 144 gbc.weighty = 1.0; 145 superHolder.add( swatchPanel, gbc ); 146 147 recentSwatchPanel.addMouseListener(recentSwatchListener); 148 recentSwatchPanel.setBorder(border); 149 JPanel recentLabelHolder = new JPanel(new BorderLayout()); 150 JLabel l = new JLabel(recentStr); 151 l.setLabelFor(recentSwatchPanel); 152 recentLabelHolder.add(l, BorderLayout.NORTH); 153 gbc.weighty = 0.0; 154 gbc.gridwidth = GridBagConstraints.REMAINDER; 155 gbc.gridheight = 1; 156 superHolder.add( recentLabelHolder, gbc ); 157 superHolder.add( recentSwatchPanel, gbc ); 158 159 add(superHolder); 160 161 } 162 163 public void uninstallChooserPanel(JColorChooser enclosingChooser) { 164 super.uninstallChooserPanel(enclosingChooser); 165 swatchPanel.removeMouseListener(mainSwatchListener); 166 recentSwatchPanel.removeMouseListener(recentSwatchListener); 167 swatchPanel = null; 168 recentSwatchPanel = null; 169 mainSwatchListener = null; 170 recentSwatchListener = null; 171 removeAll(); } 173 174 public void updateChooser() { 175 176 } 177 178 179 class RecentSwatchListener extends MouseAdapter implements Serializable { 180 public void mousePressed(MouseEvent e) { 181 Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY()); 182 getColorSelectionModel().setSelectedColor(color); 183 184 } 185 } 186 187 class MainSwatchListener extends MouseAdapter implements Serializable { 188 public void mousePressed(MouseEvent e) { 189 Color color = swatchPanel.getColorForLocation(e.getX(), e.getY()); 190 getColorSelectionModel().setSelectedColor(color); 191 recentSwatchPanel.setMostRecentColor(color); 192 193 } 194 } 195 196 } 197 198 199 200 class SwatchPanel extends JPanel { 201 202 protected Color[] colors; 203 protected Dimension swatchSize; 204 protected Dimension numSwatches; 205 protected Dimension gap; 206 207 public SwatchPanel() { 208 initValues(); 209 initColors(); 210 setToolTipText(""); setOpaque(true); 212 setBackground(Color.white); 213 setRequestFocusEnabled(false); 214 } 215 216 public boolean isFocusTraversable() { 217 return false; 218 } 219 220 protected void initValues() { 221 222 } 223 224 public void paintComponent(Graphics g) { 225 g.setColor(getBackground()); 226 g.fillRect(0,0,getWidth(), getHeight()); 227 for (int row = 0; row < numSwatches.height; row++) { 228 for (int column = 0; column < numSwatches.width; column++) { 229 230 g.setColor( getColorForCell(column, row) ); 231 int x; 232 if ((!this.getComponentOrientation().isLeftToRight()) && 233 (this instanceof RecentSwatchPanel)) { 234 x = (numSwatches.width - column - 1) * (swatchSize.width + gap.width); 235 } else { 236 x = column * (swatchSize.width + gap.width); 237 } 238 int y = row * (swatchSize.height + gap.height); 239 g.fillRect( x, y, swatchSize.width, swatchSize.height); 240 g.setColor(Color.black); 241 g.drawLine( x+swatchSize.width-1, y, x+swatchSize.width-1, y+swatchSize.height-1); 242 g.drawLine( x, y+swatchSize.height-1, x+swatchSize.width-1, y+swatchSize.height-1); 243 } 244 } 245 } 246 247 public Dimension getPreferredSize() { 248 int x = numSwatches.width * (swatchSize.width + gap.width) -1; 249 int y = numSwatches.height * (swatchSize.height + gap.height) -1; 250 return new Dimension( x, y ); 251 } 252 253 protected void initColors() { 254 255 256 } 257 258 public String getToolTipText(MouseEvent e) { 259 Color color = getColorForLocation(e.getX(), e.getY()); 260 return color.getRed()+", "+ color.getGreen() + ", " + color.getBlue(); 261 } 262 263 public Color getColorForLocation( int x, int y ) { 264 int column; 265 if ((!this.getComponentOrientation().isLeftToRight()) && 266 (this instanceof RecentSwatchPanel)) { 267 column = numSwatches.width - x / (swatchSize.width + gap.width) - 1; 268 } else { 269 column = x / (swatchSize.width + gap.width); 270 } 271 int row = y / (swatchSize.height + gap.height); 272 return getColorForCell(column, row); 273 } 274 275 private Color getColorForCell( int column, int row) { 276 return colors[ (row * numSwatches.width) + column ]; } 278 279 280 281 282 } 283 284 class RecentSwatchPanel extends SwatchPanel { 285 protected void initValues() { 286 swatchSize = UIManager.getDimension("ColorChooser.swatchesRecentSwatchSize"); 287 numSwatches = new Dimension( 5, 7 ); 288 gap = new Dimension(1, 1); 289 } 290 291 292 protected void initColors() { 293 Color defaultRecentColor = UIManager.getColor("ColorChooser.swatchesDefaultRecentColor"); 294 int numColors = numSwatches.width * numSwatches.height; 295 296 colors = new Color[numColors]; 297 for (int i = 0; i < numColors ; i++) { 298 colors[i] = defaultRecentColor; 299 } 300 } 301 302 public void setMostRecentColor(Color c) { 303 304 System.arraycopy( colors, 0, colors, 1, colors.length-1); 305 colors[0] = c; 306 repaint(); 307 } 308 309 } 310 311 class MainSwatchPanel extends SwatchPanel { 312 313 314 protected void initValues() { 315 swatchSize = UIManager.getDimension("ColorChooser.swatchesSwatchSize"); 316 numSwatches = new Dimension( 31, 9 ); 317 gap = new Dimension(1, 1); 318 } 319 320 protected void initColors() { 321 int[] rawValues = initRawValues(); 322 int numColors = rawValues.length / 3; 323 324 colors = new Color[numColors]; 325 for (int i = 0; i < numColors ; i++) { 326 colors[i] = new Color( rawValues[(i*3)], rawValues[(i*3)+1], rawValues[(i*3)+2] ); 327 } 328 } 329 330 private int[] initRawValues() { 331 332 int[] rawValues = { 333 255, 255, 255, 204, 255, 255, 335 204, 204, 255, 336 204, 204, 255, 337 204, 204, 255, 338 204, 204, 255, 339 204, 204, 255, 340 204, 204, 255, 341 204, 204, 255, 342 204, 204, 255, 343 204, 204, 255, 344 255, 204, 255, 345 255, 204, 204, 346 255, 204, 204, 347 255, 204, 204, 348 255, 204, 204, 349 255, 204, 204, 350 255, 204, 204, 351 255, 204, 204, 352 255, 204, 204, 353 255, 204, 204, 354 255, 255, 204, 355 204, 255, 204, 356 204, 255, 204, 357 204, 255, 204, 358 204, 255, 204, 359 204, 255, 204, 360 204, 255, 204, 361 204, 255, 204, 362 204, 255, 204, 363 204, 255, 204, 364 204, 204, 204, 153, 255, 255, 366 153, 204, 255, 367 153, 153, 255, 368 153, 153, 255, 369 153, 153, 255, 370 153, 153, 255, 371 153, 153, 255, 372 153, 153, 255, 373 153, 153, 255, 374 204, 153, 255, 375 255, 153, 255, 376 255, 153, 204, 377 255, 153, 153, 378 255, 153, 153, 379 255, 153, 153, 380 255, 153, 153, 381 255, 153, 153, 382 255, 153, 153, 383 255, 153, 153, 384 255, 204, 153, 385 255, 255, 153, 386 204, 255, 153, 387 153, 255, 153, 388 153, 255, 153, 389 153, 255, 153, 390 153, 255, 153, 391 153, 255, 153, 392 153, 255, 153, 393 153, 255, 153, 394 153, 255, 204, 395 204, 204, 204, 102, 255, 255, 397 102, 204, 255, 398 102, 153, 255, 399 102, 102, 255, 400 102, 102, 255, 401 102, 102, 255, 402 102, 102, 255, 403 102, 102, 255, 404 153, 102, 255, 405 204, 102, 255, 406 255, 102, 255, 407 255, 102, 204, 408 255, 102, 153, 409 255, 102, 102, 410 255, 102, 102, 411 255, 102, 102, 412 255, 102, 102, 413 255, 102, 102, 414 255, 153, 102, 415 255, 204, 102, 416 255, 255, 102, 417 204, 255, 102, 418 153, 255, 102, 419 102, 255, 102, 420 102, 255, 102, 421 102, 255, 102, 422 102, 255, 102, 423 102, 255, 102, 424 102, 255, 153, 425 102, 255, 204, 426 153, 153, 153, 51, 255, 255, 428 51, 204, 255, 429 51, 153, 255, 430 51, 102, 255, 431 51, 51, 255, 432 51, 51, 255, 433 51, 51, 255, 434 102, 51, 255, 435 153, 51, 255, 436 204, 51, 255, 437 255, 51, 255, 438 255, 51, 204, 439 255, 51, 153, 440 255, 51, 102, 441 255, 51, 51, 442 255, 51, 51, 443 255, 51, 51, 444 255, 102, 51, 445 255, 153, 51, 446 255, 204, 51, 447 255, 255, 51, 448 204, 255, 51, 449 153, 244, 51, 450 102, 255, 51, 451 51, 255, 51, 452 51, 255, 51, 453 51, 255, 51, 454 51, 255, 102, 455 51, 255, 153, 456 51, 255, 204, 457 153, 153, 153, 0, 255, 255, 459 0, 204, 255, 460 0, 153, 255, 461 0, 102, 255, 462 0, 51, 255, 463 0, 0, 255, 464 51, 0, 255, 465 102, 0, 255, 466 153, 0, 255, 467 204, 0, 255, 468 255, 0, 255, 469 255, 0, 204, 470 255, 0, 153, 471 255, 0, 102, 472 255, 0, 51, 473 255, 0 , 0, 474 255, 51, 0, 475 255, 102, 0, 476 255, 153, 0, 477 255, 204, 0, 478 255, 255, 0, 479 204, 255, 0, 480 153, 255, 0, 481 102, 255, 0, 482 51, 255, 0, 483 0, 255, 0, 484 0, 255, 51, 485 0, 255, 102, 486 0, 255, 153, 487 0, 255, 204, 488 102, 102, 102, 0, 204, 204, 490 0, 204, 204, 491 0, 153, 204, 492 0, 102, 204, 493 0, 51, 204, 494 0, 0, 204, 495 51, 0, 204, 496 102, 0, 204, 497 153, 0, 204, 498 204, 0, 204, 499 204, 0, 204, 500 204, 0, 204, 501 204, 0, 153, 502 204, 0, 102, 503 204, 0, 51, 504 204, 0, 0, 505 204, 51, 0, 506 204, 102, 0, 507 204, 153, 0, 508 204, 204, 0, 509 204, 204, 0, 510 204, 204, 0, 511 153, 204, 0, 512 102, 204, 0, 513 51, 204, 0, 514 0, 204, 0, 515 0, 204, 51, 516 0, 204, 102, 517 0, 204, 153, 518 0, 204, 204, 519 102, 102, 102, 0, 153, 153, 521 0, 153, 153, 522 0, 153, 153, 523 0, 102, 153, 524 0, 51, 153, 525 0, 0, 153, 526 51, 0, 153, 527 102, 0, 153, 528 153, 0, 153, 529 153, 0, 153, 530 153, 0, 153, 531 153, 0, 153, 532 153, 0, 153, 533 153, 0, 102, 534 153, 0, 51, 535 153, 0, 0, 536 153, 51, 0, 537 153, 102, 0, 538 153, 153, 0, 539 153, 153, 0, 540 153, 153, 0, 541 153, 153, 0, 542 153, 153, 0, 543 102, 153, 0, 544 51, 153, 0, 545 0, 153, 0, 546 0, 153, 51, 547 0, 153, 102, 548 0, 153, 153, 549 0, 153, 153, 550 51, 51, 51, 0, 102, 102, 552 0, 102, 102, 553 0, 102, 102, 554 0, 102, 102, 555 0, 51, 102, 556 0, 0, 102, 557 51, 0, 102, 558 102, 0, 102, 559 102, 0, 102, 560 102, 0, 102, 561 102, 0, 102, 562 102, 0, 102, 563 102, 0, 102, 564 102, 0, 102, 565 102, 0, 51, 566 102, 0, 0, 567 102, 51, 0, 568 102, 102, 0, 569 102, 102, 0, 570 102, 102, 0, 571 102, 102, 0, 572 102, 102, 0, 573 102, 102, 0, 574 102, 102, 0, 575 51, 102, 0, 576 0, 102, 0, 577 0, 102, 51, 578 0, 102, 102, 579 0, 102, 102, 580 0, 102, 102, 581 0, 0, 0, 0, 51, 51, 583 0, 51, 51, 584 0, 51, 51, 585 0, 51, 51, 586 0, 51, 51, 587 0, 0, 51, 588 51, 0, 51, 589 51, 0, 51, 590 51, 0, 51, 591 51, 0, 51, 592 51, 0, 51, 593 51, 0, 51, 594 51, 0, 51, 595 51, 0, 51, 596 51, 0, 51, 597 51, 0, 0, 598 51, 51, 0, 599 51, 51, 0, 600 51, 51, 0, 601 51, 51, 0, 602 51, 51, 0, 603 51, 51, 0, 604 51, 51, 0, 605 51, 51, 0, 606 0, 51, 0, 607 0, 51, 51, 608 0, 51, 51, 609 0, 51, 51, 610 0, 51, 51, 611 51, 51, 51 }; 612 return rawValues; 613 } 614 } 615 | Popular Tags |