Search in sources :

Example 51 with SignalHead

use of jmri.SignalHead in project JMRI by JMRI.

the class LsDecSignalHeadXml method load.

@Override
public boolean load(Element shared, Element perNode) {
    List<Element> l = shared.getChildren("turnout");
    NamedBeanHandle<Turnout> green = loadTurnout(l.get(0));
    NamedBeanHandle<Turnout> yellow = loadTurnout(l.get(1));
    NamedBeanHandle<Turnout> red = loadTurnout(l.get(2));
    NamedBeanHandle<Turnout> flashgreen = loadTurnout(l.get(3));
    NamedBeanHandle<Turnout> flashyellow = loadTurnout(l.get(4));
    NamedBeanHandle<Turnout> flashred = loadTurnout(l.get(5));
    NamedBeanHandle<Turnout> dark = loadTurnout(l.get(6));
    int greenstatus = loadTurnoutStatus(l.get(0));
    int yellowstatus = loadTurnoutStatus(l.get(1));
    int redstatus = loadTurnoutStatus(l.get(2));
    int flashgreenstatus = loadTurnoutStatus(l.get(3));
    int flashyellowstatus = loadTurnoutStatus(l.get(4));
    int flashredstatus = loadTurnoutStatus(l.get(5));
    int darkstatus = loadTurnoutStatus(l.get(6));
    // put it together
    String sys = getSystemName(shared);
    String uname = getUserName(shared);
    SignalHead h;
    if (uname == null) {
        h = new LsDecSignalHead(sys, green, greenstatus, yellow, yellowstatus, red, redstatus, flashgreen, flashgreenstatus, flashyellow, flashyellowstatus, flashred, flashredstatus, dark, darkstatus);
    } else {
        h = new LsDecSignalHead(sys, uname, green, greenstatus, yellow, yellowstatus, red, redstatus, flashgreen, flashgreenstatus, flashyellow, flashyellowstatus, flashred, flashredstatus, dark, darkstatus);
    }
    loadCommon(h, shared);
    InstanceManager.getDefault(jmri.SignalHeadManager.class).register(h);
    return true;
}
Also used : Element(org.jdom2.Element) SignalHead(jmri.SignalHead) LsDecSignalHead(jmri.implementation.LsDecSignalHead) Turnout(jmri.Turnout) LsDecSignalHead(jmri.implementation.LsDecSignalHead)

Example 52 with SignalHead

use of jmri.SignalHead in project JMRI by JMRI.

the class SignalHeadTableAction method createModel.

/**
     * Create the JTable DataModel, along with the changes for the specific case
     * of SignalHeads.
     */
