001package jmri.jmrit.symbolicprog;
002
003import java.util.HashMap;
004import javax.swing.JLabel;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Like {@link SplitVariableValue}, except that the string representation is in
010 * hexadecimal
011 * <br><br>
012 * All the attributes of {@link SplitVariableValue} are inherited.
013 * <br><br>
014 * An optional {@code case} attribute can be used to force the hex characters to
015 * display in {@code "upper"} or {@code "lower"} case.
016 *
017 * @author Bob Jacobsen Copyright (C) 2001, 2002, 2003, 2004, 2013, 2014
018 * @author Dave Heap Copyright (C) 2016
019 */
020public class SplitHexVariableValue extends SplitVariableValue {
021
022    public SplitHexVariableValue(String name, String comment, String cvName,
023            boolean readOnly, boolean infoOnly, boolean writeOnly, boolean opsOnly,
024            String cvNum, String mask, int minVal, int maxVal,
025            HashMap<String, CvValue> v, JLabel status, String stdname,
026            String pSecondCV, int pFactor, int pOffset, String uppermask, String extra1, String extra2, String extra3, String extra4) {
027        super(name, comment, cvName, readOnly, infoOnly, writeOnly, opsOnly, cvNum, mask, minVal, maxVal, v, status, stdname, pSecondCV, pFactor, pOffset, uppermask, extra1, extra2, extra3, extra4);
028    }
029
030    String _case;
031
032    @Override
033    public void stepOneActions(String name, String comment, String cvName,
034            boolean readOnly, boolean infoOnly, boolean writeOnly, boolean opsOnly,
035            String cvNum, String mask, int minVal, int maxVal,
036            HashMap<String, CvValue> v, JLabel status, String stdname,
037            String pSecondCV, int pFactor, int pOffset, String uppermask, String extra1, String extra2, String extra3, String extra4) {
038        _case = extra1;
039        if (extra3 != null) {
040            _minVal = getValueFromText(extra3);
041        }
042        if (extra4 != null) {
043            _maxVal = getValueFromText(extra4);
044        }
045    }
046
047    @Override
048    long getValueFromText(String s) {
049        return (Long.parseUnsignedLong(s, 16));
050    }
051
052    @Override
053    String getTextFromValue(long v) {
054        String ret = Long.toHexString(v);
055        if (_case.equals("upper")) {
056            ret = ret.toUpperCase();
057        } else if (_case.equals("lower")) {
058            ret = ret.toLowerCase();
059        }
060        return ret;
061    }
062
063    /**
064     * Set value from a String value.
065     *
066     * @param value a string representing the unsigned long hex value to be set
067     */
068    @Override
069    public void setValue(String value) {
070        try {
071            long val = Long.parseUnsignedLong(value, 16);
072            setLongValue(val);
073        } catch (NumberFormatException e) {
074            log.warn("skipping set of non-hex value \"{}\"", value);
075        }
076    }
077
078    // initialize logging
079    private final static Logger log = LoggerFactory.getLogger(SplitHexVariableValue.class);
080}