1 7 8 package javax.swing; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 import java.beans.*; 13 import java.io.*; 14 import java.util.*; 15 16 import javax.swing.colorchooser.*; 17 import javax.swing.plaf.ColorChooserUI ; 18 import javax.swing.event.*; 19 import javax.accessibility.*; 20 21 22 64 public class JColorChooser extends JComponent implements Accessible { 65 66 70 private static final String uiClassID = "ColorChooserUI"; 71 72 private ColorSelectionModel selectionModel; 73 74 private JComponent previewPanel; 75 76 private AbstractColorChooserPanel[] chooserPanels = new AbstractColorChooserPanel[0]; 77 78 private boolean dragEnabled; 79 80 83 public static final String SELECTION_MODEL_PROPERTY = "selectionModel"; 84 85 88 public static final String PREVIEW_PANEL_PROPERTY = "previewPanel"; 89 90 93 public static final String CHOOSER_PANELS_PROPERTY = "chooserPanels"; 94 95 96 112 public static Color showDialog(Component component, 113 String title, Color initialColor) throws HeadlessException { 114 115 final JColorChooser pane = new JColorChooser (initialColor != null? 116 initialColor : Color.white); 117 118 ColorTracker ok = new ColorTracker(pane); 119 JDialog dialog = createDialog(component, title, true, pane, ok, null); 120 121 dialog.show(); 123 return ok.getColor(); 124 } 125 126 127 148 public static JDialog createDialog(Component c, String title, boolean modal, 149 JColorChooser chooserPane, ActionListener okListener, 150 ActionListener cancelListener) throws HeadlessException { 151 152 Window window = JOptionPane.getWindowForComponent(c); 153 ColorChooserDialog dialog; 154 if (window instanceof Frame) { 155 dialog = new ColorChooserDialog((Frame)window, title, modal, c, chooserPane, 156 okListener, cancelListener); 157 } else { 158 dialog = new ColorChooserDialog((Dialog)window, title, modal, c, chooserPane, 159 okListener, cancelListener); 160 } 161 return dialog; 162 } 163 164 167 public JColorChooser() { 168 this(Color.white); 169 } 170 171 176 public JColorChooser(Color initialColor) { 177 this( new DefaultColorSelectionModel(initialColor) ); 178 179 } 180 181 187 public JColorChooser(ColorSelectionModel model) { 188 selectionModel = model; 189 updateUI(); 190 dragEnabled = false; 191 } 192 193 199 public ColorChooserUI getUI() { 200 return (ColorChooserUI )ui; 201 } 202 203 214 public void setUI(ColorChooserUI ui) { 215 super.setUI(ui); 216 } 217 218 225 public void updateUI() { 226 setUI((ColorChooserUI )UIManager.getUI(this)); 227 } 228 229 236 public String getUIClassID() { 237 return uiClassID; 238 } 239 240 246 public Color getColor() { 247 return selectionModel.getSelectedColor(); 248 } 249 250 261 public void setColor(Color color) { 262 selectionModel.setSelectedColor(color); 263 264 } 265 266 277 public void setColor(int r, int g, int b) { 278 setColor(new Color(r,g,b)); 279 } 280 281 290 public void setColor(int c) { 291 setColor((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF); 292 } 293 294 333 public void setDragEnabled(boolean b) { 334 if (b && GraphicsEnvironment.isHeadless()) { 335 throw new HeadlessException(); 336 } 337 dragEnabled = b; 338 } 339 340 347 public boolean getDragEnabled() { 348 return dragEnabled; 349 } 350 351 364 public void setPreviewPanel(JComponent preview) { 365 366 if (previewPanel != preview) { 367 JComponent oldPreview = previewPanel; 368 previewPanel = preview; 369 firePropertyChange(JColorChooser.PREVIEW_PANEL_PROPERTY, oldPreview, preview); 370 } 371 } 372 373 378 public JComponent getPreviewPanel() { 379 return previewPanel; 380 } 381 382 387 public void addChooserPanel( AbstractColorChooserPanel panel ) { 388 AbstractColorChooserPanel[] oldPanels = getChooserPanels(); 389 AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[oldPanels.length+1]; 390 System.arraycopy(oldPanels, 0, newPanels, 0, oldPanels.length); 391 newPanels[newPanels.length-1] = panel; 392 setChooserPanels(newPanels); 393 } 394 395 403 public AbstractColorChooserPanel removeChooserPanel( AbstractColorChooserPanel panel ) { 404 405 406 int containedAt = -1; 407 408 for (int i = 0; i < chooserPanels.length; i++) { 409 if (chooserPanels[i] == panel) { 410 containedAt = i; 411 break; 412 } 413 } 414 if (containedAt == -1) { 415 throw new IllegalArgumentException ("chooser panel not in this chooser"); 416 } 417 418 AbstractColorChooserPanel[] newArray = new AbstractColorChooserPanel[chooserPanels.length-1]; 419 420 if (containedAt == chooserPanels.length-1) { System.arraycopy(chooserPanels, 0, newArray, 0, newArray.length); 422 } 423 else if (containedAt == 0) { System.arraycopy(chooserPanels, 1, newArray, 0, newArray.length); 425 } 426 else { System.arraycopy(chooserPanels, 0, newArray, 0, containedAt); 428 System.arraycopy(chooserPanels, containedAt+1, 429 newArray, containedAt, (chooserPanels.length - containedAt - 1)); 430 } 431 432 setChooserPanels(newArray); 433 434 return panel; 435 } 436 437 438 449 public void setChooserPanels( AbstractColorChooserPanel[] panels) { 450 AbstractColorChooserPanel[] oldValue = chooserPanels; 451 chooserPanels = panels; 452 firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue, panels); 453 } 454 455 460 public AbstractColorChooserPanel[] getChooserPanels() { 461 return chooserPanels; 462 } 463 464 469 public ColorSelectionModel getSelectionModel() { 470 return selectionModel; 471 } 472 473 474 484 public void setSelectionModel(ColorSelectionModel newModel ) { 485 ColorSelectionModel oldModel = selectionModel; 486 selectionModel = newModel; 487 firePropertyChange(JColorChooser.SELECTION_MODEL_PROPERTY, oldModel, newModel); 488 } 489 490 491 496 private void writeObject(ObjectOutputStream s) throws IOException { 497 s.defaultWriteObject(); 498 if (getUIClassID().equals(uiClassID)) { 499 byte count = JComponent.getWriteObjCounter(this); 500 JComponent.setWriteObjCounter(this, --count); 501 if (count == 0 && ui != null) { 502 ui.installUI(this); 503 } 504 } 505 } 506 507 508 518 protected String paramString() { 519 StringBuffer chooserPanelsString = new StringBuffer (""); 520 for (int i=0; i<chooserPanels.length; i++) { 521 chooserPanelsString.append("[" + chooserPanels[i].toString() 522 + "]"); 523 } 524 String previewPanelString = (previewPanel != null ? 525 previewPanel.toString() : ""); 526 527 return super.paramString() + 528 ",chooserPanels=" + chooserPanelsString.toString() + 529 ",previewPanel=" + previewPanelString; 530 } 531 532 536 protected AccessibleContext accessibleContext = null; 537 538 547 public AccessibleContext getAccessibleContext() { 548 if (accessibleContext == null) { 549 accessibleContext = new AccessibleJColorChooser(); 550 } 551 return accessibleContext; 552 } 553 554 560 protected class AccessibleJColorChooser extends AccessibleJComponent { 561 562 569 public AccessibleRole getAccessibleRole() { 570 return AccessibleRole.COLOR_CHOOSER; 571 } 572 573 } } 575 576 577 583 class ColorChooserDialog extends JDialog { 584 private Color initialColor; 585 private JColorChooser chooserPane; 586 private JButton cancelButton; 587 588 public ColorChooserDialog(Dialog owner, String title, boolean modal, 589 Component c, JColorChooser chooserPane, 590 ActionListener okListener, ActionListener cancelListener) 591 throws HeadlessException { 592 super(owner, title, modal); 593 initColorChooserDialog(c, chooserPane, okListener, cancelListener); 594 } 595 596 public ColorChooserDialog(Frame owner, String title, boolean modal, 597 Component c, JColorChooser chooserPane, 598 ActionListener okListener, ActionListener cancelListener) 599 throws HeadlessException { 600 super(owner, title, modal); 601 initColorChooserDialog(c, chooserPane, okListener, cancelListener); 602 } 603 604 protected void initColorChooserDialog(Component c, JColorChooser chooserPane, 605 ActionListener okListener, ActionListener cancelListener) { 606 608 this.chooserPane = chooserPane; 609 610 String okString = UIManager.getString("ColorChooser.okText"); 611 String cancelString = UIManager.getString("ColorChooser.cancelText"); 612 String resetString = UIManager.getString("ColorChooser.resetText"); 613 614 Container contentPane = getContentPane(); 615 contentPane.setLayout(new BorderLayout()); 616 contentPane.add(chooserPane, BorderLayout.CENTER); 617 618 621 JPanel buttonPane = new JPanel (); 622 buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER)); 623 JButton okButton = new JButton (okString); 624 getRootPane().setDefaultButton(okButton); 625 okButton.setActionCommand("OK"); 626 if (okListener != null) { 627 okButton.addActionListener(okListener); 628 } 629 okButton.addActionListener(new ActionListener() { 630 public void actionPerformed(ActionEvent e) { 631 hide(); 632 } 633 }); 634 buttonPane.add(okButton); 635 636 cancelButton = new JButton (cancelString); 637 638 Action cancelKeyAction = new AbstractAction () { 640 public void actionPerformed(ActionEvent e) { 641 ((AbstractButton )e.getSource()).fireActionPerformed(e); 642 } 643 }; 644 KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke((char)KeyEvent.VK_ESCAPE, false); 645 InputMap inputMap = cancelButton.getInputMap(JComponent. 646 WHEN_IN_FOCUSED_WINDOW); 647 ActionMap actionMap = cancelButton.getActionMap(); 648 if (inputMap != null && actionMap != null) { 649 inputMap.put(cancelKeyStroke, "cancel"); 650 actionMap.put("cancel", cancelKeyAction); 651 } 652 654 cancelButton.setActionCommand("cancel"); 655 if (cancelListener != null) { 656 cancelButton.addActionListener(cancelListener); 657 } 658 cancelButton.addActionListener(new ActionListener() { 659 public void actionPerformed(ActionEvent e) { 660 hide(); 661 } 662 }); 663 buttonPane.add(cancelButton); 664 665 JButton resetButton = new JButton (resetString); 666 resetButton.addActionListener(new ActionListener() { 667 public void actionPerformed(ActionEvent e) { 668 reset(); 669 } 670 }); 671 int mnemonic = UIManager.getInt("ColorChooser.resetMnemonic", -1); 672 if (mnemonic != -1) { 673 resetButton.setMnemonic(mnemonic); 674 } 675 buttonPane.add(resetButton); 676 contentPane.add(buttonPane, BorderLayout.SOUTH); 677 678 if (JDialog.isDefaultLookAndFeelDecorated()) { 679 boolean supportsWindowDecorations = 680 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 681 if (supportsWindowDecorations) { 682 getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG); 683 } 684 } 685 applyComponentOrientation(((c == null) ? getRootPane() : c).getComponentOrientation()); 686 687 pack(); 688 setLocationRelativeTo(c); 689 690 this.addWindowListener(new Closer()); 691 this.addComponentListener(new DisposeOnClose()); 692 } 693 694 public void show() { 695 initialColor = chooserPane.getColor(); 696 super.show(); 697 } 698 699 public void reset() { 700 chooserPane.setColor(initialColor); 701 } 702 703 class Closer extends WindowAdapter implements Serializable{ 704 public void windowClosing(WindowEvent e) { 705 cancelButton.doClick(0); 706 Window w = e.getWindow(); 707 w.hide(); 708 } 709 } 710 711 static class DisposeOnClose extends ComponentAdapter implements Serializable{ 712 public void componentHidden(ComponentEvent e) { 713 Window w = (Window)e.getComponent(); 714 w.dispose(); 715 } 716 } 717 718 } 719 720 class ColorTracker implements ActionListener, Serializable { 721 JColorChooser chooser; 722 Color color; 723 724 public ColorTracker(JColorChooser c) { 725 chooser = c; 726 } 727 728 public void actionPerformed(ActionEvent e) { 729 color = chooser.getColor(); 730 } 731 732 public Color getColor() { 733 return color; 734 } 735 } 736 737 | Popular Tags |