Search in sources :

Example 6 with SignalMastLogic

use of jmri.SignalMastLogic in project JMRI by JMRI.

the class DefaultSignalMastLogicManagerXml method loadSignalMastLogic.

public boolean loadSignalMastLogic(Element signalMastLogic) {
    List<Element> logicList = signalMastLogic.getChildren("signalmastlogic");
    if (log.isDebugEnabled()) {
        log.debug("Found " + logicList.size() + " signal mast logics");
    }
    SignalMastManager sm = InstanceManager.getDefault(jmri.SignalMastManager.class);
    SignalMastLogicManager sml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
    try {
        String logicDelay = signalMastLogic.getChild("logicDelay").getText();
        sml.setSignalLogicDelay(Long.parseLong(logicDelay));
    } catch (java.lang.NullPointerException e) {
    //Considered normal if it doesn't exists
    }
    boolean loadOk = true;
    for (Element so : logicList) {
        String source = so.getChild("sourceSignalMast").getText();
        SignalMast sourceMast = sm.getSignalMast(source);
        if (sourceMast != null) {
            SignalMastLogic logic = sml.newSignalMastLogic(sourceMast);
            List<Element> destList = so.getChildren("destinationMast");
            for (Element s : destList) {
                String destination = s.getChild("destinationSignalMast").getText();
                SignalMast dest = sm.getSignalMast(destination);
                if (dest != null) {
                    logic.setDestinationMast(dest);
                    if (s.getChild("comment") != null) {
                        logic.setComment(s.getChild("comment").getText(), dest);
                    }
                    if (s.getChild("enabled") != null) {
                        if (s.getChild("enabled").getText().equals("yes")) {
                            logic.setEnabled(dest);
                        } else {
                            logic.setDisabled(dest);
                        }
                    }
                    if (s.getChild("allowAutoMaticSignalMastGeneration") != null) {
                        if (s.getChild("allowAutoMaticSignalMastGeneration").getText().equals("no")) {
                            logic.allowAutoMaticSignalMastGeneration(false, dest);
                        } else {
                            logic.allowAutoMaticSignalMastGeneration(true, dest);
                        }
                    }
                    boolean useLayoutEditorTurnout = true;
                    boolean useLayoutEditorBlock = true;
                    if (s.getChild("useLayoutEditorTurnouts") != null) {
                        if (s.getChild("useLayoutEditorTurnouts").getText().equals("no")) {
                            useLayoutEditorTurnout = false;
                        }
                    }
                    if (s.getChild("useLayoutEditorBlocks") != null) {
                        if (s.getChild("useLayoutEditorBlocks").getText().equals("no")) {
                            useLayoutEditorBlock = false;
                        }
                    }
                    try {
                        logic.useLayoutEditorDetails(useLayoutEditorTurnout, useLayoutEditorBlock, dest);
                    } catch (jmri.JmriException ex) {
                    }
                    if (s.getChild("useLayoutEditor") != null) {
                        try {
                            if (s.getChild("useLayoutEditor").getText().equals("yes")) {
                                logic.useLayoutEditor(true, dest);
                            } else {
                                logic.useLayoutEditor(false, dest);
                            }
                        } catch (jmri.JmriException e) {
                        //Considered normal if layout editor hasn't yet been set up.
                        }
                    }
                    if (s.getChild("associatedSection") != null) {
                        Section sect = InstanceManager.getDefault(jmri.SectionManager.class).getSection(s.getChild("associatedSection").getText());
                        logic.setAssociatedSection(sect, dest);
                    }
                    Element turnoutElem = s.getChild("turnouts");
                    if (turnoutElem != null) {
                        List<Element> turnoutList = turnoutElem.getChildren("turnout");
                        if (turnoutList.size() > 0) {
                            Hashtable<NamedBeanHandle<Turnout>, Integer> list = new Hashtable<NamedBeanHandle<Turnout>, Integer>();
                            for (Element t : turnoutList) {
                                String turnout = t.getChild("turnoutName").getText();
                                String state = t.getChild("turnoutState").getText();
                                int value = Turnout.CLOSED;
                                if (state.equals("thrown")) {
                                    value = Turnout.THROWN;
                                }
                                Turnout turn = InstanceManager.turnoutManagerInstance().getTurnout(turnout);
                                if (turn != null) {
                                    NamedBeanHandle<Turnout> namedTurnout = nbhm.getNamedBeanHandle(turnout, turn);
                                    list.put(namedTurnout, value);
                                }
                                log.debug("Unable to add Turnout {} as it does not exist in the panel file", turnout);
                            }
                            logic.setTurnouts(list, dest);
                        }
                    }
                    Element sensorElem = s.getChild("sensors");
                    if (sensorElem != null) {
                        List<Element> sensorList = sensorElem.getChildren("sensor");
                        if (sensorList.size() > 0) {
                            Hashtable<NamedBeanHandle<Sensor>, Integer> list = new Hashtable<NamedBeanHandle<Sensor>, Integer>();
                            for (Element sl : sensorList) {
                                String sensorName = sl.getChild("sensorName").getText();
                                String state = sl.getChild("sensorState").getText();
                                int value = Sensor.INACTIVE;
                                if (state.equals("active")) {
                                    value = Sensor.ACTIVE;
                                }
                                Sensor sen = InstanceManager.sensorManagerInstance().getSensor(sensorName);
                                if (sen != null) {
                                    NamedBeanHandle<Sensor> namedSensor = nbhm.getNamedBeanHandle(sensorName, sen);
                                    list.put(namedSensor, value);
                                }
                                log.debug("Unable to add sensor {} as it does not exist in the panel file", sensorName);
                            }
                            logic.setSensors(list, dest);
                        }
                    }
                    Element blockElem = s.getChild("blocks");
                    if (blockElem != null) {
                        List<Element> blockList = blockElem.getChildren("block");
                        if (blockList.size() > 0) {
                            Hashtable<Block, Integer> list = new Hashtable<Block, Integer>();
                            for (Element b : blockList) {
                                String block = b.getChild("blockName").getText();
                                String state = b.getChild("blockState").getText();
                                int value = 0x03;
                                if (state.equals("occupied")) {
                                    value = Block.OCCUPIED;
                                } else if (state.equals("unoccupied")) {
                                    value = Block.UNOCCUPIED;
                                }
                                Block blk = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(block);
                                if (blk != null) {
                                    list.put(blk, value);
                                }
                                log.debug("Unable to add Block {} as it does not exist in the panel file", block);
                            }
                            logic.setBlocks(list, dest);
                        }
                    }
                    Element mastElem = s.getChild("masts");
                    if (mastElem != null) {
                        List<Element> mastList = mastElem.getChildren("mast");
                        if (mastList.size() > 0) {
                            Hashtable<SignalMast, String> list = new Hashtable<SignalMast, String>();
                            for (Element m : mastList) {
                                String mast = m.getChild("mastName").getText();
                                String state = m.getChild("mastState").getText();
                                SignalMast mst = InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(mast);
                                if (mst != null) {
                                    list.put(mst, state);
                                }
                                log.debug("Unable to add Signal Mast {} as it does not exist in the panel file", mast);
                            }
                            logic.setMasts(list, dest);
                        }
                    }
                } else {
                    log.error("Destination Mast " + destination + " Not found, logic not loaded");
                    loadOk = false;
                }
            }
        } else {
            log.error("Source Mast " + source + " Not found, logic not loaded");
            loadOk = false;
        }
    }
    sml.initialise();
    return loadOk;
}
Also used : Element(org.jdom2.Element) SignalMastLogicManager(jmri.SignalMastLogicManager) SignalMast(jmri.SignalMast) NamedBeanHandle(jmri.NamedBeanHandle) Hashtable(java.util.Hashtable) SignalMastManager(jmri.SignalMastManager) Section(jmri.Section) SignalMastLogic(jmri.SignalMastLogic) Block(jmri.Block) Turnout(jmri.Turnout) Sensor(jmri.Sensor)

