001package jmri.util.swing;
002
003import javax.swing.JPanel;
004
005/**
006 * Utilities for displaying Validation Messages.
007 * <hr>
008 * This file is part of JMRI.
009 * <p>
010 * JMRI is free software; you can redistribute it and/or modify it under 
011 * the terms of version 2 of the GNU General Public License as published 
012 * by the Free Software Foundation. See the "COPYING" file for a copy
013 * of this license.
014 * <p>
015 * JMRI is distributed in the hope that it will be useful, but WITHOUT 
016 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
017 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
018 * for more details.
019 * 
020 */
021public class ValidationNotifications {
022
023    /**
024     * Parse a string for binary, decimal or hex byte value.
025     * Displays error message Dialog if unable to parse.
026     * <p>
027     * 0b, 0d or 0x prefix will force parsing of binary, decimal or hex,
028     * respectively. Entries with no prefix are parsed as decimal if decimal
029     * flag is true, otherwise hex.
030     *
031     * @param s        string to be parsed
032     * @param limit    upper bound of value to be parsed
033     * @param decimal  flag for decimal or hex default
034     * @param comp     Parent component
035     * @param errMsg   Message to be displayed if Number FormatException
036     *                 encountered
037     * @return the byte value, -1 indicates failure
038     */
039    public final static int parseBinDecHexByte(String s, int limit, boolean decimal, String errMsg,
040            JPanel comp) {
041        
042        int radix = 16;
043        if ((s.length() > 2) && s.substring(0, 2).equalsIgnoreCase("0x")) {
044            // hex, remove the prefix
045            s = s.substring(2);
046            radix = 16;
047        } else if ((s.length() > 2) && s.substring(0, 2).equalsIgnoreCase("0d")) {
048            // decimal, remove the prefix
049            s = s.substring(2);
050            radix = 10;
051        } else if ((s.length() > 2) && s.substring(0, 2).equalsIgnoreCase("0b")) {
052            // binary, remove the prefix
053            s = s.substring(2);
054            radix = 2;
055        } else if (decimal) {
056            radix = 10;
057        }
058        String errText="";
059        int data = -1;
060        try {
061            data = Integer.parseInt(s, radix);
062            if (data < 0) {
063                errText = Bundle.getMessage("ErrorConvertNegative");
064            }
065        } catch (NumberFormatException ex) {
066            errText = Bundle.getMessage("ErrorConvertFormat",s);
067        }
068        if (data > limit) {
069            errText = Bundle.getMessage("ErrorConvertNumberTooBig",data,limit);
070        }
071        if (!errText.isEmpty()) {
072            JmriJOptionPane.showMessageDialog(comp, errMsg + "\n" + errText,
073                errMsg, JmriJOptionPane.ERROR_MESSAGE);
074            data = -1;
075        }
076        return data;
077    }
078
079    // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ValidationNotifications.class);
080}