001package jmri.jmrit.display.palette;
002
003import java.awt.Font;
004import java.awt.GraphicsEnvironment;
005import java.awt.event.ActionListener;
006import java.awt.event.ItemEvent;
007import java.awt.event.ItemListener;
008import javax.swing.BoxLayout;
009import javax.swing.JComboBox;
010import javax.swing.JLabel;
011import javax.swing.JPanel;
012
013import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
014import jmri.jmrit.display.PositionablePopupUtil;
015
016public class FontPanel extends JPanel implements ItemListener {
017
018    static final String[] JUSTIFICATION = {Bundle.getMessage("left"),
019            Bundle.getMessage("center"),
020            Bundle.getMessage("right")};
021
022    static final String[] STYLES = {Bundle.getMessage("Plain"),
023        Bundle.getMessage("Bold"),
024        Bundle.getMessage("Italic"),
025        Bundle.getMessage("Bold/italic")};
026
027    static final String[] FONTSIZE = {"6", "8", "10", "11", "12", "14", "16",
028            "20", "24", "28", "32", "36"};
029
030    public static final int SIZE = 1;
031    public static final int STYLE = 2;
032    public static final int JUST = 3;
033    public static final int FACE = 4;
034
035    private AJComboBox<Font> _fontFaceBox;
036    private AJComboBox<String> _fontSizeBox;
037    private AJComboBox<String> _fontStyleBox;
038    private AJComboBox<String> _fontJustBox;
039
040    PositionablePopupUtil _util;
041    ActionListener _callBack;
042
043    static class AJComboBox<T> extends JComboBox<T> {
044        int _which;
045
046        AJComboBox(T[] items, int which) {
047            super(items);
048            _which = which;
049        }
050    }
051
052    public FontPanel(PositionablePopupUtil util, ActionListener ca) {
053        _util = util;
054        _callBack = ca;
055        makeFontPanels();
056    }
057
058    private JPanel makeBoxPanel(String caption, JComboBox<?> box) {
059        JPanel panel = new JPanel();
060        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
061        panel.add(new JLabel(Bundle.getMessage(caption)));
062        box.addItemListener(this);
063        panel.add(box);
064        return panel;
065    }
066
067    private void makeFontPanels() {
068
069        JPanel fontPanel = new JPanel();
070
071        Font defaultFont = _util.getFont();
072        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
073        String[] fontFamilyNames = ge.getAvailableFontFamilyNames();
074        Font[] fonts = new Font[fontFamilyNames.length];
075        int k = 0;
076        for (String fontFamilyName : fontFamilyNames) {
077            fonts[k++] = new Font(fontFamilyName, defaultFont.getStyle(), defaultFont.getSize()) {
078                @Override
079                public String toString() {
080                    return getFamily();
081                }
082            };
083        }
084        _fontFaceBox = new AJComboBox<>(fonts, FACE);
085        fontPanel.add(makeBoxPanel("EditFont", _fontFaceBox)); // NOI18N
086
087        _fontSizeBox = new AJComboBox<>(FONTSIZE, SIZE);
088        fontPanel.add(makeBoxPanel("FontSize", _fontSizeBox)); // NOI18N
089
090        _fontStyleBox = new AJComboBox<>(STYLES, STYLE);
091        fontPanel.add(makeBoxPanel("FontStyle", _fontStyleBox)); // NOI18N
092
093        _fontJustBox = new AJComboBox<>(JUSTIFICATION, JUST);
094        fontPanel.add(makeBoxPanel("Justification", _fontJustBox)); // NOI18N
095        this.add(fontPanel);
096    }
097
098    public void setFontSelections() {
099        _fontFaceBox.setSelectedItem(_util.getFont());
100        int row = 4;
101        for (int i = 0; i < FONTSIZE.length; i++) {
102            if (_util.getFontSize() == Integer.parseInt(FONTSIZE[i])) {
103                row = i;
104                break;
105            }
106        }
107        _fontSizeBox.setSelectedIndex(row);
108        _fontStyleBox.setSelectedIndex(_util.getFont().getStyle());
109        switch (_util.getJustification()) {
110            case PositionablePopupUtil.LEFT:
111                row = 0;
112                break;
113            case PositionablePopupUtil.CENTRE:
114                row = 1;
115                break;
116            case PositionablePopupUtil.RIGHT:
117            default:
118                row = 2;
119        }
120        _fontJustBox.setSelectedIndex(row);
121        _callBack.actionPerformed(null);
122    }
123
124    @SuppressFBWarnings(value = "Raw use of parameterized class", justification="AJComboBox is checked")
125    @Override
126    public void itemStateChanged(ItemEvent e) {
127        if (e.getSource() instanceof AJComboBox<?>) {
128            AJComboBox<?> comboBox = (AJComboBox<?>) e.getSource();
129            switch (comboBox._which) {
130                case SIZE:
131                    String size = (comboBox.getSelectedItem() != null ? (String) comboBox.getSelectedItem() : "10");
132                    _util.setFontSize(Float.parseFloat(size));
133                    break;
134                case STYLE:
135                    int style = 0;
136                    switch (comboBox.getSelectedIndex()) {
137                        case 0:
138                            style = Font.PLAIN;
139                            break;
140                        case 1:
141                            style = Font.BOLD;
142                            break;
143                        case 2:
144                            style = Font.ITALIC;
145                            break;
146                        case 3:
147                            style = (Font.BOLD | Font.ITALIC);
148                            break;
149                        default:
150                            log.warn("Unexpected index {}  in itemStateChanged", comboBox.getSelectedIndex());
151                            break;
152                    }
153                    _util.setFontStyle(style);
154                    break;
155                case JUST:
156                    int just = 0;
157                    switch (comboBox.getSelectedIndex()) {
158                        case 0:
159                            just = PositionablePopupUtil.LEFT;
160                            break;
161                        case 1:
162                            just = PositionablePopupUtil.CENTRE;
163                            break;
164                        case 2:
165                            just = PositionablePopupUtil.RIGHT;
166                            break;
167                        default:
168                            log.warn("Unexpected index {}  in itemStateChanged", comboBox.getSelectedIndex());
169                            break;
170                    }
171                    _util.setJustification(just);
172                    break;
173                case FACE:
174                    Font font = (comboBox.getSelectedItem() != null ? (Font) comboBox.getSelectedItem() : (Font) comboBox.getItemAt(0));
175                    _util.setFont(font);
176                    break;
177                default:
178                    log.warn("Unexpected _which {}  in itemStateChanged", comboBox._which);
179                    break;
180            }
181            _callBack.actionPerformed(null);
182        }
183    }
184
185    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FontPanel.class);
186}