001package jmri.jmrit.operations.trains.tools; 002 003import java.awt.Dimension; 004import java.awt.GridBagLayout; 005import java.util.List; 006 007import javax.swing.BorderFactory; 008import javax.swing.BoxLayout; 009import javax.swing.JPanel; 010 011import org.slf4j.Logger; 012import org.slf4j.LoggerFactory; 013 014import jmri.InstanceManager; 015import jmri.jmrit.operations.OperationsFrame; 016import jmri.jmrit.operations.routes.Route; 017import jmri.jmrit.operations.routes.RouteLocation; 018import jmri.jmrit.operations.routes.RouteManager; 019import jmri.jmrit.operations.setup.Control; 020import jmri.jmrit.operations.trains.Train; 021import jmri.jmrit.operations.trains.TrainManager; 022 023/** 024 * Change Departure Time frame for operations. 025 * 026 * @author Bob Jacobsen Copyright (C) 2001 027 * @author Daniel Boudreau Copyright (C) 2013 028 */ 029public class ChangeDepartureTimesFrame extends OperationsFrame { 030 031 // major buttons 032 javax.swing.JButton changeButton = new javax.swing.JButton(Bundle.getMessage("Change")); 033 034 // combo boxes 035 javax.swing.JComboBox<Integer> hourBox = new javax.swing.JComboBox<>(); 036 037 javax.swing.JCheckBox routesCheckBox = new javax.swing.JCheckBox(Bundle.getMessage("ModifyRouteTimes")); 038 039 public ChangeDepartureTimesFrame() { 040 // general GUI config 041 042 getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 043 044 // load hour combobox 045 for (int i = 1; i < 24; i++) { 046 hourBox.addItem(i); 047 } 048 049 // row 2 050 JPanel pHour = new JPanel(); 051 pHour.setLayout(new GridBagLayout()); 052 pHour.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectHours"))); 053 addItem(pHour, hourBox, 0, 0); 054 addItem(pHour, routesCheckBox, 1, 0); 055 056 // row 4 057 JPanel pButton = new JPanel(); 058 pButton.add(changeButton); 059 060 getContentPane().add(pHour); 061 getContentPane().add(pButton); 062 063 // add help menu to window 064 addHelpMenu("package.jmri.jmrit.operations.Operations_ChangeTrainDepartureTimes", true); // NOI18N 065 066 setTitle(Bundle.getMessage("TitleChangeDepartureTime")); 067 068 // setup buttons 069 addButtonAction(changeButton); 070 071 initMinimumSize(new Dimension(Control.panelWidth500, Control.panelHeight200)); 072 } 073 074 @Override 075 public void buttonActionPerformed(java.awt.event.ActionEvent ae) { 076 if (ae.getSource() == changeButton) { 077 log.debug("change button activated"); 078 TrainManager trainManager = InstanceManager.getDefault(TrainManager.class); 079 List<Train> trains = trainManager.getTrainsByIdList(); 080 for (Train train : trains) { 081 train.setDepartureTime(adjustHour(train.getDepartureTimeHour()), train.getDepartureTimeMinute()); 082 } 083 // now check every route to see if there are any departure times that need 084 // adjustment 085 if (routesCheckBox.isSelected()) { 086 RouteManager routeManager = InstanceManager.getDefault(RouteManager.class); 087 List<Route> routes = routeManager.getRoutesByNameList(); 088 for (Route route : routes) { 089 for (RouteLocation rl : route.getLocationsBySequenceList()) { 090 if (!rl.getDepartureTime().equals(RouteLocation.NONE)) 091 rl.setDepartureTime(adjustHour(rl.getDepartureTimeHour()), rl.getDepartureTimeMinute()); 092 } 093 } 094 } 095 } 096 } 097 098 private String adjustHour(String time) { 099 int hour = (int) hourBox.getSelectedItem() + Integer.parseInt(time); 100 if (hour > 23) { 101 hour = hour - 24; 102 } 103 return Integer.toString(hour); 104 } 105 106 private final static Logger log = LoggerFactory.getLogger(ChangeDepartureTimesFrame.class); 107}