Search in sources :

Example 1 with CvValue

use of jmri.jmrit.symbolicprog.CvValue in project JMRI by JMRI.

the class SymbolicProgFrame method processLocoFile.

void processLocoFile(Element loco) {
    // load the name et al
    locoRoadName.setText(loco.getAttributeValue("roadName"));
    locoRoadNumber.setText(loco.getAttributeValue("roadNumber"));
    locoMfg.setText(loco.getAttributeValue("mfg"));
    locoModel.setText(loco.getAttributeValue("model"));
    // load the variable definitions for the decoder
    Element decoder = loco.getChild("decoder");
    if (decoder != null) {
        // get the file name
        String mfg = decoder.getAttribute("mfg").getValue();
        String model = decoder.getAttribute("model").getValue();
        String filename = "xml" + File.separator + mfg + "_" + model + ".xml";
        if (log.isDebugEnabled()) {
            log.debug("will read decoder info from " + filename);
        }
        readAndParseConfigFile(new File(filename));
        if (log.isDebugEnabled()) {
            log.debug("finished processing decoder file for loco file");
        }
    } else {
        log.error("No decoder element found in config file");
    }
    // get the CVs and load
    Element values = loco.getChild("values");
    if (values != null) {
        // get the CV values and load
        List<Element> varList = values.getChildren("CVvalue");
        if (log.isDebugEnabled()) {
            log.debug("Found " + varList.size() + " CVvalues");
        }
        for (int i = 0; i < varList.size(); i++) {
            // locate the row
            if (((varList.get(i))).getAttribute("name") == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in name " + ((varList.get(i))) + " " + ((varList.get(i))).getAttributes());
                }
                break;
            }
            if (((varList.get(i))).getAttribute("value") == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in value " + ((varList.get(i))) + " " + ((varList.get(i))).getAttributes());
                }
                break;
            }
            String name = ((varList.get(i))).getAttribute("name").getValue();
            String value = ((varList.get(i))).getAttribute("value").getValue();
            if (log.isDebugEnabled()) {
                log.debug("CV: " + i + "th entry, CV number " + name + " has value: " + value);
            }
            CvValue cvObject = cvModel.allCvMap().get(name);
            cvObject.setValue(Integer.valueOf(value).intValue());
            cvObject.setState(CvValue.FROMFILE);
        }
        variableModel.configDone();
    } else {
        log.error("no values element found in config file; CVs not configured");
        return;
    }
    // get the variable values and load
    Element decoderDef = values.getChild("decoderDef");
    if (decoderDef != null) {
        List<Element> varList = decoderDef.getChildren("varValue");
        if (log.isDebugEnabled()) {
            log.debug("Found " + varList.size() + " varValues");
        }
        for (int i = 0; i < varList.size(); i++) {
            // locate the row
            Attribute itemAttr = null;
            if ((itemAttr = varList.get(i).getAttribute("item")) == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in item " + varList.get(i));
                }
                break;
            }
            if ((itemAttr = varList.get(i).getAttribute("name")) == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in name " + varList.get(i));
                }
                break;
            }
            String item = itemAttr.getValue();
            if (((varList.get(i))).getAttribute("value") == null) {
                if (log.isDebugEnabled()) {
                    log.debug("unexpected null in value " + ((varList.get(i))));
                }
                break;
            }
            String value = ((varList.get(i))).getAttribute("value").getValue();
            if (log.isDebugEnabled()) {
                log.debug("Variable " + i + " is " + item + " value: " + value);
            }
            int row;
            for (row = 0; row < variableModel.getRowCount(); row++) {
                if (variableModel.getLabel(row).equals(item)) {
                    break;
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("Variable " + item + " is row " + row);
            }
            if (!value.equals("")) {
                // don't set if no value was stored
                variableModel.setIntValue(row, Integer.valueOf(value).intValue());
            }
            variableModel.setState(row, VariableValue.FROMFILE);
        }
        variableModel.configDone();
    } else {
        log.error("no decoderDef element found in config file");
    }
    // the act of loading values marks as dirty, but we're actually in synch
    variableModel.setFileDirty(false);
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) CvValue(jmri.jmrit.symbolicprog.CvValue) DecoderFile(jmri.jmrit.decoderdefn.DecoderFile) File(java.io.File)

