001package jmri.jmrit.operations.rollingstock.cars.tools;
002
003import javax.swing.JMenu;
004import javax.swing.JMenuBar;
005
006import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
007import jmri.InstanceManager;
008import jmri.jmrit.operations.locations.tools.LocationsByCarTypeFrame;
009import jmri.jmrit.operations.rollingstock.RollingStock;
010import jmri.jmrit.operations.rollingstock.RollingStockAttributeEditFrame;
011import jmri.jmrit.operations.rollingstock.cars.*;
012import jmri.jmrit.operations.rollingstock.engines.EngineManager;
013import jmri.jmrit.operations.setup.Control;
014import jmri.jmrit.operations.trains.tools.TrainsByCarTypeFrame;
015import jmri.util.swing.JmriJOptionPane;
016
017/**
018 * Frame for editing a car attribute.
019 *
020 * @author Daniel Boudreau Copyright (C) 2008, 2014, 2020
021 */
022public class CarAttributeEditFrame extends RollingStockAttributeEditFrame {
023
024    CarManager carManager = InstanceManager.getDefault(CarManager.class);
025
026    // incremental attributes for this frame
027    public static final String COLOR = "Color";
028    public static final String KERNEL = "Kernel";
029
030    public CarAttributeEditFrame(){
031    }
032
033    /**
034     * 
035     * @param attribute One of the six possible attributes for a car.
036     */
037    public void initComponents(String attribute) {
038        initComponents(attribute, NONE);
039    }
040
041    /**
042     * 
043     * @param attribute One of the six possible attributes for a car.
044     * @param name      The name of the attribute to edit.
045     */
046    @Override
047    public void initComponents(String attribute, String name) {
048        super.initComponents(attribute, name);
049
050        setTitle(Bundle.getMessage("TitleCarEditAtrribute", attribute));
051        carManager.addPropertyChangeListener(this);
052
053        // build menu
054        JMenuBar menuBar = new JMenuBar();
055        JMenu toolMenu = new JMenu(Bundle.getMessage("MenuTools"));
056        toolMenu.add(new CarAttributeAction(this));
057        toolMenu.add(new CarDeleteAttributeAction(this));
058        menuBar.add(toolMenu);
059        setJMenuBar(menuBar);
060        // add help menu to window
061        addHelpMenu("package.jmri.jmrit.operations.Operations_EditCarAttributes", true); // NOI18N
062    }
063
064    @Override
065    protected void deleteAttributeName(String deleteItem) {
066        super.deleteAttributeName(deleteItem);
067        if (_attribute.equals(TYPE)) {
068            InstanceManager.getDefault(CarTypes.class).deleteName(deleteItem);
069        }
070        if (_attribute.equals(COLOR)) {
071            InstanceManager.getDefault(CarColors.class).deleteName(deleteItem);
072        }
073        if (_attribute.equals(LENGTH)) {
074            InstanceManager.getDefault(CarLengths.class).deleteName(deleteItem);
075        }
076        if (_attribute.equals(KERNEL)) {
077            InstanceManager.getDefault(KernelManager.class).deleteKernel(deleteItem);
078        }
079    }
080
081    @Override
082    @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "GUI ease of use")
083    protected void addAttributeName(String addItem) {
084        super.addAttributeName(addItem);
085        if (_attribute.equals(TYPE)) {
086            InstanceManager.getDefault(CarTypes.class).addName(addItem);
087            if (showDialogBox) {
088                int results = JmriJOptionPane.showOptionDialog(this, Bundle.getMessage("AddNewCarType"),
089                        Bundle.getMessage("ModifyLocations"), JmriJOptionPane.DEFAULT_OPTION,
090                        JmriJOptionPane.QUESTION_MESSAGE, null,
091                        new Object[] { Bundle.getMessage("ButtonYes"),
092                                Bundle.getMessage("ButtonNo"), Bundle.getMessage("ButtonDontShow") },
093                        Bundle.getMessage("ButtonNo"));
094                if (results == 0 ) { // array position 0, ButtonYes
095                    LocationsByCarTypeFrame lf = new LocationsByCarTypeFrame();
096                    lf.initComponents(addItem);
097                }
098                if (results == 2 ) { // array position 2, ButtonDontShow
099                    showDialogBox = false;
100                }
101                results = JmriJOptionPane.showOptionDialog(this, Bundle.getMessage("AddNewCarType"),
102                        Bundle.getMessage("ModifyTrains"), JmriJOptionPane.DEFAULT_OPTION,
103                        JmriJOptionPane.QUESTION_MESSAGE, null, new Object[] { Bundle.getMessage("ButtonYes"),
104                                Bundle.getMessage("ButtonNo"), Bundle.getMessage("ButtonDontShow") },
105                        Bundle.getMessage("ButtonNo"));
106                if (results == 0 ) { // array position 0, ButtonYes
107                    TrainsByCarTypeFrame lf = new TrainsByCarTypeFrame();
108                    lf.initComponents(addItem);
109                }
110                if (results == 2 ) { // array position 2, ButtonDontShow
111                    showDialogBox = false;
112                }
113            }
114        }
115        if (_attribute.equals(COLOR)) {
116            InstanceManager.getDefault(CarColors.class).addName(addItem);
117        }
118        if (_attribute.equals(LENGTH)) {
119            InstanceManager.getDefault(CarLengths.class).addName(addItem);
120            comboBox.setSelectedItem(addItem);
121        }
122        if (_attribute.equals(KERNEL)) {
123            InstanceManager.getDefault(KernelManager.class).newKernel(addItem);
124        }
125        if (_attribute.equals(OWNER)) {
126            InstanceManager.getDefault(CarOwners.class).addName(addItem);
127        }
128    }
129
130    @Override
131    protected void replaceItem(String oldItem, String newItem) {
132        super.replaceItem(oldItem, newItem);
133        // replace kernel
134        if (_attribute.equals(KERNEL)) {
135            InstanceManager.getDefault(KernelManager.class).replaceKernelName(oldItem, newItem);
136        }
137        // now adjust cars, locations and trains
138        if (_attribute.equals(TYPE)) {
139            InstanceManager.getDefault(CarTypes.class).replaceName(oldItem, newItem);
140            InstanceManager.getDefault(CarLoads.class).replaceType(oldItem, newItem);
141        }
142        if (_attribute.equals(LENGTH)) {
143            InstanceManager.getDefault(CarLengths.class).replaceName(oldItem, newItem);
144        }
145        if (_attribute.equals(COLOR)) {
146            InstanceManager.getDefault(CarColors.class).replaceName(oldItem, newItem);
147        }
148    }
149
150    @Override
151    protected void loadCombobox() {
152        super.loadCombobox();
153        if (_attribute.equals(TYPE)) {
154            comboBox = InstanceManager.getDefault(CarTypes.class).getComboBox();
155            InstanceManager.getDefault(CarTypes.class).addPropertyChangeListener(this);
156        }
157        if (_attribute.equals(COLOR)) {
158            comboBox = InstanceManager.getDefault(CarColors.class).getComboBox();
159            InstanceManager.getDefault(CarColors.class).addPropertyChangeListener(this);
160        }
161        if (_attribute.equals(LENGTH)) {
162            comboBox = InstanceManager.getDefault(CarLengths.class).getComboBox();
163            InstanceManager.getDefault(CarLengths.class).addPropertyChangeListener(this);
164        }
165        if (_attribute.equals(KERNEL)) {
166            comboBox = InstanceManager.getDefault(KernelManager.class).getComboBox();
167            InstanceManager.getDefault(KernelManager.class).addPropertyChangeListener(this);
168        }
169    }
170
171    @Override
172    protected void updateAttributeQuanity() {
173        if (!showQuanity) {
174            return;
175        }
176        int number = 0;
177        String item = (String) comboBox.getSelectedItem();
178        log.debug("Selected item {}", item);
179        for (Car car : carManager.getList()) {
180            if (_attribute.equals(ROAD)) {
181                if (car.getRoadName().equals(item)) {
182                    number++;
183                }
184            }
185            if (_attribute.equals(TYPE)) {
186                if (car.getTypeName().equals(item)) {
187                    number++;
188                }
189            }
190            if (_attribute.equals(COLOR)) {
191                if (car.getColor().equals(item)) {
192                    number++;
193                }
194            }
195            if (_attribute.equals(LENGTH)) {
196                if (car.getLength().equals(item)) {
197                    number++;
198                }
199            }
200            if (_attribute.equals(OWNER)) {
201                if (car.getOwnerName().equals(item)) {
202                    number++;
203                }
204            }
205            if (_attribute.equals(KERNEL)) {
206                if (car.getKernelName().equals(item)) {
207                    number++;
208                }
209            }
210        }
211        quanity.setText(Integer.toString(number));
212        // Tool to delete all attributes that haven't been assigned to a car
213        if (number == 0 && deleteUnused) {
214            // need to check if an engine is using the road name
215            if (_attribute.equals(ROAD)) {
216                for (RollingStock rs : InstanceManager.getDefault(EngineManager.class).getList()) {
217                    if (rs.getRoadName().equals(item)) {
218                        log.info("Engine ({} {}) is assigned road name ({})", rs.getRoadName(), rs.getNumber(), item); // NOI18N
219                        return;
220                    }
221                }
222            }
223            // need to check if an engine is using the road name
224            if (_attribute.equals(OWNER)) {
225                for (RollingStock rs : InstanceManager.getDefault(EngineManager.class).getList()) {
226                    if (rs.getOwnerName().equals(item)) {
227                        log.info("Engine ({} {}) is assigned owner name ({})", rs.getRoadName(), rs.getNumber(), item); // NOI18N
228                        return;
229                    }
230                }
231            }
232            // confirm that attribute is to be deleted
233            confirmDelete(item);
234        }
235    }
236
237
238    @Override
239    public void dispose() {
240        InstanceManager.getDefault(CarTypes.class).removePropertyChangeListener(this);
241        InstanceManager.getDefault(CarColors.class).removePropertyChangeListener(this);
242        InstanceManager.getDefault(CarLengths.class).removePropertyChangeListener(this);
243        InstanceManager.getDefault(KernelManager.class).removePropertyChangeListener(this);
244        carManager.removePropertyChangeListener(this);
245        super.dispose();
246    }
247
248    @Override
249    public void propertyChange(java.beans.PropertyChangeEvent e) {
250        if (Control.SHOW_PROPERTY) {
251            log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(),
252                    e.getNewValue());
253        }
254        if (e.getPropertyName().equals(CarTypes.CARTYPES_CHANGED_PROPERTY)) {
255            InstanceManager.getDefault(CarTypes.class).updateComboBox(comboBox);
256        }
257        if (e.getPropertyName().equals(CarColors.CARCOLORS_CHANGED_PROPERTY)) {
258            InstanceManager.getDefault(CarColors.class).updateComboBox(comboBox);
259        }
260        if (e.getPropertyName().equals(CarLengths.CARLENGTHS_CHANGED_PROPERTY)) {
261            InstanceManager.getDefault(CarLengths.class).updateComboBox(comboBox);
262        }
263        if (e.getPropertyName().equals(KernelManager.LISTLENGTH_CHANGED_PROPERTY)) {
264            InstanceManager.getDefault(KernelManager.class).updateComboBox(comboBox);
265        }
266        if (e.getPropertyName().equals(CarManager.LISTLENGTH_CHANGED_PROPERTY)) {
267            updateAttributeQuanity();
268        }
269        super.propertyChange(e);
270    }
271
272    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CarAttributeEditFrame.class);
273}