Search in sources :

Example 1 with TurnoutOperation

use of jmri.TurnoutOperation in project JMRI by JMRI.

the class AbstractTurnoutManagerConfigXML method loadTurnouts.

/**
     * Utility method to load the individual Turnout objects. If there's no
     * additional info needed for a specific turnout type, invoke this with the
     * parent of the set of Turnout elements.
     *
     * @param shared Element containing the Turnout elements to load.
     * @param perNode Element containing per-node Turnout data.
     * @return true if succeeded
     */
@SuppressWarnings("unchecked")
public boolean loadTurnouts(Element shared, Element perNode) {
    boolean result = true;
    List<Element> operationList = shared.getChildren("operations");
    if (operationList.size() > 1) {
        log.warn("unexpected extra elements found in turnout operations list");
        result = false;
    }
    if (operationList.size() > 0) {
        TurnoutOperationManagerXml tomx = new TurnoutOperationManagerXml();
        tomx.load(operationList.get(0), null);
    }
    List<Element> turnoutList = shared.getChildren("turnout");
    if (log.isDebugEnabled()) {
        log.debug("Found " + turnoutList.size() + " turnouts");
    }
    TurnoutManager tm = InstanceManager.turnoutManagerInstance();
    try {
        if (shared.getChild("defaultclosedspeed") != null) {
            String closedSpeed = shared.getChild("defaultclosedspeed").getText();
            if (closedSpeed != null && !closedSpeed.equals("")) {
                tm.setDefaultClosedSpeed(closedSpeed);
            }
        }
    } catch (jmri.JmriException ex) {
        log.error(ex.toString());
    }
    try {
        if (shared.getChild("defaultthrownspeed") != null) {
            String thrownSpeed = shared.getChild("defaultthrownspeed").getText();
            if (thrownSpeed != null && !thrownSpeed.equals("")) {
                tm.setDefaultThrownSpeed(thrownSpeed);
            }
        }
    } catch (jmri.JmriException ex) {
        log.error(ex.toString());
    }
    for (int i = 0; i < turnoutList.size(); i++) {
        Element elem = turnoutList.get(i);
        String sysName = getSystemName(elem);
        if (sysName == null) {
            log.error("unexpected null in systemName " + elem);
            result = false;
            break;
        }
        String userName = getUserName(elem);
        checkNameNormalization(sysName, userName, tm);
        if (log.isDebugEnabled()) {
            log.debug("create turnout: (" + sysName + ")(" + (userName == null ? "<null>" : userName) + ")");
        }
        Turnout t = tm.getBySystemName(sysName);
        if (t == null) {
            t = tm.newTurnout(sysName, userName);
        //Nothing is logged in the console window as the newTurnoutFunction already does this.
        } else if (userName != null) {
            t.setUserName(userName);
        }
        // Load common parts
        loadCommon(t, elem);
        // now add feedback if needed
        Attribute a;
        a = elem.getAttribute("feedback");
        if (a != null) {
            try {
                t.setFeedbackMode(a.getValue());
            } catch (IllegalArgumentException e) {
                log.error("Can not set feedback mode: '" + a.getValue() + "' for turnout: '" + sysName + "' user name: '" + (userName == null ? "" : userName) + "'");
                result = false;
            }
        }
        a = elem.getAttribute("sensor1");
        if (a != null) {
            try {
                t.provideFirstFeedbackSensor(a.getValue());
            } catch (jmri.JmriException e) {
                result = false;
            }
        }
        a = elem.getAttribute("sensor2");
        if (a != null) {
            try {
                t.provideSecondFeedbackSensor(a.getValue());
            } catch (jmri.JmriException e) {
                result = false;
            }
        }
        // check for turnout inverted
        t.setInverted(getAttributeBool(elem, "inverted", false));
        // check for turnout decoder
        a = turnoutList.get(i).getAttribute("decoder");
        if (a != null) {
            t.setDecoderName(a.getValue());
        }
        // check for turnout lock mode
        a = turnoutList.get(i).getAttribute("lockMode");
        if (a != null) {
            if (a.getValue().equals("both")) {
                t.enableLockOperation(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, true);
            }
            if (a.getValue().equals("cab")) {
                t.enableLockOperation(Turnout.CABLOCKOUT, true);
                t.enableLockOperation(Turnout.PUSHBUTTONLOCKOUT, false);
            }
            if (a.getValue().equals("pushbutton")) {
                t.enableLockOperation(Turnout.PUSHBUTTONLOCKOUT, true);
                t.enableLockOperation(Turnout.CABLOCKOUT, false);
            }
        }
        // check for turnout locked
        a = turnoutList.get(i).getAttribute("locked");
        if (a != null) {
            t.setLocked(Turnout.CABLOCKOUT + Turnout.PUSHBUTTONLOCKOUT, a.getValue().equals("true"));
        }
        // number of bits, if present - if not, defaults to 1
        a = turnoutList.get(i).getAttribute("numBits");
        if (a == null) {
            t.setNumberOutputBits(1);
        } else {
            int iNum = Integer.parseInt(a.getValue());
            if ((iNum == 1) || (iNum == 2)) {
                t.setNumberOutputBits(iNum);
            } else {
                log.warn("illegal number of output bits for control of turnout " + sysName);
                t.setNumberOutputBits(1);
                result = false;
            }
        }
        // control type, if present - if not, defaults to 0
        a = turnoutList.get(i).getAttribute("controlType");
        if (a == null) {
            t.setControlType(0);
        } else {
            int iType = Integer.parseInt(a.getValue());
            if (iType >= 0) {
                t.setControlType(iType);
            } else {
                log.warn("illegal control type for control of turnout " + sysName);
                t.setControlType(0);
                result = false;
            }
        }
        // operation stuff
        List<Element> myOpList = turnoutList.get(i).getChildren("operation");
        if (myOpList.size() > 0) {
            if (myOpList.size() > 1) {
                log.warn("unexpected extra elements found in turnout-specific operations");
                result = false;
            }
            TurnoutOperation toper = TurnoutOperationXml.loadOperation(myOpList.get(0));
            t.setTurnoutOperation(toper);
        } else {
            a = turnoutList.get(i).getAttribute("automate");
            if (a != null) {
                String str = a.getValue();
                if (str.equals("Off")) {
                    t.setInhibitOperation(true);
                } else if (!str.equals("Default")) {
                    t.setInhibitOperation(false);
                    TurnoutOperation toper = TurnoutOperationManager.getInstance().getOperation(str);
                    t.setTurnoutOperation(toper);
                } else {
                    t.setInhibitOperation(false);
                }
            }
        }
        //  set initial state from sensor feedback if appropriate
        t.setInitialKnownStateFromFeedback();
        try {
            t.setDivergingSpeed("Global");
            if (elem.getChild("divergingSpeed") != null) {
                String speed = elem.getChild("divergingSpeed").getText();
                if (speed != null && !speed.equals("") && !speed.contains("Global")) {
                    t.setDivergingSpeed(speed);
                }
            }
        } catch (jmri.JmriException ex) {
            log.error(ex.toString());
        }
        try {
            t.setStraightSpeed("Global");
            if (elem.getChild("straightSpeed") != null) {
                String speed = elem.getChild("straightSpeed").getText();
                if (speed != null && !speed.equals("") && !speed.contains("Global")) {
                    t.setStraightSpeed(speed);
                }
            }
        } catch (jmri.JmriException ex) {
            log.error(ex.toString());
        }
    }
    return result;
}
Also used : TurnoutOperation(jmri.TurnoutOperation) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) TurnoutManager(jmri.TurnoutManager) TurnoutOperationManagerXml(jmri.configurexml.TurnoutOperationManagerXml) Turnout(jmri.Turnout)

