001package apps.startup;
002
003import java.awt.Component;
004import java.io.File;
005import javax.swing.JFileChooser;
006
007import jmri.InstanceManager;
008import jmri.script.swing.ScriptFileChooser;
009import jmri.util.startup.StartupModelFactory;
010import jmri.util.startup.StartupModel;
011import jmri.util.startup.StartupActionsManager;
012import jmri.util.swing.JmriJOptionPane;
013
014import org.openide.util.lookup.ServiceProvider;
015
016/**
017 *
018 * @author Randall Wood 2016
019 */
020@ServiceProvider(service = StartupModelFactory.class)
021public class ScriptButtonModelFactory implements StartupModelFactory {
022
023    private ScriptFileChooser chooser = null;
024
025    public ScriptButtonModelFactory() {
026    }
027
028    @Override
029    public Class<? extends StartupModel> getModelClass() {
030        return ScriptButtonModel.class;
031    }
032
033    @Override
034    public ScriptButtonModel newModel() {
035        return new ScriptButtonModel();
036    }
037
038    @Override
039    public String getDescription() {
040        return Bundle.getMessage(this.getModelClass().getCanonicalName());
041    }
042
043    @Override
044    public String getActionText() {
045        return Bundle.getMessage("EditableStartupAction", this.getDescription()); // NOI18N
046    }
047
048    @Override
049    public void editModel(StartupModel model, Component parent) {
050        if (this.chooser == null) {
051            this.chooser = new ScriptFileChooser();
052            this.chooser.setDialogTitle(this.getDescription());
053            this.chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
054        }
055        if (ScriptButtonModel.class.isInstance(model)) {
056            ScriptButtonPanel buttonPanel = new ScriptButtonPanel(this.chooser, parent);
057            if (((ScriptButtonModel) model).getScript() != null) {
058                buttonPanel.setScript(((ScriptButtonModel) model).getScript().getPath());
059            } else {
060                buttonPanel.setScript(""); // NOI18N
061            }
062            buttonPanel.setButtonName(model.getName());
063            JmriJOptionPane.showMessageDialog(parent,
064                    buttonPanel,
065                    this.getDescription(),
066                    JmriJOptionPane.PLAIN_MESSAGE);
067            if (!buttonPanel.getButtonName().isEmpty()) {
068                model.setName(buttonPanel.getButtonName());
069                ((ScriptButtonModel) model).setScript(new File(buttonPanel.getScript()));
070                InstanceManager.getDefault(StartupActionsManager.class).setRestartRequired();
071            }
072        }
073    }
074
075    @Override
076    public void initialize() {
077        // nothing to do
078    }
079
080}