Example 2 with CvValue

use of jmri.jmrit.symbolicprog.CvValue in project JMRI by JMRI.

the class PaneProgPane method setToRead.

/**
     * Set the "ToRead" parameter in all variables and CVs on this pane.
     *
     * @param justChanges  true if this is read changes, false if read all
     * @param startProcess true if this is the start of processing, false if
     *                     cleaning up at end
     */
void setToRead(boolean justChanges, boolean startProcess) {
    if (!container.isBusy() || // the frame has already setToRead
    (!startProcess)) {
        // we want to setToRead false if the pane's process is being stopped
        for (int i = 0; i < varList.size(); i++) {
            int varNum = varList.get(i);
            VariableValue var = _varModel.getVariable(varNum);
            if (justChanges) {
                if (var.isChanged()) {
                    var.setToRead(startProcess);
                } else {
                    var.setToRead(false);
                }
            } else {
                var.setToRead(startProcess);
            }
        }
        if (isCvTablePane) {
            // make sure list of CVs up to date if table
            setCvListFromTable();
        }
        for (int cvNum : cvList) {
            CvValue cv = _cvModel.getCvByRow(cvNum);
            if (justChanges) {
                if (VariableValue.considerChanged(cv)) {
                    cv.setToRead(startProcess);
                } else {
                    cv.setToRead(false);
                }
            } else {
                cv.setToRead(startProcess);
            }
        }
        for (int i = 0; i < indexedCvList.size(); i++) {
            CvValue icv = _indexedCvModel.getCvByRow(i);
            if (justChanges) {
                if (VariableValue.considerChanged(icv)) {
                    icv.setToRead(startProcess);
                } else {
                    icv.setToRead(false);
                }
            } else {
                icv.setToRead(startProcess);
            }
        }
    }
}
Also used : VariableValue(jmri.jmrit.symbolicprog.VariableValue) CvValue(jmri.jmrit.symbolicprog.CvValue)

Example 3 with CvValue

use of jmri.jmrit.symbolicprog.CvValue in project JMRI by JMRI.

the class PaneProgFrame method saveDefaults.

/**
     * Save all CV values.
     * <p>
     * These stored values are used by {link #resetToDefaults()}
     */
protected void saveDefaults() {
    int n = cvModel.getRowCount();
    defaultCvValues = new int[n];
    defaultCvNumbers = new String[n];
    for (int i = 0; i < n; i++) {
        CvValue cv = cvModel.getCvByRow(i);
        defaultCvValues[i] = cv.getValue();
        defaultCvNumbers[i] = cv.number();
    }
    n = iCvModel.getRowCount();
    defaultIndexedCvValues = new int[n];
    for (int i = 0; i < n; i++) {
        CvValue cv = iCvModel.getCvByRow(i);
        defaultIndexedCvValues[i] = cv.getValue();
    }
}
Also used : CvValue(jmri.jmrit.symbolicprog.CvValue)

Example 4 with CvValue

use of jmri.jmrit.symbolicprog.CvValue in project JMRI by JMRI.

the class PaneProgPane method nextConfirm.

/**
     * If there are any more compare operations to be done on this pane, do the
     * next one.
     * <P>
     * Each invocation of this method compare one CV; completion of that request
     * will cause it to happen again, reading the next one, until there's
     * nothing left to read.
     * <P>
     * @return true is a compare has been started, false if the pane is
     *         complete.
     */