Example 2 with TurnoutOperation

use of jmri.TurnoutOperation in project JMRI by JMRI.

the class TurnoutTableAction method editTurnoutOperation.

/**
     * Pop up a TurnoutOperationConfig for the turnout.
     *
     * @param t   turnout
     * @param box JComboBox that triggered the edit
     */
protected void editTurnoutOperation(Turnout t, JComboBox<String> box) {
    if (!editingOps) {
        // don't open a second edit ops pane
        editingOps = true;
        TurnoutOperation op = t.getTurnoutOperation();
        if (op == null) {
            TurnoutOperation proto = TurnoutOperationManager.getInstance().getMatchingOperationAlways(t);
            if (proto != null) {
                op = proto.makeNonce(t);
                t.setTurnoutOperation(op);
            }
        }
        if (op != null) {
            if (!op.isNonce()) {
                op = op.makeNonce(t);
            }
            // make and show edit dialog
            log.debug("TurnoutOpsEditDialog starting");
            TurnoutOperationEditor dialog = new TurnoutOperationEditor(this, f, op, t, box);
            dialog.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(f, "There is no operation type suitable for this turnout", "No operation type", JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : TurnoutOperation(jmri.TurnoutOperation)

Example 3 with TurnoutOperation

use of jmri.TurnoutOperation in project JMRI by JMRI.

the class TurnoutOperationXml method loadOperation.

/**
     * Load one operation, using the appropriate adapter
     *
     * @param e element for operation
     * @return the loaded TurnoutOperation or null if unable to load from e
     */
public static TurnoutOperation loadOperation(Element e) {
    TurnoutOperation result = null;
    String className = e.getAttributeValue("class");
    if (className == null) {
        log.error("class name missing in turnout operation \"" + e + "\"");
    } else {
        log.debug("loadOperation for class {}", className);
        try {
            Class<?> adapterClass = Class.forName(className);
            TurnoutOperationXml adapter = (TurnoutOperationXml) adapterClass.newInstance();
            result = adapter.loadOne(e);
            if (result.getName().charAt(0) == '*') {
                result.setNonce(true);
            }
        } catch (ClassNotFoundException | InstantiationException e1) {
            log.error("while creating TurnoutOperation", e1);
            return null;
        } catch (IllegalAccessException e2) {
            log.error("while creating CommonTurnoutOperation", e2);
            return null;
        }
    }
    return result;
}
Also used : TurnoutOperation(jmri.TurnoutOperation)

Example 4 with TurnoutOperation

use of jmri.TurnoutOperation in project JMRI by JMRI.

the class TurnoutOperationXml method store.

/**
     * common part of store - create the element and store the name and the
     * class
     *
     * @param o TurnoutOperation object
     * @return partially filled element
     */
@Override
public Element store(Object o) {
    TurnoutOperation myOp = (TurnoutOperation) o;
    Element elem = new Element("operation");
    elem.setAttribute("name", myOp.getName());
    elem.setAttribute("class", this.getClass().getName());
    return elem;
}
Also used : TurnoutOperation(jmri.TurnoutOperation) Element(org.jdom2.Element)

Example 5 with TurnoutOperation

use of jmri.TurnoutOperation in project JMRI by JMRI.

the class AbstractTurnout method setTurnoutOperation.

@Override
public void setTurnoutOperation(TurnoutOperation toper) {
    if (log.isDebugEnabled()) {
        log.debug("setTurnoutOperation Called for turnout {}.  Operation type {}", this.getSystemName(), toper);
    }
    TurnoutOperation oldOp = myTurnoutOperation;
    if (myTurnoutOperation != null) {
        myTurnoutOperation.removePropertyChangeListener(this);
    }
    myTurnoutOperation = toper;
    if (myTurnoutOperation != null) {
        myTurnoutOperation.addPropertyChangeListener(this);
    }
    firePropertyChange("TurnoutOperationState", oldOp, myTurnoutOperation);
}
Also used : TurnoutOperation(jmri.TurnoutOperation)

Aggregations

TurnoutOperation (jmri.TurnoutOperation)9 Element (org.jdom2.Element)4 Turnout (jmri.Turnout)2 TurnoutManager (jmri.TurnoutManager)2 TurnoutOperationManagerXml (jmri.configurexml.TurnoutOperationManagerXml)2 TurnoutOperationXml (jmri.configurexml.turnoutoperations.TurnoutOperationXml)2 Component (java.awt.Component)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Vector (java.util.Vector)1 CommonTurnoutOperation (jmri.CommonTurnoutOperation)1 Sensor (jmri.Sensor)1 TurnoutOperationManager (jmri.TurnoutOperationManager)1 Attribute (org.jdom2.Attribute)1