116 */
117 public static final String CHOOSER_PANELS_PROPERTY = "chooserPanels";
118
119
120 /**
121 * Shows a modal color-chooser dialog and blocks until the
122 * dialog is hidden. If the user presses the "OK" button, then
123 * this method hides/disposes the dialog and returns the selected color.
124 * If the user presses the "Cancel" button or closes the dialog without
125 * pressing "OK", then this method hides/disposes the dialog and returns
126 * <code>null</code>.
127 *
128 * @param component the parent <code>Component</code> for the dialog
129 * @param title the String containing the dialog's title
130 * @param initialColor the initial Color set when the color-chooser is shown
131 * @return the selected color or <code>null</code> if the user opted out
132 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
133 * returns true.
134 * @see java.awt.GraphicsEnvironment#isHeadless
135 */
136 public static Color showDialog(Component component,
137 String title, Color initialColor) throws HeadlessException {
138
139 final JColorChooser pane = new JColorChooser(initialColor != null?
140 initialColor : Color.white);
141
142 ColorTracker ok = new ColorTracker(pane);
143 JDialog dialog = createDialog(component, title, true, pane, ok, null);
144
145 dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose());
146
147 dialog.show(); // blocks until user brings dialog down...
148
149 return ok.getColor();
150 }
151
152
153 /**
154 * Creates and returns a new dialog containing the specified
155 * <code>ColorChooser</code> pane along with "OK", "Cancel", and "Reset"
637
638 Locale locale = getLocale();
639 String okString = UIManager.getString("ColorChooser.okText", locale);
640 String cancelString = UIManager.getString("ColorChooser.cancelText", locale);
641 String resetString = UIManager.getString("ColorChooser.resetText", locale);
642
643 Container contentPane = getContentPane();
644 contentPane.setLayout(new BorderLayout());
645 contentPane.add(chooserPane, BorderLayout.CENTER);
646
647 /*
648 * Create Lower button panel
649 */
650 JPanel buttonPane = new JPanel();
651 buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
652 JButton okButton = new JButton(okString);
653 getRootPane().setDefaultButton(okButton);
654 okButton.getAccessibleContext().setAccessibleDescription(okString);
655 okButton.setActionCommand("OK");
656 okButton.addActionListener(new ActionListener() {
657 public void actionPerformed(ActionEvent e) {
658 hide();
659 }
660 });
661 if (okListener != null) {
662 okButton.addActionListener(okListener);
663 }
664 buttonPane.add(okButton);
665
666 cancelButton = new JButton(cancelString);
667 cancelButton.getAccessibleContext().setAccessibleDescription(cancelString);
668
669 // The following few lines are used to register esc to close the dialog
670 @SuppressWarnings("serial") // anonymous class
671 Action cancelKeyAction = new AbstractAction() {
672 public void actionPerformed(ActionEvent e) {
673 ((AbstractButton)e.getSource()).fireActionPerformed(e);
674 }
675 };
676 KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
677 InputMap inputMap = cancelButton.getInputMap(JComponent.
678 WHEN_IN_FOCUSED_WINDOW);
679 ActionMap actionMap = cancelButton.getActionMap();
680 if (inputMap != null && actionMap != null) {
681 inputMap.put(cancelKeyStroke, "cancel");
682 actionMap.put("cancel", cancelKeyAction);
683 }
684 // end esc handling
685
686 cancelButton.setActionCommand("cancel");
687 cancelButton.addActionListener(new ActionListener() {
688 public void actionPerformed(ActionEvent e) {
689 hide();
690 }
691 });
692 if (cancelListener != null) {
693 cancelButton.addActionListener(cancelListener);
694 }
695 buttonPane.add(cancelButton);
696
697 JButton resetButton = new JButton(resetString);
698 resetButton.getAccessibleContext().setAccessibleDescription(resetString);
699 resetButton.addActionListener(new ActionListener() {
700 public void actionPerformed(ActionEvent e) {
701 reset();
702 }
703 });
704 int mnemonic = SwingUtilities2.getUIDefaultsInt("ColorChooser.resetMnemonic", locale, -1);
705 if (mnemonic != -1) {
706 resetButton.setMnemonic(mnemonic);
707 }
708 buttonPane.add(resetButton);
709 contentPane.add(buttonPane, BorderLayout.SOUTH);
710
711 if (JDialog.isDefaultLookAndFeelDecorated()) {
712 boolean supportsWindowDecorations =
713 UIManager.getLookAndFeel().getSupportsWindowDecorations();
714 if (supportsWindowDecorations) {
715 getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
716 }
717 }
718 applyComponentOrientation(((c == null) ? getRootPane() : c).getComponentOrientation());
719
720 pack();
721 setLocationRelativeTo(c);
722
723 this.addWindowListener(new Closer());
724 }
725
726 public void show() {
727 initialColor = chooserPane.getColor();
728 super.show();
729 }
730
731 public void reset() {
732 chooserPane.setColor(initialColor);
733 }
734
735 @SuppressWarnings("serial") // JDK-implementation class
736 class Closer extends WindowAdapter implements Serializable{
737 public void windowClosing(WindowEvent e) {
738 cancelButton.doClick(0);
739 Window w = e.getWindow();
740 w.hide();
741 }
742 }
743
744 @SuppressWarnings("serial") // JDK-implementation class
745 static class DisposeOnClose extends ComponentAdapter implements Serializable{
746 public void componentHidden(ComponentEvent e) {
747 Window w = (Window)e.getComponent();
748 w.dispose();
749 }
750 }
751
752 }
753
754 @SuppressWarnings("serial") // JDK-implementation class
755 class ColorTracker implements ActionListener, Serializable {
756 JColorChooser chooser;
|
116 */
117 public static final String CHOOSER_PANELS_PROPERTY = "chooserPanels";
118
119
120 /**
121 * Shows a modal color-chooser dialog and blocks until the
122 * dialog is hidden. If the user presses the "OK" button, then
123 * this method hides/disposes the dialog and returns the selected color.
124 * If the user presses the "Cancel" button or closes the dialog without
125 * pressing "OK", then this method hides/disposes the dialog and returns
126 * <code>null</code>.
127 *
128 * @param component the parent <code>Component</code> for the dialog
129 * @param title the String containing the dialog's title
130 * @param initialColor the initial Color set when the color-chooser is shown
131 * @return the selected color or <code>null</code> if the user opted out
132 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
133 * returns true.
134 * @see java.awt.GraphicsEnvironment#isHeadless
135 */
136 @SuppressWarnings("deprecation")
137 public static Color showDialog(Component component,
138 String title, Color initialColor) throws HeadlessException {
139
140 final JColorChooser pane = new JColorChooser(initialColor != null?
141 initialColor : Color.white);
142
143 ColorTracker ok = new ColorTracker(pane);
144 JDialog dialog = createDialog(component, title, true, pane, ok, null);
145
146 dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose());
147
148 dialog.show(); // blocks until user brings dialog down...
149
150 return ok.getColor();
151 }
152
153
154 /**
155 * Creates and returns a new dialog containing the specified
156 * <code>ColorChooser</code> pane along with "OK", "Cancel", and "Reset"
638
639 Locale locale = getLocale();
640 String okString = UIManager.getString("ColorChooser.okText", locale);
641 String cancelString = UIManager.getString("ColorChooser.cancelText", locale);
642 String resetString = UIManager.getString("ColorChooser.resetText", locale);
643
644 Container contentPane = getContentPane();
645 contentPane.setLayout(new BorderLayout());
646 contentPane.add(chooserPane, BorderLayout.CENTER);
647
648 /*
649 * Create Lower button panel
650 */
651 JPanel buttonPane = new JPanel();
652 buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
653 JButton okButton = new JButton(okString);
654 getRootPane().setDefaultButton(okButton);
655 okButton.getAccessibleContext().setAccessibleDescription(okString);
656 okButton.setActionCommand("OK");
657 okButton.addActionListener(new ActionListener() {
658 @SuppressWarnings("deprecation")
659 public void actionPerformed(ActionEvent e) {
660 hide();
661 }
662 });
663 if (okListener != null) {
664 okButton.addActionListener(okListener);
665 }
666 buttonPane.add(okButton);
667
668 cancelButton = new JButton(cancelString);
669 cancelButton.getAccessibleContext().setAccessibleDescription(cancelString);
670
671 // The following few lines are used to register esc to close the dialog
672 @SuppressWarnings("serial") // anonymous class
673 Action cancelKeyAction = new AbstractAction() {
674 public void actionPerformed(ActionEvent e) {
675 ((AbstractButton)e.getSource()).fireActionPerformed(e);
676 }
677 };
678 KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
679 InputMap inputMap = cancelButton.getInputMap(JComponent.
680 WHEN_IN_FOCUSED_WINDOW);
681 ActionMap actionMap = cancelButton.getActionMap();
682 if (inputMap != null && actionMap != null) {
683 inputMap.put(cancelKeyStroke, "cancel");
684 actionMap.put("cancel", cancelKeyAction);
685 }
686 // end esc handling
687
688 cancelButton.setActionCommand("cancel");
689 cancelButton.addActionListener(new ActionListener() {
690 @SuppressWarnings("deprecation")
691 public void actionPerformed(ActionEvent e) {
692 hide();
693 }
694 });
695 if (cancelListener != null) {
696 cancelButton.addActionListener(cancelListener);
697 }
698 buttonPane.add(cancelButton);
699
700 JButton resetButton = new JButton(resetString);
701 resetButton.getAccessibleContext().setAccessibleDescription(resetString);
702 resetButton.addActionListener(new ActionListener() {
703 public void actionPerformed(ActionEvent e) {
704 reset();
705 }
706 });
707 int mnemonic = SwingUtilities2.getUIDefaultsInt("ColorChooser.resetMnemonic", locale, -1);
708 if (mnemonic != -1) {
709 resetButton.setMnemonic(mnemonic);
710 }
711 buttonPane.add(resetButton);
712 contentPane.add(buttonPane, BorderLayout.SOUTH);
713
714 if (JDialog.isDefaultLookAndFeelDecorated()) {
715 boolean supportsWindowDecorations =
716 UIManager.getLookAndFeel().getSupportsWindowDecorations();
717 if (supportsWindowDecorations) {
718 getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
719 }
720 }
721 applyComponentOrientation(((c == null) ? getRootPane() : c).getComponentOrientation());
722
723 pack();
724 setLocationRelativeTo(c);
725
726 this.addWindowListener(new Closer());
727 }
728
729 @SuppressWarnings("deprecation")
730 public void show() {
731 initialColor = chooserPane.getColor();
732 super.show();
733 }
734
735 public void reset() {
736 chooserPane.setColor(initialColor);
737 }
738
739 @SuppressWarnings("serial") // JDK-implementation class
740 class Closer extends WindowAdapter implements Serializable{
741 @SuppressWarnings("deprecation")
742 public void windowClosing(WindowEvent e) {
743 cancelButton.doClick(0);
744 Window w = e.getWindow();
745 w.hide();
746 }
747 }
748
749 @SuppressWarnings("serial") // JDK-implementation class
750 static class DisposeOnClose extends ComponentAdapter implements Serializable{
751 public void componentHidden(ComponentEvent e) {
752 Window w = (Window)e.getComponent();
753 w.dispose();
754 }
755 }
756
757 }
758
759 @SuppressWarnings("serial") // JDK-implementation class
760 class ColorTracker implements ActionListener, Serializable {
761 JColorChooser chooser;
|