boolean nextConfirm() {
    // look for possible CVs
    while (cvListIterator != null && cvListIterator.hasNext()) {
        int cvNum = cvListIterator.next();
        CvValue cv = _cvModel.getCvByRow(cvNum);
        if (log.isDebugEnabled()) {
            log.debug("nextConfirm cv index " + cvNum + " state " + cv.getState());
        }
        if (cv.isToRead()) {
            if (log.isDebugEnabled()) {
                log.debug("start confirm of cv " + cvNum);
            }
            setBusy(true);
            if (_programmingCV != null) {
                log.error("listener already set at confirm start");
            }
            _programmingCV = _cvModel.getCvByRow(cvNum);
            _read = true;
            // get notified when that state changes so can repeat
            _programmingCV.addPropertyChangeListener(this);
            // and make the compare request
            _programmingCV.confirm(_cvModel.getStatusLabel());
            if (log.isDebugEnabled()) {
                log.debug("return from starting CV confirm");
            }
            // only make one request at a time!
            return true;
        }
    }
    // found no CVs needing read, try indexed CVs
    while ((indexedCvList.size() > 0) && (indexedCvListIndex < indexedCvList.size())) {
        int indxVarNum = indexedCvList.get(indexedCvListIndex);
        int indxState = _varModel.getState(indxVarNum);
        if (log.isDebugEnabled()) {
            log.debug("nextRead indexed cv @ row index " + indexedCvListIndex + " state " + indxState);
        }
        VariableValue iCv = _varModel.getVariable(indxVarNum);
        indexedCvListIndex++;
        if (iCv.isToRead()) {
            String sz = "start confirm of indexed cv " + (_indexedCvModel.getCvByRow(indexedCvListIndex - 1)).cvName();
            if (log.isDebugEnabled()) {
                log.debug(sz);
            }
            setBusy(true);
            if (_programmingIndexedCV != null) {
                log.error("listener already set at confirm start");
            }
            _programmingIndexedCV = _varModel.getVariable(indxVarNum);
            _read = true;
            // get notified when that state changes so can repeat
            _programmingIndexedCV.addPropertyChangeListener(this);
            // and make the compare request
            _programmingIndexedCV.confirmAll();
            if (log.isDebugEnabled()) {
                log.debug("return from starting indexed CV confirm");
            }
            // only make one request at a time!
            return true;
        }
    }
    // nothing to program, end politely
    if (log.isDebugEnabled()) {
        log.debug("nextConfirm found nothing to do");
    }
    confirmChangesButton.setSelected(false);
    // reset both, as that's final state we want
    confirmAllButton.setSelected(false);
    setBusy(false);
    container.paneFinished();
    return false;
}
Also used : VariableValue(jmri.jmrit.symbolicprog.VariableValue) CvValue(jmri.jmrit.symbolicprog.CvValue)

Example 5 with CvValue

use of jmri.jmrit.symbolicprog.CvValue in project JMRI by JMRI.

the class PaneProgPane method printPane.

