001package jmri.jmrit.logixng;
002
003import jmri.*;
004import jmri.jmrit.entryexit.DestinationPoints;
005import jmri.jmrit.entryexit.EntryExitPairs;
006import jmri.jmrit.logix.OBlock;
007import jmri.jmrit.logix.OBlockManager;
008import jmri.jmrit.logix.Warrant;
009import jmri.jmrit.logix.WarrantManager;
010
011/**
012 * Defines types of NamedBeans, for example Turnout and Light.
013 *
014 * @author Daniel Bergqvist Copyright 2019
015 */
016public enum NamedBeanType {
017
018    Block(
019            Bundle.getMessage("BeanNameBlock"),
020            Bundle.getMessage("BeanNameBlocks"),
021            Block.class,
022            null,
023            () -> InstanceManager.getDefault(BlockManager.class),
024            new CreateBean() {
025                @Override
026                public NamedBean createBean(String systemName, String userName) {
027                    return InstanceManager.getDefault(BlockManager.class)
028                            .createNewBlock(systemName, userName);
029                }
030            },
031            (NamedBean bean, String property) -> {
032                if (!(bean instanceof Block)) {
033                    throw new IllegalArgumentException("bean is not a Block");
034                }
035                InstanceManager.getDefault(BlockManager.class)
036                        .deleteBean((Block)bean, property);
037            }),
038
039    GlobalVariable(
040            Bundle.getMessage("BeanNameGlobalVariable"),
041            Bundle.getMessage("BeanNameGlobalVariables"),
042            GlobalVariable.class,
043            "value",
044            () -> InstanceManager.getDefault(GlobalVariableManager.class),
045            new CreateBean() {
046                @Override
047                public NamedBean createBean(String systemName, String userName) {
048                    return InstanceManager.getDefault(GlobalVariableManager.class)
049                            .createGlobalVariable(systemName, userName);
050                }
051            },
052            (NamedBean bean, String property) -> {
053                if (!(bean instanceof GlobalVariable)) {
054                    throw new IllegalArgumentException("bean is not a GlobalVariable");
055                }
056                InstanceManager.getDefault(GlobalVariableManager.class)
057                        .deleteBean((GlobalVariable)bean, property);
058            }),
059
060    EntryExit(
061            Bundle.getMessage("BeanNameEntryExit"),
062            Bundle.getMessage("BeanNameEntryExits"),
063            DestinationPoints.class,
064            "active",
065            () -> InstanceManager.getDefault(EntryExitPairs.class),
066            null,
067            (NamedBean bean, String property) -> {
068                if (!(bean instanceof DestinationPoints)) {
069                    throw new IllegalArgumentException("bean is not a DestinationPoints");
070                }
071                InstanceManager.getDefault(EntryExitPairs.class)
072                        .deleteBean((DestinationPoints)bean, property);
073            }),
074
075    Light(
076            Bundle.getMessage("BeanNameLight"),
077            Bundle.getMessage("BeanNameLights"),
078            Light.class,
079            "KnownState",
080            () -> InstanceManager.getDefault(LightManager.class),
081            new CreateBean() {
082                @Override
083                public NamedBean createBean(String systemName, String userName) {
084                    return InstanceManager.getDefault(LightManager.class)
085                            .newLight(systemName, userName);
086                }
087            },
088            (NamedBean bean, String property) -> {
089                if (!(bean instanceof Light)) {
090                    throw new IllegalArgumentException("bean is not a Light");
091                }
092                InstanceManager.getDefault(LightManager.class).deleteBean((Light)bean, property);
093            }),
094
095    Memory(
096            Bundle.getMessage("BeanNameMemory"),
097            Bundle.getMessage("BeanNameMemories"),
098            Memory.class,
099            "value",
100            () -> InstanceManager.getDefault(MemoryManager.class),
101            new CreateBean() {
102                @Override
103                public NamedBean createBean(String systemName, String userName) {
104                    return InstanceManager.getDefault(MemoryManager.class)
105                            .newMemory(systemName, userName);
106                }
107            },
108            (NamedBean bean, String property) -> {
109                if (!(bean instanceof Memory)) {
110                    throw new IllegalArgumentException("bean is not a Memory");
111                }
112                InstanceManager.getDefault(MemoryManager.class)
113                        .deleteBean((Memory)bean, property);
114            }),
115
116    OBlock(
117            Bundle.getMessage("BeanNameOBlock"),
118            Bundle.getMessage("BeanNameOBlocks"),
119            OBlock.class,
120            "state",
121            () -> InstanceManager.getDefault(OBlockManager.class),
122            new CreateBean() {
123                @Override
124                public NamedBean createBean(String systemName, String userName) {
125                    return InstanceManager.getDefault(OBlockManager.class)
126                            .createNewOBlock(systemName, userName);
127                }
128            },
129            (NamedBean bean, String property) -> {
130                if (!(bean instanceof OBlock)) {
131                    throw new IllegalArgumentException("bean is not a OBlock");
132                }
133                InstanceManager.getDefault(OBlockManager.class)
134                        .deleteBean((OBlock)bean, property);
135            }),
136
137    Reporter(
138            Bundle.getMessage("BeanNameReporter"),
139            Bundle.getMessage("BeanNameReporters"),
140            Reporter.class,
141            "currentReport",
142            () -> InstanceManager.getDefault(ReporterManager.class),
143            new CreateBean() {
144                @Override
145                public NamedBean createBean(String systemName, String userName) {
146                    return InstanceManager.getDefault(ReporterManager.class)
147                            .newReporter(systemName, userName);
148                }
149            },
150            (NamedBean bean, String property) -> {
151                if (!(bean instanceof Reporter)) {
152                    throw new IllegalArgumentException("bean is not a Reporter");
153                }
154                InstanceManager.getDefault(ReporterManager.class)
155                        .deleteBean((Reporter)bean, property);
156            }),
157
158    Sensor(
159            Bundle.getMessage("BeanNameSensor"),
160            Bundle.getMessage("BeanNameSensors"),
161            Sensor.class,
162            "KnownState",
163            () -> InstanceManager.getDefault(SensorManager.class),
164            new CreateBean() {
165                @Override
166                public NamedBean createBean(String systemName, String userName) {
167                    return InstanceManager.getDefault(SensorManager.class)
168                            .newSensor(systemName, userName);
169                }
170            },
171            (NamedBean bean, String property) -> {
172                if (!(bean instanceof Sensor)) {
173                    throw new IllegalArgumentException("bean is not a Sensor");
174                }
175                InstanceManager.getDefault(SensorManager.class)
176                        .deleteBean((Sensor)bean, property);
177            }),
178
179    SignalHead(
180            Bundle.getMessage("BeanNameSignalHead"),
181            Bundle.getMessage("BeanNameSignalHeads"),
182            SignalHead.class,
183            "Appearance",
184            () -> InstanceManager.getDefault(SignalHeadManager.class),
185            null,
186            (NamedBean bean, String property) -> {
187                if (!(bean instanceof SignalHead)) {
188                    throw new IllegalArgumentException("bean is not a SignalHead");
189                }
190                InstanceManager.getDefault(SignalHeadManager.class)
191                        .deleteBean((SignalHead)bean, property);
192            }),
193
194    SignalMast(
195            Bundle.getMessage("BeanNameSignalMast"),
196            Bundle.getMessage("BeanNameSignalMasts"),
197            SignalMast.class,
198            "Aspect",
199            () -> InstanceManager.getDefault(SignalMastManager.class),
200            null,
201            (NamedBean bean, String property) -> {
202                if (!(bean instanceof SignalMast)) {
203                    throw new IllegalArgumentException("bean is not a SignalMast");
204                }
205                InstanceManager.getDefault(SignalMastManager.class)
206                        .deleteBean((SignalMast)bean, property);
207            }),
208
209    Turnout(
210            Bundle.getMessage("BeanNameTurnout"),
211            Bundle.getMessage("BeanNameTurnouts"),
212            Turnout.class, "KnownState",
213            () -> InstanceManager.getDefault(TurnoutManager.class),
214            new CreateBean() {
215                @Override
216                public NamedBean createBean(String systemName, String userName) {
217                    return InstanceManager.getDefault(TurnoutManager.class)
218                            .newTurnout(systemName, userName);
219                }
220            },
221            (NamedBean bean, String property) -> {
222                if (!(bean instanceof Turnout)) {
223                    throw new IllegalArgumentException("bean is not a Turnout");
224                }
225                InstanceManager.getDefault(TurnoutManager.class)
226                        .deleteBean((Turnout)bean, property);
227            }),
228
229    Warrant(
230            Bundle.getMessage("BeanNameWarrant"),
231            Bundle.getMessage("BeanNameWarrants"),
232            Warrant.class, "KnownState",
233            () -> InstanceManager.getDefault(WarrantManager.class),
234            null,
235            (NamedBean bean, String property) -> {
236                if (!(bean instanceof Warrant)) {
237                    throw new IllegalArgumentException("bean is not a Warrant");
238                }
239                InstanceManager.getDefault(WarrantManager.class)
240                        .deleteBean((Warrant)bean, property);
241            });
242
243
244    private final String _name;
245    private final String _namePlural;
246    private final Class<? extends NamedBean> _clazz;
247    private final String _propertyName;
248    private final GetManager _getManager;
249    private Manager<? extends NamedBean> _manager;
250    private final CreateBean _createBean;
251    private final DeleteBean _deleteBean;
252
253    NamedBeanType(
254            String name,
255            String namePlural,
256            Class<? extends NamedBean> clazz,
257            String propertyName,
258            GetManager getManager,
259            CreateBean createBean,
260            DeleteBean deleteBean) {
261        _name = name;
262        _namePlural = namePlural;
263        _clazz = clazz;
264        _propertyName = propertyName;
265        _getManager = getManager;
266        _manager = _getManager.getManager();
267        _createBean = createBean;
268        _deleteBean = deleteBean;
269    }
270
271    @Override
272    public String toString() {
273        return _name;
274    }
275
276    public String getName(boolean plural) {
277        return plural ? _namePlural : _name;
278    }
279
280    public Class<? extends NamedBean> getClazz() {
281        return _clazz;
282    }
283
284    public String getPropertyName() {
285        return _propertyName;
286    }
287
288    public Manager<? extends NamedBean> getManager() {
289        return _manager;
290    }
291
292    public CreateBean getCreateBean() {
293        return _createBean;
294    }
295
296    public DeleteBean getDeleteBean() {
297        return _deleteBean;
298    }
299
300    // This method is used by test classes to reset this enum.
301    // Each test resets the InstanceManager so we need to reset the
302    // managers in this enum.
303    public static void reset() {
304        for (NamedBeanType type : NamedBeanType.values()) {
305            type._manager = type._getManager.getManager();
306        }
307    }
308
309    private interface GetManager {
310
311        Manager<? extends NamedBean> getManager();
312    }
313
314    public interface CreateBean {
315
316        public NamedBean createBean(String systemName, String userName);
317    }
318
319    public interface DeleteBean {
320
321        public void deleteBean(NamedBean bean, String property)
322                throws java.beans.PropertyVetoException;
323    }
324
325}