@Override
protected void createModel() {
    m = new BeanTableDataModel() {

        public static final int LITCOL = NUMCOLUMN;

        public static final int HELDCOL = LITCOL + 1;

        public static final int EDITCOL = HELDCOL + 1;

        @Override
        public int getColumnCount() {
            return NUMCOLUMN + 3;
        }

        @Override
        public String getColumnName(int col) {
            if (col == VALUECOL) {
                // override default title, correct name SignalHeadAppearance i.e. "Red"
                return Bundle.getMessage("SignalMastAppearance");
            } else if (col == LITCOL) {
                return Bundle.getMessage("ColumnHeadLit");
            } else if (col == HELDCOL) {
                return Bundle.getMessage("ColumnHeadHeld");
            } else if (col == EDITCOL) {
                // no heading on "Edit"
                return "";
            } else {
                return super.getColumnName(col);
            }
        }

        @Override
        public Class<?> getColumnClass(int col) {
            if (col == VALUECOL) {
                // Use a JPanel containing a custom Appearance ComboBox
                return RowComboBoxPanel.class;
            } else if (col == LITCOL) {
                return Boolean.class;
            } else if (col == HELDCOL) {
                return Boolean.class;
            } else if (col == EDITCOL) {
                return JButton.class;
            } else {
                return super.getColumnClass(col);
            }
        }

        @Override
        public int getPreferredWidth(int col) {
            if (col == LITCOL) {
                return new JTextField(4).getPreferredSize().width;
            } else if (col == HELDCOL) {
                return new JTextField(4).getPreferredSize().width;
            } else if (col == EDITCOL) {
                return new JTextField(7).getPreferredSize().width;
            } else {
                return super.getPreferredWidth(col);
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == LITCOL) {
                return true;
            } else if (col == HELDCOL) {
                return true;
            } else if (col == EDITCOL) {
                return true;
            } else {
                return super.isCellEditable(row, col);
            }
        }

        @Override
        public Object getValueAt(int row, int col) {
            // some error checking
            if (row >= sysNameList.size()) {
                log.debug("row is greater than name list");
                return "error";
            }
            String name = sysNameList.get(row);
            SignalHead s = InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(name);
            if (s == null) {
                // if due to race condition, the device is going away
                return Boolean.valueOf(false);
            }
            if (col == LITCOL) {
                boolean val = s.getLit();
                return Boolean.valueOf(val);
            } else if (col == HELDCOL) {
                boolean val = s.getHeld();
                return Boolean.valueOf(val);
            } else if (col == EDITCOL) {
                return Bundle.getMessage("ButtonEdit");
            } else if (col == VALUECOL) {
                try {
                    return s.getAppearanceName();
                } catch (java.lang.NullPointerException e) {
                    //Appearance (head) not set
                    log.debug("Appearance for head {} not set", row);
                    // use place holder string in table
                    return Bundle.getMessage("BeanStateUnknown");
                }
            } else {
                return super.getValueAt(row, col);
            }
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            String name = sysNameList.get(row);
            SignalHead s = InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(name);
            if (s == null) {
                // device is going away anyway
                return;
            }
            if (col == VALUECOL) {
                if ((String) value != null) {
                    //row = table.convertRowIndexToModel(row); // find the right row in model instead of table (not needed here)
                    log.debug("SignalHead setValueAt (rowConverted={}; value={})", row, value);
                    // convert from String (selected item) to int
                    int newState = 99;
                    // Array of valid appearance names
                    String[] stateNameList = s.getValidStateNames();
                    // Array of valid appearance numbers
                    int[] validStateList = s.getValidStates();
                    for (int i = 0; i < stateNameList.length; i++) {
                        if (value.equals(stateNameList[i])) {
                            newState = validStateList[i];
                            break;
                        }
                    }
                    if (newState == 99) {
                        if (stateNameList.length == 0) {
                            newState = SignalHead.DARK;
                            log.warn("New signal state not found so setting to Dark " + s.getDisplayName());
                        } else {
                            newState = validStateList[0];
                            log.warn("New signal state not found so setting to the first available " + s.getDisplayName());
                        }
                    }
                    if (log.isDebugEnabled()) {
                        String oldAppearanceName = s.getAppearanceName();
                        log.debug("Signal Head set from: {} to: {} [{}]", oldAppearanceName, value, newState);
                    }
                    s.setAppearance(newState);
                    fireTableRowsUpdated(row, row);
                }
            } else if (col == LITCOL) {
                boolean b = ((Boolean) value).booleanValue();
                s.setLit(b);
            } else if (col == HELDCOL) {
                boolean b = ((Boolean) value).booleanValue();
                s.setHeld(b);
            } else if (col == EDITCOL) {
                // button clicked - edit
                editSignal(row);
            } else {
                super.setValueAt(value, row, col);
            }
        }

        @Override
        public String getValue(String name) {
            SignalHead s = InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(name);
            if (s == null) {
                // if due to race condition, the device is going away
                return "<lost>";
            }
            String val = null;
            try {
                val = s.getAppearanceName();
            } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                log.error(e.getLocalizedMessage(), e);
            }
            if (val != null) {
                return val;
            } else {
                return "Unexpected null value";
            }
        }

        @Override
        public Manager getManager() {
            return InstanceManager.getDefault(jmri.SignalHeadManager.class);
        }

        @Override
        public NamedBean getBySystemName(String name) {
            return InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(name);
        }

        @Override
        public NamedBean getByUserName(String name) {
            return InstanceManager.getDefault(jmri.SignalHeadManager.class).getByUserName(name);
        }

        /*public int getDisplayDeleteMsg() { return InstanceManager.getDefault(jmri.UserPreferencesManager.class).getMultipleChoiceOption(getClassName(),"delete"); }
             public void setDisplayDeleteMsg(int boo) { InstanceManager.getDefault(jmri.UserPreferencesManager.class).setMultipleChoiceOption(getClassName(), "delete", boo); }*/
        @Override
        protected String getMasterClassName() {
            return getClassName();
        }

        // no longer used since 4.7.1, but have to override
        @Deprecated
        @Override
        public void clickOn(NamedBean t) {
            int oldState = ((SignalHead) t).getAppearance();
            int newState = 99;
            // getValidAppearances((String)
            int[] stateList = ((SignalHead) t).getValidStates();
            for (int i = 0; i < stateList.length; i++) {
                if (oldState == stateList[i]) {
                    if (i < stateList.length - 1) {
                        newState = stateList[i + 1];
                        break;
                    } else {
                        newState = stateList[0];
                        break;
                    }
                }
            }
            if (newState == 99) {
                if (stateList.length == 0) {
                    newState = SignalHead.DARK;
                    log.warn("New signal state not found so setting to Dark " + t.getDisplayName());
                } else {
                    newState = stateList[0];
                    log.warn("New signal state not found so setting to the first available " + t.getDisplayName());
                }
            }
            log.debug("was " + oldState + " becomes " + newState);
            ((SignalHead) t).setAppearance(newState);
        }

        /**
             * Set column width.
             * @return a button to fit inside the VALUE column
             */
        @Override
        public JButton configureButton() {
            // pick a large size
            // about the longest Appearance string
            JButton b = new JButton(Bundle.getMessage("SignalHeadStateYellow"));
            b.putClientProperty("JComponent.sizeVariant", "small");
            b.putClientProperty("JButton.buttonType", "square");
            return b;
        }

        @Override
        public boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            if (e.getPropertyName().indexOf("Lit") >= 0 || e.getPropertyName().indexOf("Held") >= 0 || e.getPropertyName().indexOf("ValidStatesChanged") >= 0) {
                return true;
            } else {
                return super.matchPropertyName(e);
            }
        }

        @Override
        protected String getBeanType() {
            return Bundle.getMessage("BeanNameSignalHead");
        }

        /**
             * Respond to change from bean. Prevent Appearance change when Signal Head is set to Hold or Unlit.
             * @param e A property change of any bean
             */
        @Override
        public // At present, does not work/change when head Lit/Held checkboxes are (de)activated
        void propertyChange(java.beans.PropertyChangeEvent e) {
            if (e.getPropertyName().indexOf("Lit") < 0 || e.getPropertyName().indexOf("Held") >= 0 || e.getPropertyName().indexOf("ValidStatesChanged") >= 0) {
                if (e.getSource() instanceof NamedBean) {
                    String name = ((NamedBean) e.getSource()).getSystemName();
                    if (log.isDebugEnabled()) {
                        log.debug("Update cell {}, {} for {}", sysNameList.indexOf(name), VALUECOL, name);
                    }
                    // since we can add columns, the entire row is marked as updated
                    int row = sysNameList.indexOf(name);
                    this.fireTableRowsUpdated(row, row);
                    // activate this method below
                    clearAppearanceVector(row);
                }
            }
            super.propertyChange(e);
        }

        /**
             * Customize the SignalHead Value (Appearance) column to show an appropriate ComboBox of available Appearances
             * when the TableDataModel is being called from ListedTableAction.
             * @param table a JTable of Signal Head
             */
        @Override
        protected void configValueColumn(JTable table) {
            // have the value column hold a JPanel with a JComboBox for Appearances
            setColumnToHoldButton(table, VALUECOL, configureButton());
            // add extras, override BeanTableDataModel
            log.debug("Head configValueColumn (I am {})", super.toString());
            table.setDefaultEditor(RowComboBoxPanel.class, new AppearanceComboBoxPanel());
            // use same class for the renderer
            table.setDefaultRenderer(RowComboBoxPanel.class, new AppearanceComboBoxPanel());
        // Set more things?
        }

        /**
             * A row specific Appearance combobox cell editor/renderer.
             */
        class AppearanceComboBoxPanel extends RowComboBoxPanel {

            @Override
            protected final void eventEditorMousePressed() {
                // add editorBox to JPanel
                this.editor.add(getEditorBox(table.convertRowIndexToModel(this.currentRow)));
                this.editor.revalidate();
                SwingUtilities.invokeLater(this.comboBoxFocusRequester);
                log.debug("eventEditorMousePressed in row: {})", this.currentRow);
            }

            /**
                 * Call the method in the surrounding method for the SignalHeadTable.
                 * @param row the user clicked on in the table
                 * @return an appropriate combobox for this signal head
                 */
            @Override
            protected JComboBox getEditorBox(int row) {
                return getAppearanceEditorBox(row);
            }
        }

        // Methods to display VALUECOL (appearance) ComboBox in the Signal Head Table
        // Derived from the SignalMastJTable class (deprecated since 4.5.5):
        // All row values are in terms of the Model, not the Table as displayed.
        /**
             * Clear the old appearance comboboxes and force them to be rebuilt.
             * Used with the Single Output Signal Head to capture reconguration.
             * @param row Index of the signal mast (in TableDataModel) to be rebuilt in the Hashtables
             */
        public void clearAppearanceVector(int row) {
            boxMap.remove(this.getValueAt(row, SYSNAMECOL));
            editorMap.remove(this.getValueAt(row, SYSNAMECOL));
        }

        // Hashtables for Editors; not used for Renderer)
        /**
             * Provide a JComboBox element to display inside the JPanel CellEditor.
             * When not yet present, create, store and return a new one.
             * @param row Index number (in TableDataModel)
             * @return A combobox containing the valid appearance names for this mast
             */
        public JComboBox getAppearanceEditorBox(int row) {
            JComboBox editCombo = editorMap.get(this.getValueAt(row, SYSNAMECOL));
            if (editCombo == null) {
                // create a new one with correct appearances
                editCombo = new JComboBox<String>(getRowVector(row));
                editorMap.put(this.getValueAt(row, SYSNAMECOL), editCombo);
            }
            return editCombo;
        }

        Hashtable<Object, JComboBox> editorMap = new Hashtable<Object, JComboBox>();

        /**
             * returns a list of all the valid appearances that have not been disabled
             * @param head the name of the signal head
             * @return List of valid signal head appearance names
             */
        public Vector<String> getValidAppearances(String head) {
            // convert String[] validStateNames to Vector
            String[] app = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(head).getValidStateNames();
            Vector<String> v = new Vector<String>();
            for (int i = 0; i < app.length; i++) {
                String appearance = app[i];
                v.add(appearance);
            }
            return v;
        }

        /**
             * Holds a Hashtable of valid appearances per signal head,
             * used by getEditorBox()
             * @param row Index number (in TableDataModel)
             * @return The Vector of valid appearance names for this mast to show in the JComboBox
             */
        Vector<String> getRowVector(int row) {
            Vector<String> comboappearances = boxMap.get(this.getValueAt(row, SYSNAMECOL));
            if (comboappearances == null) {
                // create a new one with right appearance
                Vector<String> v = getValidAppearances((String) this.getValueAt(row, SYSNAMECOL));
                comboappearances = v;
                boxMap.put(this.getValueAt(row, SYSNAMECOL), comboappearances);
            }
            return comboappearances;
        }

        Hashtable<Object, Vector<String>> boxMap = new Hashtable<Object, Vector<String>>();
    };
}
Also used : NamedBean(jmri.NamedBean) JButton(javax.swing.JButton) QuadOutputSignalHead(jmri.implementation.QuadOutputSignalHead) DccSignalHead(jmri.implementation.DccSignalHead) DoubleTurnoutSignalHead(jmri.implementation.DoubleTurnoutSignalHead) TripleTurnoutSignalHead(jmri.implementation.TripleTurnoutSignalHead) SignalHead(jmri.SignalHead) SingleTurnoutSignalHead(jmri.implementation.SingleTurnoutSignalHead) TripleOutputSignalHead(jmri.implementation.TripleOutputSignalHead) JTextField(javax.swing.JTextField) InstanceManager(jmri.InstanceManager) Manager(jmri.Manager) Vector(java.util.Vector) JComboBox(javax.swing.JComboBox) Hashtable(java.util.Hashtable) JTable(javax.swing.JTable)

