package org.gnu.grub.fonttool;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ViewerFrame extends JFrame {
    private final Font font;

    private JLabel startingCodePointLabel;
    private JSlider startingCodePointSlider;
    private CharMatrix charMatrix;
    private CharView charView;
    private JLabel statusBar;

    public ViewerFrame(Font font) {
        this.font = font;

        createComponents();
        buildUI();
        setSize(800, 600);
    }

    private void createComponents() {
        startingCodePointLabel = new JLabel("Starting Code Point");
        startingCodePointSlider = new JSlider(JSlider.HORIZONTAL, 0, 65535, 0);
        startingCodePointSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                charMatrix.setStartingChar(startingCodePointSlider.getValue());
            }
        });
        charMatrix = new CharMatrix();
        charMatrix.setCharFont(font);
        charMatrix.setGlyphSelectionListener(new CharMatrix.GlyphSelectionListener() {
            public void glyphSelected(int codePoint) {
                charView.setGlyph(font.getGlyph(codePoint));
            }
        });
        charView = new CharView();
        charView.setZoom(3);
        charView.setBorder(new BevelBorder(BevelBorder.RAISED));
        statusBar = new JLabel();
    }

    private void buildUI() {
        JComponent topPanel = Box.createVerticalBox();
        topPanel.add(startingCodePointLabel);
        topPanel.add(startingCodePointSlider);

        JComponent charPanel = new JPanel(new GridLayout(2, 1));
        charPanel.add(charMatrix);
        charPanel.add(charView);

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);
        add(charPanel, BorderLayout.CENTER);
        add(statusBar, BorderLayout.SOUTH);
    }
}
