001package apps.startup;
002
003import jmri.util.startup.StartupModelFactory;
004import jmri.util.startup.StartupModel;
005import jmri.util.startup.StartupActionsManager;
006import java.awt.Component;
007import java.util.ArrayList;
008import jmri.InstanceManager;
009import jmri.RouteManager;
010import jmri.util.swing.JmriJOptionPane;
011
012import org.openide.util.lookup.ServiceProvider;
013
014/**
015 * Factory to create {@link apps.startup.TriggerRouteModel} objects.
016 *
017 * @author Randall Wood (C) 2016
018 */
019@ServiceProvider(service = StartupModelFactory.class)
020public class TriggerRouteModelFactory implements StartupModelFactory {
021
022    @Override
023    public Class<? extends StartupModel> getModelClass() {
024        return TriggerRouteModel.class;
025    }
026
027    @Override
028    public String getDescription() {
029        return Bundle.getMessage(this.getModelClass().getCanonicalName());
030    }
031
032    @Override
033    public String getActionText() {
034        return Bundle.getMessage("EditableStartupAction", this.getDescription()); // NOI18N
035    }
036
037    @Override
038    public StartupModel newModel() {
039        return new TriggerRouteModel();
040    }
041
042    @Override
043    public void editModel(StartupModel model, Component parent) {
044        if (this.getModelClass().isInstance(model)) {
045            ArrayList<String> userNames = new ArrayList<>();
046            InstanceManager.getDefault(RouteManager.class).getNamedBeanSet().stream().forEach((r) -> {
047                String userName = r.getUserName();
048                if (userName != null && !userName.isEmpty()) {
049                    userNames.add(userName);
050                }
051            });
052            userNames.sort(null);
053            String name = (String) JmriJOptionPane.showInputDialog(parent,
054                    Bundle.getMessage("TriggerRouteModelFactory.editModel.message"), // NOI18N
055                    this.getDescription(),
056                    JmriJOptionPane.PLAIN_MESSAGE,
057                    null,
058                    userNames.toArray(new String[userNames.size()]),
059                    model.getName());
060            if (name != null && !name.equals(model.getName())) {
061                model.setName(name);
062                InstanceManager.getDefault(StartupActionsManager.class).setRestartRequired();
063            }
064        }
065    }
066
067    @Override
068    public void initialize() {
069        // nothing to do
070    }
071
072    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TriggerRouteModelFactory.class);
073
074}