/*
 * Copyright (c) 2024. Elex. All Rights Reesrved.
 * https://www.elex-project.com/
 */

package com.elex_project.examples.components.chooser;

import javax.swing.*;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class ColorChooser3Example extends JPanel {
	public ColorChooser3Example() {
		super(new BorderLayout());

		final JLabel label = new JLabel("Welcome", JLabel.CENTER);
		label.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
		final JColorChooser colorChooser = new JColorChooser(label.getForeground());

		colorChooser.setPreviewPanel(new JPanel());

		AbstractColorChooserPanel[] panels = {new AbstractColorChooserPanel() {
			@Override
			public void updateChooser() {

			}

			@Override
			protected void buildChooser() {

			}

			@Override
			public String getDisplayName() {
				return null;
			}

			@Override
			public Icon getSmallDisplayIcon() {
				return null;
			}

			@Override
			public Icon getLargeDisplayIcon() {
				return null;
			}
		}};
		colorChooser.setChooserPanels(panels);

		// 레이아웃에 추가
		add(label, BorderLayout.NORTH);
		add(colorChooser, BorderLayout.CENTER);

		// ChangeListener를 통해서 변경된 색상을 받는다.
		colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent changeEvent) {
				Color color = colorChooser.getColor();
				label.setForeground(color);
			}
		});
	}

	public static void main(String... args) {

		final JFrame jFrame = new JFrame();
		jFrame.setTitle("Example");
		jFrame.setSize(800, 600);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setContentPane(new ColorChooser3Example());

		SwingUtilities.invokeLater(() -> {
			jFrame.setVisible(true);
		});


	}

	class DummyIcon implements Icon{

		@Override
		public void paintIcon(Component component, Graphics graphics, int x, int y) {
			graphics.setColor(Color.DARK_GRAY);
			graphics.fillRect(x,y,x+getIconWidth(),y+getIconHeight());
		}

		@Override
		public int getIconWidth() {
			return 16;
		}

		@Override
		public int getIconHeight() {
			return 16;
		}
	}
}
