001package jmri.jmrit.operations.rollingstock.engines.tools;
002
003import java.util.List;
004
005import javax.swing.JMenu;
006import javax.swing.JMenuBar;
007
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011import jmri.InstanceManager;
012import jmri.jmrit.operations.rollingstock.RollingStock;
013import jmri.jmrit.operations.rollingstock.RollingStockAttributeEditFrame;
014import jmri.jmrit.operations.rollingstock.cars.CarManager;
015import jmri.jmrit.operations.rollingstock.engines.*;
016import jmri.jmrit.operations.setup.Control;
017
018/**
019 * Frame for editing an engine attribute.
020 *
021 * @author Daniel Boudreau Copyright (C) 2008
022 */
023public class EngineAttributeEditFrame extends RollingStockAttributeEditFrame {
024
025    EngineManager engineManager = InstanceManager.getDefault(EngineManager.class);
026
027    // incremental attributes for this frame
028    public static final String MODEL = "Model";
029    public static final String CONSIST = "Consist";
030
031    public EngineAttributeEditFrame(){
032    }
033
034    public void initComponents(String attribute) {
035        initComponents(attribute, NONE);
036    }
037
038    /**
039     * 
040     * @param attribute One of the seven possible attributes for an engine.
041     * @param name      The name of the attribute to edit.
042     */
043    @Override
044    public void initComponents(String attribute, String name) {
045        super.initComponents(attribute, name);
046
047        setTitle(Bundle.getMessage("TitleEngineEditAtrribute", attribute));
048
049        // build menu
050        JMenuBar menuBar = new JMenuBar();
051        JMenu toolMenu = new JMenu(Bundle.getMessage("MenuTools"));
052        toolMenu.add(new EngineAttributeAction(this));
053        toolMenu.add(new EngineDeleteAttributeAction(this));
054        menuBar.add(toolMenu);
055        setJMenuBar(menuBar);
056        // add help menu to window
057        addHelpMenu("package.jmri.jmrit.operations.Operations_Locomotives", true); // NOI18N
058    }
059
060    @Override
061    protected void deleteAttributeName(String deleteItem) {
062        super.deleteAttributeName(deleteItem);
063        if (_attribute.equals(MODEL)) {
064            InstanceManager.getDefault(EngineModels.class).deleteName(deleteItem);
065        }
066        if (_attribute.equals(TYPE)) {
067            InstanceManager.getDefault(EngineTypes.class).deleteName(deleteItem);
068        }
069        if (_attribute.equals(LENGTH)) {
070            InstanceManager.getDefault(EngineLengths.class).deleteName(deleteItem);
071        }
072        if (_attribute.equals(CONSIST)) {
073            InstanceManager.getDefault(ConsistManager.class).deleteConsist(deleteItem);
074        }
075    }
076
077    @Override
078    protected void addAttributeName(String addItem) {
079        super.addAttributeName(addItem);
080        if (_attribute.equals(MODEL)) {
081            InstanceManager.getDefault(EngineModels.class).addName(addItem);
082        }
083        if (_attribute.equals(TYPE)) {
084            InstanceManager.getDefault(EngineTypes.class).addName(addItem);
085        }
086        if (_attribute.equals(LENGTH)) {
087            InstanceManager.getDefault(EngineLengths.class).addName(addItem);
088            comboBox.setSelectedItem(addItem);
089        }
090        if (_attribute.equals(CONSIST)) {
091            InstanceManager.getDefault(ConsistManager.class).newConsist(addItem);
092        }
093    }
094
095    @Override
096    protected void replaceItem(String oldItem, String newItem) {
097        super.replaceItem(oldItem, newItem);
098        if (_attribute.equals(MODEL)) {
099            List<Engine> engines = engineManager.getList();
100            for (Engine engine : engines) {
101                // we need to copy the old model attributes, so find an engine.
102                if (engine.getModel().equals(oldItem)) {
103                    // Has this model been configured?
104                    if (InstanceManager.getDefault(EngineModels.class).getModelLength(newItem) != null) {
105                        engine.setModel(newItem);
106                    } else {
107                        // get the old configuration for this model
108                        String length = engine.getLength();
109                        String hp = engine.getHp();
110                        String type = engine.getTypeName();
111                        // now update the new model
112                        engine.setModel(newItem);
113                        engine.setLength(length);
114                        engine.setHp(hp);
115                        engine.setTypeName(type);
116                    }
117                }
118            }
119            InstanceManager.getDefault(EngineModels.class).replaceName(oldItem, newItem);
120        }
121        if (_attribute.equals(CONSIST)) {
122            InstanceManager.getDefault(ConsistManager.class).replaceConsistName(oldItem, newItem);
123        }
124        if (_attribute.equals(TYPE)) {
125            InstanceManager.getDefault(EngineTypes.class).replaceName(oldItem, newItem);
126        }
127        if (_attribute.equals(LENGTH)) {
128            InstanceManager.getDefault(EngineLengths.class).replaceName(oldItem, newItem);
129        }
130    }
131
132    @Override
133    protected void loadCombobox() {
134        super.loadCombobox();
135        if (_attribute.equals(MODEL)) {
136            comboBox = InstanceManager.getDefault(EngineModels.class).getComboBox();
137            InstanceManager.getDefault(EngineModels.class).addPropertyChangeListener(this);
138        }
139        if (_attribute.equals(TYPE)) {
140            comboBox = InstanceManager.getDefault(EngineTypes.class).getComboBox();
141            InstanceManager.getDefault(EngineTypes.class).addPropertyChangeListener(this);
142        }
143        if (_attribute.equals(LENGTH)) {
144            comboBox = InstanceManager.getDefault(EngineLengths.class).getComboBox();
145            InstanceManager.getDefault(EngineLengths.class).addPropertyChangeListener(this);
146        }
147        if (_attribute.equals(CONSIST)) {
148            comboBox = InstanceManager.getDefault(ConsistManager.class).getComboBox();
149            InstanceManager.getDefault(ConsistManager.class).addPropertyChangeListener(this);
150        }
151    }
152    
153    @Override
154    protected void updateAttributeQuanity() {
155        if (!showQuanity) {
156            return;
157        }
158        int number = 0;
159        String item = (String) comboBox.getSelectedItem();
160        log.debug("Selected item {}", item);
161        for (Engine eng : engineManager.getList()) {
162            if (_attribute.equals(ROAD)) {
163                if (eng.getRoadName().equals(item)) {
164                    number++;
165                }
166            }
167            if (_attribute.equals(MODEL)) {
168                if (eng.getModel().equals(item)) {
169                    number++;
170                }
171            }
172            if (_attribute.equals(CONSIST)) {
173                if (eng.getConsistName().equals(item)) {
174                    number++;
175                }
176            }
177            if (_attribute.equals(TYPE)) {
178                if (eng.getTypeName().equals(item)) {
179                    number++;
180                }
181            }
182            if (_attribute.equals(LENGTH)) {
183                if (eng.getLength().equals(item)) {
184                    number++;
185                }
186            }
187            if (_attribute.equals(OWNER)) {
188                if (eng.getOwnerName().equals(item)) {
189                    number++;
190                }
191            }
192        }
193        quanity.setText(Integer.toString(number));
194        // Tool to delete all attributes that haven't been assigned to a car
195        if (number == 0 && deleteUnused) {
196            // need to check if a car is using the road name
197            if (_attribute.equals(ROAD)) {
198                for (RollingStock rs : InstanceManager.getDefault(CarManager.class).getList()) {
199                    if (rs.getRoadName().equals(item)) {
200                        log.info("Car ({} {}) is assigned road name ({})", rs.getRoadName(), rs.getNumber(), item); // NOI18N
201                        return;
202                    }
203                }
204            }
205            // need to check if a car is using the road name
206            if (_attribute.equals(OWNER)) {
207                for (RollingStock rs : InstanceManager.getDefault(CarManager.class).getList()) {
208                    if (rs.getOwnerName().equals(item)) {
209                        log.info("Car ({} {}) is assigned owner name ({})", rs.getRoadName(), rs.getNumber(), item); // NOI18N
210                        return;
211                    }
212                }
213            }
214            // confirm that attribute is to be deleted
215            confirmDelete(item);
216        }
217    }
218
219    @Override
220    public void dispose() {
221        InstanceManager.getDefault(EngineModels.class).removePropertyChangeListener(this);
222        InstanceManager.getDefault(EngineTypes.class).removePropertyChangeListener(this);
223        InstanceManager.getDefault(EngineLengths.class).removePropertyChangeListener(this);
224        InstanceManager.getDefault(ConsistManager.class).removePropertyChangeListener(this);
225        engineManager.removePropertyChangeListener(this);
226        super.dispose();
227    }
228
229    @Override
230    public void propertyChange(java.beans.PropertyChangeEvent e) {
231        if (Control.SHOW_PROPERTY) {
232            log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(),
233                    e.getNewValue());
234        }
235        if (e.getPropertyName().equals(EngineModels.ENGINEMODELS_CHANGED_PROPERTY)) {
236            InstanceManager.getDefault(EngineModels.class).updateComboBox(comboBox);
237        }
238        if (e.getPropertyName().equals(EngineTypes.ENGINETYPES_CHANGED_PROPERTY)) {
239            InstanceManager.getDefault(EngineTypes.class).updateComboBox(comboBox);
240        }
241        if (e.getPropertyName().equals(EngineLengths.ENGINELENGTHS_CHANGED_PROPERTY)) {
242            InstanceManager.getDefault(EngineLengths.class).updateComboBox(comboBox);
243        }
244        if (e.getPropertyName().equals(ConsistManager.LISTLENGTH_CHANGED_PROPERTY)) {
245            InstanceManager.getDefault(ConsistManager.class).updateComboBox(comboBox);
246        }
247        super.propertyChange(e);
248    }
249
250    private final static Logger log = LoggerFactory.getLogger(EngineAttributeEditFrame.class);
251}