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