001package apps.startup;
002
003import jmri.util.startup.StartupModelFactory;
004import jmri.util.startup.StartupModel;
005import jmri.util.startup.StartupActionsManager;
006import java.awt.Component;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009import javax.swing.JSpinner;
010import javax.swing.SpinnerNumberModel;
011
012import jmri.InstanceManager;
013import jmri.util.swing.JmriJOptionPane;
014
015import org.openide.util.lookup.ServiceProvider;
016
017/**
018 * Factory for {@link apps.startup.StartupPauseModel} objects.
019 *
020 * @author Randall Wood (c) 2016
021 */
022@ServiceProvider(service = StartupModelFactory.class)
023public class StartupPauseFactory implements StartupModelFactory {
024
025    @Override
026    public Class<? extends StartupModel> getModelClass() {
027        return StartupPauseModel.class;
028    }
029
030    @Override
031    public StartupModel newModel() {
032        return new StartupPauseModel();
033    }
034
035    @Override
036    public String getDescription() {
037        return Bundle.getMessage(this.getModelClass().getCanonicalName());
038    }
039
040    @Override
041    public String getActionText() {
042        return Bundle.getMessage("EditableStartupAction", this.getDescription()); // NOI18N
043    }
044
045    @Override
046    public void editModel(StartupModel model, Component parent) {
047        if (model instanceof StartupPauseModel && this.getModelClass().isInstance(model)) {
048            int delay = ((StartupPauseModel) model).getDelay();
049            SpinnerNumberModel snm = new SpinnerNumberModel(
050                    delay >= 0 ? delay : StartupPauseModel.DEFAULT_DELAY,
051                    0,
052                    600,
053                    1);
054            JSpinner spinner = new JSpinner(snm);
055            int result = JmriJOptionPane.showConfirmDialog(parent,
056                    this.getDialogMessage(spinner),
057                    this.getDescription(),
058                    JmriJOptionPane.OK_CANCEL_OPTION,
059                    JmriJOptionPane.PLAIN_MESSAGE);
060            if (result == JmriJOptionPane.OK_OPTION && delay != snm.getNumber().intValue()) {
061                ((StartupPauseModel) model).setDelay(snm.getNumber().intValue());
062                InstanceManager.getDefault(StartupActionsManager.class).setRestartRequired();
063            }
064        }
065    }
066
067    @Override
068    public void initialize() {
069        // nothing to do
070    }
071
072    private JPanel getDialogMessage(JSpinner spinner) {
073        JPanel panel = new JPanel();
074        panel.add(new JLabel(Bundle.getMessage("StartupPauseModelFactory.editModel.messagePrefix"))); // NOI18N
075        panel.add(spinner);
076        panel.add(new JLabel(Bundle.getMessage("StartupPauseModelFactory.editModel.messagePostfix"))); // NOI18N
077        return panel;
078    }
079
080}