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.locations.Location; 014import jmri.jmrit.operations.locations.LocationManager; 015import jmri.jmrit.operations.routes.*; 016import jmri.jmrit.operations.setup.Control; 017import jmri.jmrit.operations.trains.TrainManager; 018 019/** 020 * Frame to show and edit which routes can service a location 021 * 022 * @author Dan Boudreau Copyright (C) 2023 023 */ 024public class ShowRoutesServingLocationFrame extends OperationsFrame implements java.beans.PropertyChangeListener { 025 026 Location _location = null; 027 028 // panels 029 JPanel pRoutes = new JPanel(); 030 031 // combo boxes 032 JComboBox<Location> locationComboBox = new JComboBox<>(); 033 034 public ShowRoutesServingLocationFrame() { 035 super(Bundle.getMessage("TitleShowRoutes")); 036 } 037 038 public void initComponents(Location location) { 039 040 _location = location; 041 042 // general GUI config 043 getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 044 045 // Set up the panels 046 JPanel pLocations = new JPanel(); 047 pLocations.setLayout(new GridBagLayout()); 048 pLocations.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Location"))); 049 pLocations.setMaximumSize(new Dimension(2000, 50)); 050 051 addItem(pLocations, locationComboBox, 0, 0); 052 053 pRoutes.setLayout(new GridBagLayout()); 054 JScrollPane routesPane = new JScrollPane(pRoutes); 055 routesPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 056 routesPane.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TitleRoutesTable"))); 057 058 getContentPane().add(pLocations); 059 getContentPane().add(routesPane); 060 061 // setup combo box 062 addComboBoxAction(locationComboBox); 063 updateLocationsComboBox(); 064 065 if (location != null) { 066 location.addPropertyChangeListener(this); 067 } 068 069 addPropertyChangeAllRoutes(); 070 071 // add help menu to window 072 addHelpMenu("package.jmri.jmrit.operations.Operations_ShowRoutesServicingThisLocation", true); // NOI18N 073 074 setPreferredSize(null); 075 initMinimumSize(); 076 } 077 078 private void updateRoutePane() { 079 log.debug("Updating for location ({})", _location); 080 pRoutes.removeAll(); 081 int y = 0; 082 for (Route route : InstanceManager.getDefault(RouteManager.class).getRoutesByNameList()) { 083 for (RouteLocation rl : route.getLocationsBySequenceList()) { 084 if (_location != null && rl.getName().equals(_location.getName())) { 085 JButton button = new JButton(route.getName()); 086 addButtonAction(button); 087 addItemLeft(pRoutes, button, 1, y++); 088 break; 089 } 090 } 091 } 092 pRoutes.repaint(); 093 pRoutes.revalidate(); 094 pack(); 095 } 096 097 @Override 098 public void comboBoxActionPerformed(java.awt.event.ActionEvent ae) { 099 if (ae.getSource().equals(locationComboBox)) { 100 _location = (Location) locationComboBox.getSelectedItem(); 101 updateRoutePane(); 102 } 103 } 104 105 @Override 106 public void buttonActionPerformed(java.awt.event.ActionEvent ae) { 107 JButton button = (JButton) ae.getSource(); 108 log.debug("Edit route button ({})", button.getText()); 109 Route route = InstanceManager.getDefault(RouteManager.class).getRouteByName(button.getText()); 110 RouteEditFrame frame = new RouteEditFrame(); 111 frame.initComponents(route); 112 } 113 114 private void updateLocationsComboBox() { 115 InstanceManager.getDefault(LocationManager.class).updateComboBox(locationComboBox); 116 locationComboBox.setSelectedItem(_location); 117 } 118 119 @Override 120 public void dispose() { 121 if (_location != null) { 122 _location.removePropertyChangeListener(this); 123 } 124 removePropertyChangeAllRoutes(); 125 super.dispose(); 126 } 127 128 public void addPropertyChangeAllRoutes() { 129 InstanceManager.getDefault(RouteManager.class).addPropertyChangeListener(this); 130 for (Route route : InstanceManager.getDefault(RouteManager.class).getRoutesByNameList()) { 131 route.addPropertyChangeListener(this); 132 } 133 } 134 135 public void removePropertyChangeAllRoutes() { 136 InstanceManager.getDefault(TrainManager.class).removePropertyChangeListener(this); 137 for (Route route : InstanceManager.getDefault(RouteManager.class).getRoutesByNameList()) { 138 route.removePropertyChangeListener(this); 139 } 140 } 141 142 @Override 143 public void propertyChange(java.beans.PropertyChangeEvent e) { 144 if (Control.SHOW_PROPERTY) { 145 log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(), 146 e.getNewValue()); 147 } 148 if (e.getPropertyName().equals(RouteManager.LISTLENGTH_CHANGED_PROPERTY)) { 149 removePropertyChangeAllRoutes(); 150 addPropertyChangeAllRoutes(); 151 } 152 if (e.getPropertyName().equals(RouteManager.LISTLENGTH_CHANGED_PROPERTY) || 153 e.getPropertyName().equals(Route.LISTCHANGE_CHANGED_PROPERTY) || 154 e.getPropertyName().equals(Route.ROUTE_NAME_CHANGED_PROPERTY) || 155 e.getPropertyName().equals(Location.NAME_CHANGED_PROPERTY)) { 156 updateRoutePane(); 157 } 158 } 159 160 private final static Logger log = LoggerFactory.getLogger(ShowRoutesServingLocationFrame.class); 161}