Example 53 with SignalHead

use of jmri.SignalHead in project JMRI by JMRI.

the class SignalHeadTableAction method okPressed.

// @TODO We could add a check to make sure that the user has entered a turnout into a turnout field if it has been presented.
// Done for: Acela
// For now only an error is recorded in the Console window
void okPressed(ActionEvent e) {
    if (!checkUserName(userName.getText())) {
        return;
    }
    SignalHead s;
    try {
        if (se8c4Aspect.equals(typeBox.getSelectedItem())) {
            handleSE8cOkPressed();
        } else if (acelaAspect.equals(typeBox.getSelectedItem())) {
            String inputusername = userName.getText();
            String inputsysname = ato1.getText().toUpperCase();
            int headnumber;
            if (inputsysname.length() == 0) {
                JOptionPane.showMessageDialog(addFrame, Bundle.getMessage("acelaWarning"));
                log.warn("must supply a signalhead number (i.e. AH23)");
                return;
            }
            if (inputsysname.length() > 2) {
                if (inputsysname.substring(0, 2).equals("AH")) {
                    headnumber = Integer.parseInt(inputsysname.substring(2, inputsysname.length()));
                } else if (checkIntegerOnly(inputsysname)) {
                    headnumber = Integer.parseInt(inputsysname);
                } else {
                    String msg = Bundle.getMessage("acelaSkippingCreation", new Object[] { ato1.getText() });
                    JOptionPane.showMessageDialog(addFrame, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
                    return;
                }
            } else {
                headnumber = Integer.parseInt(inputsysname);
            }
            if (checkBeforeCreating("AH" + headnumber)) {
                //if (jmri.jmrix.acela.status()) { // check for an active Acela connection status
                try {
                    if (inputusername.length() == 0) {
                        s = new jmri.jmrix.acela.AcelaSignalHead("AH" + headnumber, jmri.InstanceManager.getDefault(jmri.jmrix.acela.AcelaSystemConnectionMemo.class));
                    } else {
                        s = new jmri.jmrix.acela.AcelaSignalHead("AH" + headnumber, inputusername, jmri.InstanceManager.getDefault(jmri.jmrix.acela.AcelaSystemConnectionMemo.class));
                    }
                    InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
                } catch (java.lang.NullPointerException ex) {
                    JOptionPane.showMessageDialog(addFrame, Bundle.getMessage("SystemNotActiveWarning", "Acela"));
                    log.warn("No active Acela connection to create Signal Head");
                    return;
                }
            }
            int st = signalheadTypeFromBox(stBox);
            // This bit returns null I think, will need to check though
            AcelaNode sh = AcelaAddress.getNodeFromSystemName("AH" + headnumber, jmri.InstanceManager.getDefault(jmri.jmrix.acela.AcelaSystemConnectionMemo.class));
            switch(st) {
                case 1:
                    sh.setOutputSignalHeadType(headnumber, AcelaNode.DOUBLE);
                    break;
                case 2:
                    sh.setOutputSignalHeadType(headnumber, AcelaNode.TRIPLE);
                    break;
                case 3:
                    sh.setOutputSignalHeadType(headnumber, AcelaNode.BPOLAR);
                    break;
                case 4:
                    sh.setOutputSignalHeadType(headnumber, AcelaNode.WIGWAG);
                    break;
                default:
                    log.warn("Unexpected Acela Aspect type: " + st);
                    sh.setOutputSignalHeadType(headnumber, AcelaNode.UKNOWN);
                    // default to triple
                    break;
            }
        } else if (grapevine.equals(typeBox.getSelectedItem())) {
            // the turnout field must hold a GH system name
            if (systemName.getText().length() == 0) {
                // TODO Add user dialog
                log.warn("must supply a signalhead number (i.e. GH23)");
                return;
            }
            String inputsysname = systemName.getText().toUpperCase();
            if (!inputsysname.substring(0, 2).equals("GH")) {
                log.warn("skipping creation of signal, " + inputsysname + " does not start with GH");
                String msg = Bundle.getMessage("GrapevineSkippingCreation", new Object[] { inputsysname });
                JOptionPane.showMessageDialog(addFrame, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (checkBeforeCreating(inputsysname)) {
                s = new jmri.jmrix.grapevine.SerialSignalHead(inputsysname, userName.getText());
                InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
            }
        } else if (quadOutput.equals(typeBox.getSelectedItem())) {
            if (checkBeforeCreating(systemName.getText())) {
                Turnout t1 = getTurnoutFromPanel(to1, "SignalHead:" + systemName.getText() + ":Green");
                Turnout t2 = getTurnoutFromPanel(to2, "SignalHead:" + systemName.getText() + ":Yellow");
                Turnout t3 = getTurnoutFromPanel(to3, "SignalHead:" + systemName.getText() + ":Red");
                Turnout t4 = getTurnoutFromPanel(to4, "SignalHead:" + systemName.getText() + ":Lunar");
                if (t1 == null) {
                    addTurnoutMessage(v1Border.getTitle(), to1.getDisplayName());
                }
                if (t2 == null) {
                    addTurnoutMessage(v2Border.getTitle(), to2.getDisplayName());
                }
                if (t3 == null) {
                    addTurnoutMessage(v3Border.getTitle(), to3.getDisplayName());
                }
                if (t4 == null) {
                    addTurnoutMessage(v4Border.getTitle(), to4.getDisplayName());
                }
                if (t4 == null || t3 == null || t2 == null || t1 == null) {
                    log.warn("skipping creation of signal " + systemName.getText() + " due to error");
                    return;
                }
                s = new jmri.implementation.QuadOutputSignalHead(systemName.getText(), userName.getText(), nbhm.getNamedBeanHandle(to1.getDisplayName(), t1), nbhm.getNamedBeanHandle(to2.getDisplayName(), t2), nbhm.getNamedBeanHandle(to3.getDisplayName(), t3), nbhm.getNamedBeanHandle(to4.getDisplayName(), t4));
                InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
            }
        } else if (tripleTurnout.equals(typeBox.getSelectedItem())) {
            if (checkBeforeCreating(systemName.getText())) {
                Turnout t1 = getTurnoutFromPanel(to1, "SignalHead:" + systemName.getText() + ":Green");
                Turnout t2 = getTurnoutFromPanel(to2, "SignalHead:" + systemName.getText() + ":Yellow");
                Turnout t3 = getTurnoutFromPanel(to3, "SignalHead:" + systemName.getText() + ":Red");
                if (t1 == null) {
                    addTurnoutMessage(v1Border.getTitle(), to1.getDisplayName());
                }
                if (t2 == null) {
                    addTurnoutMessage(v2Border.getTitle(), to2.getDisplayName());
                }
                if (t3 == null) {
                    addTurnoutMessage(v3Border.getTitle(), to3.getDisplayName());
                }
                if (t3 == null || t2 == null || t1 == null) {
                    log.warn("skipping creation of signal " + systemName.getText() + " due to error");
                    return;
                }
                s = new jmri.implementation.TripleTurnoutSignalHead(systemName.getText(), userName.getText(), nbhm.getNamedBeanHandle(to1.getDisplayName(), t1), nbhm.getNamedBeanHandle(to2.getDisplayName(), t2), nbhm.getNamedBeanHandle(to3.getDisplayName(), t3));
                InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
            }
        } else if (tripleOutput.equals(typeBox.getSelectedItem())) {
            if (checkBeforeCreating(systemName.getText())) {
                Turnout t1 = getTurnoutFromPanel(to1, "SignalHead:" + systemName.getText() + ":Green");
                Turnout t2 = getTurnoutFromPanel(to2, "SignalHead:" + systemName.getText() + ":Blue");
                Turnout t3 = getTurnoutFromPanel(to3, "SignalHead:" + systemName.getText() + ":Red");
                if (t1 == null) {
                    addTurnoutMessage(v1Border.getTitle(), to1.getDisplayName());
                }
                if (t2 == null) {
                    addTurnoutMessage(v2Border.getTitle(), to2.getDisplayName());
                }
                if (t3 == null) {
                    addTurnoutMessage(v3Border.getTitle(), to3.getDisplayName());
                }
                if (t3 == null || t2 == null || t1 == null) {
                    log.warn("skipping creation of signal " + systemName.getText() + " due to error");
                    return;
                }
                s = new jmri.implementation.TripleOutputSignalHead(systemName.getText(), userName.getText(), nbhm.getNamedBeanHandle(to1.getDisplayName(), t1), nbhm.getNamedBeanHandle(to2.getDisplayName(), t2), nbhm.getNamedBeanHandle(to3.getDisplayName(), t3));
                InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
            }
        } else if (doubleTurnout.equals(typeBox.getSelectedItem())) {
            if (checkBeforeCreating(systemName.getText())) {
                Turnout t1 = getTurnoutFromPanel(to1, "SignalHead:" + systemName.getText() + ":Green");
                Turnout t2 = getTurnoutFromPanel(to2, "SignalHead:" + systemName.getText() + ":Red");
                if (t1 == null) {
                    addTurnoutMessage(v1Border.getTitle(), to1.getDisplayName());
                }
                if (t2 == null) {
                    addTurnoutMessage(v2Border.getTitle(), to2.getDisplayName());
                }
                if (t2 == null || t1 == null) {
                    log.warn("skipping creation of signal " + systemName.getText() + " due to error");
                    return;
                }
                s = new jmri.implementation.DoubleTurnoutSignalHead(systemName.getText(), userName.getText(), nbhm.getNamedBeanHandle(to1.getDisplayName(), t1), nbhm.getNamedBeanHandle(to2.getDisplayName(), t2));
                s.setUserName(userName.getText());
                InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
            }
        } else if (singleTurnout.equals(typeBox.getSelectedItem())) {
            if (checkBeforeCreating(systemName.getText())) {
                Turnout t1 = getTurnoutFromPanel(to1, "SignalHead:" + systemName.getText() + ":" + (String) s2aBox.getSelectedItem() + ":" + (String) s3aBox.getSelectedItem());
                int on = signalStateFromBox(s2aBox);
                int off = signalStateFromBox(s3aBox);
                if (t1 == null) {
                    addTurnoutMessage(v1Border.getTitle(), to1.getDisplayName());
                }
                if (t1 == null) {
                    log.warn("skipping creation of signal " + systemName.getText() + " due to error");
                    return;
                }
                s = new jmri.implementation.SingleTurnoutSignalHead(systemName.getText(), userName.getText(), nbhm.getNamedBeanHandle(t1.getDisplayName(), t1), on, off);
                InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
            }
        } else if (virtualHead.equals(typeBox.getSelectedItem())) {
            if (checkBeforeCreating(systemName.getText())) {
                s = new jmri.implementation.VirtualSignalHead(systemName.getText(), userName.getText());
                InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
            }
        } else if (lsDec.equals(typeBox.getSelectedItem())) {
            // LDT LS-DEC
            if (checkBeforeCreating(systemName.getText())) {
                Turnout t1 = getTurnoutFromPanel(to1, "SignalHead:" + systemName.getText() + ":Green");
                Turnout t2 = getTurnoutFromPanel(to2, "SignalHead:" + systemName.getText() + ":Yellow");
                Turnout t3 = getTurnoutFromPanel(to3, "SignalHead:" + systemName.getText() + ":Red");
                Turnout t4 = getTurnoutFromPanel(to4, "SignalHead:" + systemName.getText() + ":FlashGreen");
                Turnout t5 = getTurnoutFromPanel(to5, "SignalHead:" + systemName.getText() + ":FlashYellow");
                Turnout t6 = getTurnoutFromPanel(to6, "SignalHead:" + systemName.getText() + ":FlashRed");
                Turnout t7 = getTurnoutFromPanel(to7, "SignalHead:" + systemName.getText() + ":Dark");
                int s1 = turnoutStateFromBox(s1Box);
                int s2 = turnoutStateFromBox(s2Box);
                int s3 = turnoutStateFromBox(s3Box);
                int s4 = turnoutStateFromBox(s4Box);
                int s5 = turnoutStateFromBox(s5Box);
                int s6 = turnoutStateFromBox(s6Box);
                int s7 = turnoutStateFromBox(s7Box);
                if (t1 == null) {
                    addTurnoutMessage(v1Border.getTitle(), to1.getDisplayName());
                }
                if (t2 == null) {
                    addTurnoutMessage(v2Border.getTitle(), to2.getDisplayName());
                }
                if (t3 == null) {
                    addTurnoutMessage(v3Border.getTitle(), to3.getDisplayName());
                }
                if (t4 == null) {
                    addTurnoutMessage(v4Border.getTitle(), to4.getDisplayName());
                }
                if (t5 == null) {
                    addTurnoutMessage(v5Border.getTitle(), to5.getDisplayName());
                }
                if (t6 == null) {
                    addTurnoutMessage(v6Border.getTitle(), to6.getDisplayName());
                }
                if (t7 == null) {
                    addTurnoutMessage(v7Border.getTitle(), to7.getDisplayName());
                }
                if (t7 == null || t6 == null || t5 == null || t4 == null || t3 == null || t2 == null || t1 == null) {
                    log.warn("skipping creation of signal " + systemName.getText() + " due to error");
                    return;
                }
                s = new jmri.implementation.LsDecSignalHead(systemName.getText(), nbhm.getNamedBeanHandle(t1.getDisplayName(), t1), s1, nbhm.getNamedBeanHandle(t2.getDisplayName(), t2), s2, nbhm.getNamedBeanHandle(t3.getDisplayName(), t3), s3, nbhm.getNamedBeanHandle(t4.getDisplayName(), t4), s4, nbhm.getNamedBeanHandle(t5.getDisplayName(), t5), s5, nbhm.getNamedBeanHandle(t6.getDisplayName(), t6), s6, nbhm.getNamedBeanHandle(t7.getDisplayName(), t7), s7);
                s.setUserName(userName.getText());
                InstanceManager.getDefault(jmri.SignalHeadManager.class).register(s);
            }
        } else if (dccSignalDecoder.equals(typeBox.getSelectedItem())) {
            handleDCCOkPressed();
        } else if (mergSignalDriver.equals(typeBox.getSelectedItem())) {
            handleMergSignalDriverOkPressed();
        } else {
            log.error("Unexpected type: " + typeBox.getSelectedItem());
        }
    } catch (NumberFormatException ex) {
        handleCreateException(ex, systemName.getText());
        // without creating
        return;
    }
}
Also used : TripleTurnoutSignalHead(jmri.implementation.TripleTurnoutSignalHead) QuadOutputSignalHead(jmri.implementation.QuadOutputSignalHead) DccSignalHead(jmri.implementation.DccSignalHead) DoubleTurnoutSignalHead(jmri.implementation.DoubleTurnoutSignalHead) TripleTurnoutSignalHead(jmri.implementation.TripleTurnoutSignalHead) SignalHead(jmri.SignalHead) SingleTurnoutSignalHead(jmri.implementation.SingleTurnoutSignalHead) TripleOutputSignalHead(jmri.implementation.TripleOutputSignalHead) AcelaNode(jmri.jmrix.acela.AcelaNode) TripleOutputSignalHead(jmri.implementation.TripleOutputSignalHead) SingleTurnoutSignalHead(jmri.implementation.SingleTurnoutSignalHead) QuadOutputSignalHead(jmri.implementation.QuadOutputSignalHead) DoubleTurnoutSignalHead(jmri.implementation.DoubleTurnoutSignalHead) Turnout(jmri.Turnout)

Example 54 with SignalHead

use of jmri.SignalHead in project JMRI by JMRI.

the class JsonSignalHeadHttpService method doPost.

@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
    SignalHead signalHead = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(name);
    this.postNamedBean(signalHead, data, name, type, locale);
    if (signalHead != null) {
        if (data.path(STATE).isIntegralNumber()) {
            int state = data.path(STATE).asInt();
            if (state == SignalHead.HELD) {
                signalHead.setHeld(true);
            } else {
                boolean isValid = false;
                for (int validState : signalHead.getValidStates()) {
                    if (state == validState) {
                        isValid = true;
                        // TODO: completely insulate JSON state from SignalHead state
                        if (signalHead.getHeld())
                            signalHead.setHeld(false);
                        signalHead.setAppearance(state);
                        break;
                    }
                }
                if (!isValid) {
                    throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", SIGNAL_HEAD, state));
                }
            }
        }
    }
    return this.doGet(type, name, locale);
}
Also used : JsonException(jmri.server.json.JsonException) SignalHeadManager(jmri.SignalHeadManager) SignalHead(jmri.SignalHead)

Example 55 with SignalHead

use of jmri.SignalHead in project JMRI by JMRI.

the class JsonSignalHeadHttpService method doGet.

@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, SIGNAL_HEAD);
    SignalHead signalHead = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(name);
    ObjectNode data = this.getNamedBean(signalHead, name, type, locale);
    root.put(DATA, data);
    if (signalHead != null) {
        data.put(LIT, signalHead.getLit());
        data.put(APPEARANCE, signalHead.getAppearance());
        data.put(TOKEN_HELD, signalHead.getHeld());
        //state is appearance, plus a flag for held status
        if (signalHead.getHeld()) {
            data.put(STATE, SignalHead.HELD);
        } else {
            data.put(STATE, signalHead.getAppearance());
        }
        data.put(APPEARANCE_NAME, signalHead.getAppearanceName());
    }
    return root;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SignalHeadManager(jmri.SignalHeadManager) SignalHead(jmri.SignalHead)

Aggregations

SignalHead (jmri.SignalHead)73 Turnout (jmri.Turnout)20 Element (org.jdom2.Element)13 SignalMast (jmri.SignalMast)12 Sensor (jmri.Sensor)11 JsonException (jmri.server.json.JsonException)8 Test (org.junit.Test)7 NamedBean (jmri.NamedBean)6 DoubleTurnoutSignalHead (jmri.implementation.DoubleTurnoutSignalHead)6 QuadOutputSignalHead (jmri.implementation.QuadOutputSignalHead)6 SingleTurnoutSignalHead (jmri.implementation.SingleTurnoutSignalHead)6 TripleOutputSignalHead (jmri.implementation.TripleOutputSignalHead)6 TripleTurnoutSignalHead (jmri.implementation.TripleTurnoutSignalHead)6 SignalHeadManager (jmri.SignalHeadManager)5 DccSignalHead (jmri.implementation.DccSignalHead)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 JmriException (jmri.JmriException)4 Light (jmri.Light)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 ActionEvent (java.awt.event.ActionEvent)3