001package jmri.jmrit.operations.routes.tools; 002 003import java.awt.Dimension; 004import java.awt.GridBagLayout; 005 006import javax.swing.*; 007 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011import jmri.InstanceManager; 012import jmri.jmrit.operations.OperationsFrame; 013import jmri.jmrit.operations.OperationsXml; 014import jmri.jmrit.operations.routes.Route; 015import jmri.jmrit.operations.routes.RouteManager; 016import jmri.jmrit.operations.setup.Control; 017import jmri.jmrit.operations.setup.Setup; 018import jmri.jmrit.operations.trains.Train; 019import jmri.swing.JTablePersistenceManager; 020 021/** 022 * Frame for user edit of q route's blocking order. 023 * 024 * @author Dan Boudreau Copyright (C) 2021 025 */ 026public class RouteBlockingOrderEditFrame extends OperationsFrame implements java.beans.PropertyChangeListener { 027 028 RouteBlockingOrderEditTableModel routeModel = new RouteBlockingOrderEditTableModel(); 029 JTable routeTable = new JTable(routeModel); 030 JScrollPane routePane; 031 032 RouteManager routeManager; 033 034 Route _route = null; 035 Train _train = null; 036 037 // major buttons 038 JButton saveRouteButton = new JButton(Bundle.getMessage("SaveRoute")); 039 JButton resetRouteButton = new JButton(Bundle.getMessage("ButtonReset")); 040 041 JLabel routeName = new JLabel(); 042 JLabel routeComment = new JLabel(); 043 044 public RouteBlockingOrderEditFrame(Route route) { 045 super(Bundle.getMessage("MenuBlockingOrder")); 046 initComponents(route); 047 } 048 049 private void initComponents(Route route) { 050 _route = route; 051 052 // load managers 053 routeManager = InstanceManager.getDefault(RouteManager.class); 054 055 // Set up the jtable in a Scroll Pane.. 056 routePane = new JScrollPane(routeTable); 057 routePane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 058 routePane.setBorder(BorderFactory.createTitledBorder("")); 059 060 routeModel.initTable(this, routeTable, _route); 061 062 if (_route != null) { 063 _route.addPropertyChangeListener(this); 064 routeName.setText(_route.getName()); 065 routeComment.setText(_route.getComment()); 066 enableButtons(!route.getStatus().equals(Route.TRAIN_BUILT)); // do not allow user to modify a built train 067 } 068 069 getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 070 071 // Set up the panels 072 JPanel p1 = new JPanel(); 073 p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS)); 074 JScrollPane p1Pane = new JScrollPane(p1); 075 p1Pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 076 p1Pane.setMaximumSize(new Dimension(2000, 200)); 077 p1Pane.setBorder(BorderFactory.createTitledBorder("")); 078 079 // name panel 080 JPanel pName = new JPanel(); 081 pName.setLayout(new GridBagLayout()); 082 pName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Name"))); 083 addItem(pName, routeName, 0, 0); 084 085 // comment panel 086 JPanel pComment = new JPanel(); 087 pComment.setLayout(new GridBagLayout()); 088 pComment.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Comment"))); 089 addItem(pComment, routeComment, 0, 0); 090 091 p1.add(pName); 092 p1.add(pComment); 093 094 JPanel pF = new JPanel(); 095 addItem(pF, new JLabel(Bundle.getMessage("TrainFront")), 0, 0); 096 097 JPanel pR = new JPanel(); 098 addItem(pR, new JLabel(Bundle.getMessage("TrainRear")), 0, 0); 099 100 JPanel pB = new JPanel(); 101 pB.setLayout(new GridBagLayout()); 102 JScrollPane pBPane = new JScrollPane(pB); 103 pBPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 104 pBPane.setMaximumSize(new Dimension(2000, 200)); 105 pBPane.setBorder(BorderFactory.createTitledBorder("")); 106 107 addItem(pB, resetRouteButton, 2, 0); 108 addItem(pB, saveRouteButton, 3, 0); 109 110 getContentPane().add(p1Pane); 111 getContentPane().add(pF); 112 getContentPane().add(routePane); 113 getContentPane().add(pR); 114 getContentPane().add(pBPane); 115 116 // setup buttons 117 addButtonAction(resetRouteButton); 118 addButtonAction(saveRouteButton); 119 120 // build menu 121 JMenuBar menuBar = new JMenuBar(); 122 setJMenuBar(menuBar); 123 addHelpMenu("package.jmri.jmrit.operations.Operations_RouteBlockingOrder", true); // NOI18N 124 125 // set frame size and route for display 126 initMinimumSize(new Dimension(Control.panelWidth500, Control.panelHeight400)); 127 } 128 129 // Save, Delete, Add 130 @Override 131 public void buttonActionPerformed(java.awt.event.ActionEvent ae) { 132 if (ae.getSource() == saveRouteButton) { 133 log.debug("route save button activated"); 134 saveRoute(); 135 if (Setup.isCloseWindowOnSaveEnabled()) { 136 dispose(); 137 } 138 } 139 if (ae.getSource() == resetRouteButton) { 140 log.debug("route reset button activated"); 141 if (_route != null) { 142 _route.resetBlockingOrder(); 143 } 144 } 145 } 146 147 private void saveRoute() { 148 if (routeTable.isEditing()) { 149 log.debug("route table edit true"); 150 routeTable.getCellEditor().stopCellEditing(); 151 } 152 // save route file 153 OperationsXml.save(); 154 } 155 156 private void enableButtons(boolean enabled) { 157 resetRouteButton.setEnabled(enabled); 158 saveRouteButton.setEnabled(enabled); 159 routeTable.setEnabled(enabled); 160 } 161 162 @Override 163 public void dispose() { 164 InstanceManager.getOptionalDefault(JTablePersistenceManager.class).ifPresent(tpm -> { 165 tpm.stopPersisting(routeTable); 166 }); 167 if (_route != null) { 168 _route.removePropertyChangeListener(this); 169 } 170 routeModel.dispose(); 171 super.dispose(); 172 } 173 174 @Override 175 public void propertyChange(java.beans.PropertyChangeEvent e) { 176 if (Control.SHOW_PROPERTY) { 177 log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(), e 178 .getNewValue()); 179 } 180 if (e.getPropertyName().equals(Route.ROUTE_STATUS_CHANGED_PROPERTY)) { 181 enableButtons(!_route.getStatus().equals(Route.TRAIN_BUILT)); // do not allow user to modify a built train 182 } 183 } 184 185 private final static Logger log = LoggerFactory.getLogger(RouteBlockingOrderEditFrame.class); 186}