public void printPane(HardcopyWriter w) {
    // if pane is empty, don't print anything
    if (varList.isEmpty() && cvList.isEmpty()) {
        return;
    }
    // future work needed here to print indexed CVs
    // Define column widths for name and value output.
    // Make col 2 slightly larger than col 1 and reduce both to allow for
    // extra spaces that will be added during concatenation
    int col1Width = w.getCharactersPerLine() / 2 - 3 - 5;
    int col2Width = w.getCharactersPerLine() / 2 - 3 + 5;
    try {
        //Create a string of spaces the width of the first column
        StringBuilder spaces = new StringBuilder();
        for (int i = 0; i < col1Width; i++) {
            spaces.append(" ");
        }
        // start with pane name in bold
        String heading1 = SymbolicProgBundle.getMessage("PrintHeadingField");
        String heading2 = SymbolicProgBundle.getMessage("PrintHeadingSetting");
        String s;
        int interval = spaces.length() - heading1.length();
        w.setFontStyle(Font.BOLD);
        // write the section name and dividing line
        s = mName.toUpperCase();
        w.write(s, 0, s.length());
        w.writeBorders();
        //Draw horizontal dividing line for each Pane section
        w.write(w.getCurrentLineNumber(), 0, w.getCurrentLineNumber(), w.getCharactersPerLine() + 1);
        s = "\n";
        w.write(s, 0, s.length());
        // if this isn't the raw CV section, write the column headings
        if (cvList.isEmpty()) {
            w.setFontStyle(Font.BOLD + Font.ITALIC);
            s = "   " + heading1 + spaces.substring(0, interval) + "   " + heading2;
            w.write(s, 0, s.length());
            w.writeBorders();
            s = "\n";
            w.write(s, 0, s.length());
        }
        w.setFontStyle(Font.PLAIN);
        // Define a vector to store the names of variables that have been printed
        // already.  If they have been printed, they will be skipped.
        // Using a vector here since we don't know how many variables will
        // be printed and it allows expansion as necessary
        ArrayList<String> printedVariables = new ArrayList<>(10);
        // index over variables
        for (int i = 0; i < varList.size(); i++) {
            int varNum = varList.get(i);
            VariableValue var = _varModel.getVariable(varNum);
            String name = var.label();
            if (name == null) {
                name = var.item();
            }
            // Check if variable has been printed.  If not store it and print
            boolean alreadyPrinted = false;
            for (String printedVariable : printedVariables) {
                if (name.equals(printedVariable)) {
                    alreadyPrinted = true;
                }
            }
            //If already printed, skip it.  If not, store it and print
            if (alreadyPrinted == true) {
                continue;
            }
            printedVariables.add(name);
            String value = var.getTextValue();
            String originalName = name;
            String originalValue = value;
            // NO I18N
            name = name + " (CV" + var.getCvNum() + ")";
            //define index values for name and value substrings
            int nameLeftIndex = 0;
            int nameRightIndex = name.length();
            int valueLeftIndex = 0;
            int valueRightIndex = value.length();
            String trimmedName;
            String trimmedValue;
            // before writing - if split, repeat until all pieces have been output
            while ((valueLeftIndex < value.length()) || (nameLeftIndex < name.length())) {
                // name split code
                if (name.substring(nameLeftIndex).length() > col1Width) {
                    for (int j = 0; j < col1Width; j++) {
                        String delimiter = name.substring(nameLeftIndex + col1Width - j - 1, nameLeftIndex + col1Width - j);
                        if (delimiter.equals(" ") || delimiter.equals(";") || delimiter.equals(",")) {
                            nameRightIndex = nameLeftIndex + col1Width - j;
                            break;
                        }
                    }
                    trimmedName = name.substring(nameLeftIndex, nameRightIndex);
                    nameLeftIndex = nameRightIndex;
                    int space = spaces.length() - trimmedName.length();
                    s = "   " + trimmedName + spaces.substring(0, space);
                } else {
                    trimmedName = name.substring(nameLeftIndex);
                    int space = spaces.length() - trimmedName.length();
                    s = "   " + trimmedName + spaces.substring(0, space);
                    name = "";
                    nameLeftIndex = 0;
                }
                // value split code
                if (value.substring(valueLeftIndex).length() > col2Width) {
                    for (int j = 0; j < col2Width; j++) {
                        String delimiter = value.substring(valueLeftIndex + col2Width - j - 1, valueLeftIndex + col2Width - j);
                        if (delimiter.equals(" ") || delimiter.equals(";") || delimiter.equals(",")) {
                            valueRightIndex = valueLeftIndex + col2Width - j;
                            break;
                        }
                    }
                    trimmedValue = value.substring(valueLeftIndex, valueRightIndex);
                    valueLeftIndex = valueRightIndex;
                    s = s + "   " + trimmedValue;
                } else {
                    trimmedValue = value.substring(valueLeftIndex);
                    s = s + "   " + trimmedValue;
                    valueLeftIndex = 0;
                    value = "";
                }
                w.write(s, 0, s.length());
                w.writeBorders();
                s = "\n";
                w.write(s, 0, s.length());
            }
            // Check for a Speed Table output and create a graphic display.
            // Java 1.5 has a known bug, #6328248, that prevents printing of progress
            //  bars using old style printing classes.  It results in blank bars on Windows,
            //  but hangs Macs. The version check is a workaround.
            float v = Float.valueOf(java.lang.System.getProperty("java.version").substring(0, 3));
            if (originalName.equals("Speed Table") && v < 1.5) {
                // set the height of the speed table graph in lines
                int speedFrameLineHeight = 11;
                s = "\n";
                // check that there is enough room on the page; if not,
                // space down the rest of the page.
                // don't use page break because we want the table borders to be written
                // to the bottom of the page
                int pageSize = w.getLinesPerPage();
                int here = w.getCurrentLineNumber();
                if (pageSize - here < speedFrameLineHeight) {
                    for (int j = 0; j < (pageSize - here); j++) {
                        w.writeBorders();
                        w.write(s, 0, s.length());
                    }
                }
                // Now that there is page space, create the window to hold the graphic speed table
                JWindow speedWindow = new JWindow();
                // Window size as wide as possible to allow for largest type size
                speedWindow.setSize(512, 165);
                speedWindow.getContentPane().setBackground(Color.white);
                speedWindow.getContentPane().setLayout(null);
                // in preparation for display, extract the speed table values into an array
                StringTokenizer valueTokens = new StringTokenizer(originalValue, ",", false);
                int[] speedVals = new int[28];
                int k = 0;
                while (valueTokens.hasMoreTokens()) {
                    speedVals[k] = Integer.parseInt(valueTokens.nextToken());
                    k++;
                }
                // on the speed table value (half height) and add them to the window
                for (int j = 0; j < 28; j++) {
                    JProgressBar printerBar = new JProgressBar(JProgressBar.VERTICAL, 0, 127);
                    printerBar.setBounds(52 + j * 15, 19, 10, 127);
                    printerBar.setValue(speedVals[j] / 2);
                    printerBar.setBackground(Color.white);
                    printerBar.setForeground(Color.darkGray);
                    printerBar.setBorder(BorderFactory.createLineBorder(Color.black));
                    speedWindow.getContentPane().add(printerBar);
                    // create a set of value labels at the top containing the speed table values
                    JLabel barValLabel = new JLabel(Integer.toString(speedVals[j]), SwingConstants.CENTER);
                    barValLabel.setBounds(50 + j * 15, 4, 15, 15);
                    barValLabel.setFont(new java.awt.Font("Monospaced", 0, 7));
                    speedWindow.getContentPane().add(barValLabel);
                    //Create a set of labels at the bottom with the CV numbers in them
                    JLabel barCvLabel = new JLabel(Integer.toString(67 + j), SwingConstants.CENTER);
                    barCvLabel.setBounds(50 + j * 15, 150, 15, 15);
                    barCvLabel.setFont(new java.awt.Font("Monospaced", 0, 7));
                    speedWindow.getContentPane().add(barCvLabel);
                }
                JLabel cvLabel = new JLabel(Bundle.getMessage("Value"));
                cvLabel.setFont(new java.awt.Font("Monospaced", 0, 7));
                cvLabel.setBounds(25, 4, 26, 15);
                speedWindow.getContentPane().add(cvLabel);
                // I18N seems undesirable for support
                JLabel valueLabel = new JLabel("CV");
                valueLabel.setFont(new java.awt.Font("Monospaced", 0, 7));
                valueLabel.setBounds(37, 150, 13, 15);
                speedWindow.getContentPane().add(valueLabel);
                // pass the complete window to the printing class
                w.write(speedWindow);
                // Now need to write the borders on sides of table
                for (int j = 0; j < speedFrameLineHeight; j++) {
                    w.writeBorders();
                    w.write(s, 0, s.length());
                }
            }
        }
        final int TABLE_COLS = 3;
        // index over CVs
        if (cvList.size() > 0) {
            //            Check how many Cvs there are to print
            int cvCount = cvList.size();
            //set font to Bold
            w.setFontStyle(Font.BOLD);
            // print a simple heading with I18N
            s = String.format("%1$21s", Bundle.getMessage("Value")) + String.format("%1$28s", Bundle.getMessage("Value")) + String.format("%1$28s", Bundle.getMessage("Value"));
            w.write(s, 0, s.length());
            w.writeBorders();
            s = "\n";
            w.write(s, 0, s.length());
            // NO I18N
            s = "            CV  Dec Hex                 CV  Dec Hex                 CV  Dec Hex";
            w.write(s, 0, s.length());
            w.writeBorders();
            s = "\n";
            w.write(s, 0, s.length());
            //set font back to Normal
            w.setFontStyle(0);
            //           }
            /*create an array to hold CV/Value strings to allow reformatting and sorting
                 Same size as the table drawn above (TABLE_COLS columns*tableHeight; heading rows
                 not included). Use the count of how many CVs there are to determine the number
                 of table rows required.  Add one more row if the divison into TABLE_COLS columns
                 isn't even.
                 */
            int tableHeight = cvCount / TABLE_COLS;
            if (cvCount % TABLE_COLS > 0) {
                tableHeight++;
            }
            String[] cvStrings = new String[TABLE_COLS * tableHeight];
            //blank the array
            for (int j = 0; j < cvStrings.length; j++) {
                cvStrings[j] = "";
            }
            // get each CV and value
            int i = 0;
            for (int cvNum : cvList) {
                CvValue cv = _cvModel.getCvByRow(cvNum);
                int value = cv.getValue();
                //convert and pad numbers as needed
                String numString = String.format("%12s", cv.number());
                String valueString = Integer.toString(value);
                String valueStringHex = Integer.toHexString(value).toUpperCase();
                if (value < 16) {
                    valueStringHex = "0" + valueStringHex;
                }
                for (int j = 1; j < 3; j++) {
                    if (valueString.length() < 3) {
                        valueString = " " + valueString;
                    }
                }
                //Create composite string of CV and its decimal and hex values
                s = "  " + numString + "  " + valueString + "  " + valueStringHex + " ";
                //populate printing array - still treated as a single column
                cvStrings[i] = s;
                i++;
            }
            //sort the array in CV order (just the members with values)
            String temp;
            boolean swap = false;
            do {
                swap = false;
                for (i = 0; i < _cvModel.getRowCount() - 1; i++) {
                    if (PrintCvAction.cvSortOrderVal(cvStrings[i + 1].substring(0, 15).trim()) < PrintCvAction.cvSortOrderVal(cvStrings[i].substring(0, 15).trim())) {
                        temp = cvStrings[i + 1];
                        cvStrings[i + 1] = cvStrings[i];
                        cvStrings[i] = temp;
                        swap = true;
                    }
                }
            } while (swap == true);
            //Print the array in four columns
            for (i = 0; i < tableHeight; i++) {
                s = cvStrings[i] + "    " + cvStrings[i + tableHeight] + "    " + cvStrings[i + tableHeight * 2];
                w.write(s, 0, s.length());
                w.writeBorders();
                s = "\n";
                w.write(s, 0, s.length());
            }
        }
        s = "\n";
        w.writeBorders();
        w.write(s, 0, s.length());
        w.writeBorders();
        w.write(s, 0, s.length());
    // handle special cases
    } catch (IOException e) {
        log.warn("error during printing: " + e);
    }
}
Also used : VariableValue(jmri.jmrit.symbolicprog.VariableValue) Font(java.awt.Font) JWindow(javax.swing.JWindow) ArrayList(java.util.ArrayList) JProgressBar(javax.swing.JProgressBar) JLabel(javax.swing.JLabel) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) CvValue(jmri.jmrit.symbolicprog.CvValue)

Aggregations

CvValue (jmri.jmrit.symbolicprog.CvValue)10 VariableValue (jmri.jmrit.symbolicprog.VariableValue)7 Font (java.awt.Font)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 JLabel (javax.swing.JLabel)1 JProgressBar (javax.swing.JProgressBar)1 JWindow (javax.swing.JWindow)1 DecoderFile (jmri.jmrit.decoderdefn.DecoderFile)1 Attribute (org.jdom2.Attribute)1 Element (org.jdom2.Element)1