001package jmri.jmrix.loconet; 002 003import java.util.Locale; 004import javax.annotation.Nonnull; 005import jmri.Light; 006import jmri.managers.AbstractLightManager; 007 008/** 009 * Implement LightManager for LocoNet systems. 010 * <p> 011 * System names are "LLnnnnn", where the first L is the user configurable 012 * system prefix, nnnnn is the bit number without padding. 013 * <p> 014 * Based in part on SerialLightManager.java 015 * 016 * @author Dave Duchamp Copyright (C) 2006 017 */ 018public class LnLightManager extends AbstractLightManager { 019 020 public LnLightManager(LocoNetSystemConnectionMemo memo) { 021 super(memo); 022 } 023 024 /** 025 * {@inheritDoc} 026 */ 027 @Override 028 @Nonnull 029 public LocoNetSystemConnectionMemo getMemo() { 030 return (LocoNetSystemConnectionMemo) memo; 031 } 032 033 /** 034 * Create a new Light based on the system name. 035 * <p> 036 * Assumes calling method has checked that a Light with 037 * this system name does not already exist. 038 * 039 * @return null if the system name is not in a valid format 040 */ 041 @Override 042 @Nonnull 043 protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException { 044 Light lgt; 045 // check if the output bit is available 046 int bitNum = getBitFromSystemName(systemName); 047 if (bitNum == 0) { 048 throw new IllegalArgumentException("Invalid Bit from System Name: " + systemName); 049 } 050 // Normalize the systemName 051 String sName = getSystemPrefix() + "L" + bitNum; // removes any leading zeros 052 // make the new Light object 053 lgt = new LnLight(sName, userName, getMemo().getLnTrafficController(), this); 054 return lgt; 055 } 056 057 /** 058 * {@inheritDoc} 059 */ 060 @Override 061 public NameValidity validSystemNameFormat(@Nonnull String systemName) { 062 return (getBitFromSystemName(systemName) != 0) ? NameValidity.VALID : NameValidity.INVALID; 063 } 064 065 /** 066 * {@inheritDoc} 067 */ 068 @Override 069 @Nonnull 070 public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) { 071 return validateIntegerSystemNameFormat(systemName, 1, 4096, locale); 072 } 073 074 /** 075 * Get the bit address from the system name. 076 * @param systemName a valid LocoNet-based Light System Name 077 * @return the turnout number extracted from the system name 078 */ 079 public int getBitFromSystemName(String systemName) { 080 try { 081 validateSystemNameFormat(systemName, Locale.getDefault()); 082 } catch (IllegalArgumentException ex) { 083 return 0; 084 } 085 return Integer.parseInt(systemName.substring(getSystemNamePrefix().length())); 086 } 087 088 /** 089 * Validate system name for configuration. 090 * Needed for the Abstract Light class. 091 * 092 * @param systemName the systemName to be validated 093 * @return 'true' if system name has a valid meaning in current configuration, 094 * else returns 'false'. For now this method always returns 'true'; 095 */ 096 @Override 097 public boolean validSystemNameConfig(@Nonnull String systemName) { 098 return (true); 099 } 100 101 /** 102 * Determine if it is possible to add a range of Lights in 103 * numerical order eg. 11 thru 18, primarily used to show/not show the add 104 * range box in the Add Light pane. 105 * @param systemName an ignored parameter 106 * @return true, always 107 */ 108 @Override 109 public boolean allowMultipleAdditions(@Nonnull String systemName) { 110 return true; 111 } 112 113 /** 114 * {@inheritDoc} 115 */ 116 @Override 117 public String getEntryToolTip() { 118 return Bundle.getMessage("AddOutputEntryToolTip"); 119 } 120 121}