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