Search in sources :

Example 6 with VariableValue

use of jmri.jmrit.symbolicprog.VariableValue 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)

Example 7 with VariableValue

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

the class PaneProgPane method newRow.

/**
     * Create a single row from the JDOM column Element
     *
     * @param element     element containing row contents
     * @param showStdName show the name following the rules for the
     * <em>nameFmt</em> element
     * @param modelElem   element containing the decoder model
     * @return a panel containing the group
     */
public JPanel newRow(Element element, boolean showStdName, Element modelElem) {
    // create a panel to add as a new column or row
    final JPanel c = new JPanel();
    panelList.add(c);
    GridBagLayout g = new GridBagLayout();
    GridBagConstraints cs = new GridBagConstraints();
    c.setLayout(g);
    // handle the xml definition
    // for all elements in the column or row
    List<Element> elemList = element.getChildren();
    log.trace("newRow starting with {} elements", elemList.size());
    for (int i = 0; i < elemList.size(); i++) {
        // update the grid position
        cs.gridy = 0;
        cs.gridx++;
        Element e = elemList.get(i);
        String name = e.getName();
        log.trace("newRow processing {} element", name);
        // decode the type
        if (name.equals("display")) {
            // its a variable
            // load the variable
            newVariable(e, c, g, cs, showStdName);
        } else if (name.equals("separator")) {
            // its a separator
            JSeparator j = new JSeparator(javax.swing.SwingConstants.VERTICAL);
            cs.fill = GridBagConstraints.BOTH;
            cs.gridheight = GridBagConstraints.REMAINDER;
            g.setConstraints(j, cs);
            c.add(j);
            cs.fill = GridBagConstraints.NONE;
            cs.gridheight = 1;
        } else if (name.equals("label")) {
            cs.gridheight = GridBagConstraints.REMAINDER;
            makeLabel(e, c, g, cs);
        } else if (name.equals("soundlabel")) {
            cs.gridheight = GridBagConstraints.REMAINDER;
            makeSoundLabel(e, c, g, cs);
        } else if (name.equals("cvtable")) {
            makeCvTable(cs, g, c);
        } else if (name.equals("indxcvtable")) {
            log.debug("starting to build IndexedCvTable pane");
            JTable indxcvTable = new JTable(_indexedCvModel);
            JScrollPane cvScroll = new JScrollPane(indxcvTable);
            indxcvTable.setDefaultRenderer(JTextField.class, new ValueRenderer());
            indxcvTable.setDefaultRenderer(JButton.class, new ValueRenderer());
            indxcvTable.setDefaultEditor(JTextField.class, new ValueEditor());
            indxcvTable.setDefaultEditor(JButton.class, new ValueEditor());
            indxcvTable.setRowHeight(new JButton("X").getPreferredSize().height);
            indxcvTable.setPreferredScrollableViewportSize(new Dimension(700, indxcvTable.getRowHeight() * 14));
            cvScroll.setColumnHeaderView(indxcvTable.getTableHeader());
            // don't want a horizontal scroll bar
            // Need to see the whole row at one time
            //                indxcvTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            cs.gridwidth = GridBagConstraints.REMAINDER;
            g.setConstraints(cvScroll, cs);
            c.add(cvScroll);
            cs.gridwidth = 1;
            // remember which indexed CVs to read/write
            for (int j = 0; j < _indexedCvModel.getRowCount(); j++) {
                String sz = "CV" + _indexedCvModel.getName(j);
                int in = _varModel.findVarIndex(sz);
                indexedCvList.add(in);
            }
            _cvTable = true;
            log.debug("end of building IndexedCvTable pane");
        } else if (name.equals("fnmapping")) {
            pickFnMapPanel(c, g, cs, modelElem);
        } else if (name.equals("dccaddress")) {
            JPanel l = addDccAddressPanel(e);
            if (l.getComponentCount() > 0) {
                cs.gridheight = GridBagConstraints.REMAINDER;
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridheight = 1;
            }
        } else if (name.equals("column")) {
            // nested "column" elements ...
            cs.gridheight = GridBagConstraints.REMAINDER;
            JPanel l = newColumn(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridheight = 1;
            }
        } else if (name.equals("row")) {
            // nested "row" elements ...
            cs.gridwidth = GridBagConstraints.REMAINDER;
            JPanel l = newRow(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridwidth = 1;
            }
        } else if (name.equals("grid")) {
            // nested "grid" elements ...
            cs.gridwidth = GridBagConstraints.REMAINDER;
            JPanel l = newGrid(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridwidth = 1;
            }
        } else if (name.equals("group")) {
            // nested "group" elements ...
            JPanel l = newGroup(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
            }
        } else if (!name.equals("qualifier")) {
            // its a mistake
            log.error("No code to handle element of type " + e.getName() + " in newRow");
        }
    }
    // add glue to the bottom to allow resize
    if (c.getComponentCount() > 0) {
        c.add(Box.createVerticalGlue());
    }
    // handle qualification if any
    QualifierAdder qa = new QualifierAdder() {

        @Override
        protected Qualifier createQualifier(VariableValue var, String relation, String value) {
            return new JComponentQualifier(c, var, Integer.parseInt(value), relation);
        }

        @Override
        protected void addListener(java.beans.PropertyChangeListener qc) {
            c.addPropertyChangeListener(qc);
        }
    };
    qa.processModifierElements(element, _varModel);
    return c;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) GridBagLayout(java.awt.GridBagLayout) VariableValue(jmri.jmrit.symbolicprog.VariableValue) Element(org.jdom2.Element) JButton(javax.swing.JButton) ValueEditor(jmri.jmrit.symbolicprog.ValueEditor) Dimension(java.awt.Dimension) JTextField(javax.swing.JTextField) JSeparator(javax.swing.JSeparator) ValueRenderer(jmri.jmrit.symbolicprog.ValueRenderer) JTable(javax.swing.JTable) QualifierAdder(jmri.jmrit.symbolicprog.QualifierAdder)