Example 7 with SignalMastLogic

use of jmri.SignalMastLogic in project JMRI by JMRI.

the class DefaultSignalMastLogicManagerXml method store.

@Override
public Element store(Object o) {
    Element signalMastLogic = new Element("signalmastlogics");
    setStoreElementClass(signalMastLogic);
    SignalMastLogicManager smlm = (SignalMastLogicManager) o;
    signalMastLogic.addContent(new Element("logicDelay").addContent(Long.toString(smlm.getSignalLogicDelay())));
    ArrayList<SignalMastLogic> sml = smlm.getSignalMastLogicList();
    for (int i = 0; i < sml.size(); i++) {
        SignalMastLogic sm = sml.get(i);
        Element source = new Element("signalmastlogic");
        // added purely to make human reading of the xml easier
        source.setAttribute("source", sm.getSourceMast().getDisplayName());
        source.addContent(new Element("sourceSignalMast").addContent(sm.getSourceMast().getDisplayName()));
        ArrayList<SignalMast> destination = sm.getDestinationList();
        if (destination.size() != 0) {
            for (int k = 0; k < destination.size(); k++) {
                SignalMast dest = destination.get(k);
                if (sml.get(i).getStoreState(dest) != SignalMastLogic.STORENONE) {
                    Element elem = new Element("destinationMast");
                    // added purely to make human reading of the xml easier
                    elem.setAttribute("destination", dest.getDisplayName());
                    elem.addContent(new Element("destinationSignalMast").addContent(dest.getDisplayName()));
                    elem.addContent(new Element("comment").addContent(sm.getComment(dest)));
                    if (sm.isEnabled(dest)) {
                        elem.addContent(new Element("enabled").addContent("yes"));
                    } else {
                        elem.addContent(new Element("enabled").addContent("no"));
                    }
                    if (sm.allowAutoMaticSignalMastGeneration(dest)) {
                        elem.addContent(new Element("allowAutoMaticSignalMastGeneration").addContent("yes"));
                    } else {
                        elem.addContent(new Element("allowAutoMaticSignalMastGeneration").addContent("no"));
                    }
                    if (sm.useLayoutEditor(dest)) {
                        elem.addContent(new Element("useLayoutEditor").addContent("yes"));
                    } else {
                        elem.addContent(new Element("useLayoutEditor").addContent("no"));
                    }
                    if (sm.useLayoutEditorTurnouts(dest)) {
                        elem.addContent(new Element("useLayoutEditorTurnouts").addContent("yes"));
                    } else {
                        elem.addContent(new Element("useLayoutEditorTurnouts").addContent("no"));
                    }
                    if (sm.useLayoutEditorBlocks(dest)) {
                        elem.addContent(new Element("useLayoutEditorBlocks").addContent("yes"));
                    } else {
                        elem.addContent(new Element("useLayoutEditorBlocks").addContent("no"));
                    }
                    if (sm.getAssociatedSection(dest) != null) {
                        elem.addContent(new Element("associatedSection").addContent(sm.getAssociatedSection(dest).getDisplayName()));
                    }
                    if (sm.isTurnoutLockAllowed(dest)) {
                        elem.addContent(new Element("lockTurnouts").addContent("yes"));
                    } else {
                        elem.addContent(new Element("lockTurnouts").addContent("no"));
                    }
                    if (sml.get(i).getStoreState(dest) == SignalMastLogic.STOREALL) {
                        ArrayList<Block> blocks = sm.getBlocks(dest);
                        if (blocks.size() > 0) {
                            Element blockElement = new Element("blocks");
                            for (int j = 0; j < blocks.size(); j++) {
                                Element bloc = new Element("block");
                                bloc.addContent(new Element("blockName").addContent(blocks.get(j).getDisplayName()));
                                String blkState = "anyState";
                                if (sm.getBlockState(blocks.get(j), dest) == Block.OCCUPIED) {
                                    blkState = "occupied";
                                } else if (sm.getBlockState(blocks.get(j), dest) == Block.UNOCCUPIED) {
                                    blkState = "unoccupied";
                                }
                                bloc.addContent(new Element("blockState").addContent(blkState));
                                blockElement.addContent(bloc);
                            }
                            elem.addContent(blockElement);
                        }
                        ArrayList<NamedBeanHandle<Turnout>> turnouts = sm.getNamedTurnouts(dest);
                        if (turnouts.size() > 0) {
                            Element turnoutElement = new Element("turnouts");
                            for (int j = 0; j < turnouts.size(); j++) {
                                Element turn = new Element("turnout");
                                turn.addContent(new Element("turnoutName").addContent(turnouts.get(j).getName()));
                                String turnState = "thrown";
                                if (sm.getTurnoutState(turnouts.get(j).getBean(), dest) == Turnout.CLOSED) {
                                    turnState = "closed";
                                }
                                turn.addContent(new Element("turnoutState").addContent(turnState));
                                turnoutElement.addContent(turn);
                            }
                            elem.addContent(turnoutElement);
                        }
                        ArrayList<NamedBeanHandle<Sensor>> sensors = sm.getNamedSensors(dest);
                        if (sensors.size() > 0) {
                            Element sensorElement = new Element("sensors");
                            for (int j = 0; j < sensors.size(); j++) {
                                Element sensor = new Element("sensor");
                                sensor.addContent(new Element("sensorName").addContent(sensors.get(j).getName()));
                                String sensorState = "inActive";
                                if (sm.getSensorState(sensors.get(j).getBean(), dest) == Sensor.ACTIVE) {
                                    sensorState = "active";
                                }
                                sensor.addContent(new Element("sensorState").addContent(sensorState));
                                sensorElement.addContent(sensor);
                            }
                            elem.addContent(sensorElement);
                        }
                        ArrayList<SignalMast> masts = sm.getSignalMasts(dest);
                        if (masts.size() > 0) {
                            Element mastElement = new Element("masts");
                            for (int j = 0; j < masts.size(); j++) {
                                Element mast = new Element("mast");
                                mast.addContent(new Element("mastName").addContent(masts.get(j).getDisplayName()));
                                mast.addContent(new Element("mastState").addContent(sm.getSignalMastState(masts.get(j), dest)));
                                mastElement.addContent(mast);
                            }
                            elem.addContent(mastElement);
                        }
                    }
                    source.addContent(elem);
                }
            }
            signalMastLogic.addContent(source);
        }
    }
    return signalMastLogic;
}
Also used : Element(org.jdom2.Element) SignalMastLogicManager(jmri.SignalMastLogicManager) SignalMastLogic(jmri.SignalMastLogic) SignalMast(jmri.SignalMast) Block(jmri.Block) NamedBeanHandle(jmri.NamedBeanHandle)

