001package apps;
002
003import apps.SystemConsole.Scheme;
004import apps.systemconsole.SystemConsolePreferencesManager;
005import java.awt.FlowLayout;
006import java.awt.Font;
007import java.awt.event.ActionEvent;
008import java.util.ResourceBundle;
009import javax.swing.BoxLayout;
010import javax.swing.JComboBox;
011import javax.swing.JComponent;
012import javax.swing.JLabel;
013import javax.swing.JList;
014import javax.swing.JPanel;
015import javax.swing.JToggleButton;
016import javax.swing.UIManager;
017import jmri.InstanceManager;
018import jmri.swing.PreferencesPanel;
019import jmri.util.swing.JComboBoxUtil;
020import org.openide.util.lookup.ServiceProvider;
021
022/**
023 * Allow certain elements of the System Console to be configured.
024 * <hr>
025 * This file is part of JMRI.
026 * <p>
027 * JMRI is free software; you can redistribute it and/or modify it under the
028 * terms of version 2 of the GNU General Public License as published by the Free
029 * Software Foundation. See the "COPYING" file for a copy of this license.
030 * <p>
031 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
032 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
033 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
034 *
035 * @author Matthew Harris copyright (c) 2010, 2011
036 */
037@ServiceProvider(service = PreferencesPanel.class)
038public class SystemConsoleConfigPanel extends JPanel implements PreferencesPanel {
039
040    private static final ResourceBundle rbc = ResourceBundle.getBundle("apps.AppsConfigBundle");
041
042    private static final Integer fontSizes[] = {
043        8,
044        9,
045        10,
046        11,
047        12,
048        13,
049        14,
050        16,
051        18,
052        20,
053        24};
054
055    private static final Integer wrapStyles[] = {
056        SystemConsole.WRAP_STYLE_NONE,
057        SystemConsole.WRAP_STYLE_LINE,
058        SystemConsole.WRAP_STYLE_WORD};
059
060    private static final String wrapStyleNames[] = {
061        rbc.getString("ConsoleWrapStyleNone"),
062        rbc.getString("ConsoleWrapStyleLine"),
063        rbc.getString("ConsoleWrapStyleWord")};
064
065    private static final JToggleButton fontStyleBold = new JToggleButton("B");
066
067    private static final JToggleButton fontStyleItalic = new JToggleButton("I");
068
069    private static final JComboBox<Scheme> schemes = new JComboBox<>(SystemConsole.getInstance().getSchemes());
070
071    private static final JComboBox<Integer> fontSize = new JComboBox<>(fontSizes);
072
073    public SystemConsoleConfigPanel() {
074
075        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
076
077        JPanel p = new JPanel(new FlowLayout());
078        p.add(new JLabel(rbc.getString("ConsoleScheme")));
079
080        schemes.setSelectedIndex(this.getPreferencesManager().getScheme());
081        schemes.addActionListener((ActionEvent e) -> {
082            this.getPreferencesManager().setScheme(schemes.getSelectedIndex());
083        });
084
085        schemes.setRenderer((JList<? extends Scheme> list, Scheme scheme, int index, boolean isSelected, boolean hasFocus) -> {
086            JPanel p1 = new JPanel();
087            p1.setOpaque(index > -1);
088            if (isSelected && index > -1) {
089                p1.setBackground(list.getSelectionBackground());
090                p1.setForeground(list.getSelectionForeground());
091            } else {
092                p1.setBackground(list.getBackground());
093                p1.setForeground(list.getForeground());
094            }
095            JLabel l = new JLabel(" " + scheme.description + " ");
096            l.setOpaque(true);
097            l.setFont(new Font("Monospaced", this.getPreferencesManager().getFontStyle(), 12));
098            l.setForeground(scheme.foreground);
099            l.setBackground(scheme.background);
100            p1.add(l);
101            // 'Oribble hack as CDE/Motif JComboBox doesn't seem to like
102            // displaying JPanel objects in the JComboBox header
103            if (UIManager.getLookAndFeel().getName().equals("CDE/Motif") && index == -1) {
104                return l;
105            }
106            return p1;
107        });
108
109        p.add(schemes);
110        add(p);
111        JComboBoxUtil.setupComboBoxMaxRows(schemes);
112
113        p = new JPanel(new FlowLayout());
114
115        fontSize.addActionListener((ActionEvent e) -> {
116            this.getPreferencesManager().setFontSize((int) fontSize.getSelectedItem());
117        });
118        fontSize.setToolTipText(rbc.getString("ConsoleFontSize"));
119        fontSize.setSelectedItem(this.getPreferencesManager().getFontSize());
120        JLabel fontSizeUoM = new JLabel(rbc.getString("ConsoleFontSizeUoM"));
121
122        p.add(fontSize);
123        p.add(fontSizeUoM);
124        JComboBoxUtil.setupComboBoxMaxRows(fontSize);
125
126        fontStyleBold.setFont(fontStyleBold.getFont().deriveFont(Font.BOLD));
127        fontStyleBold.addActionListener((ActionEvent e) -> {
128            doFontStyle();
129        });
130        fontStyleBold.setToolTipText(rbc.getString("ConsoleFontStyleBold"));
131        fontStyleBold.setSelected((this.getPreferencesManager().getFontStyle() & Font.BOLD) == Font.BOLD);
132        p.add(fontStyleBold);
133
134        fontStyleItalic.setFont(fontStyleItalic.getFont().deriveFont(Font.ITALIC));
135        fontStyleItalic.addActionListener((ActionEvent e) -> {
136            doFontStyle();
137        });
138        fontStyleItalic.setSelected((this.getPreferencesManager().getFontStyle() & Font.ITALIC) == Font.ITALIC);
139        fontStyleItalic.setToolTipText(rbc.getString("ConsoleFontStyleItalic"));
140        p.add(fontStyleItalic);
141
142        add(p);
143
144        p = new JPanel(new FlowLayout());
145        final JComboBox<String> wrapStyle = new JComboBox<>(wrapStyleNames);
146        wrapStyle.addActionListener((ActionEvent e) -> {
147            this.getPreferencesManager().setWrapStyle(wrapStyles[wrapStyle.getSelectedIndex()]);
148        });
149        wrapStyle.setSelectedIndex(this.getPreferencesManager().getWrapStyle());
150
151        p.add(new JLabel(rbc.getString("ConsoleWrapStyle")));
152        p.add(wrapStyle);
153        add(p);
154
155        p = new JPanel();
156        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
157        add(p);
158
159    }
160
161    private void doFontStyle() {
162        this.getPreferencesManager().setFontStyle((fontStyleBold.isSelected() ? Font.BOLD : Font.PLAIN)
163                | (fontStyleItalic.isSelected() ? Font.ITALIC : Font.PLAIN));
164        schemes.repaint();
165    }
166
167    @Override
168    public String getPreferencesItem() {
169        return "DISPLAY"; // NOI18N
170    }
171
172    @Override
173    public String getPreferencesItemText() {
174        return rbc.getString("MenuDisplay"); // NOI18N
175    }
176
177    @Override
178    public String getTabbedPreferencesTitle() {
179        return rbc.getString("TabbedLayoutConsole"); // NOI18N
180    }
181
182    @Override
183    public String getLabelKey() {
184        return rbc.getString("LabelTabbedLayoutConsole"); // NOI18N
185    }
186
187    @Override
188    public JComponent getPreferencesComponent() {
189        return this;
190    }
191
192    @Override
193    public boolean isPersistant() {
194        return true;
195    }
196
197    @Override
198    public String getPreferencesTooltip() {
199        return null;
200    }
201
202    @Override
203    public void savePreferences() {
204        // do nothing - the persistant manager will take care of this
205    }
206
207    @Override
208    public boolean isDirty() {
209        // console preferences take effect immediately, but are not saved
210        // immediately, so we can't tell without rereading the preferences.xml,
211        // but it's too expensive to read that file to determine if it matches
212        // the in memory preferences for this console, so simply return false
213        return false;
214    }
215
216    @Override
217    public boolean isRestartRequired() {
218        // since changes are applied immediately, this is not required
219        return false;
220    }
221
222    @Override
223    public boolean isPreferencesValid() {
224        return true; // no validity checking performed
225    }
226
227    private SystemConsolePreferencesManager getPreferencesManager() {
228        return InstanceManager.getDefault(SystemConsolePreferencesManager.class);
229    }
230}