Example 8 with VariableValue

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

the class PaneProgPane method makeSoundLabel.

/**
     * Create sound label from Element.
     *
     * @param e  element containing label contents
     * @param c  panel to insert label into
     * @param g  panel layout manager
     * @param cs constraints on layout manager
     */
protected void makeSoundLabel(Element e, JPanel c, GridBagLayout g, GridBagConstraints cs) {
    String labelText = rosterEntry.getSoundLabel(Integer.valueOf(LocaleSelector.getAttribute(e, "num")));
    final JLabel l = new JLabel(labelText);
    l.setAlignmentX(1.0f);
    cs.fill = GridBagConstraints.BOTH;
    if (log.isDebugEnabled()) {
        log.debug("Add soundlabel: " + l.getText() + " cs: " + cs.gridwidth + " " + cs.fill + " " + cs.gridx + " " + cs.gridy);
    }
    g.setConstraints(l, cs);
    c.add(l);
    cs.fill = GridBagConstraints.NONE;
    cs.gridwidth = 1;
    cs.gridheight = 1;
    // handle qualification if any
    QualifierAdder qa = new QualifierAdder() {

        @Override
        protected Qualifier createQualifier(VariableValue var, String relation, String value) {
            return new JComponentQualifier(l, var, Integer.parseInt(value), relation);
        }

        @Override
        protected void addListener(java.beans.PropertyChangeListener qc) {
            l.addPropertyChangeListener(qc);
        }
    };
    qa.processModifierElements(e, _varModel);
}
Also used : VariableValue(jmri.jmrit.symbolicprog.VariableValue) JLabel(javax.swing.JLabel) QualifierAdder(jmri.jmrit.symbolicprog.QualifierAdder)

Example 9 with VariableValue

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

the class PaneProgPane method nextWrite.

