package colorjpane;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JApplet {
        private JColorChooser chooser = new JColorChooser();
        private AbstractColorChooserPanel colorPanels[] =
                        new AbstractColorChooserPanel[] {
                                new ListPanel(),
                        };
        private JButton button = new JButton("Show Color Chooser");
        private JDialog dialog;

        public void init() {
          Container contentPane = getContentPane();

          contentPane.setLayout(new FlowLayout());
          contentPane.add(button, BorderLayout.CENTER);
          chooser.addChooserPanel(colorPanels[0]);


          button.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
               if(dialog == null)
               dialog	= JColorChooser.createDialog(
               Test.this,  // parent comp
               "Pick A Color",   // dialog title
                false, 			  // modality
                chooser,
                null, null);
                dialog.setVisible(true);
                }
              });
        }
}
class ListPanel extends AbstractColorChooserPanel
      implements ListSelectionListener {
        private JPanel labelPanel = new JPanel(),
                                   listPanel = new JPanel();

        private JList CList = new JList(), MList = new JList(),YList = new JList(),
                              KList=new JList();

        private DefaultListModel CModel = new DefaultListModel(),
            MModel = new DefaultListModel(),YModel = new DefaultListModel(),
            KModel=new DefaultListModel();

        private boolean isAdjusting = false;
        ColorTranslate CMYKRGB=new ColorTranslate();
        public void updateChooser() {
                if( ! isAdjusting) {
                        isAdjusting = true;
                        Color color = getColorFromModel();
                        CMYKRGB.RGBtoCMYK(color);
                        CList.setSelectedIndex(CMYKRGB.C);
                        MList.setSelectedIndex(CMYKRGB.M);
                        YList.setSelectedIndex(CMYKRGB.Y);
                        KList.setSelectedIndex(CMYKRGB.K);

                        CList.ensureIndexIsVisible(CMYKRGB.C);
                        MList.ensureIndexIsVisible(CMYKRGB.M);
                        YList.ensureIndexIsVisible(CMYKRGB.Y);
                        KList.ensureIndexIsVisible(CMYKRGB.K);
                        isAdjusting = false;
                }
        }
    protected void buildChooser() {
                CList.setFixedCellWidth(50);
                MList.setFixedCellWidth(50);
                YList.setFixedCellWidth(50);
                KList.setFixedCellWidth(50);
                for(int i=0; i <=100; ++i) {
                        CModel.addElement(Integer.toString(i));
                        MModel.addElement(Integer.toString(i));
                        YModel.addElement(Integer.toString(i));
                        KModel.addElement(Integer.toString(i));
                }

                CList.setModel(CModel);
                MList.setModel(MModel);
                YList.setModel(YModel);
                KList.setModel(KModel);

                listPanel.setLayout(new GridLayout(0,4,10,0));

                listPanel.add(new JScrollPane(CList));
                listPanel.add(new JScrollPane(MList));
                listPanel.add(new JScrollPane(YList));
                listPanel.add(new JScrollPane(KList));
                labelPanel.setLayout(new GridLayout(0,4,10,0));

                labelPanel.add(new JLabel("C"));
                labelPanel.add(new JLabel("M"));
                labelPanel.add(new JLabel("Y"));
                labelPanel.add(new JLabel("K"));

                setLayout(new BorderLayout());
                add(labelPanel, BorderLayout.NORTH);
                add(listPanel, BorderLayout.CENTER);

                CList.addListSelectionListener(this);
                MList.addListSelectionListener(this);
                YList.addListSelectionListener(this);
                KList.addListSelectionListener(this);
        }
        public void valueChanged(ListSelectionEvent e) {
                if(isAdjusting)
                  return;
                isAdjusting=true;
                CMYKRGB.C = CList.getSelectedIndex();
                CMYKRGB.M = MList.getSelectedIndex();
                CMYKRGB.Y = YList.getSelectedIndex();
                CMYKRGB.K = KList.getSelectedIndex();
                if((CMYKRGB.C<=100)&&(CMYKRGB.M<=100)&&(CMYKRGB.Y<=100)
                   &&(CMYKRGB.K<=100)&&(CMYKRGB.C>=0)&&(CMYKRGB.M>=0)&&(CMYKRGB.Y>=0)
                   &&(CMYKRGB.K>=0))
                     getColorSelectionModel().setSelectedColor(CMYKRGB.CMYKtoRGB());
               isAdjusting=false;
        }

      public String getDisplayName() {
                return "CMYK";
        }
    public Icon getSmallDisplayIcon() {
                return null;
        }
    public Icon getLargeDisplayIcon() {
                return null;
        }
}
