001package jmri.jmrit.tracker; 002 003import jmri.Block; 004import jmri.Memory; 005import jmri.NamedBeanHandle; 006import org.slf4j.Logger; 007import org.slf4j.LoggerFactory; 008 009/** 010 * Tracks train into memory object 011 * 012 * @author Bob Jacobsen Copyright (C) 2006 013 */ 014public class MemoryTracker { 015 016 /** 017 * Create a Tracker object, providing a list of blocks to watch 018 * @param b block to track. 019 * @param namePrefix system name prefix. 020 */ 021 public MemoryTracker(Block b, String namePrefix) throws IllegalArgumentException { 022 block = b; 023 024 // make sure Memory objects exist & remember it 025 Memory m = jmri.InstanceManager.memoryManagerInstance() 026 .provideMemory(namePrefix + block.getSystemName()); 027 namedMemory = jmri.InstanceManager.getDefault(jmri.NamedBeanHandleManager.class).getNamedBeanHandle(namePrefix + block.getSystemName(), m); 028 // set listener in the block 029 block.addPropertyChangeListener(new java.beans.PropertyChangeListener() { 030 @Override 031 public void propertyChange(java.beans.PropertyChangeEvent e) { 032 handleChange(); 033 } 034 }); 035 036 // first update 037 handleChange(); 038 } 039 040 void handleChange() { 041 if (log.isDebugEnabled() && (block.getValue() != null)) { 042 log.debug("set value {} in block {}", block.getValue(), block.getSystemName()); 043 } 044 Object o = block.getValue(); 045 if (o != null) { 046 o = o.toString(); 047 } 048 namedMemory.getBean().setValue(o); 049 } 050 051 NamedBeanHandle<Memory> namedMemory; 052 //Memory m; 053 Block block; 054 055 private final static Logger log = LoggerFactory.getLogger(MemoryTracker.class); 056}