boolean nextWrite() {
    log.debug("start nextWrite");
    // look for possible variables
    while ((varList.size() > 0) && (varListIndex < varList.size())) {
        int varNum = varList.get(varListIndex);
        int vState = _varModel.getState(varNum);
        VariableValue var = _varModel.getVariable(varNum);
        if (log.isDebugEnabled()) {
            log.debug("nextWrite var index " + varNum + " state " + VariableValue.stateNameFromValue(vState) + " isToWrite: " + var.isToWrite() + " label:" + var.label());
        }
        varListIndex++;
        if (var.isToWrite()) {
            log.debug("start write of variable " + _varModel.getLabel(varNum));
            executeWrite(var);
            if (log.isDebugEnabled()) {
                log.debug("return from starting var write");
            }
            // only make one request at a time!
            return true;
        }
    }
    // check for CVs to handle (e.g. for CV table)
    while (cvListIterator != null && cvListIterator.hasNext()) {
        int cvNum = cvListIterator.next();
        CvValue cv = _cvModel.getCvByRow(cvNum);
        if (log.isDebugEnabled()) {
            log.debug("nextWrite cv index " + cvNum + " state " + cv.getState());
        }
        if (cv.isToWrite()) {
            if (log.isDebugEnabled()) {
                log.debug("start write of cv index " + cvNum);
            }
            setBusy(true);
            if (_programmingCV != null) {
                log.error("listener already set at write start");
            }
            _programmingCV = _cvModel.getCvByRow(cvNum);
            _read = false;
            // get notified when that state changes so can repeat
            _programmingCV.addPropertyChangeListener(this);
            // and make the write request
            // _programmingCV.setToWrite(false);  // CVs set this themselves
            _programmingCV.write(_cvModel.getStatusLabel());
            if (log.isDebugEnabled()) {
                log.debug("return from starting cv write");
            }
            // only make one request at a time!
            return true;
        }
    }
    // check for Indexed CVs to handle (e.g. for Indexed CV table)
    while ((indexedCvList.size() > 0) && (indexedCvListIndex < indexedCvList.size())) {
        int indxVarNum = indexedCvList.get(indexedCvListIndex);
        int indxState = _varModel.getState(indxVarNum);
        if (log.isDebugEnabled()) {
            log.debug("nextWrite indexed cv @ row index " + indexedCvListIndex + " state " + indxState);
        }
        VariableValue iCv = _varModel.getVariable(indxVarNum);
        indexedCvListIndex++;
        if (iCv.isToWrite()) {
            String sz = "start write 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 read start");
            }
            _programmingIndexedCV = _varModel.getVariable(indxVarNum);
            _read = true;
            // get notified when that state changes so can repeat
            _programmingIndexedCV.addPropertyChangeListener(this);
            // _programmingIndexedCV.setToWrite(false);  // CVs set this themselves
            // and make the write request
            _programmingIndexedCV.writeAll();
            if (log.isDebugEnabled()) {
                log.debug("return from starting indexed CV read");
            }
            // only make one request at a time!
            return true;
        }
    }
    // nothing to program, end politely
    if (log.isDebugEnabled()) {
        log.debug("nextWrite found nothing to do");
    }
    writeChangesButton.setSelected(false);
    writeAllButton.setSelected(false);
    setBusy(false);
    container.paneFinished();
    log.debug("return from nextWrite with nothing to do");
    return false;
}
Also used : VariableValue(jmri.jmrit.symbolicprog.VariableValue) CvValue(jmri.jmrit.symbolicprog.CvValue)

Example 10 with VariableValue

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

the class PaneProgPane method newColumn.

/**
     * Create a single column from the JDOM column Element.
     *
     * @param element     element containing column contents
     * @param showStdName show the name following the rules for the
     * <em>nameFmt</em> element
     * @param modelElem   element containing the decoder model
     * @return a panel containing the group
     */
