001package jmri.jmrix.maple; 002 003import java.util.Locale; 004import javax.annotation.Nonnull; 005import jmri.Light; 006import jmri.managers.AbstractLightManager; 007import org.slf4j.Logger; 008import org.slf4j.LoggerFactory; 009 010/** 011 * Implement LightManager for Maple serial systems. 012 * <p> 013 * System names are "KLnnn", where K is the user configurable system prefix, 014 * nnn is the bit number without padding. 015 * <p> 016 * Based in part on SerialTurnoutManager.java 017 * 018 * @author Bob Jacobsen Copyright (C) 2008 019 * @author Dave Duchamp Copyright (C) 2004, 2010 020 */ 021public class SerialLightManager extends AbstractLightManager { 022 023 public SerialLightManager(MapleSystemConnectionMemo memo) { 024 super(memo); 025 } 026 027 /** 028 * {@inheritDoc} 029 */ 030 @Override 031 @Nonnull 032 public MapleSystemConnectionMemo getMemo() { 033 return (MapleSystemConnectionMemo) memo; 034 } 035 036 /** 037 * {@inheritDoc} 038 */ 039 @Override 040 @Nonnull 041 protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException { 042 043 // check if the output bit is available 044 int bitNum = SerialAddress.getBitFromSystemName(systemName, getSystemPrefix()); 045 if (bitNum == 0) { 046 throw new IllegalArgumentException("Invalid Bit from System Name: " + systemName); 047 } 048 String conflict = SerialAddress.isOutputBitFree(bitNum, getSystemPrefix()); 049 if (!conflict.isEmpty()) { 050 log.error("Assignment conflict with '{}'. Light not created.", conflict); 051 throw new IllegalArgumentException("The output bit, " + bitNum + ", is currently assigned to " + conflict); 052 } 053 // Validate the System Name 054 String sysName = SerialAddress.normalizeSystemName(systemName, getSystemPrefix()); 055 if (sysName.isEmpty()) { 056 log.error("error when normalizing system name {}", systemName); 057 throw new IllegalArgumentException("Error when normalizing system name: "+systemName); 058 } 059 if (SerialAddress.validSystemNameFormat(systemName, 'L', getSystemPrefix()) == NameValidity.VALID) { 060 Light lgt = new SerialLight(sysName, userName, getMemo()); 061 if (!SerialAddress.validSystemNameConfig(sysName, 'L', getMemo())) { 062 log.warn("Light system Name '{}' does not refer to configured hardware.", sysName); 063 javax.swing.JOptionPane.showMessageDialog(null, "WARNING - The Light just added, " + sysName 064 + ", refers to an unconfigured output bit.", "Configuration Warning", 065 javax.swing.JOptionPane.INFORMATION_MESSAGE, null); 066 } 067 return lgt; 068 } else { 069 log.error("Invalid Light system Name format: {}", systemName); 070 throw new IllegalArgumentException("Invalid Light system Name format: " + systemName); 071 } 072 } 073 074 /** 075 * {@inheritDoc} 076 */ 077 @Override 078 @Nonnull 079 public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) { 080 return SerialAddress.validateSystemNameFormat(name, this, locale); 081 } 082 083 /** 084 * {@inheritDoc} 085 */ 086 @Override 087 public NameValidity validSystemNameFormat(@Nonnull String systemName) { 088 return (SerialAddress.validSystemNameFormat(systemName, typeLetter(), getSystemPrefix())); 089 } 090 091 /** 092 * {@inheritDoc} 093 */ 094 @Override 095 public boolean validSystemNameConfig(@Nonnull String systemName) { 096 return (SerialAddress.validSystemNameConfig(systemName, 'L', getMemo())); 097 } 098 099 /** 100 * {@inheritDoc} 101 */ 102 @Override 103 public String getEntryToolTip() { 104 return Bundle.getMessage("AddOutputEntryToolTip"); 105 } 106 107 private final static Logger log = LoggerFactory.getLogger(SerialLightManager.class); 108 109}