Example 8 with SignalMastLogic

use of jmri.SignalMastLogic in project JMRI by JMRI.

the class SignalMastLogicTableAction method createModel.

@Override
protected void createModel() {
    m = new BeanTableDataModel() {

        public static final int SOURCECOL = 0;

        public static final int SOURCEAPPCOL = 1;

        public static final int DESTCOL = 2;

        public static final int DESTAPPCOL = 3;

        public static final int COMCOL = 4;

        public static final int DELCOL = 5;

        public static final int ENABLECOL = 6;

        public static final int EDITLOGICCOL = 7;

        //We have to set a manager first off, but this gets replaced.
        @Override
        protected SignalMastLogicManager getManager() {
            return InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
        }

        /*public EcosLocoAddress getByDccAddress(int address) {return getManager().getByDccAddress(address);}*/
        @Override
        public String getValue(String s) {
            return "Set";
        }

        @Override
        protected String getMasterClassName() {
            return getClassName();
        }

        @Override
        public void clickOn(jmri.NamedBean t) {
        }

        @Override
        protected synchronized void updateNameList() {
            // first, remove listeners from the individual objects
            if (signalMastLogicList != null) {
                for (int i = 0; i < signalMastLogicList.size(); i++) {
                    // if object has been deleted, it's not here; ignore it
                    Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(i);
                    Enumeration<SignalMastLogic> en = b.keys();
                    while (en.hasMoreElements()) {
                        SignalMastLogic sm = en.nextElement();
                        SignalMast dest = b.get(sm);
                        sm.removePropertyChangeListener(this);
                        sm.getSourceMast().removePropertyChangeListener(this);
                        dest.removePropertyChangeListener(this);
                    }
                }
            }
            ArrayList<SignalMastLogic> source = getManager().getSignalMastLogicList();
            signalMastLogicList = new ArrayList<Hashtable<SignalMastLogic, SignalMast>>();
            for (int i = 0; i < source.size(); i++) {
                ArrayList<SignalMast> destList = source.get(i).getDestinationList();
                source.get(i).addPropertyChangeListener(this);
                source.get(i).getSourceMast().addPropertyChangeListener(this);
                for (int j = 0; j < destList.size(); j++) {
                    Hashtable<SignalMastLogic, SignalMast> hash = new Hashtable<SignalMastLogic, SignalMast>(1);
                    hash.put(source.get(i), destList.get(j));
                    destList.get(j).addPropertyChangeListener(this);
                    signalMastLogicList.add(hash);
                }
            }
        }

        //Will need to redo this so that we work out the row number from looking in the signalmastlogiclist.
        @Override
        public void propertyChange(java.beans.PropertyChangeEvent e) {
            if (suppressUpdate) {
                return;
            }
            // updateNameList();
            if (e.getPropertyName().equals("length") || e.getPropertyName().equals("updatedDestination") || e.getPropertyName().equals("updatedSource")) {
                updateNameList();
                log.debug("Table changed length to " + signalMastLogicList.size());
                fireTableDataChanged();
            } else if (e.getSource() instanceof SignalMastLogic) {
                SignalMastLogic logic = (SignalMastLogic) e.getSource();
                if (matchPropertyName(e)) {
                    for (int i = 0; i < signalMastLogicList.size(); i++) {
                        Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(i);
                        Enumeration<SignalMastLogic> en = b.keys();
                        while (en.hasMoreElements()) {
                            SignalMastLogic sm = en.nextElement();
                            if (sm == logic) {
                                fireTableRowsUpdated(i, i);
                            }
                        }
                    }
                }
            } else if (e.getSource() instanceof jmri.SignalMast) {
                jmri.SignalMast sigMast = (jmri.SignalMast) e.getSource();
                for (int i = 0; i < signalMastLogicList.size(); i++) {
                    Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(i);
                    Enumeration<SignalMastLogic> en = b.keys();
                    while (en.hasMoreElements()) {
                        SignalMastLogic sm = en.nextElement();
                        //SignalMast dest = b.get(sm);
                        if (sm.getSourceMast() == sigMast) {
                            fireTableRowsUpdated(i, i);
                        }
                    }
                }
            }
        }

        //}
        /**
             * Is this property event announcing a change this table should
             * display?
             * <P>
             * Note that events will come both from the NamedBeans and also from
             * the manager
             */
        @Override
        protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            return ((e.getPropertyName().indexOf("Comment") >= 0) || (e.getPropertyName().indexOf("Enable") >= 0));
        }

        @Override
        public int getColumnCount() {
            return EDITLOGICCOL + 1;
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            if (col == COMCOL) {
                getLogicFromRow(row).setComment((String) value, getDestMastFromRow(row));
            } else if (col == EDITLOGICCOL) {
                class WindowMaker implements Runnable {

                    int row;

                    WindowMaker(int r) {
                        row = r;
                    }

                    @Override
                    public void run() {
                        editLogic(row, 0);
                    }
                }
                WindowMaker t = new WindowMaker(row);
                javax.swing.SwingUtilities.invokeLater(t);
            } else if (col == DELCOL) {
                // button fired, delete Bean
                deleteLogic(row, col);
            } else if (col == ENABLECOL) {
                boolean enable = ((Boolean) value).booleanValue();
                if (enable) {
                    getLogicFromRow(row).setEnabled(getDestMastFromRow(row));
                } else {
                    getLogicFromRow(row).setDisabled(getDestMastFromRow(row));
                }
            }
        }

        @Override
        public String getColumnName(int col) {
            switch(col) {
                case SOURCECOL:
                    return Bundle.getMessage("Source");
                case DESTCOL:
                    return Bundle.getMessage("Destination");
                case SOURCEAPPCOL:
                    return Bundle.getMessage("LabelAspectType");
                case DESTAPPCOL:
                    return Bundle.getMessage("LabelAspectType");
                case COMCOL:
                    return Bundle.getMessage("Comment");
                case DELCOL:
                    // override default, no title for Delete column
                    return "";
                case EDITLOGICCOL:
                    // override default, no title for Edit column
                    return "";
                case ENABLECOL:
                    return Bundle.getMessage("ColumnHeadEnabled");
                default:
                    return "unknown";
            }
        }

        @Override
        public Class<?> getColumnClass(int col) {
            switch(col) {
                case SOURCECOL:
                case DESTCOL:
                case SOURCEAPPCOL:
                case COMCOL:
                case DESTAPPCOL:
                    return String.class;
                case ENABLECOL:
                    return Boolean.class;
                case EDITLOGICCOL:
                case DELCOL:
                    return JButton.class;
                default:
                    return null;
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            switch(col) {
                case COMCOL:
                case EDITLOGICCOL:
                case DELCOL:
                case ENABLECOL:
                    return true;
                default:
                    return false;
            }
        }

        void editLogic(int row, int col) {
            sigLog.setMast(getLogicFromRow(row).getSourceMast(), getDestMastFromRow(row));
            sigLog.actionPerformed(null);
        }

        void deleteLogic(int row, int col) {
            //This needs to be looked at
            InstanceManager.getDefault(jmri.SignalMastLogicManager.class).removeSignalMastLogic(getLogicFromRow(row), getDestMastFromRow(row));
        }

        public SignalMast getDestMastFromRow(int row) {
            // if object has been deleted, it's not here; ignore it
            Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(row);
            Enumeration<SignalMastLogic> en = b.keys();
            while (en.hasMoreElements()) {
                return b.get(en.nextElement());
            }
            return null;
        }

        public SignalMastLogic getLogicFromRow(int row) {
            Hashtable<SignalMastLogic, SignalMast> b = signalMastLogicList.get(row);
            Enumeration<SignalMastLogic> en = b.keys();
            while (en.hasMoreElements()) {
                return en.nextElement();
            }
            return null;
        }

        @Override
        public int getPreferredWidth(int col) {
            switch(col) {
                case SOURCECOL:
                    return new JTextField(10).getPreferredSize().width;
                case COMCOL:
                    return 75;
                case DESTCOL:
                    return new JTextField(10).getPreferredSize().width;
                case // not actually used due to the configureTable, setColumnToHoldButton, configureButton
                EDITLOGICCOL:
                    return new JTextField(6).getPreferredSize().width;
                case // not actually used due to the configureTable, setColumnToHoldButton, configureButton
                DELCOL:
                    return new JTextField(5).getPreferredSize().width;
                case DESTAPPCOL:
                    return new JTextField(10).getPreferredSize().width;
                case SOURCEAPPCOL:
                    return new JTextField(10).getPreferredSize().width;
                case ENABLECOL:
                    return new JTextField(5).getPreferredSize().width;
                default:
                    //log.warn("Unexpected column in getPreferredWidth: "+col);
                    return new JTextField(8).getPreferredSize().width;
            }
        }

        @Override
        public void configureTable(JTable table) {
            setColumnToHoldButton(table, EDITLOGICCOL, new JButton(Bundle.getMessage("ButtonEdit")));
            table.getTableHeader().setReorderingAllowed(true);
            // have to shut off autoResizeMode to get horizontal scroll to work (JavaSwing p 541)
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            // resize columns as requested
            for (int i = 0; i < table.getColumnCount(); i++) {
                int width = getPreferredWidth(i);
                table.getColumnModel().getColumn(i).setPreferredWidth(width);
            }
            table.sizeColumnsToFit(-1);
            // configValueColumn(table);
            configDeleteColumn(table);
        }

        @Override
        public NamedBean getBySystemName(String name) {
            return null;
        }

        @Override
        public NamedBean getByUserName(String name) {
            return null;
        }

        @Override
        public synchronized void dispose() {
            getManager().removePropertyChangeListener(this);
            if (signalMastLogicList != null) {
                for (int i = 0; i < signalMastLogicList.size(); i++) {
                    SignalMastLogic b = getLogicFromRow(i);
                    if (b != null) {
                        b.removePropertyChangeListener(this);
                    }
                }
            }
        }

        @Override
        public int getRowCount() {
            return signalMastLogicList.size();
        }

        @Override
        public Object getValueAt(int row, int col) {
            // some error checking
            if (row >= signalMastLogicList.size()) {
                log.debug("row index is greater than signalMastLogicList size");
                return null;
            }
            SignalMastLogic b = getLogicFromRow(row);
            switch(col) {
                case SOURCECOL:
                    return getLogicFromRow(row).getSourceMast().getDisplayName();
                case // return user name
                DESTCOL:
                    // sometimes, the TableSorter invokes this on rows that no longer exist, so we check
                    return (b != null) ? getDestMastFromRow(row).getDisplayName() : null;
                case //
                SOURCEAPPCOL:
                    return (b != null) ? b.getSourceMast().getAspect() : null;
                case //
                DESTAPPCOL:
                    return (b != null) ? getDestMastFromRow(row).getAspect() : null;
                case COMCOL:
                    return (b != null) ? b.getComment(getDestMastFromRow(row)) : null;
                case DELCOL:
                    return Bundle.getMessage("ButtonDelete");
                case EDITLOGICCOL:
                    return Bundle.getMessage("ButtonEdit");
                case ENABLECOL:
                    return (b != null) ? b.isEnabled(getDestMastFromRow(row)) : null;
                default:
                    //log.error("internal state inconsistent with table requst for "+row+" "+col);
                    return null;
            }
        }

        @Override
        protected void configDeleteColumn(JTable table) {
            // have the delete column hold a button
            setColumnToHoldButton(table, DELCOL, new JButton(Bundle.getMessage("ButtonDelete")));
        }

        @Override
        protected String getBeanType() {
            return "Signal Mast Logic";
        }

        @Override
        protected void showPopup(MouseEvent e) {
        }
    };
}
Also used : NamedBean(jmri.NamedBean) ArrayList(java.util.ArrayList) JButton(javax.swing.JButton) SignalMast(jmri.SignalMast) JTextField(javax.swing.JTextField) NamedBean(jmri.NamedBean) SignalMastLogicManager(jmri.SignalMastLogicManager) SignalMast(jmri.SignalMast) PropertyChangeEvent(java.beans.PropertyChangeEvent) Enumeration(java.util.Enumeration) MouseEvent(java.awt.event.MouseEvent) Hashtable(java.util.Hashtable) SignalMastLogic(jmri.SignalMastLogic) JTable(javax.swing.JTable)

