001package apps.swing;
002
003import java.awt.*;
004import java.util.Locale;
005
006import javax.swing.*;
007
008import jmri.*;
009import jmri.jmrix.ConnectionConfig;
010import jmri.jmrix.ConnectionConfigManager;
011import jmri.swing.ConnectionLabel;
012import jmri.util.FileUtil;
013import jmri.util.swing.JmriJOptionPane;
014
015
016/**
017 * About dialog.
018 *
019 * @author Randall Wood Copyright (C) 2012
020 */
021public final class AboutDialog {
022
023    private final JFrame frame;
024    private final boolean modal;
025
026    // this should probably be changed to a JmriAbstractAction.
027    public AboutDialog(JFrame jframe, boolean isModal) {
028        frame = jframe;
029        modal = isModal;
030        
031    }
032
033    public void setVisible(boolean visible) {
034        if ( !visible ) {
035            return;
036        }
037        log.debug("Start UI");
038        if (modal) {
039            JmriJOptionPane.showMessageDialog(frame, getMainPanel(),
040                Bundle.getMessage("TitleAbout", Application.getApplicationName()),
041                JmriJOptionPane.PLAIN_MESSAGE);
042        } else {
043            JmriJOptionPane.showMessageDialogNonModal(frame, getMainPanel(),
044                Bundle.getMessage("TitleAbout", Application.getApplicationName()),
045                JmriJOptionPane.PLAIN_MESSAGE, null);
046        }
047    }
048
049    private JPanel getMainPanel(){
050        JPanel pane = new JPanel();
051        pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
052        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
053        pane.add(namePane());
054        pane.add(infoPane());
055        return pane;
056    }
057
058    protected JPanel namePane() {
059        String logo = Application.getLogo();
060        JPanel pane = new JPanel();
061        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
062        if (log.isDebugEnabled()) {
063            log.debug("Fetch main logo: {} ({})", logo, FileUtil.findURL(logo, FileUtil.Location.INSTALLED));
064        }
065        addCenteredComponent(new JLabel(new ImageIcon(
066            pane.getToolkit().getImage(FileUtil.findURL(logo, FileUtil.Location.INSTALLED)),
067            "JMRI logo"), SwingConstants.CENTER), pane);
068        pane.add(Box.createRigidArea(new Dimension(0, 15)));
069        String name = Application.getApplicationName();
070        name = checkRegisteredTmInString(name);
071        JLabel appName = new JLabel(name, SwingConstants.CENTER);
072        appName.setFont(pane.getFont().deriveFont(Font.BOLD, pane.getFont().getSize() * 1.2f));
073        addCenteredComponent(appName, pane);
074        addCenteredComponent(new JLabel(Application.getURL(), SwingConstants.CENTER), pane);
075        pane.add(Box.createRigidArea(new Dimension(0, 15)));
076        pane.setAlignmentX(Component.CENTER_ALIGNMENT);
077        return pane;
078    }
079
080    private static final String REGISTERED_TM_SYMBOL = "\u00ae";
081
082    protected static String checkRegisteredTmInString(String name) {
083        if ( !name.contains(REGISTERED_TM_SYMBOL) ) {
084            return name + " " + REGISTERED_TM_SYMBOL;
085        }
086        return name;
087    }
088
089    protected JPanel infoPane() {
090        JPanel pane1 = new JPanel();
091        pane1.setLayout(new BoxLayout(pane1, BoxLayout.Y_AXIS));
092
093        log.debug("start labels");
094
095        // add listener for Com port updates
096        for (ConnectionConfig conn : InstanceManager.getDefault(ConnectionConfigManager.class)) {
097            if (!conn.getDisabled()) {
098                pane1.add(new ConnectionLabel(conn));
099            }
100        }
101        pane1.add(Box.createRigidArea(new Dimension(0, 15)));
102
103        pane1.add(new JLabel(Bundle.getMessage("DefaultVersionCredit", Version.name())));
104        pane1.add(new JLabel(Version.getCopyright()));
105        pane1.add(new JLabel(Bundle.getMessage("JavaVersionCredit",
106                System.getProperty("java.version", "<unknown>"),
107                Locale.getDefault().toString())));
108        pane1.setAlignmentX(Component.CENTER_ALIGNMENT);
109        return pane1;
110    }
111
112    protected static void addCenteredComponent(JComponent c, JPanel p) {
113        c.setAlignmentX(Component.CENTER_ALIGNMENT); // doesn't work
114        p.add(c);
115    }
116
117    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AboutDialog.class);
118}