001    /*
002    * @(#)ColorPickerDialog.java  1.0  2008-03-01
003    *
004    * Copyright (c) 2008 Jeremy Wood
005    * E-mail: mickleness@gmail.com
006    * All rights reserved.
007    *
008    * The copyright of this software is owned by Jeremy Wood.
009    * You may not use, copy or modify this software, except in
010    * accordance with the license agreement you entered into with
011    * Jeremy Wood. For details see accompanying license terms.
012    */
013    package com.colorpicker.swing;
014    
015    import javax.swing.JComponent;
016    import java.awt.Color;
017    import java.awt.Component;
018    import java.awt.Frame;
019    import java.awt.GridBagConstraints;
020    import java.awt.GridBagLayout;
021    import java.awt.Insets;
022    import java.awt.Dialog;
023    import java.awt.Frame;
024    import java.awt.event.ActionEvent;
025    import java.awt.event.ActionListener;
026    import javax.swing.JButton;
027    import javax.swing.JDialog;
028    import javax.swing.JPanel;
029    import javax.swing.SwingUtilities;
030    
031    /** This wraps a <code>ColorPicker</code> in a simple dialog with "OK" and "Cancel" options.
032     * <P>(This object is used by the static calls in <code>ColorPicker</code> to show a dialog.)
033     *
034     */
035    class ColorPickerDialog extends JDialog {
036        
037            private static final long serialVersionUID = 1L;
038            
039            ColorPicker cp;
040            int alpha;
041            JButton ok = new JButton(ColorPicker.strings.getObject("OK").toString());
042            JButton cancel = new JButton(ColorPicker.strings.getObject("Cancel").toString());
043            Color returnValue = null;
044            ActionListener buttonListener = new ActionListener() {
045                    public void actionPerformed(ActionEvent e) {
046                            Object src = e.getSource();
047                            if(src==ok) {
048                                    returnValue = cp.getColor();
049                            }
050                            setVisible(false);
051                    }
052            };
053            
054            public ColorPickerDialog(Frame owner, Color color,boolean includeOpacity) {
055                    super(owner);
056                    initialize(owner,color,includeOpacity);
057            }
058    
059            public ColorPickerDialog(Dialog owner, Color color,boolean includeOpacity) {
060                    super(owner);
061                    initialize(owner,color,includeOpacity);
062            }
063            
064            private void initialize(Component owner,Color color,boolean includeOpacity) {
065                    cp = new ColorPicker(true,includeOpacity);
066                    setModal(true);
067                    setResizable(false);
068                    getContentPane().setLayout(new GridBagLayout());
069                    GridBagConstraints c = new GridBagConstraints();
070                    c.gridx = 0; c.gridy = 0;
071                    c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH;
072                    c.gridwidth = GridBagConstraints.REMAINDER;
073                    c.insets = new Insets(10,10,10,10);
074                    getContentPane().add(cp,c);
075                    c.gridy++; c.gridwidth = 1;
076                    getContentPane().add(new JPanel(),c);
077                    c.gridx++; c.weightx = 0;
078                    getContentPane().add(cancel,c);
079                    c.gridx++; c.weightx = 0;
080                    getContentPane().add(ok,c);
081                    cp.setRGB(color.getRed(), color.getGreen(), color.getBlue());
082                    cp.setOpacity( ((float)color.getAlpha())/255f );
083                    alpha = color.getAlpha();
084                    pack();
085            setLocationRelativeTo(owner);
086                    
087                    ok.addActionListener(buttonListener);
088                    cancel.addActionListener(buttonListener);
089                    
090                    getRootPane().setDefaultButton(ok);
091            }
092            
093            /** @return the color committed when the user clicked 'OK'.  Note this returns <code>null</code>
094             * if the user canceled this dialog, or exited via the close decoration.
095             */
096            public Color getColor() {
097                    return returnValue;
098            }
099    }
100