Example 9 with SignalMastLogic

use of jmri.SignalMastLogic in project JMRI by JMRI.

the class SignalMastLogicTest method testSetup.

@Test
public void testSetup() {
    jmri.NamedBeanHandleManager nbhm = jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class);
    // provide 2 turnouts:
    Turnout it1 = InstanceManager.turnoutManagerInstance().provideTurnout("IT1");
    Turnout it2 = InstanceManager.turnoutManagerInstance().provideTurnout("IT2");
    // provide 2 sensors:
    Sensor is1 = InstanceManager.sensorManagerInstance().provideSensor("IS1");
    Sensor is2 = InstanceManager.sensorManagerInstance().provideSensor("IS2");
    // provide 3 virtual signal masts:
    SignalMast sm1 = new jmri.implementation.VirtualSignalMast("IF$vsm:AAR-1946:CPL($0002)");
    Assert.assertNotNull("SignalMast is null!", sm1);
    SignalMast sm2 = new jmri.implementation.VirtualSignalMast("IF$vsm:AAR-1946:CPL($0002)");
    Assert.assertNotNull("SignalMast is null!", sm2);
    SignalMast sm3 = new jmri.implementation.VirtualSignalMast("IF$vsm:AAR-1946:CPL($0002)");
    Assert.assertNotNull("SignalMast is null!", sm3);
    // provide a signal mast logic:
    SignalMastLogic sml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).newSignalMastLogic(sm1);
    sml.setDestinationMast(sm2);
    Assert.assertNotNull("SignalMastLogic is null!", sml);
    sml.allowAutoMaticSignalMastGeneration(false, sm2);
    // add a control sensor
    // Active
    sml.addSensor("IS1", 1, sm2);
    // check config
    Assert.assertEquals("IS1 included", true, sml.isSensorIncluded(is1, sm2));
    Assert.assertEquals("IS2 not included", false, sml.isSensorIncluded(is2, sm2));
    Assert.assertEquals("IS1 state", 1, sml.getSensorState(is1, sm2));
    // add 1 control turnout
    Hashtable<NamedBeanHandle<Turnout>, Integer> hashTurnouts = new Hashtable<NamedBeanHandle<Turnout>, Integer>();
    NamedBeanHandle<Turnout> namedTurnout1 = nbhm.getNamedBeanHandle("IT1", it1);
    // 1 = Closed
    hashTurnouts.put(namedTurnout1, 1);
    sml.setTurnouts(hashTurnouts, sm2);
    // check config
    Assert.assertTrue("IT1 included", sml.isTurnoutIncluded(it1, sm2));
    Assert.assertFalse("IT2 before", sml.isTurnoutIncluded(it2, sm2));
    // add another control turnout
    NamedBeanHandle<Turnout> namedTurnout2 = nbhm.getNamedBeanHandle("IT2", it2);
    // 2 = Thrown
    hashTurnouts.put(namedTurnout2, 2);
    sml.setTurnouts(hashTurnouts, sm2);
    Assert.assertEquals("IT2 after", true, sml.isTurnoutIncluded(it2, sm2));
    Assert.assertEquals("IT1 state", 1, sml.getTurnoutState(it1, sm2));
    // add a control signal mast
    Hashtable<SignalMast, String> hashSignalMast = new Hashtable<SignalMast, String>();
    hashSignalMast.put(sm3, "Stop");
    sml.setMasts(hashSignalMast, sm2);
    // check config
    Assert.assertEquals("SM3 included", true, sml.isSignalMastIncluded(sm3, sm2));
    Assert.assertEquals("SM3 aspect before", "Stop", sml.getSignalMastState(sm3, sm2));
    // set aspect to Proceed
    hashSignalMast.put(sm3, "Proceed");
    sml.setMasts(hashSignalMast, sm2);
    Assert.assertEquals("SM3 aspect after", "Proceed", sml.getSignalMastState(sm3, sm2));
    // set comment
    sml.setComment("SMLTest", sm2);
    Assert.assertEquals("comment", "SMLTest", sml.getComment(sm2));
    sml.initialise(sm2);
    // set SML disabled
    sml.setDisabled(sm2);
    Assert.assertEquals("disabled", false, sml.isEnabled(sm2));
    // change source mast and check
    sml.replaceSourceMast(sm1, sm3);
    Assert.assertFalse("sourcemast", sml.getSourceMast() == sm1);
    Assert.assertTrue("sourcemast", sml.getSourceMast() == sm3);
    // clean up
    sml.dispose();
}
Also used : Hashtable(java.util.Hashtable) SignalMastLogic(jmri.SignalMastLogic) SignalMast(jmri.SignalMast) Turnout(jmri.Turnout) NamedBeanHandle(jmri.NamedBeanHandle) Sensor(jmri.Sensor) Test(org.junit.Test)

