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

package com.elex_project.examples.components.button;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class ButtonExample extends JPanel {
	public ButtonExample() {
		super(new FlowLayout());

		final JButton button1 = new JButton("Button 1", new DummyIcon());
		button1.setMnemonic(KeyEvent.VK_A);// Alt와 함께 누른다.
		button1.setActionCommand("disable");

		final JButton button2 = new JButton("Button 2", new DummyIcon());
		button2.setMnemonic(KeyEvent.VK_B);
		button2.setActionCommand("delete");

		// 레이아웃에 추가
		add(button1);
		add(button2);

		// 액션 리스너를 통해서 클릭 이벤트를 받는다.
		final ActionListener actionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				switch (actionEvent.getActionCommand()){
					case "disable":
						System.out.println("disable");
						break;
					case "delete":
						System.out.println("delete");
						break;
					default:
						System.out.println("oops!");
						break;
				}
			}
		};
		button1.addActionListener(actionListener);
		button2.addActionListener(actionListener);
	}

	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 ButtonExample());

		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;
		}
	}
}
