001package jmri.jmrit.logixng.util;
002
003import java.beans.PropertyChangeListener;
004import java.nio.charset.Charset;
005import java.nio.charset.StandardCharsets;
006import java.util.*;
007
008import javax.annotation.Nonnull;
009
010import jmri.*;
011import jmri.jmrit.logixng.*;
012import jmri.jmrit.logixng.implementation.AbstractBase;
013import jmri.jmrit.logixng.util.parser.ParserException;
014
015/**
016 * Select a charset for LogixNG actions and expressions.
017 *
018 * @author Daniel Bergqvist (C) 2023
019 */
020public class LogixNG_SelectCharset {
021
022    public static final List<Charset> STANDARD_CHARSETS = getStandardCharsets();
023
024    private final AbstractBase _base;
025    private final InUse _inUse;
026//    private final PropertyChangeListener _listener;
027
028    private Addressing _addressing = Addressing.Standard;
029    private Charset _standardValue = StandardCharsets.UTF_8;
030    private Charset _allValue = StandardCharsets.UTF_8;
031    private final LogixNG_SelectString _selectUserSpecifiedCharset;
032
033
034    public LogixNG_SelectCharset(AbstractBase base, PropertyChangeListener listener) {
035        _base = base;
036        _inUse = () -> _addressing == Addressing.UserSpecified;
037//        _listener = listener;
038        _selectUserSpecifiedCharset = new LogixNG_SelectString(base, _inUse, listener);
039    }
040
041    private static List<Charset> getStandardCharsets() {
042        List<Charset> standardCharsets = new ArrayList<>();
043        standardCharsets.add(StandardCharsets.US_ASCII);
044        standardCharsets.add(StandardCharsets.ISO_8859_1);
045        standardCharsets.add(StandardCharsets.UTF_8);
046        standardCharsets.add(StandardCharsets.UTF_16);
047        standardCharsets.add(StandardCharsets.UTF_16BE);
048        standardCharsets.add(StandardCharsets.UTF_16LE);
049        return Collections.unmodifiableList(standardCharsets);
050    }
051
052    public void copy(LogixNG_SelectCharset copy) throws ParserException {
053        copy.setAddressing(_addressing);
054        copy.setStandardValue(_standardValue);
055        copy.setAllValue(_standardValue);
056        _selectUserSpecifiedCharset.copy(copy._selectUserSpecifiedCharset);
057    }
058
059    public void setAddressing(@Nonnull Addressing addressing) {
060        this._addressing = addressing;
061    }
062
063    public Addressing getAddressing() {
064        return _addressing;
065    }
066
067    public void setStandardValue(@Nonnull Charset charset) {
068        _base.assertListenersAreNotRegistered(log, "setStandardValue");
069        _standardValue = charset;
070    }
071
072    public Charset getStandardValue() {
073        return _standardValue;
074    }
075
076    public void setAllValue(@Nonnull Charset charset) {
077        _base.assertListenersAreNotRegistered(log, "setListValue");
078        _allValue = charset;
079    }
080
081    public Charset getAllValue() {
082        return _allValue;
083    }
084
085    public LogixNG_SelectString getSelectUserSpecified() {
086        return _selectUserSpecifiedCharset;
087    }
088
089    public Charset evaluateCharset(ConditionalNG conditionalNG) throws JmriException {
090
091        switch (_addressing) {
092            case Standard:
093                return _standardValue;
094
095            case All:
096                return _standardValue;
097
098            case UserSpecified:
099                String charsetName = _selectUserSpecifiedCharset.evaluateValue(conditionalNG);
100                return Charset.forName(charsetName);
101
102            default:
103                throw new IllegalArgumentException("_addressing has unknown value: "+_addressing.name());
104        }
105    }
106
107    public String getDescription(Locale locale) {
108        return "ERROR !!!!";
109/*
110        String enumName;
111
112        String memoryName;
113        if (_memoryHandle != null) {
114            memoryName = _memoryHandle.getName();
115        } else {
116            memoryName = Bundle.getMessage(locale, "BeanNotSelected");
117        }
118
119        switch (_addressing) {
120            case Direct:
121                enumName = Bundle.getMessage(locale, "AddressByDirect", _value);
122                break;
123
124            case Reference:
125                enumName = Bundle.getMessage(locale, "AddressByReference", _reference);
126                break;
127
128            case Memory:
129                enumName = Bundle.getMessage(locale, "AddressByMemory_Listen", memoryName, Base.getListenString(_listenToMemory));
130                break;
131
132            case LocalVariable:
133                enumName = Bundle.getMessage(locale, "AddressByLocalVariable", _localVariable);
134                break;
135
136            case Formula:
137                enumName = Bundle.getMessage(locale, "AddressByFormula", _formula);
138                break;
139
140            case Table:
141                enumName = Bundle.getMessage(
142                        locale,
143                        "AddressByTable",
144                        _selectTable.getTableNameDescription(locale),
145                        _selectTable.getTableRowDescription(locale),
146                        _selectTable.getTableColumnDescription(locale));
147                break;
148
149            default:
150                throw new IllegalArgumentException("invalid _addressing: " + _addressing.name());
151        }
152        return enumName;
153*/
154    }
155
156    /**
157     * Register listeners if this object needs that.
158     */
159    public void registerListeners() {
160    }
161
162    /**
163     * Unregister listeners if this object needs that.
164     */
165    public void unregisterListeners() {
166    }
167
168
169    public enum Addressing {
170
171        Standard(Bundle.getMessage("LogixNG_SelectCharset_Addressing_Standard")),
172        All(Bundle.getMessage("LogixNG_SelectCharset_Addressing_All")),
173        UserSpecified(Bundle.getMessage("LogixNG_SelectCharset_Addressing_UserSpecified"));
174
175        private final String _text;
176
177        private Addressing(String text) {
178            this._text = text;
179        }
180
181        @Override
182        public String toString() {
183            return _text;
184        }
185
186    }
187
188
189    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogixNG_SelectCharset.class);
190}