001package jmri.jmrit.vsdecoder.swing;
002
003import java.awt.Frame;
004import java.awt.event.ActionEvent;
005import java.util.List;
006import java.util.Set;
007import javax.swing.AbstractAction;
008import jmri.Block;
009import jmri.BlockManager;
010import jmri.PhysicalLocationReporter;
011import jmri.Reporter;
012import jmri.ReporterManager;
013import jmri.jmrit.operations.locations.Location;
014import jmri.jmrit.operations.locations.LocationManager;
015import jmri.jmrit.vsdecoder.VSDecoderManager;
016import jmri.jmrit.vsdecoder.listener.ListeningSpot;
017import jmri.util.PhysicalLocation;
018
019/**
020 * Loading of Reporters, Blocks, Locations and Listener attributes.
021 *
022 * <hr>
023 * This file is part of JMRI.
024 * <p>
025 * JMRI is free software; you can redistribute it and/or modify it under
026 * the terms of version 2 of the GNU General Public License as published
027 * by the Free Software Foundation. See the "COPYING" file for a copy
028 * of this license.
029 * <p>
030 * JMRI is distributed in the hope that it will be useful, but WITHOUT
031 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
032 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
033 * for more details.
034 *
035 * @author Mark Underwood Copyright (C) 2011
036 */
037public class ManageLocationsAction extends AbstractAction {
038
039    private ManageLocationsFrame f = null;
040    private ListeningSpot listenerLoc;
041
042    public ManageLocationsAction(String s) {
043        super(s);
044    }
045
046    @Override
047    public void actionPerformed(ActionEvent e) {
048        if (f == null || !f.isVisible()) {
049            // Handle the Listener
050            listenerLoc = VSDecoderManager.instance().getVSDecoderPreferences().getListenerPosition();
051
052            // Handle Reporters
053            ReporterManager rmgr = jmri.InstanceManager.getDefault(ReporterManager.class);
054            Set<Reporter> reporterSet = rmgr.getNamedBeanSet();
055            Object[][] reporterTable = new Object[reporterSet.size()][7];
056            int i = 0;
057            for (Reporter r : reporterSet) {
058                if (r != null) {
059                    if (r instanceof PhysicalLocationReporter) {
060                        PhysicalLocation p = ((PhysicalLocationReporter) r).getPhysicalLocation();
061                        reporterTable[i][ManageLocationsTableModel.SYSNAMECOL] = r.getSystemName();
062                        reporterTable[i][ManageLocationsTableModel.USERNAMECOL] = r.getDisplayName();
063                        reporterTable[i][ManageLocationsTableModel.USECOL + 1] = true;
064                        reporterTable[i][ManageLocationsTableModel.XCOL + 1] = p.getX();
065                        reporterTable[i][ManageLocationsTableModel.YCOL + 1] = p.getY();
066                        reporterTable[i][ManageLocationsTableModel.ZCOL + 1] = p.getZ();
067                        reporterTable[i][ManageLocationsTableModel.TUNNELCOL + 1] = p.isTunnel();
068                    } else {
069                        reporterTable[i][ManageLocationsTableModel.SYSNAMECOL] = r.getSystemName();
070                        reporterTable[i][ManageLocationsTableModel.USERNAMECOL] = r.getDisplayName();
071                        reporterTable[i][ManageLocationsTableModel.USECOL + 1] = false;
072                        reporterTable[i][ManageLocationsTableModel.XCOL + 1] = 0.0f;
073                        reporterTable[i][ManageLocationsTableModel.YCOL + 1] = 0.0f;
074                        reporterTable[i][ManageLocationsTableModel.ZCOL + 1] = 0.0f;
075                        reporterTable[i][ManageLocationsTableModel.TUNNELCOL + 1] = false;
076                    }
077                }
078                i++;
079            }
080
081            // Handle Blocks
082            BlockManager bmgr = jmri.InstanceManager.getDefault(BlockManager.class);
083            Set<Block> blockSet = bmgr.getNamedBeanSet();
084            Object[][] blockTable = new Object[blockSet.size()][7];
085            i = 0;
086            for (Block b : blockSet) {
087                // NOTE: Unlike Reporters, all Blocks are (now) PhysicalLocationReporters, so no need to do a check here.
088                // We'll keep the explicit cast for now, but it's not actually necessary.
089                if (b != null) {
090                    PhysicalLocation p = ((PhysicalLocationReporter) b).getPhysicalLocation();
091                    blockTable[i][ManageLocationsTableModel.SYSNAMECOL] = b.getSystemName();
092                    blockTable[i][ManageLocationsTableModel.USERNAMECOL] = b.getDisplayName();
093                    blockTable[i][ManageLocationsTableModel.USECOL + 1] = true;
094                    blockTable[i][ManageLocationsTableModel.XCOL + 1] = p.getX();
095                    blockTable[i][ManageLocationsTableModel.YCOL + 1] = p.getY();
096                    blockTable[i][ManageLocationsTableModel.ZCOL + 1] = p.getZ();
097                    blockTable[i][ManageLocationsTableModel.TUNNELCOL + 1] = p.isTunnel();
098                }
099                i++;
100            }
101
102            // Handle Ops Locations
103            LocationManager lmgr = jmri.InstanceManager.getDefault(LocationManager.class);
104            List<Location> locations = lmgr.getLocationsByIdList();
105            log.debug("TableSize: {}", locations.size());
106            Object[][] opsTable = new Object[locations.size()][6];
107            i = 0;
108            for (Location l : locations) {
109                log.debug("i: {}, MLA: {}, Name: {}, table: {}", i, l.getId(), l.getName(), java.util.Arrays.toString(opsTable[i]));
110                opsTable[i][ManageLocationsTableModel.NAMECOL] = l.getName();
111                PhysicalLocation p = l.getPhysicalLocation();
112                if (p == PhysicalLocation.Origin) {
113                    opsTable[i][ManageLocationsTableModel.USECOL] = false;
114                } else {
115                    opsTable[i][ManageLocationsTableModel.USECOL] = true;
116                }
117                opsTable[i][ManageLocationsTableModel.XCOL] = p.getX();
118                opsTable[i][ManageLocationsTableModel.YCOL] = p.getY();
119                opsTable[i][ManageLocationsTableModel.ZCOL] = p.getZ();
120                opsTable[i][ManageLocationsTableModel.TUNNELCOL] = p.isTunnel();
121                i++;
122            }
123
124            f = new ManageLocationsFrame(listenerLoc, reporterTable, opsTable, blockTable);
125        }
126        f.setExtendedState(Frame.NORMAL);
127    }
128
129    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ManageLocationsAction.class);
130
131}