001package jmri.jmrit.ctc.editor.code;
002
003import java.util.ArrayList;
004import jmri.jmrit.ctc.NBHSensor;
005import jmri.jmrit.ctc.NBHSignal;
006import jmri.jmrit.ctc.NBHTurnout;
007import jmri.jmrit.ctc.ctcserialdata.CallOnData;
008import jmri.jmrit.ctc.ctcserialdata.CodeButtonHandlerData;
009import jmri.jmrit.ctc.ctcserialdata.TrafficLockingData;
010
011/**
012 *
013 * @author Gregory J. Bedlek Copyright (C) 2018, 2019
014 *
015 * The purpose of this module is to support patterned information
016 * in CodeButtonHandlerData objects.  It is also a "factory" to produce
017 * new CodeButtonHandlerData objects from those patterns..
018 */
019public class CodeButtonHandlerDataRoutines {
020    public static CodeButtonHandlerData createNewCodeButtonHandlerData(int newUniqueID, int newSwitchNumber, int newSignalEtcNumber, int newGUIColumnNumber, ProgramProperties programProperties) {
021        CodeButtonHandlerData returnValue = new CodeButtonHandlerData(newUniqueID, newSwitchNumber, newSignalEtcNumber, newGUIColumnNumber);
022        returnValue = updateExistingCodeButtonHandlerDataWithSubstitutedData(programProperties, returnValue);
023        returnValue._mOSSectionOccupiedExternalSensor = new NBHSensor("CodeButtonHandlerDataRoutines", "Empty _mOSSectionOccupiedExternalSensor", "", "", true);
024        returnValue._mOSSectionOccupiedExternalSensor2 = new NBHSensor("CodeButtonHandlerDataRoutines", "Empty _mOSSectionOccupiedExternalSensor2", "", "", true);
025        returnValue._mOSSectionSwitchSlavedToUniqueID = CodeButtonHandlerData.SWITCH_NOT_SLAVED;
026        returnValue._mGUIGeneratedAtLeastOnceAlready = false;
027        returnValue._mCodeButtonDelayTime = programProperties._mCodeButtonDelayTime;
028        returnValue._mSIDI_Enabled = false;
029        returnValue._mSIDI_CodingTimeInMilliseconds = programProperties._mSIDI_CodingTimeInMilliseconds;
030        returnValue._mSIDI_TimeLockingTimeInMilliseconds = programProperties._mSIDI_TimeLockingTimeInMilliseconds;
031        returnValue._mSIDI_TrafficDirection = CodeButtonHandlerData.TRAFFIC_DIRECTION.BOTH;
032        returnValue._mSIDI_LeftRightTrafficSignals = new ArrayList<NBHSignal>();
033        returnValue._mSIDI_RightLeftTrafficSignals = new ArrayList<NBHSignal>();
034        returnValue._mSIDL_Enabled = false;
035        returnValue._mSWDI_Enabled = false;
036        returnValue._mSWDI_ExternalTurnout = new NBHTurnout("CodeButtonHandlerDataRoutines", "Empty _mSWDI_ExternalTurnout", "");
037        returnValue._mSWDI_CodingTimeInMilliseconds = programProperties._mSWDI_CodingTimeInMilliseconds;
038        returnValue._mSWDI_FeedbackDifferent = false;
039        returnValue._mSWDI_GUITurnoutType = CodeButtonHandlerData.TURNOUT_TYPE.TURNOUT;
040        returnValue._mSWDI_GUITurnoutLeftHand = false;
041        returnValue._mSWDI_GUICrossoverLeftHand = false;
042        returnValue._mSWDL_Enabled = false;
043        returnValue._mCO_Enabled = false;
044        returnValue._mCO_GroupingsList = new ArrayList<CallOnData>();
045        returnValue._mTRL_LeftTrafficLockingRules = new ArrayList<TrafficLockingData>();
046        returnValue._mTRL_RightTrafficLockingRules = new ArrayList<TrafficLockingData>();
047        returnValue._mTRL_Enabled = false;
048        returnValue._mTUL_Enabled = false;
049        returnValue._mTUL_ExternalTurnout = new NBHTurnout("CodeButtonHandlerDataRoutines", "Empty _mTUL_ExternalTurnout", "");
050        returnValue._mTUL_ExternalTurnoutFeedbackDifferent = false;
051        returnValue._mTUL_NoDispatcherControlOfSwitch = false;
052        returnValue._mTUL_ndcos_WhenLockedSwitchStateIsClosed = true;
053        returnValue._mTUL_GUI_IconsEnabled = true;
054        returnValue._mTUL_LockImplementation = CodeButtonHandlerData.LOCK_IMPLEMENTATION.GREGS;
055        returnValue._mTUL_AdditionalExternalTurnout1 = new NBHTurnout("CodeButtonHandlerDataRoutines", "Empty _mTUL_AdditionalExternalTurnout1", "");
056        returnValue._mTUL_AdditionalExternalTurnout1FeedbackDifferent = false;
057        returnValue._mTUL_AdditionalExternalTurnout2 = new NBHTurnout("CodeButtonHandlerDataRoutines", "Empty _mTUL_AdditionalExternalTurnout2", "");
058        returnValue._mTUL_AdditionalExternalTurnout2FeedbackDifferent = false;
059        returnValue._mTUL_AdditionalExternalTurnout3 = new NBHTurnout("CodeButtonHandlerDataRoutines", "Empty _mTUL_AdditionalExternalTurnout3", "");
060        returnValue._mTUL_AdditionalExternalTurnout3FeedbackDifferent = false;
061        returnValue._mIL_Enabled = false;
062        returnValue._mIL_Signals = new ArrayList<NBHSignal>();
063        return returnValue;
064    }
065
066    public static CodeButtonHandlerData updateExistingCodeButtonHandlerDataWithSubstitutedData(ProgramProperties programProperties, CodeButtonHandlerData returnValue) {
067        uECBHDWSD_CodeButton(programProperties, returnValue);
068        uECBHDWSD_SIDI(programProperties, returnValue);
069        uECBHDWSD_SIDL(programProperties, returnValue);
070        uECBHDWSD_SWDI(programProperties, returnValue);
071        uECBHDWSD_SWDL(programProperties, returnValue);
072        uECBHDWSD_CallOn(programProperties, returnValue);
073        uECBHDWSD_TUL(programProperties, returnValue);
074        return returnValue;
075    }
076
077//  uECBHDWSD is short for "updateExistingCodeButtonHandlerDataWithSubstitutedData"
078    public static CodeButtonHandlerData uECBHDWSD_CodeButton(ProgramProperties programProperties, CodeButtonHandlerData returnValue) {
079        returnValue._mCodeButtonInternalSensor = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties._mCodeButtonInternalSensorPattern);
080        return returnValue;
081    }
082
083    public static CodeButtonHandlerData uECBHDWSD_SIDI(ProgramProperties programProperties, CodeButtonHandlerData returnValue) {
084        returnValue._mSIDI_LeftInternalSensor = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties._mSIDI_LeftInternalSensorPattern);
085        returnValue._mSIDI_NormalInternalSensor = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties._mSIDI_NormalInternalSensorPattern);
086        returnValue._mSIDI_RightInternalSensor = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties._mSIDI_RightInternalSensorPattern);
087        return returnValue;
088    }
089
090    public static CodeButtonHandlerData uECBHDWSD_SIDL(ProgramProperties programProperties, CodeButtonHandlerData returnValue) {
091        returnValue._mSIDL_LeftInternalSensor = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties._mSIDL_LeftInternalSensorPattern);
092        returnValue._mSIDL_NormalInternalSensor = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties. _mSIDL_NormalInternalSensorPattern);
093        returnValue._mSIDL_RightInternalSensor = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties._mSIDL_RightInternalSensorPattern);
094        return returnValue;
095    }
096
097    public static CodeButtonHandlerData uECBHDWSD_SWDI(ProgramProperties programProperties, CodeButtonHandlerData returnValue) {
098        returnValue._mSWDI_NormalInternalSensor = createInternalNBHSensor(returnValue._mSwitchNumber, programProperties._mSWDI_NormalInternalSensorPattern);
099        returnValue._mSWDI_ReversedInternalSensor = createInternalNBHSensor(returnValue._mSwitchNumber, programProperties._mSWDI_ReversedInternalSensorPattern);
100        return returnValue;
101    }
102
103    public static CodeButtonHandlerData uECBHDWSD_SWDL(ProgramProperties programProperties, CodeButtonHandlerData returnValue) {
104        returnValue._mSWDL_InternalSensor = createInternalNBHSensor(returnValue._mSwitchNumber, programProperties._mSWDL_InternalSensorPattern);
105        return returnValue;
106    }
107
108    public static CodeButtonHandlerData uECBHDWSD_CallOn(ProgramProperties programProperties, CodeButtonHandlerData returnValue) {
109        returnValue._mCO_CallOnToggleInternalSensor = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties. _mCO_CallOnToggleInternalSensorPattern);
110        return returnValue;
111    }
112
113    public static CodeButtonHandlerData uECBHDWSD_TUL(ProgramProperties programProperties, CodeButtonHandlerData returnValue) {
114        returnValue._mTUL_DispatcherInternalSensorLockToggle = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties._mTUL_DispatcherInternalSensorLockTogglePattern);
115        returnValue._mTUL_DispatcherInternalSensorUnlockedIndicator = createInternalNBHSensor(returnValue._mSignalEtcNumber, programProperties._mTUL_DispatcherInternalSensorUnlockedIndicatorPattern);
116        return returnValue;
117    }
118
119    /**
120     * Create an internal NBHSensor using the provide(String) based constructor.
121     * @param number The signal or switch number for the sensor name.
122     * @param pattern The pattern to be used, such as IS#:CB.
123     * @return the NBHSensor
124     */
125    public static NBHSensor createInternalNBHSensor(int number, String pattern) {
126        String sensorName = substituteValueForPoundSigns(number, pattern);
127        NBHSensor sensor = CommonSubs.getNBHSensor(sensorName, true);
128        return sensor;
129    }
130
131    /**
132     * This is the "heart" of the pattern match system: It substitutes the passed
133     * value where ever it see a single "#" in the passed template.  It does not
134     * support escapes, it will fix ALL "#" with that number in the passed template.
135     * It is indiscriminate.
136     *
137     * @param value     The "number" that will be substituted where the template parameter is
138     * @param template  The pattern used to generate the result.
139     * @return          Modified string.  ALL locations modified indiscriminately.
140     */
141    private static String substituteValueForPoundSigns(int value, String template) {
142        int indexOf;
143        while (-1 != (indexOf = template.indexOf('#'))) { template = template.substring(0, indexOf) + Integer.toString(value) + template.substring(indexOf+1); }
144        return template;
145    }
146}