Example 10 with SignalMastLogic

use of jmri.SignalMastLogic in project JMRI by JMRI.

the class LevelXing method addSignalMastLogic.

public void addSignalMastLogic(SignalMast sm) {
    if (sml.contains(sm)) {
        return;
    }
    if (sml.isEmpty()) {
        sml.add(sm);
        return;
    }
    SignalMastLogic sl = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).getSignalMastLogic(sm);
    for (int i = 0; i < sml.size(); i++) {
        SignalMastLogic s = InstanceManager.getDefault(jmri.SignalMastLogicManager.class).getSignalMastLogic(sml.get(i));
        if (s != null) {
            s.setConflictingLogic(sm, this);
        }
        sl.setConflictingLogic(sml.get(i), this);
    }
    sml.add(sm);
}
Also used : SignalMastLogic(jmri.SignalMastLogic)

Aggregations

SignalMastLogic (jmri.SignalMastLogic)12 SignalMast (jmri.SignalMast)7 DefaultSignalMastLogic (jmri.implementation.DefaultSignalMastLogic)6 Hashtable (java.util.Hashtable)4 NamedBean (jmri.NamedBean)4 ArrayList (java.util.ArrayList)3 NamedBeanHandle (jmri.NamedBeanHandle)3 Sensor (jmri.Sensor)3 SignalMastLogicManager (jmri.SignalMastLogicManager)3 Block (jmri.Block)2 JmriException (jmri.JmriException)2 Section (jmri.Section)2 Turnout (jmri.Turnout)2 Element (org.jdom2.Element)2 MouseEvent (java.awt.event.MouseEvent)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 JButton (javax.swing.JButton)1 JTable (javax.swing.JTable)1