public JPanel newColumn(Element element, boolean showStdName, Element modelElem) {
    // create a panel to add as a new column or row
    final JPanel c = new JPanel();
    panelList.add(c);
    GridBagLayout g = new GridBagLayout();
    GridBagConstraints cs = new GridBagConstraints();
    c.setLayout(g);
    // handle the xml definition
    // for all elements in the column or row
    List<Element> elemList = element.getChildren();
    log.trace("newColumn starting with {} elements", elemList.size());
    for (int i = 0; i < elemList.size(); i++) {
        // update the grid position
        cs.gridy++;
        cs.gridx = 0;
        Element e = (elemList.get(i));
        String name = e.getName();
        log.trace("newColumn processing {} element", name);
        // decode the type
        if (name.equals("display")) {
            // its a variable
            // load the variable
            newVariable(e, c, g, cs, showStdName);
        } else if (name.equals("separator")) {
            // its a separator
            JSeparator j = new JSeparator(javax.swing.SwingConstants.HORIZONTAL);
            cs.fill = GridBagConstraints.BOTH;
            cs.gridwidth = GridBagConstraints.REMAINDER;
            g.setConstraints(j, cs);
            c.add(j);
            cs.gridwidth = 1;
        } else if (name.equals("label")) {
            cs.gridwidth = GridBagConstraints.REMAINDER;
            makeLabel(e, c, g, cs);
        } else if (name.equals("soundlabel")) {
            cs.gridwidth = GridBagConstraints.REMAINDER;
            makeSoundLabel(e, c, g, cs);
        } else if (name.equals("cvtable")) {
            makeCvTable(cs, g, c);
        } else if (name.equals("indxcvtable")) {
            log.debug("starting to build IndexedCvTable pane");
            JTable indxcvTable = new JTable(_indexedCvModel);
            JScrollPane cvScroll = new JScrollPane(indxcvTable);
            indxcvTable.setDefaultRenderer(JTextField.class, new ValueRenderer());
            indxcvTable.setDefaultRenderer(JButton.class, new ValueRenderer());
            indxcvTable.setDefaultEditor(JTextField.class, new ValueEditor());
            indxcvTable.setDefaultEditor(JButton.class, new ValueEditor());
            indxcvTable.setRowHeight(new JButton("X").getPreferredSize().height);
            indxcvTable.setPreferredScrollableViewportSize(new Dimension(700, indxcvTable.getRowHeight() * 14));
            cvScroll.setColumnHeaderView(indxcvTable.getTableHeader());
            // don't want a horizontal scroll bar
            // Need to see the whole row at one time
            //                indxcvTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            cs.gridwidth = GridBagConstraints.REMAINDER;
            g.setConstraints(cvScroll, cs);
            c.add(cvScroll);
            cs.gridwidth = 1;
            // remember which indexed CVs to read/write
            for (int j = 0; j < _indexedCvModel.getRowCount(); j++) {
                String sz = "CV" + _indexedCvModel.getName(j);
                int in = _varModel.findVarIndex(sz);
                indexedCvList.add(in);
            }
            _cvTable = true;
            log.debug("end of building IndexedCvTable pane");
        } else if (name.equals("fnmapping")) {
            pickFnMapPanel(c, g, cs, modelElem);
        } else if (name.equals("dccaddress")) {
            JPanel l = addDccAddressPanel(e);
            if (l.getComponentCount() > 0) {
                cs.gridwidth = GridBagConstraints.REMAINDER;
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridwidth = 1;
            }
        } else if (name.equals("column")) {
            // nested "column" elements ...
            cs.gridheight = GridBagConstraints.REMAINDER;
            JPanel l = newColumn(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridheight = 1;
            }
        } else if (name.equals("row")) {
            // nested "row" elements ...
            cs.gridwidth = GridBagConstraints.REMAINDER;
            JPanel l = newRow(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridwidth = 1;
            }
        } else if (name.equals("grid")) {
            // nested "grid" elements ...
            cs.gridwidth = GridBagConstraints.REMAINDER;
            JPanel l = newGrid(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
                cs.gridwidth = 1;
            }
        } else if (name.equals("group")) {
            // nested "group" elements ...
            JPanel l = newGroup(e, showStdName, modelElem);
            if (l.getComponentCount() > 0) {
                panelList.add(l);
                g.setConstraints(l, cs);
                c.add(l);
            }
        } else if (!name.equals("qualifier")) {
            // its a mistake
            log.error("No code to handle element of type " + e.getName() + " in newColumn");
        }
    }
    // add glue to the bottom to allow resize
    if (c.getComponentCount() > 0) {
        c.add(Box.createVerticalGlue());
    }
    // handle qualification if any
    QualifierAdder qa = new QualifierAdder() {

        @Override
        protected Qualifier createQualifier(VariableValue var, String relation, String value) {
            return new JComponentQualifier(c, var, Integer.parseInt(value), relation);
        }

        @Override
        protected void addListener(java.beans.PropertyChangeListener qc) {
            c.addPropertyChangeListener(qc);
        }
    };
    qa.processModifierElements(element, _varModel);
    return c;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) GridBagLayout(java.awt.GridBagLayout) VariableValue(jmri.jmrit.symbolicprog.VariableValue) Element(org.jdom2.Element) JButton(javax.swing.JButton) ValueEditor(jmri.jmrit.symbolicprog.ValueEditor) Dimension(java.awt.Dimension) JTextField(javax.swing.JTextField) JSeparator(javax.swing.JSeparator) ValueRenderer(jmri.jmrit.symbolicprog.ValueRenderer) JTable(javax.swing.JTable) QualifierAdder(jmri.jmrit.symbolicprog.QualifierAdder)

Aggregations

VariableValue (jmri.jmrit.symbolicprog.VariableValue)17 QualifierAdder (jmri.jmrit.symbolicprog.QualifierAdder)9 CvValue (jmri.jmrit.symbolicprog.CvValue)7 JPanel (javax.swing.JPanel)6 Element (org.jdom2.Element)6 GridBagConstraints (java.awt.GridBagConstraints)5 GridBagLayout (java.awt.GridBagLayout)5 Dimension (java.awt.Dimension)4 JButton (javax.swing.JButton)4 JScrollPane (javax.swing.JScrollPane)4 JSeparator (javax.swing.JSeparator)4 JTable (javax.swing.JTable)4 JTextField (javax.swing.JTextField)4 ValueEditor (jmri.jmrit.symbolicprog.ValueEditor)4 ValueRenderer (jmri.jmrit.symbolicprog.ValueRenderer)4 JLabel (javax.swing.JLabel)3 ArrayList (java.util.ArrayList)2 Attribute (org.jdom2.Attribute)2 Font (java.awt.Font)1 Insets (java.awt.Insets)1