001package jmri.jmrit.ctc;
002
003import java.util.ArrayList;
004
005import jmri.InstanceManagerAutoDefault;
006
007
008/**
009 * Most times the user does not have the System Console displayed when the CTC
010 * system is being started up.  As such, errors logged to the CTCException class
011 * just "disappear" into the ether on that console, and the user has no
012 * knowledge of any problems.
013 * 
014 * In this object, I will also gather up all of the errors, warnings and info
015 * messages that my system generates in CTCException, and display them to the
016 * user via a dialog box of some form, after the CTC system is fully started.
017 * 
018 * For safety, I implement InstanceManagerAutoDefault so that the objects
019 * default constructor is called (for future safety).  I'm not sure
020 * if "class" variables below are initialized properly if this is not done.
021 *
022 * @author Gregory J. Bedlek Copyright (C) 2018, 2019, 2020
023 */
024public class CTCExceptionBuffer implements InstanceManagerAutoDefault {
025    public enum ExceptionBufferRecordSeverity {
026        INFO(0), WARN(1), ERROR(2);     // Order: The more severe, the HIGHER the number.  See function "getHighestExceptionBufferRecordSeverity" for why.
027        private final int _mSeverity;
028        ExceptionBufferRecordSeverity(int severity) { this._mSeverity = severity; }
029        public int getSeverity() { return _mSeverity; }
030    }
031    private static class ExceptionBufferRecord {
032        public final ExceptionBufferRecordSeverity _mExceptionBufferRecordSeverity;
033        public final String _mMessage;
034        public ExceptionBufferRecord(ExceptionBufferRecordSeverity exceptionBufferRecordSeverity, String message) {
035            _mExceptionBufferRecordSeverity = exceptionBufferRecordSeverity;
036            switch(exceptionBufferRecordSeverity) {
037                case ERROR:
038                    _mMessage = Bundle.getMessage("CTCExceptionBufferERROR") + message;  // NOI18N
039                    break;
040                case WARN:
041                    _mMessage = Bundle.getMessage("CTCExceptionBufferWARN") + message;  // NOI18N
042                    break;
043                default:    // INFO too
044                    _mMessage = Bundle.getMessage("CTCExceptionBufferINFO") + message;  // NOI18N
045                    break;
046            }
047        }
048    }
049    ArrayList<ExceptionBufferRecord> _mArrayListOfExceptionBufferRecords = new ArrayList<>();
050    public CTCExceptionBuffer() {}
051    public void logString(ExceptionBufferRecordSeverity exceptionBufferRecordSeverity, String string) { _mArrayListOfExceptionBufferRecords.add(new ExceptionBufferRecord(exceptionBufferRecordSeverity, string)); }
052    public boolean isEmpty() { return _mArrayListOfExceptionBufferRecords.isEmpty(); }
053    public void clear() { _mArrayListOfExceptionBufferRecords.clear(); }
054    /**
055     * You SHOULD call "isEmpty()" first, because this routine returns by default "INFO"
056     * IF there are NO entries in the list.
057     * 
058     * It's purpose is to give the user an idea of the worst case scenario in the errors.
059     * 
060     * @return The highest level of severity in our list.
061     */
062    public ExceptionBufferRecordSeverity getHighestExceptionBufferRecordSeverity() {
063        ExceptionBufferRecordSeverity highestExceptionBufferRecordSeverityEncountered = ExceptionBufferRecordSeverity.INFO; // Start with lowest, in case there are none
064        for (ExceptionBufferRecord exceptionBufferRecord : _mArrayListOfExceptionBufferRecords) {
065            if (exceptionBufferRecord._mExceptionBufferRecordSeverity.getSeverity() > highestExceptionBufferRecordSeverityEncountered.getSeverity()) {
066                highestExceptionBufferRecordSeverityEncountered = exceptionBufferRecord._mExceptionBufferRecordSeverity;
067            }
068        }
069        return highestExceptionBufferRecordSeverityEncountered;
070    }
071    public String getAllMessages() {
072        StringBuilder returnStringBuilder = new StringBuilder("<html>");  // NOI18N
073        for (ExceptionBufferRecord exceptionBufferRecord : _mArrayListOfExceptionBufferRecords) {
074            returnStringBuilder.append(exceptionBufferRecord._mMessage + "<br>");  // NOI18N
075        }
076        returnStringBuilder.append("</html>");  // NOI18N
077        return returnStringBuilder.toString();
078    }
079}
080