001package jmri.jmrit.operations.automation.actions; 002 003import java.beans.PropertyChangeEvent; 004import java.beans.PropertyChangeListener; 005import jmri.InstanceManager; 006import jmri.jmrit.operations.locations.Location; 007import jmri.jmrit.operations.locations.LocationManager; 008import jmri.jmrit.operations.setup.Control; 009import org.slf4j.Logger; 010import org.slf4j.LoggerFactory; 011 012public class WaitSwitchListAction extends Action implements PropertyChangeListener { 013 014 private static final int _code = ActionCodes.WAIT_SWITCHLIST; 015 016 @Override 017 public int getCode() { 018 return _code; 019 } 020 021 @Override 022 public String getName() { 023 return Bundle.getMessage("WaitForSwitchListChange"); 024 } 025 026 @Override 027 public boolean isConcurrentAction() { 028 return true; 029 } 030 031 @Override 032 public void doAction() { 033 if (getAutomationItem() != null) { 034 setRunning(true); 035 addPropertyChangeLocations(); 036 } 037 } 038 039 /* 040 * Waiting for any location's switch list to change 041 */ 042 private void checkForlocationChange() { 043 for (Location location : InstanceManager.getDefault(LocationManager.class).getList()) { 044 if (location != null && location.isSwitchListEnabled() && location.getStatus().equals(Location.MODIFIED)) { 045 removePropertyChangeLocations(); 046 finishAction(true); 047 break; 048 } 049 } 050 } 051 052 private synchronized void addPropertyChangeLocations() { 053 for (Location location : InstanceManager.getDefault(LocationManager.class).getList()) { 054 location.addPropertyChangeListener(this); 055 } 056 } 057 058 private synchronized void removePropertyChangeLocations() { 059 for (Location location : InstanceManager.getDefault(LocationManager.class).getList()) { 060 location.removePropertyChangeListener(this); 061 } 062 } 063 064 @Override 065 public void cancelAction() { 066 if (getAutomationItem() != null) { 067 setRunning(false); 068 removePropertyChangeLocations(); 069 } 070 } 071 072 @Override 073 public void propertyChange(PropertyChangeEvent evt) { 074 if (Control.SHOW_PROPERTY) 075 log.debug("Property change: ({}) old: ({}) new: ({})", evt.getPropertyName(), evt.getOldValue(), evt 076 .getNewValue()); 077 if (evt.getPropertyName().equals(Location.STATUS_CHANGED_PROPERTY) && evt 078 .getNewValue().equals(Location.MODIFIED)) { 079 checkForlocationChange(); 080 } 081 } 082 083 private final static Logger log = LoggerFactory.getLogger(WaitSwitchListAction.class); 084 085}