001package jmri.jmrit.operations.rollingstock.engines.tools;
002
003import java.util.List;
004import java.util.ResourceBundle;
005
006import javax.swing.JLabel;
007import javax.swing.JPanel;
008
009import jmri.InstanceManager;
010import jmri.jmrit.operations.rollingstock.engines.Engine;
011import jmri.jmrit.operations.rollingstock.engines.EngineManager;
012import jmri.jmrit.operations.setup.Control;
013import jmri.jmrit.roster.Roster;
014import jmri.jmrit.roster.RosterEntry;
015import jmri.jmrit.roster.swing.RosterGroupComboBox;
016import jmri.util.JmriJFrame;
017import jmri.util.swing.JmriJOptionPane;
018
019/**
020 * Import engines from the jmri Roster
021 *
022 * @author Daniel Boudreau Copyright (C) 2008
023 *
024 */
025public class ImportRosterEngines extends Thread {
026
027    EngineManager manager = InstanceManager.getDefault(EngineManager.class);
028    private static String defaultEngineLength = Bundle.getMessage("engineDefaultLength");
029    private static String defaultEngineType = Bundle.getMessage("engineDefaultType");
030    private static String defaultEngineHp = Bundle.getMessage("engineDefaultHp");
031
032    JLabel textEngine = new JLabel(Bundle.getMessage("AddEngine"));
033    JLabel textId = new JLabel();
034
035    // we use a thread so the status frame will update!
036    @Override
037    public void run() {
038
039        // create a status frame
040        JmriJFrame fstatus = new JmriJFrame(Bundle.getMessage("TitleImportEngines"));
041        fstatus.setSize(Control.panelWidth500, Control.panelHeight100);
042        JPanel ps = new JPanel();
043        ps.add(textEngine);
044        ps.add(textId);
045        fstatus.getContentPane().add(ps);
046        fstatus.setVisible(true);
047
048        // create dialog with roster group comboBox
049        RosterGroupComboBox comboBox = new RosterGroupComboBox();
050        ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrit.roster.JmritRosterBundle");
051        JmriJOptionPane.showMessageDialog(null, comboBox, rb.getString("SelectRosterGroup"), JmriJOptionPane.QUESTION_MESSAGE);
052        String groupName = comboBox.getSelectedItem();
053        log.debug("User selected roster group: {}", groupName);
054
055        // Now get engines from the JMRI roster
056        int enginesAdded = 0;
057
058        List<RosterEntry> engines = Roster.getDefault().getEntriesMatchingCriteria(null, null, null, null, null, null,
059                null, groupName, null, null, null);
060
061        for (RosterEntry re : engines) {
062            // add engines that have a road name and number
063            if (re.getRoadName().isEmpty() || re.getRoadNumber().isEmpty()) {
064                log.error("Roster Id: {} doesn't have a road name and road number", re.getId());
065                continue;
066            }
067            String road = re.getRoadName().trim();
068            if (road.length() > Control.max_len_string_attibute) {
069                road = road.substring(0, Control.max_len_string_attibute);
070            }
071            String number = re.getRoadNumber().trim();
072            if (number.length() > Control.max_len_string_road_number) {
073                number = number.substring(0, Control.max_len_string_road_number);
074            }
075            textId.setText(road + " " + number);
076            Engine engine = manager.getByRoadAndNumber(road, number);
077            if (engine == null) {
078                engine = manager.newRS(road, number);
079                String model = re.getModel().trim();
080                if (model.length() > Control.max_len_string_attibute) {
081                    model = model.substring(0, Control.max_len_string_attibute);
082                }
083                if (model.isEmpty()) {
084                    log.warn("Roster Id: {} hasn't been assigned a model name", re.getId());
085                }
086                engine.setModel(model);
087                // does this model already have a length?
088                if (engine.getLength().equals(Engine.NONE)) {
089                    engine.setLength(defaultEngineLength);
090                }
091                // does this model already have a type?
092                if (engine.getTypeName().equals(Engine.NONE)) {
093                    engine.setTypeName(defaultEngineType);
094                }
095                // does this model already have a hp?
096                if (engine.getHp().equals(Engine.NONE)) {
097                    engine.setHp(defaultEngineHp);
098                }
099                String owner = re.getOwner().trim();
100                if (owner.length() > Control.max_len_string_attibute) {
101                    owner = owner.substring(0, Control.max_len_string_attibute);
102                }
103                engine.setOwnerName(owner);
104                enginesAdded++;
105            } else {
106                log.info("Can not add roster Id: {}, engine road name ({}) road number ({}) already exists", re.getId(),
107                        re.getRoadName(), re.getRoadNumber());
108            }
109        }
110
111        // kill status panel
112        fstatus.dispose();
113
114        if (enginesAdded > 0) {
115            JmriJOptionPane.showMessageDialog(null,
116                    Bundle.getMessage("ImportEnginesAdded", enginesAdded),
117                    Bundle.getMessage("SuccessfulImport"), JmriJOptionPane.INFORMATION_MESSAGE);
118        } else {
119            JmriJOptionPane.showMessageDialog(null,
120                    Bundle.getMessage("ImportEnginesAdded", enginesAdded),
121                    Bundle.getMessage("ImportFailed"), JmriJOptionPane.ERROR_MESSAGE);
122        }
123    }
124
125    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ImportRosterEngines.class);
126}