001package jmri.jmrit.display;
002
003import java.awt.Color;
004import java.awt.event.ActionEvent;
005
006import javax.swing.BorderFactory;
007import javax.swing.BoxLayout;
008import javax.swing.JComboBox;
009import javax.swing.JLabel;
010import javax.swing.JPanel;
011import javax.swing.SwingConstants;
012
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016import jmri.jmrit.display.palette.Bundle;
017import jmri.util.swing.ImagePanel;
018
019/**
020 * Companion class to DisplayFrame.
021 * @author peteCressman 2020
022 * @author Egbert Broerse
023 */
024public class PreviewPanel extends JPanel {
025
026    DisplayFrame _parent;
027    JComboBox<String> _bgColorBox;
028
029    public PreviewPanel(DisplayFrame parent, ImagePanel panel1, ImagePanel panel2, boolean hasComboBox) {
030        super();
031        _parent = parent;
032        init(panel1, panel2, hasComboBox);
033    }
034    
035    private void init(ImagePanel panel1, ImagePanel panel2, boolean hasComboBox) {
036        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
037        setBorder(BorderFactory.createTitledBorder(
038                BorderFactory.createLineBorder(Color.black, 1),
039                Bundle.getMessage("PreviewBorderTitle")));
040        if (hasComboBox) {
041            add(makeBgButtonPanel(panel1, panel2));
042        }
043        if (panel1 != null) {
044            add(panel1);
045        }
046        if (panel2 != null) {
047            add(panel2);
048        }
049    }
050
051    public void setBackgroundSelection(int index) {
052        if (_bgColorBox != null) {
053            _bgColorBox.setSelectedIndex(index);
054        }
055    }
056
057    private JPanel makeBgButtonPanel(ImagePanel preview1, ImagePanel preview2) {
058        _bgColorBox = new JComboBox<>();
059        _bgColorBox.addItem(Bundle.getMessage("PanelBgColor")); // PanelColor key is specific for CPE, but too long for combo
060        _bgColorBox.addItem(Bundle.getMessage("White"));
061        _bgColorBox.addItem(Bundle.getMessage("LightGray"));
062        _bgColorBox.addItem(Bundle.getMessage("DarkGray"));
063        _bgColorBox.addItem(Bundle.getMessage("Checkers"));
064        _bgColorBox.setSelectedIndex(_parent.getPreviewBg()); // Global field, starts as 0 = panel bg color
065
066        JPanel bkgdBoxPanel = new JPanel();
067        bkgdBoxPanel.setLayout(new BoxLayout(bkgdBoxPanel, BoxLayout.Y_AXIS));
068        bkgdBoxPanel.add(new JLabel(Bundle.getMessage("setBackground"), SwingConstants.RIGHT));
069        bkgdBoxPanel.add(_bgColorBox);
070
071        _bgColorBox.addActionListener((ActionEvent e) -> {
072            log.debug("PreviewPanel _bgColorBox action");
073            int previewBgSet = _bgColorBox.getSelectedIndex();
074            _parent.setPreviewBg(previewBgSet); // notify user choice in field on children override
075            // load background image
076            log.debug("PreviewPanel color #{} set", previewBgSet);
077            if (preview1 != null) {
078                preview1.setImage(_parent.getPreviewBackground());
079                preview1.revalidate(); // force redraw
080            }
081            if (preview2 != null) {
082                preview2.setImage(_parent.getPreviewBackground());
083                preview2.revalidate(); // force redraw
084            }
085        });
086        JPanel panel = new JPanel();
087        panel.add(bkgdBoxPanel);
088        return panel;
089    }
090
091    private final static Logger log = LoggerFactory.getLogger(PreviewPanel.class);
092
093}